2021-07-02 01:16:14 +02:00
|
|
|
/* This file is part of Strawberry.
|
2023-05-14 11:34:55 +02:00
|
|
|
Copyright 2018-2023, Jonas Kvinge <jonas@jkvinge.net>
|
2021-07-02 01:16:14 +02:00
|
|
|
|
|
|
|
Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Strawberry is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2023-03-18 20:03:07 +01:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QString>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
|
|
|
|
#include "core/logging.h"
|
2021-07-02 01:16:14 +02:00
|
|
|
#include "tagreaderbase.h"
|
|
|
|
|
2021-08-25 02:55:54 +02:00
|
|
|
TagReaderBase::TagReaderBase() = default;
|
|
|
|
TagReaderBase::~TagReaderBase() = default;
|
2022-07-20 11:44:46 +02:00
|
|
|
|
|
|
|
float TagReaderBase::ConvertPOPMRating(const int POPM_rating) {
|
|
|
|
|
|
|
|
if (POPM_rating < 0x01) return 0.0F;
|
|
|
|
else if (POPM_rating < 0x40) return 0.20F;
|
|
|
|
else if (POPM_rating < 0x80) return 0.40F;
|
|
|
|
else if (POPM_rating < 0xC0) return 0.60F;
|
|
|
|
else if (POPM_rating < 0xFC) return 0.80F;
|
|
|
|
|
|
|
|
return 1.0F;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int TagReaderBase::ConvertToPOPMRating(const float rating) {
|
|
|
|
|
|
|
|
if (rating < 0.20) return 0x00;
|
|
|
|
else if (rating < 0.40) return 0x01;
|
|
|
|
else if (rating < 0.60) return 0x40;
|
|
|
|
else if (rating < 0.80) return 0x80;
|
|
|
|
else if (rating < 1.0) return 0xC0;
|
|
|
|
|
|
|
|
return 0xFF;
|
|
|
|
|
|
|
|
}
|
2023-03-18 20:03:07 +01:00
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
TagReaderBase::Cover TagReaderBase::LoadCoverFromRequest(const spb::tagreader::SaveFileRequest &request) {
|
2023-03-18 20:03:07 +01:00
|
|
|
|
|
|
|
if (!request.has_save_cover() || !request.save_cover()) {
|
2023-05-14 11:34:55 +02:00
|
|
|
return Cover();
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
|
|
|
|
2023-06-06 23:18:49 +02:00
|
|
|
const QString song_filename = QString::fromUtf8(request.filename().data(), static_cast<qint64>(request.filename().size()));
|
2023-03-18 20:03:07 +01:00
|
|
|
QString cover_filename;
|
|
|
|
if (request.has_cover_filename()) {
|
2023-06-06 23:18:49 +02:00
|
|
|
cover_filename = QString::fromUtf8(request.cover_filename().data(), static_cast<qint64>(request.cover_filename().size()));
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
|
|
|
QByteArray cover_data;
|
|
|
|
if (request.has_cover_data()) {
|
2023-06-06 23:18:49 +02:00
|
|
|
cover_data = QByteArray(request.cover_data().data(), static_cast<qint64>(request.cover_data().size()));
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
2023-05-14 11:34:55 +02:00
|
|
|
QString cover_mime_type;
|
|
|
|
if (request.has_cover_mime_type()) {
|
2023-06-06 23:18:49 +02:00
|
|
|
cover_mime_type = QByteArray(request.cover_mime_type().data(), static_cast<qint64>(request.cover_mime_type().size()));
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
return LoadCoverFromRequest(song_filename, cover_filename, cover_data, cover_mime_type);
|
2023-03-18 20:03:07 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
TagReaderBase::Cover TagReaderBase::LoadCoverFromRequest(const spb::tagreader::SaveEmbeddedArtRequest &request) {
|
2023-03-18 20:03:07 +01:00
|
|
|
|
2023-06-06 23:18:49 +02:00
|
|
|
const QString song_filename = QString::fromUtf8(request.filename().data(), static_cast<qint64>(request.filename().size()));
|
2023-03-18 20:03:07 +01:00
|
|
|
QString cover_filename;
|
|
|
|
if (request.has_cover_filename()) {
|
2023-06-06 23:18:49 +02:00
|
|
|
cover_filename = QString::fromUtf8(request.cover_filename().data(), static_cast<qint64>(request.cover_filename().size()));
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
|
|
|
QByteArray cover_data;
|
|
|
|
if (request.has_cover_data()) {
|
2023-06-06 23:18:49 +02:00
|
|
|
cover_data = QByteArray(request.cover_data().data(), static_cast<qint64>(request.cover_data().size()));
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
2023-05-14 11:34:55 +02:00
|
|
|
QString cover_mime_type;
|
|
|
|
if (request.has_cover_mime_type()) {
|
2023-06-06 23:18:49 +02:00
|
|
|
cover_mime_type = QByteArray(request.cover_mime_type().data(), static_cast<qint64>(request.cover_mime_type().size()));
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
return LoadCoverFromRequest(song_filename, cover_filename, cover_data, cover_mime_type);
|
2023-03-18 20:03:07 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
TagReaderBase::Cover TagReaderBase::LoadCoverFromRequest(const QString &song_filename, const QString &cover_filename, QByteArray cover_data, QString cover_mime_type) {
|
2023-03-18 20:03:07 +01:00
|
|
|
|
|
|
|
if (cover_data.isEmpty() && !cover_filename.isEmpty()) {
|
|
|
|
qLog(Debug) << "Loading cover from" << cover_filename << "for" << song_filename;
|
|
|
|
QFile file(cover_filename);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
qLog(Error) << "Failed to open file" << cover_filename << "for reading:" << file.errorString();
|
2023-05-14 11:34:55 +02:00
|
|
|
return Cover();
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
|
|
|
cover_data = file.readAll();
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cover_data.isEmpty()) {
|
2023-05-14 11:34:55 +02:00
|
|
|
if (cover_mime_type.isEmpty()) {
|
|
|
|
cover_mime_type = QMimeDatabase().mimeTypeForData(cover_data).name();
|
|
|
|
}
|
|
|
|
if (cover_mime_type == "image/jpeg") {
|
2023-03-18 20:03:07 +01:00
|
|
|
qLog(Debug) << "Using cover from JPEG data for" << song_filename;
|
2023-05-14 11:34:55 +02:00
|
|
|
return Cover(cover_data, cover_mime_type);
|
|
|
|
}
|
|
|
|
if (cover_mime_type == "image/png") {
|
|
|
|
qLog(Debug) << "Using cover from PNG data for" << song_filename;
|
|
|
|
return Cover(cover_data, cover_mime_type);
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
|
|
|
// Convert image to JPEG.
|
|
|
|
qLog(Debug) << "Converting cover to JPEG data for" << song_filename;
|
|
|
|
QImage cover_image(cover_data);
|
|
|
|
cover_data.clear();
|
|
|
|
QBuffer buffer(&cover_data);
|
|
|
|
if (buffer.open(QIODevice::WriteOnly)) {
|
|
|
|
cover_image.save(&buffer, "JPEG");
|
|
|
|
buffer.close();
|
|
|
|
}
|
2024-04-09 23:20:26 +02:00
|
|
|
return Cover(cover_data, QStringLiteral("image/jpeg"));
|
2023-03-18 20:03:07 +01:00
|
|
|
}
|
|
|
|
|
2023-05-14 11:34:55 +02:00
|
|
|
return Cover();
|
2023-03-18 20:03:07 +01:00
|
|
|
|
|
|
|
}
|