2
ITmages upload client for Windows
3
Copyright (C) 2011 Dmitriy Simbiriatin <slpiv@itmages.ru>
5
This program is free software; you can redistribute it and/or
6
modify it under the terms of the GNU General Public License
7
as published by the Free Software Foundation; either version 2
8
of the License, or (at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26
#include "itmagescmd.h"
27
#include "itmagesgui.h"
28
#include "errorshandler.h"
30
char g_ErrorBuffer[CURL_ERROR_SIZE];
32
size_t ItmagesCallbackWrite(
38
return fwrite(data, size, nmemb, write_file);
41
size_t ItmagesCallbackRead(
47
return fread(data, size, nmemb, read_file);
50
int CallbackProgressbar(
57
SendMessage(upload_progress, PBM_SETRANGE, 0, (LPARAM) MAKELONG(0, 10));
58
SendMessage(upload_progress, PBM_SETPOS, (WPARAM) (up / total_up * 10), 0);
59
SendMessage(upload_progress, PBM_SETSTEP, 0, 0);
63
int ItmagesCheckConnection(
73
write_file = fopen("check.xml", "w");
74
if (write_file == NULL) {
75
ErrorsHandler(ERROR_WRITEFILE);
78
curl = curl_easy_init();
80
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, g_ErrorBuffer);
81
curl_easy_setopt(curl, CURLOPT_URL, ITMAGES_CHECK_URL);
82
curl_easy_setopt(curl, CURLOPT_USERAGENT, ITMAGES_USER_AGENT);
83
curl_easy_setopt(curl, CURLOPT_REFERER, ITMAGES_REFERER);
84
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip,deflate");
85
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookies_path);
86
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies_path);
87
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
88
if (proxy_adrs != NULL && proxy_port != NULL) {
89
size_t port_num = atoi(proxy_port);
90
curl_easy_setopt(curl, CURLOPT_PROXY, proxy_adrs);
91
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port_num);
93
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ItmagesCallbackWrite);
94
curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_file);
95
result = curl_easy_perform(curl);
97
if (result != CURLE_OK) {
98
curl_easy_cleanup(curl);
101
ErrorsHandler(ERROR_CURL);
104
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &resp_code);
106
curl_easy_cleanup(curl);
109
if (resp_code == 404) {
127
struct curl_httppost *post = NULL;
128
struct curl_httppost *last = NULL;
130
write_file = fopen("login.xml", "w");
131
if (write_file == NULL) {
132
ErrorsHandler(ERROR_WRITEFILE);
135
curl = curl_easy_init();
137
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, g_ErrorBuffer);
138
curl_easy_setopt(curl, CURLOPT_URL, ITMAGES_LOGIN_URL);
139
curl_easy_setopt(curl, CURLOPT_USERAGENT, ITMAGES_USER_AGENT);
140
curl_easy_setopt(curl, CURLOPT_REFERER, ITMAGES_REFERER);
141
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip,deflate");
142
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookies_path);
143
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies_path);
144
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
145
if (proxy_adrs != NULL && proxy_port != NULL) {
146
size_t port_num = atoi(proxy_port);
147
curl_easy_setopt(curl, CURLOPT_PROXY, proxy_adrs);
148
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port_num);
150
curl_easy_setopt(curl, CURLOPT_POST, 1);
153
CURLFORM_COPYNAME, "name",
154
CURLFORM_COPYCONTENTS, usr_login,
158
CURLFORM_COPYNAME, "passwd",
159
CURLFORM_COPYCONTENTS, usr_passwd,
161
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
162
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ItmagesCallbackWrite);
163
curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_file);
164
result = curl_easy_perform(curl);
166
curl_easy_cleanup(curl);
167
if (result != CURLE_OK) {
170
ErrorsHandler(ERROR_CURL);
179
read_file = fopen("login.xml", "r");
180
if (read_file == NULL) {
181
ErrorsHandler(ERROR_READFILE);
184
mxml_node_t *response_tree;
185
response_tree = mxmlLoadFile(NULL, read_file, MXML_OPAQUE_CALLBACK);
188
mxml_node_t *response_node;
189
response_node = mxmlFindElement(response_tree, response_tree,
190
"response", NULL, NULL, MXML_DESCEND);
191
if (response_node == NULL) {
192
ErrorsHandler(ERROR_PARSERESPONSE);
195
status = response_node->child->child->value.opaque;
196
if (memcmp(status, "ok", 3) == 0) {
197
mxmlDelete(response_tree);
200
mxmlDelete(response_tree);
214
struct curl_httppost *post = NULL;
215
struct curl_httppost *last = NULL;
217
write_file = fopen("logout.xml", "w");
218
if (write_file == NULL) {
219
ErrorsHandler(ERROR_WRITEFILE);
222
curl = curl_easy_init();
224
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, g_ErrorBuffer);
225
curl_easy_setopt(curl, CURLOPT_URL, ITMAGES_LOGOUT_URL);
226
curl_easy_setopt(curl, CURLOPT_USERAGENT, ITMAGES_USER_AGENT);
227
curl_easy_setopt(curl, CURLOPT_REFERER, ITMAGES_REFERER);
228
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip,deflate");
229
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies_path);
230
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookies_path);
231
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
232
if (proxy_adrs != NULL && proxy_port != NULL) {
233
size_t port_num = atoi(proxy_port);
234
curl_easy_setopt(curl, CURLOPT_PROXY, proxy_adrs);
235
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port_num);
237
curl_easy_setopt(curl, CURLOPT_POST, 1);
240
CURLFORM_COPYNAME, "submit",
241
CURLFORM_COPYCONTENTS, "Logout",
243
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
244
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ItmagesCallbackWrite);
245
curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_file);
246
result = curl_easy_perform(curl);
248
curl_easy_cleanup(curl);
249
if (result != CURLE_OK) {
251
remove("logout.xml");
252
ErrorsHandler(ERROR_CURL);
261
read_file = fopen("logout.xml", "r");
262
if (read_file == NULL) {
263
ErrorsHandler(ERROR_READFILE);
266
mxml_node_t *response_tree;
267
response_tree = mxmlLoadFile(NULL, read_file, MXML_OPAQUE_CALLBACK);
269
remove("logout.xml");
270
mxml_node_t *response_node;
271
response_node = mxmlFindElement(response_tree, response_tree,
272
"response", NULL, NULL, MXML_DESCEND);
273
if (response_node == NULL) {
274
ErrorsHandler(ERROR_PARSERESPONSE);
277
status = response_node->child->child->value.opaque;
278
if (memcmp(status, "ok", 3) == 0) {
279
mxmlDelete(response_tree);
282
mxmlDelete(response_tree);
289
HWND upload_progress,
297
ItmagesImageLinks *imageLinks)
301
FILE *write_file, *read_file;
302
struct curl_httppost *post = NULL;
303
struct curl_httppost *last = NULL;
305
write_file = fopen("upload.xml", "w");
306
if (write_file == NULL) {
307
ErrorsHandler(ERROR_WRITEFILE);
310
read_file = _wfopen(image_path, L"rb");
311
if (read_file == NULL) {
312
ErrorsHandler(ERROR_READFILE);
315
curl = curl_easy_init();
317
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, g_ErrorBuffer);
318
curl_easy_setopt(curl, CURLOPT_URL, ITMAGES_ADD_URL);
319
curl_easy_setopt(curl, CURLOPT_USERAGENT, ITMAGES_USER_AGENT);
320
curl_easy_setopt(curl, CURLOPT_REFERER, ITMAGES_REFERER);
321
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip, default");
322
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookies_path);
323
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookies_path);
324
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
325
if (proxy_adrs != NULL && proxy_port != NULL) {
326
size_t port_num = atoi(proxy_port);
327
curl_easy_setopt(curl, CURLOPT_PROXY, proxy_adrs);
328
curl_easy_setopt(curl, CURLOPT_PROXYPORT, port_num);
330
curl_easy_setopt(curl, CURLOPT_POST, 1);
333
CURLFORM_COPYNAME, "UFileManager[picture]",
334
CURLFORM_FILENAME, name,
335
CURLFORM_STREAM, read_file,
336
CURLFORM_CONTENTSLENGTH, size,
337
CURLFORM_CONTENTTYPE, mime,
341
CURLFORM_COPYNAME, "UFileManager[picture]",
342
CURLFORM_COPYCONTENTS, "",
344
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
345
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ItmagesCallbackWrite);
346
curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_file);
347
curl_easy_setopt(curl, CURLOPT_READFUNCTION, ItmagesCallbackRead);
348
curl_easy_setopt(curl, CURLOPT_READDATA, read_file);
349
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
350
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, CallbackProgressbar);
351
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, upload_progress);
352
result = curl_easy_perform(curl);
354
curl_easy_cleanup(curl);
355
if (result != CURLE_OK) {
358
remove("upload.xml");
359
ErrorsHandler(ERROR_CURL);
369
read_file = fopen("upload.xml", "r");
370
if (read_file == NULL) {
371
ErrorsHandler(ERROR_READFILE);
374
mxml_node_t *response_tree;
375
response_tree = mxmlLoadFile(NULL, read_file, MXML_OPAQUE_CALLBACK);
377
remove("upload.xml");
378
mxml_node_t *response_node;
379
response_node = mxmlFindElement(response_tree, response_tree,
380
"obj", NULL, NULL, MXML_DESCEND);
381
if (response_node == NULL) {
382
ErrorsHandler(ERROR_PARSERESPONSE);
385
response_node = mxmlFindElement(response_node, response_tree,
386
"full", NULL, NULL, MXML_DESCEND);
387
if (response_node == NULL) {
388
ErrorsHandler(ERROR_PARSERESPONSE);
392
node_len = strlen(response_node->child->value.opaque);
393
imageLinks->full = (char*) malloc((node_len + 1) * sizeof(char));
394
if (imageLinks->full == NULL) {
395
ErrorsHandler(ERROR_MEMALLOC);
398
memcpy(imageLinks->full, response_node->child->value.opaque, node_len + 1);
400
node_len = strlen(response_node->next->child->value.opaque);
401
imageLinks->thumb = (char*) malloc((node_len + 1) * sizeof(char));
402
if (imageLinks->thumb == NULL) {
403
ErrorsHandler(ERROR_MEMALLOC);
406
memcpy(imageLinks->thumb, response_node->next->child->value.opaque, node_len + 1);
408
response_node = mxmlFindElement(response_node, response_tree,
409
"id", NULL, NULL, MXML_DESCEND);
410
if (response_node == NULL) {
411
ErrorsHandler(ERROR_PARSERESPONSE);
415
node_len = strlen(response_node->child->value.opaque);
416
imageLinks->id = (char*) malloc((node_len + 1) * sizeof(char));
417
if (imageLinks->id == NULL) {
418
ErrorsHandler(ERROR_MEMALLOC);
421
memcpy(imageLinks->id, response_node->child->value.opaque, node_len + 1);
423
node_len = strlen(response_node->next->child->value.opaque);
424
imageLinks->server = (char*) malloc((node_len + 1) * sizeof(char));
425
if (imageLinks->server == NULL) {
426
ErrorsHandler(ERROR_MEMALLOC);
429
memcpy(imageLinks->server, response_node->next->child->value.opaque, node_len + 1);
431
node_len = strlen(response_node->next->next->child->value.opaque);
432
imageLinks->key = (char*) malloc((node_len + 1) * sizeof(char));
433
if (imageLinks->key == NULL) {
434
ErrorsHandler(ERROR_MEMALLOC);
437
memcpy(imageLinks->key, response_node->next->next->child->value.opaque, node_len + 1);
439
mxmlDelete(response_tree);
445
void FreeImageLinks(ItmagesImageLinks *imageLinks)
447
if (imageLinks->full != NULL) {
448
free(imageLinks->full);
449
imageLinks->full = NULL;
451
if (imageLinks->thumb != NULL) {
452
free(imageLinks->thumb);
453
imageLinks->thumb = NULL;
455
if (imageLinks->id != NULL) {
456
free(imageLinks->id);
457
imageLinks->id = NULL;
459
if (imageLinks->server != NULL) {
460
free(imageLinks->server);
461
imageLinks->server = NULL;
463
if (imageLinks->key != NULL) {
464
free(imageLinks->key);
465
imageLinks->key = NULL;