From 82c87204e48654a9b2661c68fe66b9f16f81c4ba Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Sat, 9 Jan 2016 16:29:49 +0100 Subject: If all else fails try loading images from cloud server 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 Signed-off-by: Dirk Hohndel --- subsurface-core/imagedownloader.cpp | 44 +++++++++++++++++++++++++++---------- subsurface-core/imagedownloader.h | 5 ++++- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/subsurface-core/imagedownloader.cpp b/subsurface-core/imagedownloader.cpp index 8be1daccc..9451f8a1b 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,16 +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 fd6a91158..cd85c9509 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; -- cgit v1.2.3-70-g09d2