Added a scan step to scan artist images, ref #435

This commit is contained in:
emeric
2024-06-29 16:04:16 +02:00
parent 504c0824fb
commit 71dc99ad46
37 changed files with 983 additions and 348 deletions
@@ -0,0 +1,347 @@
/*
* 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 "ScanStepScanArtistImages.hpp"
#include <array>
#include <cassert>
#include <deque>
#include <set>
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/Path.hpp"
#include "database/Artist.hpp"
#include "database/Db.hpp"
#include "database/Image.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{ 10 };
constexpr std::size_t writeBatchSize{ 5 };
struct ImageInfo
{
operator bool() const { return !imagePath.empty(); }
void clear()
{
imagePath.clear();
lastWriteTime = {};
fileSize = {};
height = {};
width = {};
}
std::filesystem::path imagePath;
Wt::WDateTime lastWriteTime;
std::size_t fileSize{};
std::size_t height{};
std::size_t width{};
};
bool tryDecodeImage(const std::filesystem::path& imagePath, ImageInfo& imageInfo)
{
assert(!imageInfo);
try
{
std::unique_ptr<image::IRawImage> rawImage{ image::decodeImage(imagePath) };
imageInfo.imagePath = imagePath;
imageInfo.fileSize = std::filesystem::file_size(imagePath);
imageInfo.width = rawImage->getWidth();
imageInfo.height = rawImage->getHeight();
imageInfo.lastWriteTime = core::pathUtils::getLastWriteTime(imagePath);
}
catch (const image::Exception& e)
{
LMS_LOG(DBUPDATER, ERROR, "Cannot read image in file '" << imagePath.string() << "': " << e.what());
return false;
}
return true;
}
struct ArtistImageInfo
{
db::ArtistId artistId;
ImageInfo imageInfo;
};
using ArtistImageInfoContainer = std::deque<ArtistImageInfo>;
bool isFileSupported(const std::filesystem::path& file)
{
static const std::array<std::filesystem::path, 4> fileExtensions{ ".jpg", ".jpeg", ".png", ".bmp" }; // TODO parametrize
return (std::find(std::cbegin(fileExtensions), std::cend(fileExtensions), file.extension()) != std::cend(fileExtensions));
}
std::multimap<std::string, std::filesystem::path> getImagePaths(const std::filesystem::path& directoryPath, const std::vector<std::string>& fileNames)
{
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 };
const std::string stem{ path.stem().string() };
if (isFileSupported(path)
&& std::any_of(std::cbegin(fileNames), std::cend(fileNames), [&](const std::string& fileName) { return core::stringUtils::stringCaseInsensitiveEqual(stem, fileName); }))
{
res.emplace(stem, path);
}
itPath.increment(ec);
}
return res;
}
bool findImageInDirectory(const std::filesystem::path& directory, const std::vector<std::string>& fileNames, ImageInfo& imageInfo)
{
assert(!imageInfo);
const std::multimap<std::string, std::filesystem::path> coverPaths{ getImagePaths(directory, fileNames) };
for (const std::string_view fileName : fileNames)
{
const auto range{ coverPaths.equal_range(std::string{ fileName }) };
for (auto it{ range.first }; it != range.second; ++it)
{
if (tryDecodeImage(it->second, imageInfo))
return true;
}
}
return false;
}
void fetchArtistImageInfo(db::Session& session, const std::vector<std::string>& genericArtistFileNames, const db::Artist::pointer& artist, ImageInfo& imageInfo)
{
const std::string artistMBID{ [&] {
std::string artistMBID;
if (auto mbid{ artist->getMBID() })
artistMBID = mbid->getAsString();
return artistMBID;
}() };
std::set<std::filesystem::path> releasePaths;
std::set<std::filesystem::path> multiArtistReleasePaths;
db::Track::FindParameters params;
params.setArtist(artist->getId(), { db::TrackArtistLinkType::ReleaseArtist });
db::Track::find(session, params, [&](const db::Track::pointer& track) {
db::Artist::FindParameters artistFindParams;
artistFindParams.setTrack(track->getId());
artistFindParams.setLinkType(db::TrackArtistLinkType::ReleaseArtist);
const auto releaseArtists{ db::Artist::findIds(session, artistFindParams) };
if (releaseArtists.results.size() == 1)
releasePaths.insert(track->getAbsoluteFilePath().parent_path());
else
multiArtistReleasePaths.insert(track->getAbsoluteFilePath().parent_path());
});
std::vector<std::string> artistFileNames;
if (!artistMBID.empty())
artistFileNames.push_back(artistMBID);
artistFileNames.push_back(artist->getName());
std::vector<std::string> artistFileNamesWithGenericNames{ artistFileNames };
artistFileNamesWithGenericNames.insert(artistFileNamesWithGenericNames.end(), std::cbegin(genericArtistFileNames), std::cend(genericArtistFileNames));
// Expect layout like this:
// ReleaseArtist/Release/Tracks'
// /artist-mbid.jpg
// /artist-name.jpg
// /artist.jpg
if (!releasePaths.empty())
{
const std::filesystem::path artistPath{ releasePaths.size() == 1 ? releasePaths.begin()->parent_path() : core::pathUtils::getLongestCommonPath(std::cbegin(releasePaths), std::cend(releasePaths)) };
if (findImageInDirectory(artistPath, artistFileNamesWithGenericNames, imageInfo))
return;
}
// Expect layout like this:
// ReleaseArtist/Release/Tracks'
// /artist-mbid.jpg
// /artist-name.jpg
// /artist.jpg
for (const std::filesystem::path& releasePath : releasePaths)
{
// TODO: what if an artist has released an album that bears their name?
if (findImageInDirectory(releasePath, artistFileNamesWithGenericNames, imageInfo))
return;
}
// Expect layout like this:
// Only search for the artist's name in the release path, as we can't map a generic name to several artists
// ReleaseArtist/Release/Tracks'
// /artist-name.jpg
// /artist-mbid.jpg
for (const std::filesystem::path& releasePath : multiArtistReleasePaths)
{
if (findImageInDirectory(releasePath, artistFileNames, imageInfo))
return;
}
}
bool artistImageNeedsUpdate(const db::Image::pointer& image, const ImageInfo& imageInfo)
{
if (!imageInfo && !image) // no image as before
return false;
else if (!imageInfo && image) // no longer has image
return true;
else if (imageInfo && !image) // image has been added
return true;
assert(imageInfo);
// artist image still here, consider it is the same only if the last modified time is the same
return imageInfo.lastWriteTime != image->getLastWriteTime();
}
struct SearchImageContext
{
db::Session& session;
db::ArtistId lastRetrievedArtistId;
const std::vector<std::string>& artistFileNames;
bool fullScan;
};
bool fetchNextArtistImagesToUpdate(SearchImageContext& searchContext, ArtistImageInfoContainer& artistImageInfoList)
{
const db::ArtistId artistId{ searchContext.lastRetrievedArtistId };
ImageInfo imageInfo;
{
auto transaction{ searchContext.session.createReadTransaction() };
db::Artist::find(searchContext.session, searchContext.lastRetrievedArtistId, readBatchSize, [&](const db::Artist::pointer& artist) {
imageInfo.clear();
fetchArtistImageInfo(searchContext.session, searchContext.artistFileNames, artist, imageInfo);
if (imageInfo)
LMS_LOG(DBUPDATER, DEBUG, "Found artist image for artist '" << artist->getName() << "' at '" << imageInfo.imagePath << "'");
if (searchContext.fullScan || artistImageNeedsUpdate(artist->getImage(), imageInfo))
artistImageInfoList.push_back(ArtistImageInfo{ artist->getId(), imageInfo });
});
}
return artistId != searchContext.lastRetrievedArtistId;
}
void updateArtistImage(db::Session& session, const ArtistImageInfo& artistImageInfo)
{
db::Artist::pointer artist{ db::Artist::find(session, artistImageInfo.artistId) };
assert(artist);
db::Image::pointer image{ artist->getImage() };
const ImageInfo& imageInfo{ artistImageInfo.imageInfo };
if (!imageInfo)
{
if (image)
image.remove();
return;
}
if (!image)
{
image = session.create<db::Image>(imageInfo.imagePath);
image.modify()->setArtist(artist);
}
else
image.modify()->setPath(imageInfo.imagePath);
image.modify()->setLastWriteTime(imageInfo.lastWriteTime);
image.modify()->setFileSize(imageInfo.fileSize);
image.modify()->setHeight(imageInfo.height);
image.modify()->setWidth(imageInfo.width);
}
void updateArtistImages(db::Session& session, ArtistImageInfoContainer& imageInfoList)
{
if (imageInfoList.empty())
return;
auto transaction{ session.createWriteTransaction() };
for (std::size_t i{}; !imageInfoList.empty() && i < writeBatchSize; ++i)
{
updateArtistImage(session, imageInfoList.front());
imageInfoList.pop_front();
}
}
std::vector<std::string> constructArtistFileNames()
{
std::vector<std::string> res;
core::Service<core::IConfig>::get()->visitStrings("artist-image-file-names",
[&res](std::string_view fileName) {
res.emplace_back(fileName);
},
{ "artist" });
return res;
}
} // namespace
ScanStepScanArtistImages::ScanStepScanArtistImages(InitParams& initParams)
: ScanStepBase{ initParams }
, _artistFileNames{ constructArtistFileNames() }
{
}
void ScanStepScanArtistImages::process(ScanContext& context)
{
auto& session{ _db.getTLSSession() };
{
auto transaction{ session.createReadTransaction() };
context.currentStepStats.totalElems = db::Artist::getCount(session);
}
SearchImageContext searchContext{
.session = session,
.lastRetrievedArtistId = {},
.artistFileNames = _artistFileNames,
.fullScan = context.scanOptions.fullScan
};
ArtistImageInfoContainer imageInfoList;
while (fetchNextArtistImagesToUpdate(searchContext, imageInfoList))
{
updateArtistImages(session, imageInfoList);
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 ScanStepScanArtistImages : public ScanStepBase
{
public:
ScanStepScanArtistImages(InitParams& initParams);
private:
ScanStep getStep() const override { return ScanStep::ScanArtistImages; }
core::LiteralString getStepName() const override { return "Scan artist images"; }
void process(ScanContext& context) override;
const std::vector<std::string> _artistFileNames;
};
} // namespace lms::scanner
@@ -17,7 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ScanStepScanFiles.hpp"
#include "ScanStepScanAudioFiles.hpp"
#include "core/Exception.hpp"
#include "core/IConfig.hpp"
@@ -301,14 +301,14 @@ namespace lms::scanner
}
} // namespace
ScanStepScanFiles::MetadataScanQueue::MetadataScanQueue(metadata::IParser& parser, std::size_t threadCount, bool& abort)
ScanStepScanAudioFiles::MetadataScanQueue::MetadataScanQueue(metadata::IParser& parser, std::size_t threadCount, bool& abort)
: _metadataParser{ parser }
, _scanContextRunner{ _scanContext, threadCount, "ScannerMetadata" }
, _abort{ abort }
{
}
void ScanStepScanFiles::MetadataScanQueue::pushScanRequest(const std::filesystem::path& path)
void ScanStepScanAudioFiles::MetadataScanQueue::pushScanRequest(const std::filesystem::path& path)
{
{
std::scoped_lock lock{ _mutex };
@@ -348,13 +348,13 @@ namespace lms::scanner
});
}
std::size_t ScanStepScanFiles::MetadataScanQueue::getResultsCount() const
std::size_t ScanStepScanAudioFiles::MetadataScanQueue::getResultsCount() const
{
std::scoped_lock lock{ _mutex };
return _scanResults.size();
}
size_t ScanStepScanFiles::MetadataScanQueue::popResults(std::vector<MetaDataScanResult>& results, std::size_t maxCount)
size_t ScanStepScanAudioFiles::MetadataScanQueue::popResults(std::vector<MetaDataScanResult>& results, std::size_t maxCount)
{
results.clear();
results.reserve(maxCount);
@@ -372,7 +372,7 @@ namespace lms::scanner
return results.size();
}
void ScanStepScanFiles::MetadataScanQueue::wait(std::size_t maxScanRequestCount)
void ScanStepScanAudioFiles::MetadataScanQueue::wait(std::size_t maxScanRequestCount)
{
LMS_SCOPED_TRACE_OVERVIEW("Scanner", "WaitParseResults");
@@ -380,7 +380,7 @@ namespace lms::scanner
_condVar.wait(lock, [=, this] { return _ongoingScanCount <= maxScanRequestCount; });
}
ScanStepScanFiles::ScanStepScanFiles(InitParams& initParams)
ScanStepScanAudioFiles::ScanStepScanAudioFiles(InitParams& initParams)
: ScanStepBase{ initParams }
, _metadataParser{ metadata::createParser(metadata::ParserBackend::TagLib, getParserReadStyle()) } // For now, always use TagLib
, _metadataScanQueue{ *_metadataParser, getScanMetaDataThreadCount(), _abortScan }
@@ -388,7 +388,7 @@ namespace lms::scanner
LMS_LOG(DBUPDATER, INFO, "Using " << _metadataScanQueue.getThreadCount() << " thread(s) for scanning file metadata");
}
void ScanStepScanFiles::process(ScanContext& context)
void ScanStepScanAudioFiles::process(ScanContext& context)
{
const std::size_t scanQueueMaxScanRequestCount{ 100 * _metadataScanQueue.getThreadCount() };
const std::size_t processMetaDataBatchSize{ 5 };
@@ -446,7 +446,7 @@ namespace lms::scanner
}
}
bool ScanStepScanFiles::checkFileNeedScan(ScanContext& context, const std::filesystem::path& file, const ScannerSettings::MediaLibraryInfo& libraryInfo)
bool ScanStepScanAudioFiles::checkFileNeedScan(ScanContext& context, const std::filesystem::path& file, const ScannerSettings::MediaLibraryInfo& libraryInfo)
{
ScanStats& stats{ context.stats };
@@ -498,7 +498,7 @@ namespace lms::scanner
return true; // need to scan
}
void ScanStepScanFiles::processMetaDataScanResults(ScanContext& context, std::span<const MetaDataScanResult> scanResults, const ScannerSettings::MediaLibraryInfo& libraryInfo)
void ScanStepScanAudioFiles::processMetaDataScanResults(ScanContext& context, std::span<const MetaDataScanResult> scanResults, const ScannerSettings::MediaLibraryInfo& libraryInfo)
{
LMS_SCOPED_TRACE_OVERVIEW("Scanner", "ProcessScanResults");
@@ -525,7 +525,7 @@ namespace lms::scanner
}
}
void ScanStepScanFiles::processFileMetaData(ScanContext& context, const std::filesystem::path& file, const metadata::Track& trackMetadata, const ScannerSettings::MediaLibraryInfo& libraryInfo)
void ScanStepScanAudioFiles::processFileMetaData(ScanContext& context, const std::filesystem::path& file, const metadata::Track& trackMetadata, const ScannerSettings::MediaLibraryInfo& libraryInfo)
{
ScanStats& stats{ context.stats };
@@ -34,14 +34,14 @@
namespace lms::scanner
{
class ScanStepScanFiles : public ScanStepBase
class ScanStepScanAudioFiles : public ScanStepBase
{
public:
ScanStepScanFiles(InitParams& initParams);
ScanStepScanAudioFiles(InitParams& initParams);
private:
ScanStep getStep() const override { return ScanStep::ScanFiles; }
core::LiteralString getStepName() const override { return "Scan files"; }
ScanStep getStep() const override { return ScanStep::ScanAudioFiles; }
core::LiteralString getStepName() const override { return "Scan audio files"; }
void process(ScanContext& context) override;
bool checkFileNeedScan(ScanContext& context, const std::filesystem::path& file, const ScannerSettings::MediaLibraryInfo& libraryInfo);
@@ -36,7 +36,8 @@
#include "ScanStepDiscoverFiles.hpp"
#include "ScanStepOptimize.hpp"
#include "ScanStepRemoveOrphanDbFiles.hpp"
#include "ScanStepScanFiles.hpp"
#include "ScanStepScanArtistImages.hpp"
#include "ScanStepScanAudioFiles.hpp"
namespace lms::scanner
{
@@ -335,11 +336,13 @@ namespace lms::scanner
_abortScan,
_db
};
// Order is important
_scanSteps.clear();
_scanSteps.push_back(std::make_unique<ScanStepDiscoverFiles>(params));
_scanSteps.push_back(std::make_unique<ScanStepScanFiles>(params));
_scanSteps.push_back(std::make_unique<ScanStepScanAudioFiles>(params));
_scanSteps.push_back(std::make_unique<ScanStepRemoveOrphanDbFiles>(params));
_scanSteps.push_back(std::make_unique<ScanStepScanArtistImages>(params));
_scanSteps.push_back(std::make_unique<ScanStepCompact>(params));
_scanSteps.push_back(std::make_unique<ScanStepOptimize>(params));
_scanSteps.push_back(std::make_unique<ScanStepComputeClusterStats>(params));