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>
This commit is contained in:
Berthold Stoeger 2018-06-02 18:03:03 +02:00 committed by Dirk Hohndel
parent 5375eee4e6
commit 08962cb38d
5 changed files with 100 additions and 48 deletions

View file

@ -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();