Add QImageReader::imageFormatsForMimeType replacement function

This commit is contained in:
Jonas Kvinge 2020-05-12 19:48:37 +02:00
parent 7e22e0e552
commit c5c7a07c12
4 changed files with 30 additions and 4 deletions

View File

@ -989,6 +989,16 @@ bool IsColorDark(const QColor &color) {
return ((30 * color.red() + 59 * color.green() + 11 * color.blue()) / 100) <= 130;
}
QList<QByteArray> ImageFormatsForMimeType(const QByteArray &mimetype) {
if (mimetype == "image/bmp") return QList<QByteArray>() << "BMP";
else if (mimetype == "image/gif") return QList<QByteArray>() << "GIF";
else if (mimetype == "image/jpeg") return QList<QByteArray>() << "JPG";
else if (mimetype == "image/png") return QList<QByteArray>() << "PNG";
else return QList<QByteArray>();
}
} // namespace Utilities
ScopedWCharArray::ScopedWCharArray(const QString &str)

View File

@ -163,6 +163,8 @@ QString ReplaceVariable(const QString &variable, const Song &song, const QString
bool IsColorDark(const QColor &color);
QList<QByteArray> ImageFormatsForMimeType(const QByteArray &mimetype);
} // namespace
class ScopedWCharArray {

View File

@ -44,6 +44,7 @@
#include "core/logging.h"
#include "core/song.h"
#include "core/timeconstants.h"
#include "core/utilities.h"
#include "covermanager/albumcoverloader.h"
#include "subsonicservice.h"
#include "subsonicurlhandler.h"
@ -769,15 +770,21 @@ void SubsonicRequest::AlbumCoverReceived(QNetworkReply *reply, const QString alb
AlbumCoverFinishCheck();
return;
}
QByteArray format = QImageReader::imageFormatsForMimeType(mimetype.toUtf8()).first();
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
QList<QByteArray> format_list = QImageReader::imageFormatsForMimeType(mimetype.toUtf8());
#else
QList<QByteArray> format_list = Utilities::ImageFormatsForMimeType(mimetype.toUtf8());
#endif
QByteArray data = reply->readAll();
if (data.isEmpty()) {
if (format_list.isEmpty() || data.isEmpty()) {
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);
AlbumCoverFinishCheck();
return;
}
QByteArray format = format_list.first();
QString fullfilename = filename + "." + format.toLower();
QImage image;

View File

@ -37,6 +37,7 @@
#include "core/song.h"
#include "core/timeconstants.h"
#include "core/application.h"
#include "core/utilities.h"
#include "covermanager/albumcoverloader.h"
#include "tidalservice.h"
#include "tidalurlhandler.h"
@ -1166,15 +1167,21 @@ void TidalRequest::AlbumCoverReceived(QNetworkReply *reply, const QString &album
AlbumCoverFinishCheck();
return;
}
QByteArray format = QImageReader::imageFormatsForMimeType(mimetype.toUtf8()).first();
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
QList<QByteArray> format_list = QImageReader::imageFormatsForMimeType(mimetype.toUtf8());
#else
QList<QByteArray> format_list = Utilities::ImageFormatsForMimeType(mimetype.toUtf8());
#endif
QByteArray data = reply->readAll();
if (data.isEmpty()) {
if (format_list.isEmpty() || data.isEmpty()) {
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);
AlbumCoverFinishCheck();
return;
}
QByteArray format = format_list.first();
QImage image;
if (image.loadFromData(data, format)) {