From b0b8ff2d494e8ba5c9b33bd28acbba54e6868cd3 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Tue, 18 Apr 2023 18:42:37 +0200 Subject: [PATCH] CollectionModel: URL percent encode disk cache keys Fixes #1183 --- src/collection/collectionmodel.cpp | 33 ++++++++++++++++++------------ src/collection/collectionmodel.h | 1 + 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/collection/collectionmodel.cpp b/src/collection/collectionmodel.cpp index 8c7614df0..a006ffb2a 100644 --- a/src/collection/collectionmodel.cpp +++ b/src/collection/collectionmodel.cpp @@ -558,7 +558,7 @@ void CollectionModel::SongsDeleted(const SongList &songs) { // Remove from pixmap cache const QString cache_key = AlbumIconPixmapCacheKey(ItemToIndex(node)); QPixmapCache::remove(cache_key); - if (use_disk_cache_ && sIconCache) sIconCache->remove(QUrl(cache_key)); + if (use_disk_cache_ && sIconCache) sIconCache->remove(AlbumIconPixmapDiskCacheKey(cache_key)); if (pending_cache_keys_.contains(cache_key)) { pending_cache_keys_.remove(cache_key); } @@ -613,6 +613,12 @@ QString CollectionModel::AlbumIconPixmapCacheKey(const QModelIndex &idx) const { } +QUrl CollectionModel::AlbumIconPixmapDiskCacheKey(const QString &cache_key) const { + + return QUrl(QUrl::toPercentEncoding(cache_key)); + +} + QVariant CollectionModel::AlbumIcon(const QModelIndex &idx) { CollectionItem *item = IndexToItem(idx); @@ -628,10 +634,10 @@ QVariant CollectionModel::AlbumIcon(const QModelIndex &idx) { // Try to load it from the disk cache if (use_disk_cache_ && sIconCache) { - std::unique_ptr cache(sIconCache->data(QUrl(cache_key))); - if (cache) { + std::unique_ptr disk_cache_img(sIconCache->data(AlbumIconPixmapDiskCacheKey(cache_key))); + if (disk_cache_img) { QImage cached_image; - if (cached_image.load(cache.get(), "XPM")) { + if (cached_image.load(disk_cache_img.get(), "XPM")) { QPixmapCache::insert(cache_key, QPixmap::fromImage(cached_image)); return QPixmap::fromImage(cached_image); } @@ -682,15 +688,16 @@ void CollectionModel::AlbumCoverLoaded(const quint64 id, AlbumCoverLoaderResultP // If we have a valid cover not already in the disk cache if (use_disk_cache_ && sIconCache && result->success && !result->image_scaled.isNull()) { - std::unique_ptr cached_img(sIconCache->data(QUrl(cache_key))); - if (!cached_img) { - QNetworkCacheMetaData item_metadata; - item_metadata.setSaveToDisk(true); - item_metadata.setUrl(QUrl(cache_key)); - QIODevice *cache = sIconCache->prepare(item_metadata); - if (cache) { - result->image_scaled.save(cache, "XPM"); - sIconCache->insert(cache); + const QUrl disk_cache_key = AlbumIconPixmapDiskCacheKey(cache_key); + std::unique_ptr disk_cache_img(sIconCache->data(disk_cache_key)); + if (!disk_cache_img) { + QNetworkCacheMetaData disk_cache_metadata; + disk_cache_metadata.setSaveToDisk(true); + disk_cache_metadata.setUrl(disk_cache_key); + QIODevice *device_iconcache = sIconCache->prepare(disk_cache_metadata); + if (device_iconcache) { + result->image_scaled.save(device_iconcache, "XPM"); + sIconCache->insert(device_iconcache); } } } diff --git a/src/collection/collectionmodel.h b/src/collection/collectionmodel.h index a5970ff1d..723094ca8 100644 --- a/src/collection/collectionmodel.h +++ b/src/collection/collectionmodel.h @@ -267,6 +267,7 @@ class CollectionModel : public SimpleTreeModel { // Helpers static bool IsCompilationArtistNode(const CollectionItem *node) { return node == node->parent->compilation_artist_node_; } QString AlbumIconPixmapCacheKey(const QModelIndex &idx) const; + QUrl AlbumIconPixmapDiskCacheKey(const QString &cache_key) const; QVariant AlbumIcon(const QModelIndex &idx); QVariant data(const CollectionItem *item, const int role) const; bool CompareItems(const CollectionItem *a, const CollectionItem *b) const;