From 51e98f874474fbfb81fa4f3c62912df7994d58e1 Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Sat, 9 Jan 2016 16:29:49 +0100 Subject: [PATCH 2/3] If all else fails try loading images from cloud server To: subsurface@subsurface-divelog.org Of course, as of this writing, there are no images on the server. In addition, this patch adds comments to explain the by now convoluted image retrieval logic (local file, filename as URL, by hash, cloud server). Signed-off-by: Robert C. Helling --- subsurface-core/imagedownloader.cpp | 43 ++++++++++++++++++++++++++----------- subsurface-core/imagedownloader.h | 5 ++++- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/subsurface-core/imagedownloader.cpp b/subsurface-core/imagedownloader.cpp index f365340..89e3b08 100644 --- a/subsurface-core/imagedownloader.cpp +++ b/subsurface-core/imagedownloader.cpp @@ -7,13 +7,22 @@ #include +QUrl cloudImageURL(const char *hash) +{ + return QUrl::fromUserInput(QString("https://cloud.subsurface-divelog.org/images/").append(hash)); +} + ImageDownloader::ImageDownloader(struct picture *pic) { picture = pic; } -void ImageDownloader::load(){ - QUrl url = QUrl::fromUserInput(QString(picture->filename)); +void ImageDownloader::load(bool fromHash){ + QUrl url; + if(fromHash) + url = cloudImageURL(picture->hash); + else + url = QUrl::fromUserInput(QString(picture->filename)); if (url.isValid()) { QEventLoop loop; QNetworkRequest request(url); @@ -52,10 +61,10 @@ void ImageDownloader::saveImage(QNetworkReply *reply) reply->deleteLater(); } -void loadPicture(struct picture *picture) +void loadPicture(struct picture *picture, bool fromHash) { ImageDownloader download(picture); - download.load(); + download.load(fromHash); } SHashedImage::SHashedImage(struct picture *picture) : QImage() @@ -64,17 +73,27 @@ SHashedImage::SHashedImage(struct picture *picture) : QImage() if(url.isLocalFile()) load(url.toLocalFile()); if (isNull()) { - // Hash lookup. - load(fileFromHash(picture->hash)); - if (!isNull()) { - QtConcurrent::run(updateHash, picture); + // This did not load anything. Let's try to get the image from other sources + // Let's try to load it locally via its hash + QString filename = fileFromHash(picture->hash); + if (filename.isNull()) { + // That didn't produce a local filename. + // Try the cloud server + QtConcurrent::run(loadPicture, picture, true); } else { - QtConcurrent::run(loadPicture, picture); + // Load locally from translated file name + load(filename); + if (!isNull()) { + // Make sure the hash still matches the image file + QtConcurrent::run(updateHash, picture); + } else { + // Interpret filename as URL + QtConcurrent::run(loadPicture, picture, false); + } } } else { - QByteArray hash = hashFile(url.toLocalFile()); - free(picture->hash); - picture->hash = strdup(hash.toHex().data()); + // We loaded successfully. Now, make sure hash is up to date. + QtConcurrent::run(hashPicture, picture); } } diff --git a/subsurface-core/imagedownloader.h b/subsurface-core/imagedownloader.h index e3dbfb5..c898b11 100644 --- a/subsurface-core/imagedownloader.h +++ b/subsurface-core/imagedownloader.h @@ -7,11 +7,14 @@ typedef QPair SHashedFilename; +extern QUrl cloudImageURL(const char *hash); + + class ImageDownloader : public QObject { Q_OBJECT; public: ImageDownloader(struct picture *picture); - void load(); + void load(bool fromHash); private: struct picture *picture; -- 2.5.4 (Apple Git-61)