Skip to content

Commit 520c6be

Browse files
committed
Helper methods to simplify conversions
1 parent 441a3f9 commit 520c6be

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/com/convertapi/ConvertApi.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,39 +108,39 @@ public static User getUser(Config config) {
108108
}
109109

110110
@SuppressWarnings("unused")
111-
public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat, Param... params) throws IOException {
112-
return convert(fromFile, toFormat, Config.defaults().getSecret(), params);
111+
public static CompletableFuture<ConversionResult> convertFile(Path fromFile, String toFormat, Param... params) throws IOException {
112+
return convertFile(fromFile, toFormat, Config.defaults().getSecret(), params);
113113
}
114114

115-
public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat, String secret, Param... params) throws IOException {
115+
public static CompletableFuture<ConversionResult> convertFile(Path fromFile, String toFormat, String secret, Param... params) throws IOException {
116116
Param[] fileParam = new Param[]{new Param("file", fromFile)};
117117
return convert(getFileExtension(fromFile), toFormat, Param.concat(fileParam, params), Config.defaults(secret));
118118
}
119119

120120
@SuppressWarnings("unused")
121-
public static void convert(String fromPathToFile, String toPathToFile) {
122-
convert(fromPathToFile, toPathToFile, Config.defaults().getSecret());
121+
public static void convertFile(String fromPathToFile, String toPathToFile) {
122+
convertFile(fromPathToFile, toPathToFile, Config.defaults().getSecret());
123123
}
124124

125-
public static void convert(String fromPathToFile, String toPathToFile, String secret, Param... params) {
125+
public static void convertFile(String fromPathToFile, String toPathToFile, String secret, Param... params) {
126126
try {
127127
Path fromPath = Paths.get(fromPathToFile);
128128
Path toPath = Paths.get(toPathToFile);
129-
convert(fromPath, getFileExtension(toPath), secret, params).get().saveFile(toPath).get();
129+
convertFile(fromPath, getFileExtension(toPath), secret, params).get().saveFile(toPath).get();
130130
} catch (IOException | ExecutionException | InterruptedException e) {
131131
throw new RuntimeException(e);
132132
}
133133
}
134134

135-
public static List<Path> convertFile(String fromPathToFile, String toFormat, String outputDirectory, Param... params) {
136-
return convertFile(fromPathToFile, toFormat, outputDirectory, Config.defaults().getSecret(), params);
135+
public static List<Path> convertFileToDir(String fromPathToFile, String toFormat, String outputDirectory, Param... params) {
136+
return convertFileToDir(fromPathToFile, toFormat, outputDirectory, Config.defaults().getSecret(), params);
137137
}
138138

139-
public static List<Path> convertFile(String fromPathToFile, String toFormat, String outputDirectory, String secret, Param... params) {
139+
public static List<Path> convertFileToDir(String fromPathToFile, String toFormat, String outputDirectory, String secret, Param... params) {
140140
try {
141141
Path fromPath = Paths.get(fromPathToFile);
142142
Path toPath = Paths.get(outputDirectory);
143-
return convert(fromPath, toFormat, secret).get().saveFilesSync(toPath);
143+
return convertFile(fromPath, toFormat, secret, params).get().saveFilesSync(toPath);
144144
} catch (IOException | ExecutionException | InterruptedException e) {
145145
throw new RuntimeException(e);
146146
}
@@ -176,6 +176,21 @@ public static Path convertRemoteFile(String url, String toPathToFile, String sec
176176
}
177177
}
178178

179+
public static List<Path> convertRemoteFileToDir(String url, String toFormat, String outputDirectory, Param... params) {
180+
return convertRemoteFileToDir(url, toFormat, outputDirectory, Config.defaults().getSecret(), params);
181+
}
182+
183+
public static List<Path> convertRemoteFileToDir(String url, String toFormat, String outputDirectory, String secret, Param... params) {
184+
RemoteUploadResponse response = Http.remoteUpload(url, Config.defaults(secret));
185+
try {
186+
Path toPath = Paths.get(outputDirectory);
187+
Param[] fileParam = new Param[]{new Param("file", response.FileId)};
188+
return convert(response.FileExt, toFormat, Param.concat(fileParam, params), Config.defaults(secret)).get().saveFilesSync(toPath);
189+
} catch (ExecutionException | InterruptedException e) {
190+
throw new RuntimeException(e);
191+
}
192+
}
193+
179194
private static String getFileExtension(Path path) {
180195
String name = path.getFileName().toString();
181196
return name.substring(name.lastIndexOf(".") + 1);

src/com/convertapi/examples/SimpleConversion.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ public static void main(String[] args) {
1414
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
1515

1616
// Simplified file to file conversion example
17-
ConvertApi.convert("test-files/test.docx", "/tmp/result.pdf");
17+
ConvertApi.convertFile("test-files/test.docx", "/tmp/result.pdf");
1818

1919
// Simplified file to multiple files conversion example
20-
ConvertApi.convertFile("test-files/test.docx", "jpg", "/tmp");
20+
ConvertApi.convertFileToDir("test-files/test.docx", "jpg", "/tmp");
2121

2222
// Simplified web site to pdf conversion example
2323
ConvertApi.convertUrl("http://example.com", "/tmp/example.pdf");
2424

2525
// Simplified remote file to local file conversion example
26-
ConvertApi.convertRemoteFile("http://www.pdf995.com/samples/pdf.pdf", "/tmp/example.jpg");
26+
ConvertApi.convertRemoteFile("https://calibre-ebook.com/downloads/demos/demo.docx", "/tmp/demo.pdf");
27+
28+
// Simplified remote file to local file conversion example
29+
ConvertApi.convertRemoteFileToDir("http://www.pdf995.com/samples/pdf.pdf", "jpg", "/tmp");
2730
}
2831
}

0 commit comments

Comments
 (0)