-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathSource.java
More file actions
64 lines (51 loc) · 2.1 KB
/
Copy pathSource.java
File metadata and controls
64 lines (51 loc) · 2.1 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
package com.tinify;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Source {
private String url;
private Options commands;
public static Source fromFile(final String path) throws IOException {
return fromBuffer(Files.readAllBytes(Paths.get(path)));
}
public static Source fromBuffer(final byte[] buffer) {
Response response = Tinify.client().request(Client.Method.POST, "/shrink", buffer);
return new Source(response.header("location"), new Options());
}
public static Source fromUrl(final String url) {
Options body = new Options().with("source", new Options().with("url", url));
Response response = Tinify.client().request(Client.Method.POST, "/shrink", body);
return new Source(response.header("location"), new Options());
}
public Source(final String url, final Options commands) {
this.url = url;
this.commands = commands;
}
public final Source preserve(final String... options) {
return new Source(url, new Options(commands).with("preserve", options));
}
public final Source resize(final Options options) {
return new Source(url, new Options(commands).with("resize", options));
}
public final ResultMeta store(final Options options) {
Response response = Tinify.client().request(
Client.Method.POST, url, new Options(commands).with("store", options));
return new ResultMeta(response.headers());
}
public final Result result() throws IOException {
Response response;
if (commands == null) {
response = Tinify.client().request(Client.Method.GET, url);
} else {
response = Tinify.client().request(Client.Method.POST, url, commands);
}
return new Result(response.headers(), response.body().bytes());
}
public void toFile(final String path) throws IOException {
result().toFile(path);
}
public final byte[] toBuffer() throws IOException {
return result().toBuffer();
}
}