MoodbarLoader: Fix loading cached moodbars

This commit is contained in:
Jonas Kvinge 2023-06-07 00:50:35 +02:00
parent 9db7896828
commit 972053c699
2 changed files with 31 additions and 13 deletions

View File

@ -91,6 +91,12 @@ QStringList MoodbarLoader::MoodFilenames(const QString &song_filename) {
} }
QUrl MoodbarLoader::CacheUrlEntry(const QString &filename) {
return QUrl(QUrl::toPercentEncoding(filename));
}
MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, const bool has_cue, QByteArray *data, MoodbarPipeline **async_pipeline) { MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, const bool has_cue, QByteArray *data, MoodbarPipeline **async_pipeline) {
if (!url.isLocalFile() || has_cue) { if (!url.isLocalFile() || has_cue) {
@ -122,12 +128,16 @@ MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, const bool has_cue, Q
} }
// Maybe it exists in the cache? // Maybe it exists in the cache?
std::unique_ptr<QIODevice> cache_device(cache_->data(url));
if (cache_device) { QNetworkCacheMetaData disk_cache_metadata = cache_->metaData(CacheUrlEntry(filename));
qLog(Info) << "Loading cached moodbar data for" << filename; if (disk_cache_metadata.isValid()) {
*data = cache_device->readAll(); std::unique_ptr<QIODevice> device_cache_file(cache_->data(disk_cache_metadata.url()));
if (!data->isEmpty()) { if (device_cache_file) {
return Result::Loaded; qLog(Info) << "Loading cached moodbar data for" << filename;
*data = device_cache_file->readAll();
if (!data->isEmpty()) {
return Result::Loaded;
}
} }
} }
@ -169,16 +179,23 @@ void MoodbarLoader::RequestFinished(MoodbarPipeline *request, const QUrl &url) {
Q_ASSERT(QThread::currentThread() == qApp->thread()); Q_ASSERT(QThread::currentThread() == qApp->thread());
if (request->success()) { if (request->success()) {
qLog(Info) << "Moodbar data generated successfully for" << url.toLocalFile();
const QString filename = url.toLocalFile();
qLog(Info) << "Moodbar data generated successfully for" << filename;
// Save the data in the cache // Save the data in the cache
QNetworkCacheMetaData metadata; QNetworkCacheMetaData disk_cache_metadata;
metadata.setUrl(url); disk_cache_metadata.setSaveToDisk(true);
disk_cache_metadata.setUrl(CacheUrlEntry(filename));
// Qt 6 now ignores any entry without headers, so add a fake header.
disk_cache_metadata.setRawHeaders(QNetworkCacheMetaData::RawHeaderList() << qMakePair(QByteArray(), QByteArray()));
QIODevice *cache_file = cache_->prepare(metadata); QIODevice *device_cache_file = cache_->prepare(disk_cache_metadata);
if (cache_file) { if (device_cache_file) {
if (cache_file->write(request->data()) > 0) { const qint64 data_written = device_cache_file->write(request->data());
cache_->insert(cache_file); if (data_written > 0) {
cache_->insert(device_cache_file);
} }
} }

View File

@ -62,6 +62,7 @@ class MoodbarLoader : public QObject {
private: private:
static QStringList MoodFilenames(const QString &song_filename); static QStringList MoodFilenames(const QString &song_filename);
static QUrl CacheUrlEntry(const QString &filename);
private: private:
QNetworkDiskCache *cache_; QNetworkDiskCache *cache_;