-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageShackUploader.cpp
More file actions
657 lines (556 loc) · 16.8 KB
/
ImageShackUploader.cpp
File metadata and controls
657 lines (556 loc) · 16.8 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*********************************************************************
* Thumbnail me 3.0
*
* Thumbnail me is a user interface for Movie thumbnailer.
* Generate thumbnails from any movie is now easier !
*
* @package Thumbnail me 3.0
* @author Christ Azika-Eros <[email protected]>
* @copyright Quentin Rousseau (c) 2010 2009, Quentin Rousseau
* @license Creative Commons GNU GPL
* http://creativecommons.org/licenses/GPL/2.0/
* @link http://thumbnailme.sourceforge.net/
* @version 3.0
*
* @filesource ImageShackUploader.cpp
* @role The Implementation of the ImageShackUploader class.
* This class creates the object which will upload
* photos to ImageShack.
*
*********************************************************************/
#include "ImageShackUploader.hpp"
/**
* @brief Constructor of ImageShackUploader
*
* QString developerKey the developer key received
* to use APIs
* QString userName the user name
* QString userPassword the user password
* QNetworkProxy * prosy the proxy
**/
ImageShackUploader::ImageShackUploader(QString developerKey ,
QNetworkProxy * proxy )
{
this->developerKey = developerKey;
this->userName = "";
this->userPassword = "";
this->proxy = proxy;
this->imageUploadUrl = "http://www.imageshack.us/upload_api.php" ;
this->removeInformationBar = true;
this->authentificationUrl = "http://www.imageshack.us/auth.php";
this->nbFilesToUploads = 0;
this->nbFilesUploaded = 0;
this->uploadsProcessing = false;
this->uploadAborted = false;
this->timeoutTimer = new QTimer(this);
connect(timeoutTimer, SIGNAL(timeout()), this, SLOT(handleTimeOut()));
connect(this,SIGNAL(uploadDone(ImageShackResponse *)),this,SLOT(manageMultiUploads(ImageShackResponse *)));
}
/**
* @brief Destructor of ImageShackUploader
**/
ImageShackUploader::~ImageShackUploader(void)
{
}
/**----------
* Methods
**----------*/
/**
* @brief Check wether the user password is correct
*
* @see signal authentificationResponse(bool response)
*
* @param QString userName the user name
* @param QString userPassword the user password
*/
void ImageShackUploader::checkUserPassword(QString userName ,
QString userPassword)
{
QUrl url = QUrl(authentificationUrl + "?username=" + userName + "&password=" + userPassword + "&nocookie=1&format=xml");
QNetworkRequest request(url);
QNetworkAccessManager * manager = new QNetworkAccessManager;
this->uploadAborted = false;
// manage proxy
if(this->proxy != NULL)
manager->setProxy(*this->proxy);
this->networkReply = manager->get(request);
connect(this->networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
this , SLOT(manageUploadError(QNetworkReply::NetworkError)));
connect(this->networkReply, SIGNAL(finished()), this, SLOT(manageAuthentificationResponse()));
}
/**
* @brief manage authentification response
*
* @see signal authentificationResponse(bool response)
**/
void ImageShackUploader::manageAuthentificationResponse()
{
QByteArray httpResponse = networkReply->readAll();
QDomDocument xmlResponse;
xmlResponse.setContent(httpResponse);
qDebug() << httpResponse;
if(uploadAborted==false)
{
QDomElement rootTag = xmlResponse.documentElement();
if(rootTag.isNull())
{
emit authentificationResponse(false);
return;
}
QDomElement errorTag = rootTag.firstChildElement("error");
if(!errorTag.isNull())
{
emit authentificationResponse(false);
return;
}
QDomElement userNameTag = rootTag.firstChildElement("username");
if(userNameTag.isNull())
{
emit authentificationResponse(false);
return;
}
else
{
this->userName = userNameTag.text();
emit authentificationResponse(true);
}
}
}
/**
* Upload a list of images
*
* @param QList<ImageShackObject *> images images to upload
* @param QString userName the user name (optional)
* @param QString userPassword the user password (optional)
*/
void ImageShackUploader::uploadImages(QList<ImageShackObject *> images ,
QString userName ,
QString userPassword)
{
if(this->uploadsProcessing == false) // if a previons mutli-upload is not finished
{
this->filesToUpload = images;
this->nbFilesToUploads = images.size();
this->nbFilesUploaded = 0;
if(this->filesToUpload.size() > 0) // if there are images to upload
{
this->uploadsProcessing = true;
this->uploadAborted = false;
this->userName = userName;
this->userPassword = userPassword;
//qDebug() << " * Nombre de fichiers a uploader = " << this->filesToUpload.size();
//ImageShackObject * image = (ImageShackObject *) filesToUpload.takeFirst(); // return and remove the first image
ImageShackObject * image = (ImageShackObject *) filesToUpload.first(); // return and remove the first image
//qDebug() << " * Fichier allant etre uploade = " + image->getObjectPath();
//uploadOneImage(image->getObjectPath() ,
// image->getTags() ,
// image->getResizeOption(),
// userName, userPassword );
uploadOneImage(image, userName, userPassword);
filesToUpload.removeFirst();
//qDebug() << " * Nombre de fichiers restant = " << this->filesToUpload.size();
}
}
else // multiupload already started. The method is called as a slot
emit uploadAlreadyStarted();
}
/**
* Upload an image to an account
*
* @param QString imagePath the image path
* @param QString tags image tags
* @param QString resizeOption the resize option,
* default value is resample
* @param QString user the user ident
* @param QString password the user password
* @access private
*/
void ImageShackUploader::uploadOneImage(ImageShackObject * image, QString userName, QString userPassword)
{
QHash<QString, QString> headers;
//headers["fileupload"] = imageInfos.fileName();
headers["optsize"] = image->getResizeOption();
headers["optimage"] = (image->getResizeOption() == "") ? QString("0") : QString("1");
headers["tags"] = image->getTags();
headers["rembar"] = (this->removeInformationBar == true) ? QString("yes") : QString("no");
headers["public"] = (image->isPublic()) ? QString("yes") : QString("no");
headers["key"] = this->developerKey;
headers["xml"] = QString("yes");
if(userName != "" )
{
headers["a_username"] = userName;
headers["a_password"] = userPassword;
}
sendImage(image,headers);
}
/**
* Upload an image. This method is called by
* userUploadImage and anonymousUploadImage
* methods
*
* @access public
*/
void ImageShackUploader::sendImage(ImageShackObject * imageToUpload ,
QHash<QString, QString> headers)
{
QString imagePath = imageToUpload->getObjectPath();
QFile image(imagePath);
QFileInfo imageInfos(imagePath);
QNetworkAccessManager * manager = new QNetworkAccessManager;
QNetworkRequest request(this->imageUploadUrl);
QByteArray boundary = "IMAGESHACKUPLOADER";
QByteArray cr = "\r\n";
QByteArray data;
if(!image.open(QIODevice::ReadOnly))
{
if(uploadAborted==false)
{
emit uploadError(ImageShackError::FailedOpeningTheFile);
this->abortUploads();
}
return;
}
// build of the header
data.append("--" + boundary + cr);
data.append("Content-Disposition: form-data; ");
data.append( "name=\"fileupload\"; filename=\"" + imageInfos.absoluteFilePath() + "\";" + cr);
data.append("Content-Type: " + this->mimeType(imagePath) + cr + cr);
// insertion of the image
data.append(image.readAll() + cr);
image.close();
// build the footer
QHashIterator<QString, QString> h(headers);
while (h.hasNext())
{
h.next();
data.append("--" + boundary + cr);
data.append("Content-Disposition: form-data; ");
data.append("name=\"" + h.key() + "\"" + cr + cr);
data.append(h.value() + cr);
}
data.append("--" + boundary + "--" + cr);
request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + boundary);
request.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(data.size()).toAscii());
// manage proxy
if(this->proxy != NULL)
manager->setProxy(*this->proxy);
this->fileBeingUploaded = imageToUpload;
//this->uploadsProcessing = true;
timeoutTimer->start(TIMEOUT);
this->networkReply = manager->post(request, data);
connect(this->networkReply, SIGNAL(finished()) ,
this , SLOT (imageUploaded()));
connect(this->networkReply, SIGNAL(error(QNetworkReply::NetworkError)) ,
this , SLOT (manageUploadError(QNetworkReply::NetworkError)));
connect(this->networkReply, SIGNAL(uploadProgress(qint64,qint64)),
this , SLOT (manageUploadProgress(qint64,qint64)));
connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
this , SLOT (manageAuthentificationRequired(QNetworkReply*,QAuthenticator*)));
connect(manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
this , SLOT (manageProxyAuthentificationRequired(QNetworkProxy,QAuthenticator*)));
}
/**
* Manage image uploads
*
* @param QString fileUploaded the file uploaded
* @param QHash<QString, QString> informations concerning
* the uploads
* @access private
*/
void ImageShackUploader::manageMultiUploads(ImageShackResponse * uploadResponse)
{
//qDebug() << " * ManageImageUploads";
//qDebug() << " * Nombre fichiers restant = " << filesToUpload.size();
if((this->filesToUpload.size() > 0) && (this->uploadAborted == false))
{
//ImageShackObject * file = filesToUpload.takeFirst();
ImageShackObject * file = (ImageShackObject *) filesToUpload.first();
//qDebug() << " * Fichier sur le point de partir = " << file->getObjectPath();
timeoutTimer->stop();
uploadOneImage(file, this->userName, this->userPassword);
filesToUpload.removeFirst();
// qDebug() << " * Nombre de fichiers restant = " << filesToUpload.size();
}
}
/**
* Manage replies after the image is uploaded
*
* @access private
*/
void ImageShackUploader::imageUploaded()
{
QHash<QString, QString> usableResponse;
ImageShackError::UploadError error;
if(uploadsProcessing)
{
usableResponse = ImageShackResponse::makeResponseUsable(this->networkReply);
//qDebug() << usableResponse;
if(usableResponse.contains(QString("error")))
{
//qDebug() << "* Error in response";
//this->uploadsProcessing = false;
if(uploadAborted==false)
{
error = ImageShackError::getErrorCode(usableResponse);
if(error != ImageShackError::NoError)
{
emit uploadError(error);
}
this->abortUploads();
}
}
else
{
//qDebug() << "* Image uploadé avec succès : \n " << usableResponse;
if(uploadAborted==false)
{
emit uploadDone(new ImageShackResponse(this->fileBeingUploaded,usableResponse));
this->nbFilesUploaded ++;
}
//qDebug() << "* Nombre de fichiers restant " << filesToUpload.size();
if(nbFilesUploaded == nbFilesToUploads && uploadAborted == false)
{
this->uploadsProcessing = false;
timeoutTimer->stop();
emit endOfUploads();
}
}
}
}
/**
* @brief Abort uploads
*/
void ImageShackUploader::abortUploads()
{
timeoutTimer->stop();
this->uploadAborted = true;
this->uploadsProcessing = false;
this->networkReply->abort();
}
/**
* Return the mimeType list
*
* @access private
*/
QHash<QString, QStringList> ImageShackUploader::mimeTypeList()
{
QHash<QString, QStringList> mimeTypes;
QStringList jpeg, png, tiff, gif, bmp;
jpeg << "jpeg" << "jpg" << "jpe" << "jfif";
jpeg << "pjpeg" << "pjp";
png << "png" ;
tiff << "tiff" << "tif";
gif << "gif";
bmp << "bmp";
mimeTypes["image/jpeg"] = jpeg;
mimeTypes["image/png"] = png ;
mimeTypes["image/tiff"] = tiff;
mimeTypes["image/gif"] = gif;
mimeTypes["image/bmp"] = bmp;
return mimeTypes;
}
/**
* Return the mimetype basing on the file extension
* By default, the mimetype returns is image/jpeg
*
* @return QString the mimetype
* @access private
*/
QString ImageShackUploader::mimeType(QString imagePath)
{
QFileInfo imageInfos(imagePath);
QHashIterator<QString, QStringList> h(this->mimeTypeList());
while (h.hasNext())
{
h.next();
if(h.value().contains(imageInfos.suffix()))
return h.key();
}
return QString("image/jpeg");
}
/**
* manage authentification requirements
*
**/
void ImageShackUploader::manageAuthentificationRequired(QNetworkReply * reply,
QAuthenticator * authentificator)
{
if(uploadAborted==false)
{
emit authentificationRequired(reply,authentificator);
this->abortUploads();
}
}
/**
* manage proxy authentification requirements
*
**/
void ImageShackUploader::manageProxyAuthentificationRequired(const QNetworkProxy & proxy,
QAuthenticator * authenticator)
{
if(uploadAborted==false)
{
emit proxyAuthentificationRequired(proxy,authenticator);
this->abortUploads();
}
}
/**
* Manage upload errors
*
* @access private
*/
void ImageShackUploader::manageUploadError(QNetworkReply::NetworkError errorCode)
{
//qDebug() << "Error : " << errorCode;
ImageShackError::UploadError error;
if(uploadAborted==false)
{
error = ImageShackError::getErrorCode(errorCode);
if(error != ImageShackError::NoError)
{
emit uploadError(error);
this->abortUploads();
}
}
}
/**
* Manage the upload progression
*
* @access private
**/
void ImageShackUploader::manageUploadProgress(qint64 bytesReceived,qint64 bytesTotal)
{
//qDebug() << this->fileBeingUploaded->getObjectPath() << " Uploading " << bytesReceived << "/"<< bytesTotal;
if(uploadAborted==false)
{
timeoutTimer->start(TIMEOUT);
emit uploadProgress(this->fileBeingUploaded,bytesReceived,bytesTotal);
}
}
/**
* @brief slot handling time out
*/
void ImageShackUploader::handleTimeOut()
{
if(uploadAborted==false)
{
emit uploadError(ImageShackError::TimeoutError);
this->abortUploads();
}
}
/**
* @brief return true if uploads are in progress
*
* @return true/false in uploads are in progress
*/
bool ImageShackUploader::uploadsInProgress()
{
return uploadsProcessing;
}
/**
* set proxy variable to NULL in order to not
* use any proxy when uploading
*
* @access public
*/
void ImageShackUploader::noProxy()
{
this->proxy = NULL;
}
/**----------
* Getters and Setters
**----------*/
/**
* the getter of the boolean removeInformationBar
*
* @return removeInformationBar
* @access public
*/
bool ImageShackUploader::getRemoveInformationBar()
{
return this->removeInformationBar;
}
/**
* the getter of the proxy
*
* @return * QNetworkProxy
* @access public
*/
QNetworkProxy * ImageShackUploader::getProxy()
{
return this->proxy;
}
/**
* the getter of developerKey
*
* @return developerKey
* @access public
*/
QString ImageShackUploader::getDeveloperKey()
{
return this->developerKey;
}
/**
* the getter of imageUploadUrl
*
* @return developerKey
* @access public
*/
QString ImageShackUploader::getImageUploadUrl()
{
return this->imageUploadUrl;
}
/**
* the getter of userName
*
* @return developerKey
* @access public
*/
QString ImageShackUploader::getLastUserName()
{
return this->userName;
}
/**
* the setter of developerKey
*
* @param developerKey The developer key needed to upload
* to unified ImageShack API
* @access public
*/
void ImageShackUploader::setDeveloperKey(QString developerKey)
{
this->developerKey = developerKey;
}
/**
* the setter of imageUploadUrl
*
* @param developerKey The entry point to upload
* images to ImageShack
* @access public
*/
void ImageShackUploader::setImageUploadUrl(QString imageUploadUrl)
{
this->imageUploadUrl = imageUploadUrl;
}
/**
* the setter of the proxy use to upload
*
* @param QNetworkProxy the proxy to use
* @access public
*/
void ImageShackUploader::setProxy(QNetworkProxy * proxy)
{
this->proxy = proxy;
}
/**
* the setter of imageEntryUpload
*
* @param removeInformationBar the boolean to remove/leave
* the information bar from
* thumbnails
* @access public
*/
void ImageShackUploader::setRemoveInformationBar(bool removeInformationBar)
{
this->removeInformationBar = removeInformationBar;
}