Skip to content

Commit a70b8e7

Browse files
authored
Merge pull request #28 from ConvertAPI/develop
Merge develop into master
2 parents e29e650 + 349c83c commit a70b8e7

File tree

6 files changed

+85
-28
lines changed

6 files changed

+85
-28
lines changed

examples/files/test.docx

-32 KB
Binary file not shown.

examples/files/test.pdf

-1.04 MB
Binary file not shown.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.convertapi.examples;
2+
3+
import com.convertapi.client.Config;
4+
import com.convertapi.client.ConversionResult;
5+
import com.convertapi.client.ConvertApi;
6+
import com.convertapi.client.Param;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.util.concurrent.CompletableFuture;
15+
import java.util.concurrent.ExecutionException;
16+
17+
import static java.lang.System.getenv;
18+
19+
/**
20+
* Example of the file conversion when data is passed as a stream.
21+
*/
22+
public class ConvertStream {
23+
24+
public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
25+
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
26+
27+
// Creating file data stream
28+
InputStream stream = Files.newInputStream(new File("src/main/resources/test.docx").toPath());
29+
30+
System.out.println("Converting stream of DOCX data to PDF");
31+
CompletableFuture<ConversionResult> result = ConvertApi.convert("docx", "pdf",
32+
new Param("file", stream, "test.docx")
33+
);
34+
35+
Path pdfFile = Paths.get(System.getProperty("java.io.tmpdir") + "/myfile.pdf");
36+
result.get().saveFile(pdfFile).get();
37+
38+
System.out.println("PDF file saved to: " + pdfFile.toString());
39+
}
40+
}

