Skip to content

Commit 7645150

Browse files
committed
Param without multivalues
1 parent 27f018d commit 7645150

File tree

7 files changed

+87
-121
lines changed

7 files changed

+87
-121
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baltsoft;
2+
3+
public class ConversionException extends RuntimeException{
4+
public ConversionException(String message){
5+
super(message);
6+
}
7+
}

src/com/baltsoft/ConversionResult.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,23 @@ public ConversionResult(CompletableFuture<ConversionResponse> responseFuture) {
1717
this.responseFuture = responseFuture;
1818
}
1919

20-
public CompletableFuture<ConversionResponse> getResponseFuture() {
21-
return responseFuture;
20+
public CompletableFuture<Integer> fileCount() throws ExecutionException, InterruptedException {
21+
return responseFuture.thenApplyAsync(r -> r.Files.length);
2222
}
2323

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);
24+
public CompletableFuture<ConversionResultFile> getFile(int index) {
25+
return responseFuture.thenApplyAsync(r -> new ConversionResultFile(r.Files[index]));
3026
}
3127

3228
public Path saveFile(Path file) throws ExecutionException, InterruptedException {
33-
return new ConversionResultFile(responseFuture, 0).saveFile(file).get();
29+
return getFile(0).thenCompose(f -> f.saveFile(file)).get();
3430
}
3531

3632
public List<Path> saveFiles(Path directory) throws ExecutionException, InterruptedException, NotDirectoryException {
3733
if (!Files.isDirectory(directory)) throw new NotDirectoryException(directory.toString());
3834
List<Path> paths = new ArrayList<Path>();
3935
for (int i = 0; i < responseFuture.get().Files.length; i++) {
40-
paths.add(new ConversionResultFile(responseFuture, i).saveFile(directory).get());
36+
paths.add(getFile(i).thenCompose(f -> f.saveFile(directory)).get());
4137
}
4238
return paths;
4339
}

src/com/baltsoft/ConversionResultFile.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.baltsoft;
22

3-
import com.baltsoft.Model.ConversionResponse;
43
import com.baltsoft.Model.ConversionResponseFile;
54

65
import java.io.IOException;
@@ -10,32 +9,37 @@
109
import java.nio.file.Paths;
1110
import java.nio.file.StandardCopyOption;
1211
import java.util.concurrent.CompletableFuture;
13-
import java.util.concurrent.ExecutionException;
1412

