-
Notifications
You must be signed in to change notification settings - Fork 5
/
Imgur.cs
342 lines (273 loc) · 13.5 KB
/
Imgur.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using AddonHelper;
using System.Diagnostics;
using MrAG.OAuth;
namespace Imgur {
public class Imgur : Addon {
public Settings settings;
public NotifyIcon Tray;
public string imgurApiKey = "1caece560704b3ee19d7b112e63ea2ac";
public string imgurConsumerKey = "0d3b44ffdecba1c44057fbb8b78f2c4204fcb7c1f";
public string imgurConsumerSecret = "62842910a06d90f793d773657bfae555";
public bool authenticated;
public bool isPro;
public string username;
public string imageFormat = "PNG";
public string shortCutDragModifiers = "";
public string shortCutDragKey = "";
public string shortCutPasteModifiers = "";
public string shortCutPasteKey = "";
public bool jpegCompression = false;
public int jpegCompressionFilesize = 1000;
public int jpegCompressionRate = 75;
private Bitmap bmpIcon;
public OAuth oauth;
public void Initialize(NotifyIcon Tray) {
this.Tray = Tray;
this.settings = new Settings("Addons/Imgur/settings.txt");
this.bmpIcon = new Icon("Addons/Imgur/Icon.ico").ToBitmap();
LoadSettings();
}
public void LoadSettings() {
if (settings.Contains("ShortcutModifiers")) {
// Migrate old 3.00 config file
settings.SetString("ShortcutDragModifiers", settings.GetString("ShortcutModifiers"));
settings.SetString("ShortcutDragKey", settings.GetString("ShortcutKey"));
settings.SetString("ShortcutPasteModifiers", "");
settings.SetString("ShortcutPasteKey", "");
settings.Delete("ShortcutModifiers");
settings.Delete("ShortcutKey");
settings.Save();
}
if (!settings.Contains("AccessToken")) {
// Migrate old 3.10 config file
settings.SetString("Username", "");
settings.SetBool("IsPro", false);
settings.SetString("AccessToken", "");
settings.SetString("AccessTokenSecret", "");
settings.SetBool("JpegCompression", false);
settings.SetInt("JpegCompressionFilesize", 1000);
settings.SetInt("JpegCompressionRate", 75);
settings.Save();
}
imageFormat = settings.GetString("Format");
shortCutDragModifiers = settings.GetString("ShortcutDragModifiers");
shortCutDragKey = settings.GetString("ShortcutDragKey");
shortCutPasteModifiers = settings.GetString("ShortcutPasteModifiers");
shortCutPasteKey = settings.GetString("ShortcutPasteKey");
jpegCompression = settings.GetBool("JpegCompression");
jpegCompressionFilesize = settings.GetInt("JpegCompressionFilesize");
jpegCompressionRate = settings.GetInt("JpegCompressionRate");
this.oauth = new OAuth("https://" + "api.imgur.com/oauth/", this.imgurConsumerKey, this.imgurConsumerSecret);
this.oauth.ServiceIcon = new Icon("Addons/Imgur/Icon.ico");
this.oauth.ServiceName = "Imgur";
this.username = settings.GetString("Username");
this.isPro = settings.GetBool("IsPro");
this.oauth.AccessToken = settings.GetString("AccessToken");
this.oauth.AccessTokenSecret = settings.GetString("AccessTokenSecret");
authenticated = username != "" && this.oauth.AccessToken != "";
}
public Hashtable[] Menu() {
List<Hashtable> ret = new List<Hashtable>();
Hashtable DragItem = new Hashtable();
DragItem.Add("Visible", true);
DragItem.Add("Text", "Drag -> Imgur" + (this.authenticated ? " (" + this.username + ")" : ""));
DragItem.Add("Image", this.bmpIcon);
DragItem.Add("Action", new Action(delegate { this.Drag(new Action<DragCallback>(DragCallback)); }));
DragItem.Add("ShortcutModifiers", this.shortCutDragModifiers);
DragItem.Add("ShortcutKey", this.shortCutDragKey);
ret.Add(DragItem);
Hashtable UpItem = new Hashtable();
UpItem.Add("Visible", Clipboard.ContainsImage() || Clipboard.ContainsFileDropList());
UpItem.Add("Text", "Imgur" + (this.authenticated ? " (" + this.username + ")" : ""));
UpItem.Add("Image", this.bmpIcon);
UpItem.Add("Action", new Action(Upload));
UpItem.Add("ShortcutModifiers", this.shortCutPasteModifiers);
UpItem.Add("ShortcutKey", this.shortCutPasteKey);
ret.Add(UpItem);
return ret.ToArray();
}
public void Settings() {
new FormSettings(this).ShowDialog();
}
public void DragCallback(DragCallback callback) {
switch (callback.Type) {
case DragCallbackType.Image:
UploadImage(callback.Image);
break;
case DragCallbackType.Animation:
UploadAnimation(callback.Animation);
break;
}
}
public void UploadImage(Image img) {
Icon defIcon = (Icon)Tray.Icon.Clone();
Tray.Icon = new Icon("Addons/Imgur/Icon.ico");
MemoryStream ms = new MemoryStream();
ImageFormat format = ImageFormat.Png;
string formatStr = imageFormat.ToLower();
switch (formatStr) {
case "png": format = ImageFormat.Png; break;
case "jpg": format = ImageFormat.Jpeg; break;
case "gif": format = ImageFormat.Gif; break;
}
this.ImagePipeline(img);
img.Save(ms, format);
if (jpegCompression) {
if (ms.Length / 1000 > jpegCompressionFilesize) {
ms.Dispose();
ms = new MemoryStream();
// Set up the encoder, codec and params
System.Drawing.Imaging.Encoder jpegEncoder = System.Drawing.Imaging.Encoder.Compression;
ImageCodecInfo jpegCodec = this.GetEncoder(ImageFormat.Jpeg);
EncoderParameters jpegParams = new EncoderParameters();
jpegParams.Param[0] = new EncoderParameter(jpegEncoder, jpegCompressionRate);
// Now save it with the new encoder
img.Save(ms, jpegCodec, jpegParams);
// And make sure the filename gets set correctly (for the Imgur addon this is only for the backups)
formatStr = "jpg";
}
}
string url = this.UploadToImgur(ms, img.Width, img.Height);
img.Dispose();
if (url != "CANCELED" && url != "")
this.Backup(ms.GetBuffer(), url.Split('/', '\\').Last());
else
this.Backup(ms.GetBuffer(), this.RandomFilename(5) + "." + formatStr);
if (url != "CANCELED") {
if (url != "") {
this.SetClipboardText(url);
Tray.ShowBalloonTip(1000, "Upload success!", "Image uploaded to Imgur and URL copied to clipboard.", ToolTipIcon.Info);
} else {
this.ProgressBar.Done();
Tray.ShowBalloonTip(1000, "Upload failed!", "Something went wrong, probably on Imgur's side. Try again.", ToolTipIcon.Error);
}
}
Tray.Icon = defIcon;
}
public void UploadAnimation(MemoryStream ms) {
Icon defIcon = (Icon)Tray.Icon.Clone();
Tray.Icon = new Icon("Addons/Imgur/Icon.ico");
string url = this.UploadToImgur(ms, -1, -1);
if (url != "CANCELED" && url != "")
this.Backup(ms.GetBuffer(), url.Split('/', '\\').Last());
else
this.Backup(ms.GetBuffer(), this.RandomFilename(5) + ".gif");
if (url != "CANCELED") {
if (url != "") {
this.SetClipboardText(url);
Tray.ShowBalloonTip(1000, "Upload success!", "Animation uploaded to Imgur and URL copied to clipboard.", ToolTipIcon.Info);
} else {
this.ProgressBar.Done();
Tray.ShowBalloonTip(1000, "Upload failed!", "Something went wrong, probably on Imgur's side. Try again.", ToolTipIcon.Error);
}
}
Tray.Icon = defIcon;
}
public void UploadImages(StringCollection files) {
if (files.Count == 0)
return;
Icon defIcon = (Icon)Tray.Icon.Clone();
Tray.Icon = new Icon("Addons/Imgur/Icon.ico");
string finalCopy = "";
foreach (string file in files) {
if (!(file.EndsWith(".png") || file.EndsWith(".jpg") || file.EndsWith(".gif")))
continue;
Image img = Image.FromFile(file);
MemoryStream ms = new MemoryStream(File.ReadAllBytes(file));
string url = this.UploadToImgur(ms, img.Width, img.Height);
img.Dispose();
if (url == "CANCELED") {
finalCopy = "CANCELED";
break;
}
if (url != "") {
finalCopy += url + "\n";
}
}
if (finalCopy != "CANCELED") {
if (finalCopy != "") {
this.SetClipboardText(finalCopy.Substring(0, finalCopy.Length - 1));
Tray.ShowBalloonTip(1000, "Upload success!", "Image(s) uploaded to Imgur and URL(s) copied to clipboard.", ToolTipIcon.Info);
} else {
this.ProgressBar.Done();
Tray.ShowBalloonTip(1000, "Upload failed!", "You didn't copy any images, or the image format is not supported.", ToolTipIcon.Error);
}
}
Tray.Icon = defIcon;
}
public string UploadToImgur(MemoryStream ms, int width, int height) {
byte[] writeData;
if (this.authenticated)
writeData = Encoding.ASCII.GetBytes("image=" + LongDataEscape(Convert.ToBase64String(ms.ToArray())));
else
writeData = Encoding.ASCII.GetBytes("key=" + imgurApiKey + "&image=" + LongDataEscape(Convert.ToBase64String(ms.ToArray())));
this.ProgressBar.Start("Imgur", writeData.Length);
HttpWebRequest req = null;
string url = "";
try {
if (this.authenticated)
req = this.oauth.AuthenticatedWebRequest("https://" + "api.imgur.com/2/upload.xml");
else
req = (HttpWebRequest)HttpWebRequest.Create("https://" + "api.imgur.com/2/upload.xml");
req.Proxy = this.GetProxy();
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = writeData.Length;
req.UserAgent = "ClipUpload 3/clipupload.net";
Stream stream = req.GetRequestStream();
ms.Dispose();
ms = new MemoryStream(writeData);
int sr = 1024;
for (int i = 0; i < ms.Length; i += 1024) {
if (ms.Length - i < 1024)
sr = (int)ms.Length - i;
else
sr = 1024;
byte[] buffer = new byte[sr];
ms.Seek((long)i, SeekOrigin.Begin);
ms.Read(buffer, 0, sr);
stream.Write(buffer, 0, sr);
if (this.ProgressBar.Canceled) {
req.Abort();
ms.Dispose();
req = null;
ms = null;
return "CANCELED";
}
this.ProgressBar.Set(i);
}
WebResponse response = req.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string result = reader.ReadToEnd();
if (result.Contains("<original>http")) {
url = GetBetween(result, "<original>", "</original>");
if (width != -1)
this.AddLog(url, width + " x " + height);
else
this.AddLog(url, (ms.Length / 1000) + " kB");
}
} catch (Exception ex) {
this.Debug("UploadToImgur threw " + ex.GetType().FullName + ": '" + ex.Message + "'");
}
this.ProgressBar.Done();
return url;
}
public void Upload() {
if (Clipboard.ContainsImage())
UploadImage(Clipboard.GetImage());
else if (Clipboard.ContainsFileDropList())
UploadImages(Clipboard.GetFileDropList());
}
}
}