Repository navigation
Expand file tree
/
Copy pathStreamImages.java
More file actions
125 lines (113 loc) · 4.2 KB
/
Copy pathStreamImages.java
File metadata and controls
125 lines (113 loc) · 4.2 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
package io.getstream.core;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static io.getstream.core.utils.Request.*;
import static io.getstream.core.utils.Routes.buildImagesURL;
import static io.getstream.core.utils.Serialization.deserialize;
import static io.getstream.core.utils.Serialization.deserializeError;
import io.getstream.core.exceptions.StreamException;
import io.getstream.core.http.HTTPClient;
import io.getstream.core.http.Token;
import io.getstream.core.options.CustomQueryParameter;
import io.getstream.core.options.RequestOption;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java8.util.concurrent.CompletableFuture;
import java8.util.concurrent.CompletionException;
public class StreamImages {
private final String key;
private final URL baseURL;
private final HTTPClient httpClient;
StreamImages(String key, URL baseURL, HTTPClient httpClient) {
this.key = key;
this.baseURL = baseURL;
this.httpClient = httpClient;
}
public CompletableFuture<URL> upload(Token token, String fileName, byte[] content)
throws StreamException {
checkNotNull(content, "No data to upload");
checkArgument(content.length > 0, "No data to upload");
try {
final URL url = buildImagesURL(baseURL);
return httpClient
.execute(buildMultiPartPost(url, key, token, fileName, content))
.thenApply(
response -> {
try {
return deserialize(response, "file", URL.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<URL> upload(Token token, File content) throws StreamException {
checkNotNull(content, "No file to upload");
checkArgument(content.exists(), "No file to upload");
try {
final URL url = buildImagesURL(baseURL);
return httpClient
.execute(buildMultiPartPost(url, key, token, content))
.thenApply(
response -> {
try {
return deserialize(response, "file", URL.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<Void> delete(Token token, URL targetURL) throws StreamException {
checkNotNull(targetURL, "No image to delete");
try {
final URL url = buildImagesURL(baseURL);
return httpClient
.execute(
buildDelete(
url, key, token, new CustomQueryParameter("url", targetURL.toExternalForm())))
.thenApply(
response -> {
try {
return deserializeError(response);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
public CompletableFuture<URL> process(Token token, URL targetURL, RequestOption option)
throws StreamException {
checkNotNull(targetURL, "No image to process");
try {
final URL url = buildImagesURL(baseURL);
return httpClient
.execute(
buildGet(
url,
key,
token,
option,
new CustomQueryParameter("url", targetURL.toExternalForm())))
.thenApply(
response -> {
try {
return deserialize(response, "file", URL.class);
} catch (StreamException | IOException e) {
throw new CompletionException(e);
}
});
} catch (MalformedURLException | URISyntaxException e) {
throw new StreamException(e);
}
}
}