1513
public class ConversionResultFile {
16-
private final CompletableFuture<ConversionResponse> responseFuture;
17-
private final int fileIndex;
14+
private final ConversionResponseFile conversionResponseFile;
1815

19-
public ConversionResultFile(CompletableFuture<ConversionResponse> responseFuture, int fileIndex) {
20-
this.responseFuture = responseFuture;
21-
this.fileIndex = fileIndex;
16+
public ConversionResultFile(ConversionResponseFile conversionResponseFile) {
17+
this.conversionResponseFile = conversionResponseFile;
2218
}
2319

24-
public ConversionResponseFile fileInfo() throws ExecutionException, InterruptedException {
25-
return responseFuture.get().Files[fileIndex];
20+
public String getName() {
21+
return conversionResponseFile.FileName;
22+
}
23+
24+
public int getSize() {
25+
return conversionResponseFile.FileSize;
26+
}
27+
28+
public String getUrl() {
29+
return conversionResponseFile.Url;
2630
}
2731

2832
public CompletableFuture<InputStream> asStream() {
29-
return responseFuture.thenComposeAsync(r -> Http.get(r.Files[fileIndex].Url));
33+
return Http.requestGet(getUrl());
3034
}
3135

3236
public CompletableFuture<Path> saveFile(Path path) {
3337
return asStream().thenApplyAsync(s -> {
3438
try {
35-
Path filePath = Files.isDirectory(path) ? Paths.get(path.toString(), fileInfo().FileName) : path;
39+
Path filePath = Files.isDirectory(path) ? Paths.get(path.toString(), getName()) : path;
3640
Files.copy(s, filePath, StandardCopyOption.REPLACE_EXISTING);
3741
return filePath;
38-
} catch (IOException | ExecutionException | InterruptedException e) {
42+
} catch (IOException e) {
3943
throw new RuntimeException(e);
4044
}
4145
});

src/com/baltsoft/ConvertApi.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import okhttp3.HttpUrl;
66
import okhttp3.MultipartBody;
77
import okhttp3.Request;
8-
import okhttp3.internal.http.HttpHeaders;
8+
import okhttp3.Response;
99

1010
import java.io.IOException;
1111
import java.util.Arrays;
1212
import java.util.List;
1313
import java.util.concurrent.CompletableFuture;
14+
import java.util.concurrent.ExecutionException;
1415

1516
public class ConvertApi {
1617
private static final List<String> IGNORE_PARAMS = Arrays.asList( "storefile", "async", "jobid", "timeout");
@@ -32,16 +33,9 @@ public static ConversionResult convert(String fromFormat, String toFormat, Param
3233
for (Param param: params) {
3334
if (!IGNORE_PARAMS.contains(param.getName())) {
3435
try {
35-
String[] values = param.getValues();
36-
if (values.length == 1) {
37-
multipartBuilder.addFormDataPart(param.getName(), values[0]);
38-
} else {
39-
for (int i = 0; i < values.length; i++) {
40-
multipartBuilder.addFormDataPart(param.getName() + "[" + i + "]", values[i]);
41-
}
42-
}
43-
} catch (Exception e) {
44-
e.printStackTrace();
36+
multipartBuilder.addFormDataPart(param.getName(), param.getValue());
37+
} catch (ExecutionException | InterruptedException e) {
38+
throw new RuntimeException(e);
4539
}
4640
}
4741
}
@@ -52,11 +46,15 @@ public static ConversionResult convert(String fromFormat, String toFormat, Param
5246
.post(multipartBuilder.build())
5347
.build();
5448

55-
String bodyString = null;
49+
String bodyString;
5650
try {
57-
bodyString = Http.getClient().newCall(request).execute().body().string();
51+
Response response = Http.getClient().newCall(request).execute();
52+
bodyString = response.body().string();
53+
if (response.code() != 200) {
54+
throw new ConversionException(bodyString);
55+
}
5856
} catch (IOException e) {
59-
e.printStackTrace();
57+
throw new RuntimeException(e);
6058
}
6159

6260
return new Gson().fromJson(bodyString, ConversionResponse.class);

src/com/baltsoft/Http.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static HttpUrl.Builder getUrlBuilder(Config config) {
2424
.addQueryParameter("secret", config.getSecret());
2525
}
2626

27-
public static CompletableFuture<InputStream> get(String url) {
27+
public static CompletableFuture<InputStream> requestGet(String url) {
2828
return CompletableFuture.supplyAsync(() -> {
2929
Request request = new Request.Builder().url(url).build();
3030
Response response = null;

src/com/baltsoft/Main.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ public class Main {
99

1010
public static void main(String[] args) throws IOException, URISyntaxException, ExecutionException, InterruptedException {
1111
Config.setDefaultSecret("1234567890123456");
12+
System.out.println("1");
1213
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);
19-
20-
14+
System.out.println("2");
15+
ConversionResult rez1 = ConvertApi.convert("jpg", "pdf", new Param[]{new Param("File", rez)});
16+
System.out.println("3");
17+
rez1.saveFiles(Paths.get("/tmp"));
2118
}
2219

2320
}

src/com/baltsoft/Param.java

Lines changed: 40 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
package com.baltsoft;
22

3-
import com.baltsoft.Model.ConversionResponse;
4-
import com.baltsoft.Model.ConversionResponseFile;
53
import okhttp3.*;
64
import javax.activation.MimetypesFileTypeMap;
75
import java.io.*;
86
import java.math.BigDecimal;
7+
import java.nio.file.Files;
98
import java.nio.file.Path;
10-
import java.util.Arrays;
11-
import java.util.List;
129
import java.util.concurrent.CompletableFuture;
1310
import java.util.concurrent.ExecutionException;
1411

1512
public class Param {
1613
private String name;
17-
private CompletableFuture<List<String>> values;
18-
// private InputStream streamValue;
19-
private MediaType fileContentType;
20-
private String fileExtension;
21-
private Path file;
14+
private CompletableFuture<String> value;
2215
private Config config = Config.defaults();
2316

24-
public Param(String name, String[] values) {
25-
this.name = name.toLowerCase();
26-
this.values = CompletableFuture.completedFuture(values);
27-
}
28-
2917
public Param(String name, String value) {
30-
this(name, new String[]{value});
18+
this.name = name.toLowerCase();
19+
this.value = CompletableFuture.completedFuture(value);
3120
}
3221

3322
public Param(String name, int value) {
@@ -38,58 +27,34 @@ public Param(String name, BigDecimal value) {
3827
this(name, String.valueOf(value));
3928
}
4029

41-
// public Param(String name, InputStream value, String fileFormat) {
42-
// }
43-
44-
public Param(String name, Path value) throws FileNotFoundException {
45-
this(name, value, Config.defaults());
30+
public Param(String name, byte[] value, String fileFormat) {
31+
this(name, value, fileFormat, Config.defaults());
4632
}
4733

48-
public Param(String name, Path value, Config config) throws FileNotFoundException {
49-
this(name, new String[]{});
50-
file = value;
51-
String contentTypeString = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(file.toFile());
52-
fileContentType = MediaType.parse(contentTypeString);
53-
fileExtension = getFileExtension(file);
54-
values = upload(file, fileContentType, config);
34+
public Param(String name, byte[] value, String fileFormat, Config config) {
35+
this(name, "");
36+
String fileName = "getFile." + fileFormat;
37+
String contentTypeString = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(fileName);
38+
this.value = upload(value, fileName, MediaType.parse(contentTypeString), config);
5539
}
5640

57-
public Param(String name, ConversionResult value) throws ExecutionException, InterruptedException {
58-
this(name, new String[]{});
59-
60-
value.getResponseFuture().thenApplyAsync(r -> {
61-
String[] urls = Arrays.stream(r.Files).map(Param::apply).;
62-
});
63-
64-
ConversionResponse conversionResponse = value.get();
65-
String[] urls = new String[conversionResponse.Files.length];
66-
for (int i = 0; i < conversionResponse.Files.length; i++) {
67-
urls[i] = conversionResponse.Files[i].Url;
68-
}
69-
this.values = CompletableFuture.completedFuture(urls);
41+
public Param(String name, Path value) throws IOException {
42+
this(name, value, Config.defaults());
7043
}
7144

72-
private static CompletableFuture<String[]> upload(Path file, MediaType fileContentType, Config config) {
73-
return CompletableFuture.supplyAsync(() -> {
74-
Request request = new Request.Builder()
75-
.url(Http.getUrlBuilder(config).addPathSegment("upload")
76-
.addQueryParameter("filename", file.getFileName().toString())
77-
.build())
78-
.post(RequestBody.create(fileContentType, file.toFile()))
79-
.build();
45+
public Param(String name, Path value, Config config) throws IOException {
46+
this(name, "");
47+
String contentTypeString = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(value.toFile());
48+
this.value = upload(Files.readAllBytes(value), value.getFileName().toString(), MediaType.parse(contentTypeString), config);
49+
}
8050

81-
String bodyString = null;
82-
try {
83-
bodyString = Http.getClient().newCall(request).execute().body().string();
84-
} catch (IOException e) {
85-
throw new RuntimeException(e);
86-
}
87-
return new String[] {bodyString};
88-
});
51+
public Param(String name, ConversionResult value) throws ExecutionException, InterruptedException {
52+
this(name, value, 0);
8953
}
9054

91-
private static String apply(ConversionResponseFile f) {
92-
return f.Url;
55+
public Param(String name, ConversionResult value, int fileIndex) throws ExecutionException, InterruptedException {
56+
this(name, "");
57+
this.value = value.getFile(fileIndex).thenApplyAsync(f -> f.getUrl());
9358
}
9459

9560
public String getName() {
@@ -101,24 +66,23 @@ public Param setConfig(Config config) {
10166
return this;
10267
}
10368

104-
private String getFileExtension(Path file) {
105-
String name = file.getFileName().toString();
106-
try {
107-
return name.substring(name.lastIndexOf(".") + 2);
108-
} catch (Exception e) {
109-
return "";
110-
}
69+
public String getValue() throws ExecutionException, InterruptedException {
70+
return this.value.get();
11171
}
11272

113-
public MediaType getFileContentType() {
114-
return fileContentType;
115-
}
116-
117-
public String getFileExtension() {
118-
return fileExtension;
119-
}
120-
121-
public String[] getValues() throws ExecutionException, InterruptedException {
122-
return values.get();
73+
private static CompletableFuture<String> upload(byte[] data, String fileName, MediaType fileContentType, Config config) {
74+
return CompletableFuture.supplyAsync(() -> {
75+
Request request = new Request.Builder()
76+
.url(Http.getUrlBuilder(config).addPathSegment("upload")
77+
.addQueryParameter("filename", fileName.toString())
78+
.build())
79+
.post(RequestBody.create(fileContentType, data))
80+
.build();
81+
try {
82+
return Http.getClient().newCall(request).execute().body().string();
83+
} catch (IOException e) {
84+
throw new RuntimeException(e);
85+
}
86+
});
12387
}
124-
}
88+
}

0 commit comments

Comments
 (0)