Skip to content

Commit 441a3f9

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

File tree

6 files changed

+133
-15
lines changed

6 files changed

+133
-15
lines changed

convertapi.iml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
11+
<excludeFolder url="file://$MODULE_DIR$/target" />
12+
</content>
13+
<orderEntry type="inheritedJdk" />
14+
<orderEntry type="sourceFolder" forTests="false" />
15+
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.5" level="project" />
16+
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.10.0" level="project" />
17+
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.14.0" level="project" />
18+
</component>
19+
</module>

src/com/convertapi/ConvertApi.java

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.convertapi;
22

33
import com.convertapi.model.ConversionResponse;
4+
import com.convertapi.model.RemoteUploadResponse;
45
import com.convertapi.model.User;
56
import com.google.gson.Gson;
67
import okhttp3.HttpUrl;
@@ -107,29 +108,74 @@ public static User getUser(Config config) {
107108
}
108109

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

114-
public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat, String secret) throws IOException {
115-
return convert(getFileExtension(fromFile), toFormat, new Param[]{new Param("file", fromFile)}, Config.defaults(secret));
115+
public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat, String secret, Param... params) throws IOException {
116+
Param[] fileParam = new Param[]{new Param("file", fromFile)};
117+
return convert(getFileExtension(fromFile), toFormat, Param.concat(fileParam, params), Config.defaults(secret));
116118
}
117119

118120
@SuppressWarnings("unused")
119121
public static void convert(String fromPathToFile, String toPathToFile) {
120122
convert(fromPathToFile, toPathToFile, Config.defaults().getSecret());
121123
}
122124

