Skip to content

Commit 27f018d

Browse files
committed
ConversionResult
1 parent 911b261 commit 27f018d

File tree

7 files changed

+148
-34
lines changed

7 files changed

+148
-34
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baltsoft;
2+
3+
import com.baltsoft.Model.ConversionResponse;
4+
5+
import java.nio.file.Files;
6+
import java.nio.file.NotDirectoryException;
7+
import java.nio.file.Path;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.concurrent.ExecutionException;
12+
13+
public class ConversionResult {
14+
private final CompletableFuture<ConversionResponse> responseFuture;
15+
16+
public ConversionResult(CompletableFuture<ConversionResponse> responseFuture) {
17+
this.responseFuture = responseFuture;
18+
}
19+
20+
public CompletableFuture<ConversionResponse> getResponseFuture() {
21+
return responseFuture;
22+
}
23+
24+
public int fileCount() throws ExecutionException, InterruptedException {
25+
return responseFuture.get().Files.length;
26+
}
27+
28+
public ConversionResultFile file(int index) {
29+
return new ConversionResultFile(responseFuture, index);
30+
}
31+
32+
public Path saveFile(Path file) throws ExecutionException, InterruptedException {
33+
return new ConversionResultFile(responseFuture, 0).saveFile(file).get();
34+
}
35+
36+
public List<Path> saveFiles(Path directory) throws ExecutionException, InterruptedException, NotDirectoryException {
37+
if (!Files.isDirectory(directory)) throw new NotDirectoryException(directory.toString());
38+
List<Path> paths = new ArrayList<Path>();
39+
for (int i = 0; i < responseFuture.get().Files.length; i++) {
40+
paths.add(new ConversionResultFile(responseFuture, i).saveFile(directory).get());
41+
}
42+
return paths;
43+
}
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baltsoft;
2+
3+
import com.baltsoft.Model.ConversionResponse;
4+
import com.baltsoft.Model.ConversionResponseFile;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.nio.file.StandardCopyOption;
12+
import java.util.concurrent.CompletableFuture;
13+
import java.util.concurrent.ExecutionException;
14+
15+
public class ConversionResultFile {
16+
private final CompletableFuture<ConversionResponse> responseFuture;
17+
private final int fileIndex;
18+
19+
public ConversionResultFile(CompletableFuture<ConversionResponse> responseFuture, int fileIndex) {
20+
this.responseFuture = responseFuture;
21+
this.fileIndex = fileIndex;
22+
}
23+
24+
public ConversionResponseFile fileInfo() throws ExecutionException, InterruptedException {
25+
return responseFuture.get().Files[fileIndex];
26+
}
27+
28+
public CompletableFuture<InputStream> asStream() {
29+
return responseFuture.thenComposeAsync(r -> Http.get(r.Files[fileIndex].Url));
30+
}
31+
32+
public CompletableFuture<Path> saveFile(Path path) {
33+
return asStream().thenApplyAsync(s -> {
34+
try {
35+
Path filePath = Files.isDirectory(path) ? Paths.get(path.toString(), fileInfo().FileName) : path;
36+
Files.copy(s, filePath, StandardCopyOption.REPLACE_EXISTING);
37+
return filePath;
38+
} catch (IOException | ExecutionException | InterruptedException e) {
39+
throw new RuntimeException(e);
40+
}
41+
});
42+
}
43+
}

src/com/baltsoft/ConvertApi.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import okhttp3.HttpUrl;
66
import okhttp3.MultipartBody;
77
import okhttp3.Request;
8+
import okhttp3.internal.http.HttpHeaders;
9+
810
import java.io.IOException;
911
import java.util.Arrays;
1012
import java.util.List;
@@ -13,12 +15,12 @@
1315
public class ConvertApi {
1416
private static final List<String> IGNORE_PARAMS = Arrays.asList( "storefile", "async", "jobid", "timeout");
1517

16-
public static CompletableFuture<ConversionResponse> convert(String fromFormat, String toFormat, Param[] params) {
18+
public static ConversionResult convert(String fromFormat, String toFormat, Param[] params) {
1719
return convert(fromFormat, toFormat, params, Config.defaults());
1820
}
1921

20-
public static CompletableFuture<ConversionResponse> convert(String fromFormat, String toFormat, Param[] params, Config config) {
21-
return CompletableFuture.supplyAsync(() -> {
22+
public static ConversionResult convert(String fromFormat, String toFormat, Param[] params, Config config) {
23+
CompletableFuture<ConversionResponse> completableResponse = CompletableFuture.supplyAsync(() -> {
2224
HttpUrl url = Http.getUrlBuilder(config)
2325
.addPathSegment(fromFormat)
2426
.addPathSegment("to")
@@ -46,6 +48,7 @@ public static CompletableFuture<ConversionResponse> convert(String fromFormat, S
4648

4749
Request request = new Request.Builder()
4850
.url(url)
51+
.addHeader("Accept", "application/json")
4952
.post(multipartBuilder.build())
5053
.build();
5154

@@ -58,5 +61,7 @@ public static CompletableFuture<ConversionResponse> convert(String fromFormat, S
5861

5962
return new Gson().fromJson(bodyString, ConversionResponse.class);
6063
});
64+
65+
return new ConversionResult(completableResponse);
6166
}
6267
}

src/com/baltsoft/Http.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import okhttp3.HttpUrl;
44
import okhttp3.OkHttpClient;
5+
import okhttp3.Request;
6+
import okhttp3.Response;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.concurrent.CompletableFuture;
511

612
public class Http {
713
private static OkHttpClient client = new OkHttpClient();
@@ -17,4 +23,17 @@ public static HttpUrl.Builder getUrlBuilder(Config config) {
1723
.addQueryParameter("timeout", String.valueOf(config.getTimeout()))
1824
.addQueryParameter("secret", config.getSecret());
1925
}
20-
}
26+
27+
public static CompletableFuture<InputStream> get(String url) {
28+
return CompletableFuture.supplyAsync(() -> {
29+
Request request = new Request.Builder().url(url).build();
30+
Response response = null;
31+
try {
32+
response = getClient().newCall(request).execute();
33+
} catch (IOException e) {
34+
throw new RuntimeException(e);
35+
}
36+
return response.body().byteStream();
37+
});
38+
}
39+
}

src/com/baltsoft/Main.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package com.baltsoft;
22

3-
import com.baltsoft.Model.ConversionResponse;
4-
5-
import java.io.File;
63
import java.io.IOException;
74
import java.net.URISyntaxException;
8-
import java.util.concurrent.CompletableFuture;
5+
import java.nio.file.Paths;
96
import java.util.concurrent.ExecutionException;
107

118
public class Main {
129

1310
public static void main(String[] args) throws IOException, URISyntaxException, ExecutionException, InterruptedException {
14-
Config.setDefaultSecret("");
15-
CompletableFuture<ConversionResponse> rez = ConvertApi.convert("docx", "pdf", new Param[]{new Param("File", new File("/home/jon/trinti/test.docx"))});
16-
17-
String s = ""; //f.get();
18-
String a = s;
19-
Thread.sleep(100000);
11+
Config.setDefaultSecret("1234567890123456");
12+
ConversionResult rez = ConvertApi.convert("docx", "jpg", new Param[]{new Param("File", Paths.get("/home/jon/trinti/test.docx"))});
13+
System.out.println("LIAU");
14+
rez.saveFiles(Paths.get("/tmp"));
15+
//
16+
// String s = ""; //f.get();
17+
// String a = s;
18+
// Thread.sleep(100000);
2019

2120

2221
}

src/com/baltsoft/Param.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
import javax.activation.MimetypesFileTypeMap;
77
import java.io.*;
88
import java.math.BigDecimal;
9+
import java.nio.file.Path;
10+
import java.util.Arrays;
11+
import java.util.List;
912
import java.util.concurrent.CompletableFuture;
1013
import java.util.concurrent.ExecutionException;
1114

1215
public class Param {
1316
private String name;
14-
private CompletableFuture<String[]> values;
17+
private CompletableFuture<List<String>> values;
1518
// private InputStream streamValue;
1619
private MediaType fileContentType;
1720
private String fileExtension;
18-
private File file;
21+
private Path file;
1922
private Config config = Config.defaults();
2023

2124
public Param(String name, String[] values) {
@@ -38,21 +41,26 @@ public Param(String name, BigDecimal value) {
3841
// public Param(String name, InputStream value, String fileFormat) {
3942
// }
4043

41-
public Param(String name, File value) throws FileNotFoundException {
44+
public Param(String name, Path value) throws FileNotFoundException {
4245
this(name, value, Config.defaults());
4346
}
4447

45-
public Param(String name, File value, Config config) throws FileNotFoundException {
48+
public Param(String name, Path value, Config config) throws FileNotFoundException {
4649
this(name, new String[]{});
4750
file = value;
48-
String contentTypeString = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(file);
51+
String contentTypeString = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(file.toFile());
4952
fileContentType = MediaType.parse(contentTypeString);
5053
fileExtension = getFileExtension(file);
5154
values = upload(file, fileContentType, config);
5255
}
5356

54-
public Param(String name, CompletableFuture<ConversionResponse> value) throws ExecutionException, InterruptedException {
57+
public Param(String name, ConversionResult value) throws ExecutionException, InterruptedException {
5558
this(name, new String[]{});
59+
60+
value.getResponseFuture().thenApplyAsync(r -> {
61+
String[] urls = Arrays.stream(r.Files).map(Param::apply).;
62+
});
63+
5664
ConversionResponse conversionResponse = value.get();
5765
String[] urls = new String[conversionResponse.Files.length];
5866
for (int i = 0; i < conversionResponse.Files.length; i++) {
@@ -61,25 +69,29 @@ public Param(String name, CompletableFuture<ConversionResponse> value) throws Ex
6169
this.values = CompletableFuture.completedFuture(urls);
6270
}
6371

64-
private static CompletableFuture<String[]> upload(File file, MediaType fileContentType, Config config) {
72+
private static CompletableFuture<String[]> upload(Path file, MediaType fileContentType, Config config) {
6573
return CompletableFuture.supplyAsync(() -> {
6674
Request request = new Request.Builder()
6775
.url(Http.getUrlBuilder(config).addPathSegment("upload")
68-
.addQueryParameter("filename", file.getName())
76+
.addQueryParameter("filename", file.getFileName().toString())
6977
.build())
70-
.post(RequestBody.create(fileContentType, file))
78+
.post(RequestBody.create(fileContentType, file.toFile()))
7179
.build();
7280

7381
String bodyString = null;
7482
try {
7583
bodyString = Http.getClient().newCall(request).execute().body().string();
7684
} catch (IOException e) {
77-
e.printStackTrace();
85+
throw new RuntimeException(e);
7886
}
7987
return new String[] {bodyString};
8088
});
8189
}
8290

91+
private static String apply(ConversionResponseFile f) {
92+
return f.Url;
93+
}
94+
8395
public String getName() {
8496
return name;
8597
}
@@ -89,8 +101,8 @@ public Param setConfig(Config config) {
89101
return this;
90102
}
91103

92-
private String getFileExtension(File file) {
93-
String name = file.getName();
104+
private String getFileExtension(Path file) {
105+
String name = file.getFileName().toString();
94106
try {
95107
return name.substring(name.lastIndexOf(".") + 2);
96108
} catch (Exception e) {

src/com/baltsoft/Response.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)