Changed the way album covers are associated, fixes #503

This commit is contained in:
emeric
2024-09-29 16:21:46 +02:00
parent 4e1b422e31
commit 3f6177cedd
31 changed files with 502 additions and 231 deletions
+19 -193
View File
@@ -19,8 +19,6 @@
#include "CoverService.hpp"
#include <set>
#include "av/IAudioFile.hpp"
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
@@ -41,52 +39,6 @@ namespace lms::cover
{
namespace
{
struct TrackInfo
{
bool hasCover{};
bool isMultiDisc{};
std::filesystem::path trackPath;
std::optional<db::ReleaseId> releaseId;
};
std::optional<TrackInfo> getTrackInfo(db::Session& dbSession, db::TrackId trackId)
{
std::optional<TrackInfo> res;
auto transaction{ dbSession.createReadTransaction() };
const db::Track::pointer track{ db::Track::find(dbSession, trackId) };
if (!track)
return res;
res = TrackInfo{};
res->hasCover = track->hasCover();
res->trackPath = track->getAbsoluteFilePath();
if (const db::Release::pointer & release{ track->getRelease() })
{
res->releaseId = release->getId();
if (release->getTotalDisc() > 1)
res->isMultiDisc = true;
}
return res;
}
std::vector<std::string> constructPreferredFileNames()
{
std::vector<std::string> res;
core::Service<core::IConfig>::get()->visitStrings("cover-preferred-file-names",
[&res](std::string_view fileName) {
res.emplace_back(fileName);
},
{ "cover", "front" });
return res;
}
bool isFileSupported(const std::filesystem::path& file, const std::vector<std::filesystem::path>& extensions)
{
return (std::find(std::cbegin(extensions), std::cend(extensions), file.extension()) != std::cend(extensions));
@@ -104,15 +56,11 @@ namespace lms::cover
const std::filesystem::path& defaultSvgCoverPath)
: _db{ db }
, _cache{ core::Service<core::IConfig>::get()->getULong("cover-max-cache-size", 30) * 1000 * 1000 }
, _maxFileSize{ core::Service<core::IConfig>::get()->getULong("cover-max-file-size", 10) * 1000 * 1000 }
, _preferredFileNames{ constructPreferredFileNames() }
{
setJpegQuality(core::Service<core::IConfig>::get()->getULong("cover-jpeg-quality", 75));
LMS_LOG(COVER, INFO, "Default cover path = '" << defaultSvgCoverPath.string() << "'");
LMS_LOG(COVER, INFO, "Max cache size = " << _cache.getMaxCacheSize());
LMS_LOG(COVER, INFO, "Max file size = " << _maxFileSize);
LMS_LOG(COVER, INFO, "Preferred file names: " << core::stringUtils::joinStrings(_preferredFileNames, ","));
_defaultCover = image::readSvgFile(defaultSvgCoverPath); // may throw
}
@@ -140,7 +88,7 @@ namespace lms::cover
return image;
}
std::unique_ptr<IEncodedImage> CoverService::getFromCoverFile(const std::filesystem::path& p, ImageSize width) const
std::unique_ptr<IEncodedImage> CoverService::getFromImageFile(const std::filesystem::path& p, ImageSize width) const
{
std::unique_ptr<IEncodedImage> image;
@@ -163,66 +111,7 @@ namespace lms::cover
return _defaultCover;
}
std::unique_ptr<IEncodedImage> CoverService::getFromDirectory(const std::filesystem::path& directory, ImageSize width, const std::vector<std::string>& preferredFileNames, bool allowPickRandom) const
{
const std::multimap<std::string, std::filesystem::path> coverPaths{ getCoverPaths(directory) };
auto tryLoadImageFromFilename = [&](std::string_view fileName) {
std::unique_ptr<IEncodedImage> image;
auto range{ coverPaths.equal_range(std::string{ fileName }) };
for (auto it{ range.first }; it != range.second; ++it)
{
image = getFromCoverFile(it->second, width);
if (image)
break;
}
return image;
};
std::unique_ptr<IEncodedImage> image;
for (const std::string_view filename : preferredFileNames)
{
image = tryLoadImageFromFilename(filename);
if (image)
return image;
}
if (allowPickRandom)
{
for (const auto& [filename, coverPath] : coverPaths)
{
image = getFromCoverFile(coverPath, width);
if (image)
return image;
}
}
return image;
}
std::unique_ptr<IEncodedImage> CoverService::getFromSameNamedFile(const std::filesystem::path& filePath, ImageSize width) const
{
std::unique_ptr<IEncodedImage> res;
std::filesystem::path coverPath{ filePath };
for (const std::filesystem::path& extension : _fileExtensions)
{
coverPath.replace_extension(extension);
if (!checkCoverFile(coverPath))
continue;
res = getFromCoverFile(coverPath, width);
if (res)
break;
}
return res;
}
bool CoverService::checkCoverFile(const std::filesystem::path& filePath) const
bool CoverService::checkImageFile(const std::filesystem::path& filePath) const
{
std::error_code ec;
@@ -235,35 +124,9 @@ namespace lms::cover
if (!std::filesystem::is_regular_file(filePath, ec))
return false;
if (std::filesystem::file_size(filePath, ec) > _maxFileSize && !ec)
{
LMS_LOG(COVER, INFO, "Image file '" << filePath.string() << " is too big (" << std::filesystem::file_size(filePath, ec) << "), limit is " << _maxFileSize);
return false;
}
return true;
}
std::multimap<std::string, std::filesystem::path> CoverService::getCoverPaths(const std::filesystem::path& directoryPath) const
{
std::multimap<std::string, std::filesystem::path> res;
std::error_code ec;
std::filesystem::directory_iterator itPath(directoryPath, ec);
const std::filesystem::directory_iterator itEnd;
while (!ec && itPath != itEnd)
{
const std::filesystem::path& path{ *itPath };
if (checkCoverFile(path))
res.emplace(std::filesystem::path{ path }.filename().replace_extension("").string(), path);
itPath.increment(ec);
}
return res;
}
std::unique_ptr<IEncodedImage> CoverService::getFromTrack(const std::filesystem::path& p, ImageSize width) const
{
std::unique_ptr<IEncodedImage> image;
@@ -282,35 +145,19 @@ namespace lms::cover
std::shared_ptr<IEncodedImage> CoverService::getFromTrack(db::TrackId trackId, ImageSize width)
{
return getFromTrack(_db.getTLSSession(), trackId, width, true /* allow release fallback*/);
}
std::shared_ptr<IEncodedImage> CoverService::getFromTrack(db::Session& dbSession, db::TrackId trackId, ImageSize width, bool allowReleaseFallback)
{
using namespace db;
const ImageCache::EntryDesc cacheEntryDesc{ trackId, width };
std::shared_ptr<IEncodedImage> cover{ _cache.getImage(cacheEntryDesc) };
if (cover)
return cover;
if (const std::optional<TrackInfo> trackInfo{ getTrackInfo(dbSession, trackId) })
{
if (trackInfo->hasCover)
cover = getFromTrack(trackInfo->trackPath, width);
db::Session& session{ _db.getTLSSession() };
auto transaction{ session.createReadTransaction() };
if (!cover)
cover = getFromSameNamedFile(trackInfo->trackPath, width);
if (!cover && trackInfo->releaseId && allowReleaseFallback)
cover = getFromRelease(*trackInfo->releaseId, width);
if (!cover && trackInfo->isMultiDisc)
{
if (trackInfo->trackPath.parent_path().has_parent_path())
cover = getFromDirectory(trackInfo->trackPath.parent_path().parent_path(), width, _preferredFileNames, true);
}
const db::Track::pointer track{ db::Track::find(session, trackId) };
if (track && track->hasCover())
cover = getFromTrack(track->getAbsoluteFilePath(), width);
}
if (cover)
@@ -324,47 +171,26 @@ namespace lms::cover
using namespace db;
const ImageCache::EntryDesc cacheEntryDesc{ releaseId, width };
std::shared_ptr<IEncodedImage> cover{ _cache.getImage(cacheEntryDesc) };
if (cover)
return cover;
std::shared_ptr<IEncodedImage> image{ _cache.getImage(cacheEntryDesc) };
if (image)
return image;
struct ReleaseInfo
{
TrackId firstTrackId;
std::filesystem::path releaseDirectory;
};
Session& session{ _db.getTLSSession() };
auto getReleaseInfo{ [&] {
std::optional<ReleaseInfo> res;
Session& session{ _db.getTLSSession() };
auto transaction{ session.createReadTransaction() };
// get a track in this release, consider the release is in a single directory
const auto tracks{ Track::find(session, Track::FindParameters{}.setRelease(releaseId).setRange(Range{ 0, 1 }).setSortMethod(TrackSortMethod::Release)) };
if (!tracks.results.empty())
const db::Release::pointer release{ db::Release::find(session, releaseId) };
if (release)
{
const Track::pointer& track{ tracks.results.front() };
res = ReleaseInfo{};
res->firstTrackId = track->getId();
res->releaseDirectory = track->getAbsoluteFilePath().parent_path();
if (const db::Image::pointer dbImage{ release->getImage() })
image = getFromImageFile(dbImage->getAbsoluteFilePath(), width);
}
return res;
} };
if (const std::optional<ReleaseInfo> releaseInfo{ getReleaseInfo() })
{
cover = getFromDirectory(releaseInfo->releaseDirectory, width, _preferredFileNames, true);
if (!cover)
cover = getFromTrack(session, releaseInfo->firstTrackId, width, false /* no release fallback */);
}
if (cover)
_cache.addImage(cacheEntryDesc, cover);
if (image)
_cache.addImage(cacheEntryDesc, image);
return cover;
return image;
}
std::shared_ptr<IEncodedImage> CoverService::getFromArtist(db::ArtistId artistId, ImageSize width)
@@ -384,7 +210,7 @@ namespace lms::cover
if (const Artist::pointer artist{ Artist::find(session, artistId) })
{
if (const db::Image::pointer image{ artist->getImage() })
artistImage = getFromCoverFile(image->getAbsoluteFilePath(), width);
artistImage = getFromImageFile(image->getAbsoluteFilePath(), width);
}
}
@@ -20,7 +20,6 @@
#pragma once
#include <filesystem>
#include <map>
#include <vector>
#include "database/Types.hpp"
@@ -59,14 +58,11 @@ namespace lms::cover
std::shared_ptr<image::IEncodedImage> getFromTrack(db::Session& dbSession, db::TrackId trackId, image::ImageSize width, bool allowReleaseFallback);
std::unique_ptr<image::IEncodedImage> getFromAvMediaFile(const av::IAudioFile& input, image::ImageSize width) const;
std::unique_ptr<image::IEncodedImage> getFromCoverFile(const std::filesystem::path& p, image::ImageSize width) const;
std::unique_ptr<image::IEncodedImage> getFromImageFile(const std::filesystem::path& p, image::ImageSize width) const;
std::unique_ptr<image::IEncodedImage> getFromTrack(const std::filesystem::path& path, image::ImageSize width) const;
std::multimap<std::string, std::filesystem::path> getCoverPaths(const std::filesystem::path& directoryPath) const;
std::unique_ptr<image::IEncodedImage> getFromDirectory(const std::filesystem::path& directory, image::ImageSize width, const std::vector<std::string>& preferredFileNames, bool allowPickRandom) const;
std::unique_ptr<image::IEncodedImage> getFromSameNamedFile(const std::filesystem::path& filePath, image::ImageSize width) const;
bool checkCoverFile(const std::filesystem::path& filePath) const;
bool checkImageFile(const std::filesystem::path& filePath) const;
db::Db& _db;
@@ -74,8 +70,6 @@ namespace lms::cover
std::shared_ptr<image::IEncodedImage> _defaultCover;
static inline const std::vector<std::filesystem::path> _fileExtensions{ ".jpg", ".jpeg", ".png", ".bmp" }; // TODO parametrize
const std::size_t _maxFileSize;
const std::vector<std::string> _preferredFileNames;
unsigned _jpegQuality;
};
@@ -39,10 +39,14 @@ namespace lms::cover
public:
virtual ~ICoverService() = default;
virtual std::shared_ptr<image::IEncodedImage> getFromTrack(db::TrackId trackId, image::ImageSize width) = 0;
virtual std::shared_ptr<image::IEncodedImage> getFromRelease(db::ReleaseId releaseId, image::ImageSize width) = 0;
virtual std::shared_ptr<image::IEncodedImage> getFromArtist(db::ArtistId artistId, image::ImageSize width) = 0;
// no logic to fallback to release here
virtual std::shared_ptr<image::IEncodedImage> getFromTrack(db::TrackId trackId, image::ImageSize width) = 0;
// no logic to fallback to track here
virtual std::shared_ptr<image::IEncodedImage> getFromRelease(db::ReleaseId releaseId, image::ImageSize width) = 0;
virtual std::shared_ptr<image::IEncodedImage> getDefaultSvgCover() = 0;
virtual void flushCache() = 0;
+1
View File
@@ -4,6 +4,7 @@ add_library(lmsscanner SHARED
impl/ScannerService.cpp
impl/ScannerStats.cpp
impl/ScanStepAssociateArtistImages.cpp
impl/ScanStepAssociateReleaseImages.cpp
impl/ScanStepCheckForDuplicatedFiles.cpp
impl/ScanStepCheckForRemovedFiles.cpp
impl/ScanStepCompact.cpp
@@ -0,0 +1,234 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ScanStepAssociateReleaseImages.hpp"
#include <array>
#include <cassert>
#include <deque>
#include <set>
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/Path.hpp"
#include "database/Db.hpp"
#include "database/Directory.hpp"
#include "database/Image.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "image/Exception.hpp"
#include "image/Image.hpp"
namespace lms::scanner
{
namespace
{
constexpr std::size_t readBatchSize{ 100 };
constexpr std::size_t writeBatchSize{ 10 };
struct ReleaseImageAssociation
{
db::ReleaseId releaseId;
db::ImageId imageId;
};
using ReleaseImageAssociationContainer = std::deque<ReleaseImageAssociation>;
struct SearchImageContext
{
db::Session& session;
db::ReleaseId lastRetrievedReleaseId;
const std::vector<std::string>& releaseFileNames;
};
db::Image::pointer findImageInDirectory(SearchImageContext& searchContext, const std::filesystem::path& directoryPath)
{
db::Image::pointer image;
const db::Directory::pointer directory{ db::Directory::find(searchContext.session, directoryPath) };
if (directory) // may not exist for releases that are split on different media libraries
{
for (std::string_view fileStem : searchContext.releaseFileNames)
{
db::Image::FindParameters params;
params.setDirectory(directory->getId());
params.setFileStem(fileStem);
db::Image::find(searchContext.session, params, [&](const db::Image::pointer foundImg) {
if (!image)
image = foundImg;
});
if (image)
break;
}
}
return image;
}
db::Image::pointer computeBestReleaseImage(SearchImageContext& searchContext, const db::Release::pointer& release)
{
db::Image::pointer image;
const auto mbid{ release->getMBID() };
if (mbid)
{
// Find anywhere, since it is suppoed to be unique!
db::Image::find(searchContext.session, db::Image::FindParameters{}.setFileStem(mbid->getAsString()), [&](const db::Image::pointer foundImg) {
if (!image)
image = foundImg;
});
}
if (!image)
{
std::set<std::filesystem::path> releasePaths;
db::Directory::FindParameters params;
params.setRelease(release->getId());
db::Directory::find(searchContext.session, params, [&](const db::Directory::pointer& directory) {
releasePaths.insert(directory->getAbsolutePath());
});
// Expect layout like this:
// Artist/Release/CD1/...
// /CD2/...
// /cover.jpg
if (releasePaths.size() > 1)
{
const std::filesystem::path releasePath{ core::pathUtils::getLongestCommonPath(std::cbegin(releasePaths), std::cend(releasePaths)) };
image = findImageInDirectory(searchContext, releasePath);
}
if (!image)
{
for (const std::filesystem::path& releasePath : releasePaths)
{
image = findImageInDirectory(searchContext, releasePath);
if (image)
break;
}
}
}
return image;
}
bool fetchNextReleaseImagesToUpdate(SearchImageContext& searchContext, ReleaseImageAssociationContainer& releaseImageAssociations)
{
const db::ReleaseId releaseId{ searchContext.lastRetrievedReleaseId };
{
auto transaction{ searchContext.session.createReadTransaction() };
db::Release::find(searchContext.session, searchContext.lastRetrievedReleaseId, readBatchSize, [&](const db::Release::pointer& release) {
db::Image::pointer image{ computeBestReleaseImage(searchContext, release) };
if (image != release->getImage())
{
LMS_LOG(DBUPDATER, DEBUG, "Updating release image for release '" << release->getName() << "', using '" << (image ? image->getAbsoluteFilePath().c_str() : "<none>") << "'");
releaseImageAssociations.push_back(ReleaseImageAssociation{ release->getId(), image ? image->getId() : db::ImageId{} });
}
});
}
return releaseId != searchContext.lastRetrievedReleaseId;
}
void updateReleaseImage(db::Session& session, const ReleaseImageAssociation& releaseImageAssociation)
{
db::Release::pointer release{ db::Release::find(session, releaseImageAssociation.releaseId) };
assert(release);
db::Image::pointer image;
if (releaseImageAssociation.imageId.isValid())
image = db::Image::find(session, releaseImageAssociation.imageId);
release.modify()->setImage(image);
}
void updateReleaseImages(db::Session& session, ReleaseImageAssociationContainer& imageAssociations)
{
if (imageAssociations.empty())
return;
auto transaction{ session.createWriteTransaction() };
for (std::size_t i{}; !imageAssociations.empty() && i < writeBatchSize; ++i)
{
updateReleaseImage(session, imageAssociations.front());
imageAssociations.pop_front();
}
}
std::vector<std::string> constructReleaseFileNames()
{
std::vector<std::string> res;
core::Service<core::IConfig>::get()->visitStrings("cover-preferred-file-names",
[&res](std::string_view fileName) {
res.emplace_back(fileName);
},
{ "cover", "front", "folder", "default" });
return res;
}
} // namespace
ScanStepAssociateReleaseImages::ScanStepAssociateReleaseImages(InitParams& initParams)
: ScanStepBase{ initParams }
, _releaseFileNames{ constructReleaseFileNames() }
{
}
void ScanStepAssociateReleaseImages::process(ScanContext& context)
{
if (_abortScan)
return;
if (context.stats.nbChanges() == 0)
return;
auto& session{ _db.getTLSSession() };
{
auto transaction{ session.createReadTransaction() };
context.currentStepStats.totalElems = db::Release::getCount(session);
}
SearchImageContext searchContext{
.session = session,
.lastRetrievedReleaseId = {},
.releaseFileNames = _releaseFileNames,
};
ReleaseImageAssociationContainer releaseImageAssociations;
while (fetchNextReleaseImagesToUpdate(searchContext, releaseImageAssociations))
{
if (_abortScan)
return;
updateReleaseImages(session, releaseImageAssociations);
context.currentStepStats.processedElems += readBatchSize;
_progressCallback(context.currentStepStats);
}
}
} // namespace lms::scanner
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <vector>
#include "ScanStepBase.hpp"
namespace lms::scanner
{
class ScanStepAssociateReleaseImages : public ScanStepBase
{
public:
ScanStepAssociateReleaseImages(InitParams& initParams);
private:
ScanStep getStep() const override { return ScanStep::AssociateArtistImages; }
core::LiteralString getStepName() const override { return "Associate release images"; }
void process(ScanContext& context) override;
const std::vector<std::string> _releaseFileNames;
};
} // namespace lms::scanner
@@ -32,6 +32,7 @@
#include "image/Image.hpp"
#include "ScanStepAssociateArtistImages.hpp"
#include "ScanStepAssociateReleaseImages.hpp"
#include "ScanStepCheckForDuplicatedFiles.hpp"
#include "ScanStepCheckForRemovedFiles.hpp"
#include "ScanStepCompact.hpp"
@@ -347,6 +348,7 @@ namespace lms::scanner
_scanSteps.push_back(std::make_unique<ScanStepCheckForRemovedFiles>(params));
_scanSteps.push_back(std::make_unique<ScanStepUpdateLibraryFields>(params));
_scanSteps.push_back(std::make_unique<ScanStepAssociateArtistImages>(params));
_scanSteps.push_back(std::make_unique<ScanStepAssociateReleaseImages>(params));
_scanSteps.push_back(std::make_unique<ScanStepRemoveOrphanedDbEntries>(params));
_scanSteps.push_back(std::make_unique<ScanStepCompact>(params));
_scanSteps.push_back(std::make_unique<ScanStepOptimize>(params));
@@ -62,6 +62,7 @@ namespace lms::scanner
enum class ScanStep
{
AssociateArtistImages,
AssociateReleaseImages,
CheckForDuplicatedFiles,
CheckForRemovedFiles,
ComputeClusterStats,