From 7efced069b8740833fec4682ad80dde6b7767d1d Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Sat, 9 Jan 2016 16:32:19 +0100 Subject: [PATCH 3/3] Upload images to cloud server To: subsurface@subsurface-divelog.org When we successfully loaded an image, we check the if the cloud server already knows about an image with that hash and if not does an HTTP PUT of that image file. Signed-off-by: Robert C. Helling --- subsurface-core/imagedownloader.cpp | 50 +++++++++++++++++++++++++++++++++++++ subsurface-core/imagedownloader.h | 16 ++++++++++++ subsurface-core/qthelper.cpp | 2 ++ 3 files changed, 68 insertions(+) diff --git a/subsurface-core/imagedownloader.cpp b/subsurface-core/imagedownloader.cpp index 89e3b08..ada4f51 100644 --- a/subsurface-core/imagedownloader.cpp +++ b/subsurface-core/imagedownloader.cpp @@ -111,3 +111,53 @@ SHashedImage::SHashedImage(const SHashedImage &image) : QImage(image) { } + + +CloudImageUploader::CloudImageUploader(QString myFilename, QByteArray myHash) +{ + filename = myFilename; + hash = myHash; + url = cloudImageURL(hash.toHex()); + headRequest = QNetworkRequest(url); +} + +void CloudImageUploader::put() +{ + if(!prefs.cloud_background_sync) + return; + // Let's see if the server already knows about this image + QNetworkReply *reply = manager.head(headRequest); + connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError))); + reply->deleteLater(); + QEventLoop loop; + while (reply->isRunning()) { + loop.processEvents(); + sleep(1); + } +} + +void CloudImageUploader::error(QNetworkReply::NetworkError error) +{ + if (error == QNetworkReply::ContentNotFoundError) { + // Not found + // Let's tell the server about the file + qDebug() << "Trying to upload" << filename; + QFile imagefile(filename); + QNetworkRequest request(url); + if (imagefile.exists() && imagefile.open(QIODevice::ReadOnly)) { + QNetworkReply *reply = manager.put(request, imagefile.readAll()); + QEventLoop loop; + reply->deleteLater(); + while (reply->isRunning() && !reply->error()) { + loop.processEvents(); + sleep(1); + } + } + } +} + +void uploadImage(QString filename, const QByteArray hash) +{ + CloudImageUploader uploader(filename, hash); + uploader.put(); +} diff --git a/subsurface-core/imagedownloader.h b/subsurface-core/imagedownloader.h index c898b11..f4f51b9 100644 --- a/subsurface-core/imagedownloader.h +++ b/subsurface-core/imagedownloader.h @@ -8,6 +8,7 @@ typedef QPair SHashedFilename; extern QUrl cloudImageURL(const char *hash); +extern void uploadImage(QString filename, const QByteArray hash); class ImageDownloader : public QObject { @@ -32,4 +33,19 @@ public: SHashedImage(); }; +class CloudImageUploader : public QObject { + Q_OBJECT; +public: + CloudImageUploader(QString myFilename, QByteArray myHash); + void put(); +private: + QString filename; + QByteArray hash; + QNetworkAccessManager manager; + QUrl url; + QNetworkRequest headRequest; +private slots: + void error(QNetworkReply::NetworkError error); +}; + #endif // IMAGEDOWNLOADER_H diff --git a/subsurface-core/qthelper.cpp b/subsurface-core/qthelper.cpp index 0a0aeda..839faa3 100644 --- a/subsurface-core/qthelper.cpp +++ b/subsurface-core/qthelper.cpp @@ -6,6 +6,7 @@ #include "subsurfacesysinfo.h" #include "version.h" #include "divecomputer.h" +#include "imagedownloader.h" #include "time.h" #include "gettextfromc.h" #include @@ -1317,6 +1318,7 @@ QByteArray hashFile(const QString filename) if (imagefile.exists() && imagefile.open(QIODevice::ReadOnly)) { hash.addData(&imagefile); add_hash(filename, hash.result()); + QtConcurrent::run(uploadImage, filename, hash.result()); return hash.result(); } else { return QByteArray(); -- 2.5.4 (Apple Git-61)