Use art id from the API as cover filename for Subsonic

Fixes #433
This commit is contained in:
Jonas Kvinge 2020-05-16 14:13:22 +02:00
parent 3fbc7031b5
commit 79ba6e628e
2 changed files with 20 additions and 17 deletions

View File

@ -27,6 +27,7 @@
#include <QByteArray> #include <QByteArray>
#include <QString> #include <QString>
#include <QUrl> #include <QUrl>
#include <QUrlQuery>
#include <QRegExp> #include <QRegExp>
#include <QImage> #include <QImage>
#include <QImageReader> #include <QImageReader>
@ -682,20 +683,22 @@ void SubsonicRequest::GetAlbumCovers() {
void SubsonicRequest::AddAlbumCoverRequest(Song &song) { void SubsonicRequest::AddAlbumCoverRequest(Song &song) {
QUrl cover_url(song.art_automatic()); QUrl cover_url(song.art_automatic());
QUrlQuery cover_url_query(cover_url);
if (!cover_url.isValid()) return; if (!cover_url.isValid()) return;
if (album_covers_requests_sent_.contains(song.album_id())) { if (album_covers_requests_sent_.contains(cover_url)) {
album_covers_requests_sent_.insertMulti(song.album_id(), &song); album_covers_requests_sent_.insertMulti(cover_url, &song);
return; return;
} }
AlbumCoverRequest request; AlbumCoverRequest request;
request.album_id = song.album_id(); request.album_id = song.album_id();
request.url = cover_url; request.url = cover_url;
request.filename = app_->album_cover_loader()->CoverFilePath(song.source(), song.effective_albumartist(), song.effective_album(), song.album_id(), QString(), cover_url); request.filename = Song::ImageCacheDir(Song::Source_Subsonic) + "/" + cover_url_query.queryItemValue("id");
if (request.filename.isEmpty()) return; if (request.filename.isEmpty()) return;
album_covers_requests_sent_.insertMulti(song.album_id(), &song); album_covers_requests_sent_.insertMulti(cover_url, &song);
++album_covers_requested_; ++album_covers_requested_;
album_cover_requests_queue_.enqueue(request); album_cover_requests_queue_.enqueue(request);
@ -720,13 +723,13 @@ void SubsonicRequest::FlushAlbumCoverRequests() {
QNetworkReply *reply = network_->get(req); QNetworkReply *reply = network_->get(req);
album_cover_replies_ << reply; album_cover_replies_ << reply;
connect(reply, &QNetworkReply::finished, [=] { AlbumCoverReceived(reply, request.album_id, request.url, request.filename); }); connect(reply, &QNetworkReply::finished, [=] { AlbumCoverReceived(reply, request.url, request.filename); });
} }
} }
void SubsonicRequest::AlbumCoverReceived(QNetworkReply *reply, const QString album_id, const QUrl url, const QString filename) { void SubsonicRequest::AlbumCoverReceived(QNetworkReply *reply, const QUrl url, const QString filename) {
if (album_cover_replies_.contains(reply)) { if (album_cover_replies_.contains(reply)) {
album_cover_replies_.removeAll(reply); album_cover_replies_.removeAll(reply);
@ -745,21 +748,21 @@ void SubsonicRequest::AlbumCoverReceived(QNetworkReply *reply, const QString alb
emit UpdateProgress(album_covers_received_); emit UpdateProgress(album_covers_received_);
if (!album_covers_requests_sent_.contains(album_id)) { if (!album_covers_requests_sent_.contains(url)) {
AlbumCoverFinishCheck(); AlbumCoverFinishCheck();
return; return;
} }
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {
Error(QString("%1 (%2) for %3").arg(reply->errorString()).arg(reply->error()).arg(url.toString())); Error(QString("%1 (%2) for %3").arg(reply->errorString()).arg(reply->error()).arg(url.toString()));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id); if (album_covers_requests_sent_.contains(url)) album_covers_requests_sent_.remove(url);
AlbumCoverFinishCheck(); AlbumCoverFinishCheck();
return; return;
} }
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) { if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
Error(QString("Received HTTP code %1 for %2.").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()).arg(url.toString())); Error(QString("Received HTTP code %1 for %2.").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()).arg(url.toString()));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id); if (album_covers_requests_sent_.contains(url)) album_covers_requests_sent_.remove(url);
AlbumCoverFinishCheck(); AlbumCoverFinishCheck();
return; return;
} }
@ -767,7 +770,7 @@ void SubsonicRequest::AlbumCoverReceived(QNetworkReply *reply, const QString alb
QString mimetype = reply->header(QNetworkRequest::ContentTypeHeader).toString(); QString mimetype = reply->header(QNetworkRequest::ContentTypeHeader).toString();
if (!QImageReader::supportedMimeTypes().contains(mimetype.toUtf8())) { if (!QImageReader::supportedMimeTypes().contains(mimetype.toUtf8())) {
Error(QString("Unsupported mimetype for image reader %1 for %2").arg(mimetype).arg(url.toString())); Error(QString("Unsupported mimetype for image reader %1 for %2").arg(mimetype).arg(url.toString()));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id); if (album_covers_requests_sent_.contains(url)) album_covers_requests_sent_.remove(url);
AlbumCoverFinishCheck(); AlbumCoverFinishCheck();
return; return;
} }
@ -781,7 +784,7 @@ void SubsonicRequest::AlbumCoverReceived(QNetworkReply *reply, const QString alb
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();
if (format_list.isEmpty() || data.isEmpty()) { if (format_list.isEmpty() || data.isEmpty()) {
Error(QString("Received empty image data for %1").arg(url.toString())); Error(QString("Received empty image data for %1").arg(url.toString()));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id); if (album_covers_requests_sent_.contains(url)) album_covers_requests_sent_.remove(url);
AlbumCoverFinishCheck(); AlbumCoverFinishCheck();
return; return;
} }
@ -791,19 +794,19 @@ void SubsonicRequest::AlbumCoverReceived(QNetworkReply *reply, const QString alb
QImage image; QImage image;
if (image.loadFromData(data, format)) { if (image.loadFromData(data, format)) {
if (image.save(fullfilename, format)) { if (image.save(fullfilename, format)) {
while (album_covers_requests_sent_.contains(album_id)) { while (album_covers_requests_sent_.contains(url)) {
Song *song = album_covers_requests_sent_.take(album_id); Song *song = album_covers_requests_sent_.take(url);
song->set_art_automatic(QUrl::fromLocalFile(fullfilename)); song->set_art_automatic(QUrl::fromLocalFile(fullfilename));
} }
} }
else { else {
Error(QString("Error saving image data to %1.").arg(fullfilename)); Error(QString("Error saving image data to %1.").arg(fullfilename));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id); if (album_covers_requests_sent_.contains(url)) album_covers_requests_sent_.remove(url);
} }
} }
else { else {
Error(QString("Error decoding image data from %1.").arg(url.toString())); Error(QString("Error decoding image data from %1.").arg(url.toString()));
if (album_covers_requests_sent_.contains(album_id)) album_covers_requests_sent_.remove(album_id); if (album_covers_requests_sent_.contains(url)) album_covers_requests_sent_.remove(url);
} }
AlbumCoverFinishCheck(); AlbumCoverFinishCheck();

