-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadWorker.cs
63 lines (57 loc) · 2.4 KB
/
UploadWorker.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
using System;
using System.Windows.Forms;
namespace InstantBackgroundUploader
{
class UploadWorker
{
private System.IO.Stream fileStream = null;
private String fileStreamName;
public UploadWorker(System.IO.Stream fileStream, String fileStreamName)
{
this.fileStream = fileStream;
this.fileStreamName = fileStreamName;
}
public void Run(object parameter)
{
MultipartForm form;
form = new MultipartForm("http://www.imageshack.us/upload_api.php");
if (fileStreamName.EndsWith(".png"))
form.FileContentType = "image/png";
else
form.FileContentType = "image/jpeg";
//form.setField("uploadtype", "on");
//form.setField("transurl", "");
//form.setField("email", "");
//form.setField("MAX_FILE_SIZE", "13145728");
//form.setField("refer", "");
//form.setField("brand", "");
//form.setField("optsize", "320x320");
form.setField("key", "12DEFKTYa5517607af7de06ec6272205d57a9cf4");
try {
form.sendFile(fileStream, fileStreamName);
} catch (Exception exception) {
MessageBox.Show("Exception caught:\n" + exception.Message + "\n" + exception.StackTrace, "Instant Background Uploader", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Parse out the Image URL
String response = form.ResponseText.ToString().ToLower();
/*System.IO.StreamWriter log = System.IO.File.CreateText("C:\\Users\\Dmitri\\Desktop\\IBU.log");
log.Write(response);
log.Close();*/
String imageUrl = "";
try
{
/*int index = response.IndexOf("forum code");
int index2 = response.IndexOf("[img]", index);
imageUrl = response.Substring(index2 + "[img]".Length, response.IndexOf("[/img]", index2 + "[img]".Length) - (index2 + "[img]".Length));*/
int StartIndex = response.IndexOf("<image_link>") + "<image_link>".Length;
int EndIndex = response.IndexOf("</image_link>", StartIndex);
imageUrl = response.Substring(StartIndex, EndIndex - StartIndex);
if (imageUrl.Length <= 0) throw new Exception();
} catch (Exception) {
MessageBox.Show("Couldn't parse image URL out of web response (probably failed to upload for some reason).", "Instant Background Uploader", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
((UploaderApplicationContext)parameter).CompletedImageUpload(imageUrl);
}
}
}