-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImgurHandler.java
More file actions
110 lines (97 loc) · 3.2 KB
/
ImgurHandler.java
File metadata and controls
110 lines (97 loc) · 3.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
import javax.imageio.*;
import java.awt.image.*;
import org.apache.commons.codec.binary.*;
import com.google.common.base.*;
import com.google.gson.*;
import org.apache.commons.io.*;
import java.net.*;
import java.io.*;
public class ImgurHandler extends ImageHost {
public static final String CLIENT_ID = "70df5d8909b8da8";
private JsonObject lastUploadData;
public ImgurHandler() {
super("imgur");
System.out.println("added");
this.lastUploadData = null;
}
@Override
public boolean upload(final BufferedImage image, final UPLOAD_METHOD uploadMethod, final String... description) {
OutputStreamWriter wr = null;
BufferedReader in = null;
HttpURLConnection conn = null;
ByteArrayOutputStream baos = null;
try {
final URL url = new URL("https://api.imgur.com/3/upload.json");
baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
final byte[] imageInByte = baos.toByteArray();
final String encoded = Base64.encodeBase64String(imageInByte);
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(encoded, "UTF-8");
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Client-ID 70df5d8909b8da8");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
this.lastUploadData = new JsonParser().parse((Reader) in).getAsJsonObject();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
IOUtils.close((URLConnection) conn);
IOUtils.closeQuietly((Writer) wr);
IOUtils.closeQuietly((Reader) in);
IOUtils.closeQuietly((OutputStream) baos);
}
return true;
}
@Override
public boolean deleteLast() {
HttpURLConnection conn = null;
try {
final URL url = new URL("https://api.imgur.com/3/image/" + this.getDeleteHash());
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("DELETE");
conn.setRequestProperty("Authorization", "Client-ID 70df5d8909b8da8");
if (conn.getResponseCode() == 200) {
this.lastUploadData = null;
return true;
}
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
IOUtils.close((URLConnection) conn);
}
}
@Override
public String getLink() {
if (this.lastUploadData != null) {
final JsonObject dataJson = this.lastUploadData.get("data").getAsJsonObject();
return "https://imgur.com/" + dataJson.get("id").getAsString();
}
return null;
}
private String getDeleteHash() throws JsonParseException {
if (this.lastUploadData != null) {
final JsonObject dataJson = this.lastUploadData.get("data").getAsJsonObject();
return dataJson.get("deletehash").getAsString();
}
return null;
}
@Override
public boolean canUploadAnon() {
return true;
}
@Override
public boolean canUploadAccount() {
return false;
}
}