Repository navigation
Expand file tree
/
Copy pathParam.java
More file actions
133 lines (112 loc) · 4.39 KB
/
Copy pathParam.java
File metadata and controls
133 lines (112 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.convertapi;
import okhttp3.MediaType;
import okhttp3.Request;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static java.nio.file.StandardOpenOption.READ;
@SuppressWarnings("WeakerAccess")
public class Param {
private final String name;
private CompletableFuture<List<String>> value;
private boolean isUploadedFile = false;
@SuppressWarnings("unused")
private Param(String name) {
this.name = name.toLowerCase();
}
@SuppressWarnings("unused")
public Param(String name, String value) {
this(name.toLowerCase());
List<String> valueList = new ArrayList<>();
valueList.add(value);
this.value = CompletableFuture.completedFuture(valueList);
}
@SuppressWarnings("unused")
public Param(String name, int value) {
this(name, String.valueOf(value));
}
@SuppressWarnings("unused")
public Param(String name, BigDecimal value) {
this(name, String.valueOf(value));
}
public Param(String name, Path value) throws IOException {
this(name, value, Config.defaults());
}
@SuppressWarnings("WeakerAccess")
public Param(String name, InputStream stream, String fileName) {
this(name, stream, fileName, Config.defaults());
}
@SuppressWarnings("WeakerAccess")
public Param(String name, InputStream stream, String fileName, Config config) {
this(name);
this.value = upload(stream, fileName, config);
isUploadedFile = true;
}
@SuppressWarnings("WeakerAccess")
public Param(String name, Path value, Config config) throws IOException {
this(name, Files.newInputStream(value, READ), value.getFileName().toString(), config);
}
@SuppressWarnings("unused")
public Param(String name, ConversionResult value) {
this(name, value, 0);
this.value = CompletableFuture.completedFuture(value.urls());
}
@SuppressWarnings("WeakerAccess")
public Param(String name, ConversionResult value, int fileIndex) {
this(name);
List<String> valueList = new ArrayList<>();
valueList.add(value.getFile(fileIndex).getUrl());
this.value = CompletableFuture.completedFuture(valueList);
}
@SuppressWarnings("WeakerAccess")
public Param(String name, CompletableFuture<ConversionResult> value, int fileIndex) {
this(name);
this.value = value.thenApply((res) -> Collections.singletonList(res.getFile(fileIndex).getUrl()));
}
@SuppressWarnings("unused")
public Param(String name, CompletableFuture<ConversionResult> value) {
this(name);
this.value = value.thenApply(ConversionResult::urls);
}
public String getName() {
return name;
}
public List<String> getValue() throws ExecutionException, InterruptedException {
return this.value.get();
}
public CompletableFuture<Void> delete() {
return isUploadedFile
? value.thenCompose(urls -> Http.requestDelete(urls.get(0)))
: CompletableFuture.completedFuture(null);
}
private static CompletableFuture<List<String>> upload(InputStream stream, String fileName, Config config) {
return CompletableFuture.supplyAsync(() -> {
Request request = Http.getRequestBuilder()
.url(Http.getUrlBuilder(config).addPathSegment("upload")
.addQueryParameter("filename", fileName)
.build())
.post(RequestBodyStream.create(MediaType.parse("application/octet-stream"), stream))
.build();
try {
String id = Http.getClient().newCall(request).execute().body().string();
//noinspection ConstantConditions
return Collections.singletonList(id);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
public static Param[] concat(Param[] a, Param[] b) {
Param[] result = new Param[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
}