2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-11-06 18:39:59 +00:00
|
|
|
#include "dive.h"
|
|
|
|
#include "metrics.h"
|
|
|
|
#include "divelist.h"
|
|
|
|
#include "qthelper.h"
|
|
|
|
#include "imagedownloader.h"
|
|
|
|
#include <unistd.h>
|
2016-04-30 22:00:29 +00:00
|
|
|
#include <QString>
|
2018-03-30 05:57:30 +00:00
|
|
|
#include <QImageReader>
|
2015-11-06 18:39:59 +00:00
|
|
|
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
2018-02-18 15:22:34 +00:00
|
|
|
static QUrl cloudImageURL(const char *filename)
|
2016-01-09 15:29:49 +00:00
|
|
|
{
|
2018-02-18 15:22:34 +00:00
|
|
|
QString hash = hashString(filename);
|
2016-01-09 15:29:49 +00:00
|
|
|
return QUrl::fromUserInput(QString("https://cloud.subsurface-divelog.org/images/").append(hash));
|
|
|
|
}
|
|
|
|
|
2018-03-07 15:37:31 +00:00
|
|
|
ImageDownloader::ImageDownloader(const QString &filename_in) : filename(filename_in)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2016-03-15 20:31:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 21:45:55 +00:00
|
|
|
void ImageDownloader::load(bool fromHash)
|
|
|
|
{
|
2018-03-07 15:37:31 +00:00
|
|
|
if (fromHash && loadFromUrl(cloudImageURL(qPrintable(filename))))
|
2018-02-08 21:45:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// If loading from hash failed, try to load from filename
|
2018-03-07 15:37:31 +00:00
|
|
|
loadFromUrl(QUrl::fromUserInput(filename));
|
2018-02-08 21:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ImageDownloader::loadFromUrl(const QUrl &url)
|
|
|
|
{
|
|
|
|
bool success = false;
|
2015-11-06 18:39:59 +00:00
|
|
|
if (url.isValid()) {
|
|
|
|
QEventLoop loop;
|
2018-02-08 21:19:03 +00:00
|
|
|
QNetworkAccessManager manager;
|
2015-11-06 18:39:59 +00:00
|
|
|
QNetworkRequest request(url);
|
2018-02-08 21:45:55 +00:00
|
|
|
connect(&manager, &QNetworkAccessManager::finished, this,
|
|
|
|
[this,&success] (QNetworkReply *reply) { saveImage(reply, success); });
|
2018-02-09 22:07:42 +00:00
|
|
|
connect(&manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);
|
2018-03-25 14:46:16 +00:00
|
|
|
qDebug() << "Downloading image from" << url;
|
2015-11-06 18:39:59 +00:00
|
|
|
QNetworkReply *reply = manager.get(request);
|
2018-02-09 22:07:42 +00:00
|
|
|
loop.exec();
|
2018-02-08 21:09:01 +00:00
|
|
|
delete reply;
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2018-02-08 21:45:55 +00:00
|
|
|
return success;
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 21:45:55 +00:00
|
|
|
void ImageDownloader::saveImage(QNetworkReply *reply, bool &success)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2018-02-08 21:45:55 +00:00
|
|
|
success = false;
|
2015-11-06 18:39:59 +00:00
|
|
|
QByteArray imageData = reply->readAll();
|
2018-02-08 22:16:20 +00:00
|
|
|
QImage image;
|
2015-11-06 18:39:59 +00:00
|
|
|
image.loadFromData(imageData);
|
2018-02-08 21:45:55 +00:00
|
|
|
if (image.isNull())
|
2015-11-06 18:39:59 +00:00
|
|
|
return;
|
2018-02-08 21:45:55 +00:00
|
|
|
success = true;
|
2015-11-06 18:39:59 +00:00
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
|
|
|
hash.addData(imageData);
|
|
|
|
QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first();
|
|
|
|
QDir dir(path);
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkpath(path);
|
|
|
|
QFile imageFile(path.append("/").append(hash.result().toHex()));
|
|
|
|
if (imageFile.open(QIODevice::WriteOnly)) {
|
2018-03-25 14:46:16 +00:00
|
|
|
qDebug() << "Write image to" << imageFile.fileName();
|
2015-11-06 18:39:59 +00:00
|
|
|
QDataStream stream(&imageFile);
|
|
|
|
stream.writeRawData(imageData.data(), imageData.length());
|
|
|
|
imageFile.waitForBytesWritten(-1);
|
|
|
|
imageFile.close();
|
2018-03-07 15:37:31 +00:00
|
|
|
learnHash(filename, imageFile.fileName(), hash.result());
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2016-03-15 21:45:17 +00:00
|
|
|
// This should be called to make the picture actually show.
|
2016-04-05 05:02:03 +00:00
|
|
|
// Problem is DivePictureModel is not in core.
|
2016-03-15 21:45:17 +00:00
|
|
|
// Nevertheless, the image shows when the dive is selected the next time.
|
|
|
|
// DivePictureModel::instance()->updateDivePictures();
|
|
|
|
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 15:37:31 +00:00
|
|
|
static void loadPicture(QString filename, bool fromHash)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2018-02-06 18:58:07 +00:00
|
|
|
static QSet<QString> queuedPictures;
|
|
|
|
static QMutex pictureQueueMutex;
|
|
|
|
|
2016-04-30 22:00:29 +00:00
|
|
|
QMutexLocker locker(&pictureQueueMutex);
|
2018-03-07 15:37:31 +00:00
|
|
|
if (queuedPictures.contains(filename))
|
2016-04-30 22:00:29 +00:00
|
|
|
return;
|
2018-03-07 15:37:31 +00:00
|
|
|
queuedPictures.insert(filename);
|
2018-02-06 19:04:09 +00:00
|
|
|
locker.unlock();
|
|
|
|
|
2018-03-07 15:37:31 +00:00
|
|
|
ImageDownloader download(filename);
|
2016-01-09 15:29:49 +00:00
|
|
|
download.load(fromHash);
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 05:57:30 +00:00
|
|
|
// Overwrite QImage::load() so that we can perform better error reporting.
|
2018-03-04 15:40:06 +00:00
|
|
|
static QImage loadImage(const QString &fileName, const char *format = nullptr)
|
2018-03-30 05:57:30 +00:00
|
|
|
{
|
|
|
|
QImageReader reader(fileName, format);
|
2018-03-04 15:40:06 +00:00
|
|
|
QImage res = reader.read();
|
|
|
|
if (res.isNull())
|
2018-03-30 05:57:30 +00:00
|
|
|
qInfo() << "Error loading image" << fileName << (int)reader.error() << reader.errorString();
|
2018-03-04 15:40:06 +00:00
|
|
|
return res;
|
2018-03-30 05:57:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 15:37:31 +00:00
|
|
|
QImage getHashedImage(const QString &file)
|
2015-11-06 18:39:59 +00:00
|
|
|
{
|
2018-03-04 15:40:06 +00:00
|
|
|
QImage res;
|
2018-03-07 15:37:31 +00:00
|
|
|
QUrl url = QUrl::fromUserInput(localFilePath(file));
|
|
|
|
if (url.isLocalFile())
|
2018-03-04 15:40:06 +00:00
|
|
|
res = loadImage(url.toLocalFile());
|
|
|
|
if (res.isNull()) {
|
2016-01-09 15:29:49 +00:00
|
|
|
// 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
|
2018-03-07 15:37:31 +00:00
|
|
|
QString filenameLocal = localFilePath(qPrintable(file));
|
|
|
|
qDebug() << QStringLiteral("Translated filename: %1 -> %2").arg(file, filenameLocal);
|
|
|
|
if (filenameLocal.isNull()) {
|
2016-01-09 15:29:49 +00:00
|
|
|
// That didn't produce a local filename.
|
|
|
|
// Try the cloud server
|
2018-02-18 15:22:34 +00:00
|
|
|
// TODO: This is dead code at the moment.
|
2018-03-07 15:37:31 +00:00
|
|
|
QtConcurrent::run(loadPicture, file, true);
|
2015-11-06 18:39:59 +00:00
|
|
|
} else {
|
2016-01-09 15:29:49 +00:00
|
|
|
// Load locally from translated file name
|
2018-03-07 15:37:31 +00:00
|
|
|
res = loadImage(filenameLocal);
|
2018-03-04 15:40:06 +00:00
|
|
|
if (!res.isNull()) {
|
2016-01-09 15:29:49 +00:00
|
|
|
// Make sure the hash still matches the image file
|
2018-03-07 15:37:31 +00:00
|
|
|
QtConcurrent::run(hashPicture, filenameLocal);
|
2016-01-09 15:29:49 +00:00
|
|
|
} else {
|
|
|
|
// Interpret filename as URL
|
2018-03-07 15:37:31 +00:00
|
|
|
QtConcurrent::run(loadPicture, filenameLocal, false);
|
2016-01-09 15:29:49 +00:00
|
|
|
}
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-01-09 15:29:49 +00:00
|
|
|
// We loaded successfully. Now, make sure hash is up to date.
|
2018-03-07 15:37:31 +00:00
|
|
|
QtConcurrent::run(hashPicture, file);
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|
2018-03-04 15:40:06 +00:00
|
|
|
return res;
|
2015-11-06 18:39:59 +00:00
|
|
|
}
|