src/main/java/com/convertapi/client/ConvertApi.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static CompletableFuture<ConversionResult> convert(String fromFormat, Str
5252

5353
HttpUrl url = urlBuilder.addQueryParameter("storefile", "true").build();
5454

55-
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
55+
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
5656
HashMap<String, List<String>> paramValues = getParamValues(params);
5757

5858
for (String name : paramValues.keySet()) {
@@ -219,21 +219,20 @@ public static Path convertRemoteFile(String url, String toPathToFile, Param... p
219219

220220
@SuppressWarnings("unused")
221221
public static Path convertRemoteFile(String url, String toPathToFile, String secret, Param... params) {
222-
return convertRemoteFile(url, toPathToFile, Config.defaults(secret));
222+
return convertRemoteFile(url, toPathToFile, Config.defaults(secret), params);
223223
}
224224

225225
@SuppressWarnings("unused")
226226
public static Path convertRemoteFile(String url, String toPathToFile, String token, String apiKey, Param... params) {
227-
return convertRemoteFile(url, toPathToFile, Config.defaults(token, apiKey));
227+
return convertRemoteFile(url, toPathToFile, Config.defaults(token, apiKey), params);
228228
}
229229

230230
public static Path convertRemoteFile(String url, String toPathToFile, Config config, Param... params) {
231231
RemoteUploadResponse response = Http.remoteUpload(url, config);
232232
try {
233233
Path toPath = Paths.get(toPathToFile);
234-
return convert(response.FileExt, getFileExtension(toPath), new Param[]{
235-
new Param("file", response.FileId)
236-
}, config).get().saveFile(toPath).get();
234+
Param[] fileParam = new Param[]{new Param("file", response.FileId)};
235+
return convert(response.FileExt, getFileExtension(toPath), Param.concat(fileParam, params), config).get().saveFile(toPath).get();
237236
} catch (ExecutionException | InterruptedException e) {
238237
throw new RuntimeException(e);
239238
}

src/main/java/com/convertapi/client/Http.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,32 @@ static HttpUrl.Builder getUrlBuilder(Config config) {
4242
static CompletableFuture<InputStream> requestGet(String url) {
4343
return CompletableFuture.supplyAsync(() -> {
4444
Request request = getRequestBuilder().url(url).build();
45-
Response response;
4645
try {
47-
response = getClient().newCall(request).execute();
46+
Response response = getClient().newCall(request).execute();
47+
ResponseBody body = response.body();
48+
if (body != null) {
49+
if (response.code() != 200) {
50+
throw new ConversionException(body.string(), response.code());
51+
}
52+
return body.byteStream();
53+
} else {
54+
throw new ConversionException("Response body is empty", response.code());
55+
}
4856
} catch (IOException e) {
4957
throw new RuntimeException(e);
5058
}
51-
//noinspection ConstantConditions
52-
return response.body().byteStream();
5359
});
5460
}
5561

5662
static CompletableFuture<Void> requestDelete(String url) {
5763
return CompletableFuture.supplyAsync(() -> {
5864
Request request = getRequestBuilder().delete().url(url).build();
5965
try {
60-
getClient().newCall(request).execute();
66+
getClient().newCall(request).execute().close();
67+
return null;
6168
} catch (IOException e) {
6269
throw new RuntimeException(e);
6370
}
64-
return null;
6571
});
6672
}
6773

@@ -77,8 +83,8 @@ static RemoteUploadResponse remoteUpload(String urlToFile, Config config) {
7783
.build();
7884

7985
Request request = Http.getRequestBuilder()
80-
.url(url)
81-
.method("POST", RequestBody.create(null, ""))
86+
.url(url)
87+
.method("POST", RequestBody.create("", null))
8288
.addHeader("Accept", "application/json")
8389
.build();
8490

src/main/java/com/convertapi/client/Param.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import okhttp3.MediaType;
44
import okhttp3.Request;
5+
import okhttp3.Response;
6+
import okhttp3.ResponseBody;
57

68
import java.io.IOException;
79
import java.io.InputStream;
@@ -101,29 +103,39 @@ public List<String> getValue() throws ExecutionException, InterruptedException {
101103
return this.value.get();
102104
}
103105

104-
public CompletableFuture<Void> delete() {
105-
return isUploadedFile
106-
? value.thenCompose(urls -> Http.requestDelete(urls.get(0)))
107-
: CompletableFuture.completedFuture(null);
108-
}
109-
110106
private static CompletableFuture<List<String>> upload(InputStream stream, String fileName, Config config) {
111107
return CompletableFuture.supplyAsync(() -> {
112108
Request request = Http.getRequestBuilder()
113-
.url(Http.getUrlBuilder(config).addPathSegment("upload")
114-
.addQueryParameter("filename", fileName)
115-
.build())
116-
.post(RequestBodyStream.create(MediaType.parse("application/octet-stream"), stream))
117-
.build();
118-
try {
119-
String id = Http.getClient().newCall(request).execute().body().string();
120-
return Collections.singletonList(id);
109+
.url(Http.getUrlBuilder(config).addPathSegment("upload")
110+
.addQueryParameter("filename", fileName)
111+
.build())
112+
.post(RequestBodyStream.create(MediaType.parse("application/octet-stream"), stream))
113+
.build();
114+
115+
try (Response response = Http.getClient().newCall(request).execute()) {
116+
ResponseBody body = response.body();
117+
if (body != null) {
118+
if (response.code() != 200) {
119+
throw new ConversionException(body.string(), response.code());
120+
}
121+
String id = body.string();
122+
return Collections.singletonList(id);
123+
} else {
124+
throw new ConversionException("Response body is empty", response.code());
125+
}
121126
} catch (IOException e) {
122127
throw new RuntimeException(e);
123128
}
124129
});
125130
}
126131

132+
@SuppressWarnings("unused")
133+
public CompletableFuture<Void> delete() {
134+
return isUploadedFile
135+
? value.thenCompose(urls -> Http.requestDelete(urls.get(0)))
136+
: CompletableFuture.completedFuture(null);
137+
}
138+
127139
public static Param[] concat(Param[] a, Param[] b) {
128140
Param[] result = new Param[a.length + b.length];
129141
System.arraycopy(a, 0, result, 0, a.length);

0 commit comments

Comments
 (0)