View File

@ -69,7 +69,7 @@ class SubsonicRequest : public SubsonicBaseRequest {
private slots: private slots:
void AlbumsReplyReceived(QNetworkReply *reply, const int offset_requested); void AlbumsReplyReceived(QNetworkReply *reply, const int offset_requested);
void AlbumSongsReplyReceived(QNetworkReply *reply, const QString artist_id, const QString album_id, const QString album_artist); void AlbumSongsReplyReceived(QNetworkReply *reply, const QString artist_id, const QString album_id, const QString album_artist);
void AlbumCoverReceived(QNetworkReply *reply, const QString album_id, const QUrl url, const QString filename); void AlbumCoverReceived(QNetworkReply *reply, const QUrl url, const QString filename);
private: private:
typedef QPair<QString, QString> Param; typedef QPair<QString, QString> Param;
@ -128,7 +128,7 @@ class SubsonicRequest : public SubsonicBaseRequest {
QQueue<AlbumCoverRequest> album_cover_requests_queue_; QQueue<AlbumCoverRequest> album_cover_requests_queue_;
QHash<QString, Request> album_songs_requests_pending_; QHash<QString, Request> album_songs_requests_pending_;
QMultiMap<QString, Song*> album_covers_requests_sent_; QMultiMap<QUrl, Song*> album_covers_requests_sent_;
int albums_requests_active_; int albums_requests_active_;