-
Notifications
You must be signed in to change notification settings - Fork 7
/
imgur_uploader.js
155 lines (122 loc) · 4.57 KB
/
imgur_uploader.js
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
// taken from https://github.com/OttoAllmendinger/gnome-shell-imgur/
const Lang = imports.lang;
const Signals = imports.signals;
const Mainloop = imports.mainloop;
const Gio = imports.gi.Gio;
const Soup = imports.gi.Soup;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const ClientId = "7714a4928bff2fd";
const _httpSession = new Soup.SessionAsync();
function supported_format(filename) {
let supported_format = (
Utils.ends_with(filename, '.jpg') ||
Utils.ends_with(filename, '.jpeg') ||
Utils.ends_with(filename, '.png') ||
Utils.ends_with(filename, '.gif')
);
return supported_format;
}
const Uploader = new Lang.Class({
Name: 'Uploader',
_init: function() {
// nothing
}
});
Signals.addSignalMethods(Uploader.prototype);
const ImgurUploader = new Lang.Class({
Name: 'GPasteImgurUploader',
Extends: Uploader,
baseUrl: 'https://api.imgur.com/3/',
_init: function(clientId) {
this._clientId = clientId || ClientId;
},
_getPostMessage: function(filename, mimetype, callback) {
let url = this.baseUrl + 'image';
let file = Gio.File.new_for_path(filename);
file.load_contents_async(null, Lang.bind(this, function(f, res) {
let contents;
try {
[, contents] = f.load_contents_finish(res);
}
catch(e) {
log('error loading file: ' + e.message);
callback(e, null);
return;
}
let buffer = new Soup.Buffer(contents, contents.length);
let multipart = new Soup.Multipart(Soup.FORM_MIME_TYPE_MULTIPART);
multipart.append_form_file('image', filename, mimetype, buffer);
let message = Soup.form_request_new_from_multipart(url, multipart);
message.request_headers.append(
'Authorization', 'Client-ID ' + this._clientId
);
callback(null, message);
}), null);
},
upload: function (filename, mimetype) {
this._getPostMessage(filename, mimetype,
Lang.bind(this, function(error, message) {
let total = message.request_body.length;
let uploaded = 0;
if(error) {
this.emit('error', error);
return;
}
let signalProgress = message.connect(
'wrote-body-data',
Lang.bind(this, function(message, buffer) {
uploaded += buffer.length;
this.emit('progress', uploaded, total);
})
);
_httpSession.queue_message(message,
Lang.bind(this, function(session, {status_code, response_body}) {
if(status_code == 200) {
this.emit('done', JSON.parse(response_body.data).data);
}
else {
log('getJSON error status code: ' + status_code);
log('getJSON error response: ' + response_body.data);
let errorMessage;
try {
errorMessage = JSON.parse(response_body.data).data.error;
}
catch(e) {
log('failed to parse error message ' + e);
errorMessage = response_body.data;
}
this.emit('error', 'HTTP ' + status_code + ' - ' + errorMessage);
}
message.disconnect(signalProgress);
})
);
})
);
}
});
const DummyUploader = new Lang.Class({
Name: "DummyUploader",
Extends: Uploader,
_init: function () {
// nothing
},
upload: function (filename) {
let testImage = 'http://i.imgur.com/Vkapy8W.png';
let size = 200000;
let chunk = 5000;
let progress = 0;
log('DummyUploader.upload() filename=' + filename);
let update = Lang.bind(this, function() {
if(progress < size) {
this.emit("progress", (progress += chunk), size);
Mainloop.timeout_add(100, update);
}
else {
this.emit("done", {link: testImage});
}
});
Mainloop.idle_add(update);
}
});