- Move sha1 cover hash to utilities

- When initializing song check if we have a cover art. If not, check if there is one in the cache and use this.
This commit is contained in:
Andreas 2013-12-31 15:21:50 +01:00
parent b50b2c27ea
commit 94ccd325d7
5 changed files with 47 additions and 6 deletions

View File

@ -442,6 +442,8 @@ void Song::InitFromProtobuf(const pb::tagreader::SongMetadata& pb) {
if (pb.has_rating()) {
d->rating_ = pb.rating();
}
InitArtManual();
}
void Song::ToProtobuf(pb::tagreader::SongMetadata* pb) const {
@ -545,6 +547,8 @@ void Song::InitFromQuery(const SqlRow& q, bool reliable_metadata, int col) {
d->performer_ = tostr(col + 38);
d->grouping_ = tostr(col + 39);
InitArtManual();
#undef tostr
#undef toint
#undef tolonglong
@ -569,6 +573,17 @@ void Song::InitFromFilePartial(const QString& filename) {
}
}
void Song::InitArtManual() {
// If we don't have an art, check if we have one in the cache
if (d->art_manual_.isEmpty() && d->art_automatic_.isEmpty()) {
QString filename(Utilities::Sha1CoverHash(d->artist_, d->album_).toHex() + ".jpg");
QString path(AlbumCoverLoader::ImageCacheDir() + "/" + filename);
if (QFile::exists(path)) {
d->art_manual_ = path;
}
}
}
#ifdef HAVE_LIBLASTFM
void Song::InitFromLastFM(const lastfm::Track& track) {
d->valid_ = true;

View File

@ -106,6 +106,7 @@ class Song {
void InitFromProtobuf(const pb::tagreader::SongMetadata& pb);
void InitFromQuery(const SqlRow& query, bool reliable_metadata, int col = 0);
void InitFromFilePartial(const QString& filename); // Just store the filename: incomplete but fast
void InitArtManual(); // Check if there is already a art in the cache and store the filename in art_manual
#ifdef HAVE_LIBLASTFM
void InitFromLastFM(const lastfm::Track& track);
#endif

View File

@ -25,6 +25,7 @@
#include <QDateTime>
#include <QDesktopServices>
#include <QDir>
#include <QFile>
#include <QIODevice>
#include <QMetaEnum>
#include <QMouseEvent>
@ -455,6 +456,31 @@ QByteArray Sha256(const QByteArray& data) {
return ret;
}
// File must not be open and will be closed afterwards!
QByteArray Md5File(QFile &file) {
file.open(QIODevice::ReadOnly);
QCryptographicHash hash(QCryptographicHash::Md5);
QByteArray data;
while(!file.atEnd()) {
data = file.read(1000000); // 1 mib
hash.addData(data.data(), data.length());
data.clear();
}
file.close();
return hash.result();
}
QByteArray Sha1CoverHash(const QString& artist, const QString& album) {
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(artist.toLower().toUtf8().constData());
hash.addData(album.toLower().toUtf8().constData());
return hash.result();
}
QString PrettySize(const QSize& size) {
return QString::number(size.width()) + "x" +
QString::number(size.height());

View File

@ -19,6 +19,7 @@
#define UTILITIES_H
#include <QColor>
#include <QFile>
#include <QLocale>
#include <QCryptographicHash>
#include <QSize>
@ -66,6 +67,8 @@ namespace Utilities {
QByteArray HmacSha256(const QByteArray& key, const QByteArray& data);
QByteArray HmacSha1(const QByteArray& key, const QByteArray& data);
QByteArray Sha256(const QByteArray& data);
QByteArray Md5File(QFile& file);
QByteArray Sha1CoverHash(const QString& artist, const QString& album);
// Picks an unused ephemeral port number. Doesn't hold the port open so

View File

@ -18,6 +18,7 @@
#include "core/application.h"
#include "core/logging.h"
#include "core/utilities.h"
#include "covers/albumcoverfetcher.h"
#include "covers/albumcoverloader.h"
#include "covers/currentartloader.h"
@ -29,7 +30,6 @@
#include "ui/iconloader.h"
#include <QAction>
#include <QCryptographicHash>
#include <QDialog>
#include <QDragEnterEvent>
#include <QFileDialog>
@ -247,11 +247,7 @@ QString AlbumCoverChoiceController::SaveCoverInCache(
const QString& artist, const QString& album, const QImage& image) {
// Hash the artist and album into a filename for the image
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(artist.toLower().toUtf8().constData());
hash.addData(album.toLower().toUtf8().constData());
QString filename(hash.result().toHex() + ".jpg");
QString filename(Utilities::Sha1CoverHash(artist, album).toHex() + ".jpg");
QString path(AlbumCoverLoader::ImageCacheDir() + "/" + filename);
// Make sure this directory exists first