Associate user provided images with playlists (using EXTIMG or same named file), fixes #833

This commit is contained in:
emeric
2026-06-02 12:48:54 +02:00
parent 531c5ca24d
commit 64e183da38
22 changed files with 340 additions and 7 deletions
@@ -42,6 +42,7 @@
#include "steps/ScanStepAssociateArtistImages.hpp"
#include "steps/ScanStepAssociateExternalLyrics.hpp"
#include "steps/ScanStepAssociateMediumImages.hpp"
#include "steps/ScanStepAssociatePlayListImages.hpp"
#include "steps/ScanStepAssociatePlayListTracks.hpp"
#include "steps/ScanStepAssociateReleaseImages.hpp"
#include "steps/ScanStepAssociateTrackImages.hpp"
@@ -516,6 +517,7 @@ namespace lms::scanner
_scanSteps.emplace_back(std::make_unique<ScanStepCheckForRemovedFiles>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepArtistReconciliation>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepAssociatePlayListTracks>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepAssociatePlayListImages>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepUpdateLibraryFields>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepAssociateReleaseImages>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepAssociateArtistImages>(params)); // must come after ScanStepAssociateReleaseImages (because and artist image can fallback on a release image)
@@ -97,6 +97,7 @@ namespace lms::scanner
else
playList.modify()->setName(getFilePath().stem().string());
playList.modify()->setFiles(_parsedPlayList->files);
playList.modify()->setCoverImageFile(_parsedPlayList->coverImage.value_or(std::filesystem::path{}));
db::MediaLibrary::pointer mediaLibrary{ db::MediaLibrary::find(dbSession, getMediaLibrary().id) }; // may be null if settings are updated in // => next scan will correct this
playList.modify()->setDirectory(utils::getOrCreateDirectory(dbSession, getFilePath().parent_path(), mediaLibrary));
@@ -106,11 +107,9 @@ namespace lms::scanner
LMS_LOG(DBUPDATER, DEBUG, "Added playlist file " << getFilePath());
return OperationResult::Added;
}
else
{
LMS_LOG(DBUPDATER, DEBUG, "Updated playlist file '" << getFilePath());
return OperationResult::Updated;
}
LMS_LOG(DBUPDATER, DEBUG, "Updated playlist file '" << getFilePath());
return OperationResult::Updated;
}
} // namespace
@@ -54,7 +54,7 @@ namespace lms::scanner
else
{
comment.directive = line.substr(0, parameterSeparator + 1);
comment.parameter = line.substr(parameterSeparator + 1);
comment.parameter = core::stringUtils::stringTrim(line.substr(parameterSeparator + 1));
};
return comment;
@@ -87,6 +87,12 @@ namespace lms::scanner
{
if (comment->directive == "#PLAYLIST:")
playlist.name = comment->parameter;
else if (comment->directive == "#EXTIMG:")
{
// skip URLs (contain ':' in the path value)
if (!comment->parameter.empty() && comment->parameter.find(':') == std::string_view::npos)
playlist.coverImage = std::filesystem::path{ comment->parameter }.lexically_normal();
}
continue;
}
@@ -0,0 +1,152 @@
/*
* 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 "ScanStepAssociatePlayListImages.hpp"
#include <deque>
#include "core/ILogger.hpp"
#include "database/IDb.hpp"
#include "database/Session.hpp"
#include "database/objects/Artwork.hpp"
#include "database/objects/Directory.hpp"
#include "database/objects/Image.hpp"
#include "database/objects/PlayListFile.hpp"
#include "ScanContext.hpp"
namespace lms::scanner
{
namespace
{
struct PlayListArtworkAssociation
{
db::PlayListFileId playListFileId;
db::ArtworkId artworkId; // invalid = clear
};
using PlayListArtworkAssociationContainer = std::deque<PlayListArtworkAssociation>;
db::Artwork::pointer computePreferredArtwork(db::Session& session, const db::PlayListFile::pointer& playListFile)
{
// Priority 1: explicit #EXTIMG: hint stored in the playlist file
const std::filesystem::path& coverImageFile{ playListFile->getCoverImageFile() };
if (!coverImageFile.empty())
{
const std::filesystem::path coverImagePath{ coverImageFile.is_relative()
? (playListFile->getDirectory()->getAbsolutePath() / coverImageFile).lexically_normal()
: coverImageFile };
const db::Image::pointer image{ db::Image::find(session, coverImagePath) };
if (image)
return db::Artwork::find(session, image->getId());
}
// Priority 2: image file with the same stem as the playlist in the same directory
db::Artwork::pointer artwork;
db::Image::FindParameters params;
params.setDirectory(playListFile->getDirectoryId());
params.setFileStem(playListFile->getAbsoluteFilePath().stem().string());
db::Image::find(session, params, [&](const db::Image::pointer& image) {
if (!artwork)
artwork = db::Artwork::find(session, image->getId());
});
return artwork;
}
void updatePlayListPreferredArtworks(db::Session& session, PlayListArtworkAssociationContainer& associations, bool forceFullBatch)
{
constexpr std::size_t writeBatchSize{ 50 };
while ((forceFullBatch && associations.size() >= writeBatchSize) || (!forceFullBatch && !associations.empty()))
{
auto transaction{ session.createWriteTransaction() };
for (std::size_t i{}; !associations.empty() && i < writeBatchSize; ++i)
{
const auto& assoc{ associations.front() };
db::PlayListFile::updatePreferredArtwork(session, assoc.playListFileId, assoc.artworkId);
associations.pop_front();
}
}
}
} // namespace
bool ScanStepAssociatePlayListImages::needProcess(const ScanContext& context) const
{
return context.stats.getChangesCount() > 0;
}
void ScanStepAssociatePlayListImages::process(ScanContext& context)
{
auto& session{ _db.getTLSSession() };
{
auto transaction{ session.createReadTransaction() };
context.currentStepStats.totalElems = db::PlayListFile::getCount(session);
}
PlayListArtworkAssociationContainer associations;
db::PlayListFileId lastRetrievedId;
db::IdRange<db::PlayListFileId> idRange;
while (true)
{
{
auto transaction{ session.createReadTransaction() };
idRange = db::PlayListFile::findNextIdRange(session, lastRetrievedId, 100);
lastRetrievedId = idRange.last;
}
if (!idRange.isValid())
break;
{
auto transaction{ session.createReadTransaction() };
db::PlayListFile::find(session, idRange, [&](const db::PlayListFile::pointer& playListFile) {
const db::Artwork::pointer preferredArtwork{ computePreferredArtwork(session, playListFile) };
const db::ArtworkId newId{ preferredArtwork ? preferredArtwork->getId() : db::ArtworkId{} };
if (newId != playListFile->getPreferredArtworkId())
{
associations.push_back({ playListFile->getId(), newId });
if (preferredArtwork)
LMS_LOG(DBUPDATER, DEBUG, "Updating preferred artwork for playlist '" << playListFile->getName() << "' with image " << preferredArtwork->getAbsoluteFilePath());
else
LMS_LOG(DBUPDATER, DEBUG, "Removing preferred artwork from playlist '" << playListFile->getName() << "'");
}
context.currentStepStats.processedElems++;
});
}
updatePlayListPreferredArtworks(session, associations, true);
_progressCallback(context.currentStepStats);
if (_abortScan)
return;
}
updatePlayListPreferredArtworks(session, associations, false);
}
} // namespace lms::scanner
@@ -0,0 +1,37 @@
/*
* 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 "ScanStepBase.hpp"
namespace lms::scanner
{
class ScanStepAssociatePlayListImages : public ScanStepBase
{
public:
using ScanStepBase::ScanStepBase;
private:
ScanStep getStep() const override { return ScanStep::AssociatePlayListImages; }
core::LiteralString getStepName() const override { return "Associate playlist images"; }
bool needProcess(const ScanContext& context) const override;
void process(ScanContext& context) override;
};
} // namespace lms::scanner
@@ -20,6 +20,7 @@
#pragma once
#include <filesystem>
#include <optional>
#include <string>
#include <vector>
@@ -28,6 +29,7 @@ namespace lms::scanner
struct PlayList
{
std::string name;
std::optional<std::filesystem::path> coverImage;
std::vector<std::filesystem::path> files;
};
} // namespace lms::scanner