123-
public static void convert(String fromPathToFile, String toPathToFile, String secret) {
125+
public static void convert(String fromPathToFile, String toPathToFile, String secret, Param... params) {
124126
try {
125127
Path fromPath = Paths.get(fromPathToFile);
126128
Path toPath = Paths.get(toPathToFile);
127-
convert(fromPath, getFileExtension(toPath), secret).get().saveFile(toPath).get();
129+
convert(fromPath, getFileExtension(toPath), secret, params).get().saveFile(toPath).get();
128130
} catch (IOException | ExecutionException | InterruptedException e) {
129131
throw new RuntimeException(e);
130132
}
131133
}
132134

135+
public static List<Path> convertFile(String fromPathToFile, String toFormat, String outputDirectory, Param... params) {
136+
return convertFile(fromPathToFile, toFormat, outputDirectory, Config.defaults().getSecret(), params);
137+
}
138+
139+
public static List<Path> convertFile(String fromPathToFile, String toFormat, String outputDirectory, String secret, Param... params) {
140+
try {
141+
Path fromPath = Paths.get(fromPathToFile);
142+
Path toPath = Paths.get(outputDirectory);
143+
return convert(fromPath, toFormat, secret).get().saveFilesSync(toPath);
144+
} catch (IOException | ExecutionException | InterruptedException e) {
145+
throw new RuntimeException(e);
146+
}
147+
}
148+
149+
public static Path convertUrl(String url, String toPathToFile, Param... params) {
150+
return convertUrl(url, toPathToFile, Config.defaults().getSecret(), params);
151+
}
152+
153+
public static Path convertUrl(String url, String toPathToFile, String secret, Param... params) {
154+
try {
155+
Path toPath = Paths.get(toPathToFile);
156+
Param[] urlParam = new Param[]{new Param("url", url)};
157+
return convert("web", getFileExtension(toPath), Param.concat(urlParam, params), Config.defaults(secret)).get().saveFile(toPath).get();
158+
} catch (ExecutionException | InterruptedException e) {
159+
throw new RuntimeException(e);
160+
}
161+
}
162+
163+
public static Path convertRemoteFile(String url, String toPathToFile, Param... params) {
164+
return convertRemoteFile(url, toPathToFile, Config.defaults().getSecret(), params);
165+
}
166+
167+
public static Path convertRemoteFile(String url, String toPathToFile, String secret, Param... params) {
168+
RemoteUploadResponse response = Http.remoteUpload(url, Config.defaults(secret));
169+
try {
170+
Path toPath = Paths.get(toPathToFile);
171+
return convert(response.FileExt, getFileExtension(toPath), new Param[]{
172+
new Param("file", response.FileId)
173+
}, Config.defaults(secret)).get().saveFile(toPath).get();
174+
} catch (ExecutionException | InterruptedException e) {
175+
throw new RuntimeException(e);
176+
}
177+
}
178+
133179
private static String getFileExtension(Path path) {
134180
String name = path.getFileName().toString();
135181
return name.substring(name.lastIndexOf(".") + 1);

src/com/convertapi/Http.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.convertapi;
22

3-
import okhttp3.HttpUrl;
4-
import okhttp3.OkHttpClient;
5-
import okhttp3.Request;
6-
import okhttp3.Response;
3+
import com.convertapi.model.RemoteUploadResponse;
4+
import com.google.gson.Gson;
5+
import okhttp3.*;
76

87
import java.io.IOException;
98
import java.io.InputStream;
@@ -59,7 +58,34 @@ static CompletableFuture<Void> requestDelete(String url) {
5958
}
6059

6160
static Request.Builder getRequestBuilder() {
62-
String agent = String.format("ConvertAPI-Java/%.1f (%s)", 1.5, System.getProperty("os.name"));
61+
String agent = String.format("ConvertAPI-Java/%.1f (%s)", 1.6, System.getProperty("os.name"));
6362
return new Request.Builder().header("User-Agent", agent);
6463
}
64+
65+
static RemoteUploadResponse remoteUpload(String urlToFile, Config config) {
66+
HttpUrl url = Http.getUrlBuilder(config)
67+
.addPathSegment("upload-from-url")
68+
.addQueryParameter("url", urlToFile)
69+
.build();
70+
71+
Request request = Http.getRequestBuilder()
72+
.url(url)
73+
.method("POST", RequestBody.create(null, ""))
74+
.addHeader("Accept", "application/json")
75+
.build();
76+
77+
String bodyString;
78+
try {
79+
Response response = Http.getClient().newCall(request).execute();
80+
//noinspection ConstantConditions
81+
bodyString = response.body().string();
82+
if (response.code() != 200) {
83+
throw new ConversionException(bodyString, response.code());
84+
}
85+
} catch (IOException e) {
86+
throw new RuntimeException(e);
87+
}
88+
89+
return new Gson().fromJson(bodyString, RemoteUploadResponse.class);
90+
}
6591
}

src/com/convertapi/Param.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,19 @@ private static CompletableFuture<List<String>> upload(byte[] data, String fileNa
116116
.post(RequestBody.create(MediaType.parse("application/octet-stream"), data))
117117
.build();
118118
try {
119-
List<String> valueList = new ArrayList<>();
119+
String id = Http.getClient().newCall(request).execute().body().string();
120120
//noinspection ConstantConditions
121-
valueList.add(Http.getClient().newCall(request).execute().body().string());
122-
return valueList;
121+
return Collections.singletonList(id);
123122
} catch (IOException e) {
124123
throw new RuntimeException(e);
125124
}
126125
});
127126
}
127+
128+
public static Param[] concat(Param[] a, Param[] b) {
129+
Param[] result = new Param[a.length + b.length];
130+
System.arraycopy(a, 0, result, 0, a.length);
131+
System.arraycopy(b, 0, result, a.length, b.length);
132+
return result;
133+
}
128134
}

src/com/convertapi/examples/SimpleConversion.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.convertapi.examples;
22

3+
import com.convertapi.Config;
34
import com.convertapi.ConvertApi;
45

56
import static java.lang.System.getenv;
@@ -10,6 +11,18 @@
1011

1112
public class SimpleConversion {
1213
public static void main(String[] args) {
13-
ConvertApi.convert("test-files/test.docx", "result.pdf", getenv("CONVERTAPI_SECRET"));
14+
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
15+
16+
// Simplified file to file conversion example
17+
ConvertApi.convert("test-files/test.docx", "/tmp/result.pdf");
18+
19+
// Simplified file to multiple files conversion example
20+
ConvertApi.convertFile("test-files/test.docx", "jpg", "/tmp");
21+
22+
// Simplified web site to pdf conversion example
23+
ConvertApi.convertUrl("http://example.com", "/tmp/example.pdf");
24+
25+
// Simplified remote file to local file conversion example
26+
ConvertApi.convertRemoteFile("http://www.pdf995.com/samples/pdf.pdf", "/tmp/example.jpg");
1427
}
1528
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.convertapi.model;
2+
3+
@SuppressWarnings("CanBeFinal")
4+
public class RemoteUploadResponse {
5+
public String FileId;
6+
public String FileName;
7+
public String FileExt;
8+
}

0 commit comments

Comments
 (0)