diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-06-02 18:03:03 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-07-04 02:27:36 +0800 |
commit | 08962cb38dbd7e07d98397826c5192f0a4156143 (patch) | |
tree | b974f574d8b68bd97f6b50bd0150fbd83ac6dd87 /core/imagedownloader.cpp | |
parent | 5375eee4e6a6af1402c2bf8f7cce40fc4f5340f5 (diff) | |
download | subsurface-08962cb38dbd7e07d98397826c5192f0a4156143.tar.gz |
Dive pictures: index local file name by canonical filname
The connection canonical filename to local filename was done via
two maps:
1) canonical filename -> hash
2) hash -> local filename
But the local filename was always queried from the canonical filename.
Therefore, directly index the former with the latter.
On startup, convert the old map to the new one.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/imagedownloader.cpp')
-rw-r--r-- | core/imagedownloader.cpp | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/core/imagedownloader.cpp b/core/imagedownloader.cpp index 18ecd5cd6..c201e28fe 100644 --- a/core/imagedownloader.cpp +++ b/core/imagedownloader.cpp @@ -44,22 +44,27 @@ void ImageDownloader::saveImage(QNetworkReply *reply) emit failed(filename); } else { QByteArray imageData = reply->readAll(); - 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)) { - qDebug() << "Write image to" << imageFile.fileName(); - QDataStream stream(&imageFile); - stream.writeRawData(imageData.data(), imageData.length()); - imageFile.waitForBytesWritten(-1); - imageFile.close(); - learnHash(filename, imageFile.fileName(), hash.result()); + if (imageData.isEmpty()) { + emit failed(filename); + } else { + QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first(); + QDir dir(path); + if (!dir.exists()) + dir.mkpath(path); + QCryptographicHash hash(QCryptographicHash::Sha1); + hash.addData(filename.toUtf8()); + QFile imageFile(path.append("/").append(hash.result().toHex())); + if (imageFile.open(QIODevice::WriteOnly)) { + qDebug() << "Write image to" << imageFile.fileName(); + QDataStream stream(&imageFile); + stream.writeRawData(imageData.data(), imageData.length()); + imageFile.waitForBytesWritten(-1); + imageFile.close(); + learnPictureFilename(filename, imageFile.fileName()); + hashPicture(filename); // hashPicture transforms canonical into local filename + } + emit loaded(filename); } - emit loaded(filename); } reply->deleteLater(); |