Skip to content

Commit 71f3ca3

Browse files
committed
Result file index
1 parent e7ef6f8 commit 71f3ca3

File tree

7 files changed

+90
-23
lines changed

7 files changed

+90
-23
lines changed

convertapi-java.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<exclude-output />
55
<content url="file://$MODULE_DIR$">
66
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
<sourceFolder url="file://$MODULE_DIR$/src/com/convertapi/examples" isTestSource="false" />
78
<excludeFolder url="file://$MODULE_DIR$/.idea" />
89
<excludeFolder url="file://$MODULE_DIR$/lib" />
9-
<excludeFolder url="file://$MODULE_DIR$/src/com/convertapi/examples" />
1010
</content>
1111
<orderEntry type="inheritedJdk" />
1212
<orderEntry type="sourceFolder" forTests="false" />

src/com/convertapi/ConversionResult.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public Integer conversionCost() {
3737
}
3838

3939
public ConversionResultFile getFile(int index) {
40+
if (index < 0) index = response.Files.length + index;
4041
return new ConversionResultFile(response.Files[index]);
4142
}
4243

src/com/convertapi/ConvertApi.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import java.io.IOException;
1212
import java.nio.file.Path;
1313
import java.nio.file.Paths;
14+
import java.util.ArrayList;
1415
import java.util.Arrays;
16+
import java.util.HashMap;
1517
import java.util.List;
1618
import java.util.concurrent.CompletableFuture;
1719
import java.util.concurrent.ExecutionException;
@@ -22,7 +24,7 @@ public class ConvertApi {
2224
private static final List<String> IGNORE_PARAMS = Arrays.asList( "storefile", "async", "jobid", "timeout");
2325

2426
@SuppressWarnings("unused")
25-
public static CompletableFuture<ConversionResult> convert(String fromFormat, String toFormat, Param[] params) {
27+
public static CompletableFuture<ConversionResult> convert(String fromFormat, String toFormat, Param... params) {
2628
return convert(fromFormat, toFormat, params, Config.defaults());
2729
}
2830

@@ -38,18 +40,17 @@ public static CompletableFuture<ConversionResult> convert(String fromFormat, Str
3840
.build();
3941

4042
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
41-
for (Param param: params) {
42-
if (!IGNORE_PARAMS.contains(param.getName())) {
43-
try {
44-
if (param.getValue().size() == 1) {
45-
multipartBuilder.addFormDataPart(param.getName(), param.getValue().get(0));
46-
} else {
47-
for (int i = 0; i < param.getValue().size(); i++) {
48-
multipartBuilder.addFormDataPart(param.getName() + "[" + i + "]", param.getValue().get(i));
49-
}
43+
HashMap<String, List<String>> paramValues = getParamValues(params);
44+
45+
for (String name : paramValues.keySet()) {
46+
if (!IGNORE_PARAMS.contains(name)) {
47+
List<String> values = paramValues.get(name);
48+
if (paramValues.get(name).size() == 1) {
49+
multipartBuilder.addFormDataPart(name, values.get(0));
50+
} else {
51+
for (int i = 0; i < values.size(); i++) {
52+
multipartBuilder.addFormDataPart(name + "[" + i + "]", values.get(i));
5053
}
51-
} catch (ExecutionException | InterruptedException e) {
52-
throw new RuntimeException(e);
5354
}
5455
}
5556
}
@@ -133,4 +134,19 @@ private static String getFileExtension(Path path) {
133134
String name = path.getFileName().toString();
134135
return name.substring(name.lastIndexOf(".") + 1);
135136
}
137+
138+
private static HashMap<String, List<String>> getParamValues(Param[] params) {
139+
HashMap<String, List<String>> result = new HashMap<>();
140+
141+
try {
142+
for (Param param : params) {
143+
List<String> values = result.computeIfAbsent(param.getName(), (v) -> new ArrayList<>());
144+
values.addAll(param.getValue());
145+
}
146+
} catch (InterruptedException | ExecutionException e) {
147+
throw new RuntimeException(e);
148+
}
149+
150+
return result;
151+
}
136152
}

src/com/convertapi/Param.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.nio.file.Files;
1111
import java.nio.file.Path;
1212
import java.util.ArrayList;
13+
import java.util.Arrays;
1314
import java.util.List;
1415
import java.util.concurrent.CompletableFuture;
1516
import java.util.concurrent.ExecutionException;
@@ -83,6 +84,12 @@ public Param(String name, ConversionResult value, int fileIndex) {
8384
this.value = CompletableFuture.completedFuture(valueList);
8485
}
8586

87+
@SuppressWarnings("WeakerAccess")
88+
public Param(String name, CompletableFuture<ConversionResult> value, int fileIndex) {
89+
this(name);
90+
this.value = value.thenApply((res) -> Arrays.asList(res.getFile(fileIndex).getUrl()));
91+
}
92+
8693
@SuppressWarnings("unused")
8794
public Param(String name, CompletableFuture<ConversionResult> value) {
8895
this(name);

src/com/convertapi/examples/Advanced.java

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

3+
import com.convertapi.Config;
4+
import com.convertapi.ConversionResult;
5+
import com.convertapi.ConvertApi;
6+
import com.convertapi.Param;
7+
38
import java.io.IOException;
9+
import java.net.InetSocketAddress;
10+
import java.net.Proxy;
411
import java.nio.file.Path;
512
import java.nio.file.Paths;
613
import java.util.concurrent.CompletableFuture;
714
import java.util.concurrent.ExecutionException;
815
import java.util.concurrent.TimeUnit;
916

10-
import com.convertapi.Config;
11-
import com.convertapi.ConversionResult;
12-
import com.convertapi.ConvertApi;
13-
import com.convertapi.Param;
14-
1517
/**
1618
* Example of HTTP client setup to use HTTP proxy server.
1719
*/

src/com/convertapi/examples/CreatePdfThumbnail.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.concurrent.CompletableFuture;
1212
import java.util.concurrent.ExecutionException;
1313

14+
import static java.lang.System.getenv;
15+
1416
/**
1517
* Example of extracting first page from PDF and then chaining conversion PDF page to JPG.
1618
* https://www.convertapi.com/pdf-to-extract
@@ -19,22 +21,22 @@
1921

2022
public class CreatePdfThumbnail {
2123
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
22-
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
24+
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
2325
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
2426

2527
System.out.println("Creating PDF thumbnail");
2628

27-
CompletableFuture<ConversionResult> pdfFirstPageResult = ConvertApi.convert("pdf", "extract", new Param[]{
29+
CompletableFuture<ConversionResult> pdfFirstPageResult = ConvertApi.convert("pdf", "extract",
2830
new Param("file", Paths.get("test-files/test.pdf")),
2931
new Param("pagerange", "1")
30-
});
32+
);
3133

32-
CompletableFuture<ConversionResult> thumbnailResult = ConvertApi.convert("pdf", "jpg", new Param[]{
34+
CompletableFuture<ConversionResult> thumbnailResult = ConvertApi.convert("pdf", "jpg",
3335
new Param("file", pdfFirstPageResult),
3436
new Param("scaleimage", "true"),
3537
new Param("scaleproportions", "true"),
3638
new Param("imageheight", 300)
37-
});
39+
);
3840

3941
System.out.println("JPG thumbnail file saved to: " + thumbnailResult.get().saveFile(tempDir).get());
4042
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import com.convertapi.Config;
2+
import com.convertapi.ConversionResult;
3+
import com.convertapi.ConvertApi;
4+
import com.convertapi.Param;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.concurrent.CompletableFuture;
10+
import java.util.concurrent.ExecutionException;
11+
12+
import static java.lang.System.getenv;
13+
14+
/**
15+
* Example of extracting the first and the last page from PDF and then chaining merge conversion.
16+
* https://www.convertapi.com/pdf-to-extract
17+
* https://www.convertapi.com/pdf-to-merge
18+
*/
19+
20+
public class SplitAndMergePdf {
21+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
22+
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
23+
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
24+
25+
System.out.println("Creating PDF with the first and the last pages");
26+
27+
CompletableFuture<ConversionResult> splitResult = ConvertApi.convert("pdf", "split",
28+
new Param("file", Paths.get("test-files/test.pdf"))
29+
);
30+
31+
CompletableFuture<ConversionResult> mergeResult = ConvertApi.convert("pdf", "merge",
32+
new Param("files", splitResult, 0),
33+
new Param("files", "http://www.pdf995.com/samples/pdf.pdf"),
34+
new Param("files", splitResult, -1)
35+
);
36+
37+
System.out.println("PDF file saved to: " + mergeResult.get().saveFile(tempDir).get());
38+
}
39+
}

0 commit comments

Comments
 (0)