MoodbarLoader: Fix loading cached moodbars
This commit is contained in:
parent
9db7896828
commit
972053c699
|
@ -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,14 +128,18 @@ 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));
|
||||||
|
if (disk_cache_metadata.isValid()) {
|
||||||
|
std::unique_ptr<QIODevice> device_cache_file(cache_->data(disk_cache_metadata.url()));
|
||||||
|
if (device_cache_file) {
|
||||||
qLog(Info) << "Loading cached moodbar data for" << filename;
|
qLog(Info) << "Loading cached moodbar data for" << filename;
|
||||||
*data = cache_device->readAll();
|
*data = device_cache_file->readAll();
|
||||||
if (!data->isEmpty()) {
|
if (!data->isEmpty()) {
|
||||||
return Result::Loaded;
|
return Result::Loaded;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!thread_->isRunning()) thread_->start(QThread::IdlePriority);
|
if (!thread_->isRunning()) thread_->start(QThread::IdlePriority);
|
||||||
|
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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_;
|
||||||
|
|
Loading…
Reference in New Issue