More details for scan error reports #676
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include "ArtistInfoFileScanner.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <system_error>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
#include "core/String.hpp"
|
||||
@@ -29,45 +30,33 @@
|
||||
#include "database/MediaLibrary.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "metadata/ArtistInfo.hpp"
|
||||
#include "metadata/Types.hpp"
|
||||
#include "services/scanner/ScanErrors.hpp"
|
||||
|
||||
#include "IFileScanOperation.hpp"
|
||||
#include "ScanContext.hpp"
|
||||
#include "FileScanOperationBase.hpp"
|
||||
#include "ScannerSettings.hpp"
|
||||
#include "Utils.hpp"
|
||||
#include "helpers/ArtistHelpers.hpp"
|
||||
#include "metadata/Types.hpp"
|
||||
|
||||
namespace lms::scanner
|
||||
{
|
||||
namespace
|
||||
{
|
||||
class ArtistInfoFileScanOperation : public IFileScanOperation
|
||||
class ArtistInfoFileScanOperation : public FileScanOperationBase
|
||||
{
|
||||
public:
|
||||
ArtistInfoFileScanOperation(const FileToScan& file, const ScannerSettings& settings, db::Db& db)
|
||||
: _file{ file.file }
|
||||
, _mediaLibrary{ file.mediaLibrary }
|
||||
, _settings{ settings }
|
||||
, _db{ db }
|
||||
{
|
||||
}
|
||||
using FileScanOperationBase::FileScanOperationBase;
|
||||
~ArtistInfoFileScanOperation() override = default;
|
||||
ArtistInfoFileScanOperation(const ArtistInfoFileScanOperation&) = delete;
|
||||
ArtistInfoFileScanOperation& operator=(const ArtistInfoFileScanOperation&) = delete;
|
||||
|
||||
private:
|
||||
const std::filesystem::path& getFile() const override { return _file; };
|
||||
core::LiteralString getName() const override { return "ScanArtistInfoFile"; }
|
||||
void scan() override;
|
||||
void processResult(ScanContext& context) override;
|
||||
OperationResult processResult() override;
|
||||
|
||||
std::string getArtistNameFromArtistInfoFilePath();
|
||||
|
||||
const std::filesystem::path _file;
|
||||
const MediaLibraryInfo _mediaLibrary;
|
||||
const ScannerSettings& _settings;
|
||||
db::Db& _db;
|
||||
|
||||
std::optional<metadata::ArtistInfo> _parsedArtistInfo;
|
||||
};
|
||||
|
||||
@@ -75,91 +64,82 @@ namespace lms::scanner
|
||||
{
|
||||
try
|
||||
{
|
||||
std::ifstream ifs{ _file };
|
||||
std::ifstream ifs{ getFilePath() };
|
||||
if (!ifs)
|
||||
{
|
||||
LMS_LOG(DBUPDATER, ERROR, "Cannot open file " << _file);
|
||||
const std::error_code ec{ errno, std::generic_category() };
|
||||
addError<IOScanError>(getFilePath(), ec);
|
||||
return;
|
||||
}
|
||||
|
||||
_parsedArtistInfo = metadata::parseArtistInfo(ifs);
|
||||
if (_parsedArtistInfo->name.empty())
|
||||
{
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Discarding artist info in file " << _file << ": no name set");
|
||||
addError<MissingArtistNameError>(getFilePath());
|
||||
_parsedArtistInfo.reset();
|
||||
}
|
||||
}
|
||||
catch (const metadata::ArtistInfoParseException& e)
|
||||
{
|
||||
LMS_LOG(DBUPDATER, ERROR, "Cannot read artist info in file " << _file << ": " << e.what());
|
||||
addError<ArtistInfoFileScanError>(getFilePath());
|
||||
}
|
||||
}
|
||||
|
||||
void ArtistInfoFileScanOperation::processResult(ScanContext& context)
|
||||
ArtistInfoFileScanOperation::OperationResult ArtistInfoFileScanOperation::processResult()
|
||||
{
|
||||
ScanStats& stats{ context.stats };
|
||||
|
||||
const std::optional<FileInfo> fileInfo{ utils::retrieveFileInfo(_file, _mediaLibrary.rootDirectory) };
|
||||
if (!fileInfo)
|
||||
{
|
||||
stats.skips++;
|
||||
return;
|
||||
}
|
||||
|
||||
db::Session& dbSession{ _db.getTLSSession() };
|
||||
db::ArtistInfo::pointer artistInfo{ db::ArtistInfo::find(dbSession, _file) };
|
||||
db::Session& dbSession{ getDb().getTLSSession() };
|
||||
db::ArtistInfo::pointer artistInfo{ db::ArtistInfo::find(dbSession, getFilePath()) };
|
||||
if (!_parsedArtistInfo)
|
||||
{
|
||||
if (artistInfo)
|
||||
{
|
||||
artistInfo.remove();
|
||||
stats.deletions++;
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Removed artist info file " << _file);
|
||||
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Removed artist info file " << getFilePath());
|
||||
return OperationResult::Removed;
|
||||
}
|
||||
context.stats.errors.emplace_back(_file, ScanErrorType::CannotReadArtistInfoFile);
|
||||
return;
|
||||
|
||||
return OperationResult::Skipped;
|
||||
}
|
||||
|
||||
const bool added{ !artistInfo };
|
||||
if (!artistInfo)
|
||||
{
|
||||
artistInfo = dbSession.create<db::ArtistInfo>();
|
||||
artistInfo.modify()->setAbsoluteFilePath(_file);
|
||||
artistInfo.modify()->setAbsoluteFilePath(getFilePath());
|
||||
}
|
||||
|
||||
artistInfo.modify()->setScanVersion(_settings.artistInfoScanVersion);
|
||||
artistInfo.modify()->setScanVersion(getScannerSettings().artistInfoScanVersion);
|
||||
artistInfo.modify()->setName(_parsedArtistInfo->name);
|
||||
artistInfo.modify()->setSortName(_parsedArtistInfo->sortName);
|
||||
artistInfo.modify()->setLastWriteTime(fileInfo->lastWriteTime);
|
||||
artistInfo.modify()->setLastWriteTime(getLastWriteTime());
|
||||
artistInfo.modify()->setType(_parsedArtistInfo->type);
|
||||
artistInfo.modify()->setGender(_parsedArtistInfo->gender);
|
||||
artistInfo.modify()->setDisambiguation(_parsedArtistInfo->disambiguation);
|
||||
artistInfo.modify()->setBiography(_parsedArtistInfo->biography);
|
||||
|
||||
db::MediaLibrary::pointer mediaLibrary{ db::MediaLibrary::find(dbSession, _mediaLibrary.id) }; // may be null if settings are updated in // => next scan will correct this
|
||||
artistInfo.modify()->setDirectory(utils::getOrCreateDirectory(dbSession, _file.parent_path(), mediaLibrary));
|
||||
db::MediaLibrary::pointer mediaLibrary{ db::MediaLibrary::find(dbSession, getMediaLibrary().id) }; // may be null if settings are updated in // => next scan will correct this
|
||||
artistInfo.modify()->setDirectory(utils::getOrCreateDirectory(dbSession, getFilePath().parent_path(), mediaLibrary));
|
||||
|
||||
const metadata::Artist artistMetadata{ _parsedArtistInfo->mbid, _parsedArtistInfo->name, _parsedArtistInfo->sortName.empty() ? std::nullopt : std::make_optional<std::string>(_parsedArtistInfo->sortName) };
|
||||
db::Artist::pointer artist{ helpers::getOrCreateArtist(dbSession, artistMetadata, helpers::AllowFallbackOnMBIDEntry{ _settings.allowArtistMBIDFallback }) };
|
||||
db::Artist::pointer artist{ helpers::getOrCreateArtist(dbSession, artistMetadata, helpers::AllowFallbackOnMBIDEntry{ getScannerSettings().allowArtistMBIDFallback }) };
|
||||
artistInfo.modify()->setArtist(artist);
|
||||
artistInfo.modify()->setMBIDMatched(_parsedArtistInfo->mbid.has_value() && _parsedArtistInfo->mbid == artist->getMBID());
|
||||
|
||||
if (added)
|
||||
{
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Added artist info file " << _file);
|
||||
stats.additions++;
|
||||
}
|
||||
else
|
||||
{
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Updated artist info file " << _file);
|
||||
stats.updates++;
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Added artist info file " << getFilePath());
|
||||
return OperationResult::Added;
|
||||
}
|
||||
|
||||
LMS_LOG(DBUPDATER, DEBUG, "Updated artist info file " << getFilePath());
|
||||
return OperationResult::Updated;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ArtistInfoFileScanner::ArtistInfoFileScanner(const ScannerSettings& settings, db::Db& db)
|
||||
: _settings{ settings }
|
||||
, _db{ db }
|
||||
ArtistInfoFileScanner::ArtistInfoFileScanner(db::Db& db, const ScannerSettings& settings)
|
||||
: _db{ db }
|
||||
, _settings{ settings }
|
||||
{
|
||||
}
|
||||
|
||||
@@ -168,45 +148,34 @@ namespace lms::scanner
|
||||
return "Artist info scanner";
|
||||
}
|
||||
|
||||
std::span<const std::filesystem::path> ArtistInfoFileScanner::getSupportedExtensions() const
|
||||
std::span<const std::filesystem::path> ArtistInfoFileScanner::getSupportedFiles() const
|
||||
{
|
||||
return metadata::getSupportedInfoFileExtensions();
|
||||
return metadata::getSupportedArtistInfoFiles();
|
||||
}
|
||||
|
||||
bool ArtistInfoFileScanner::needsScan(ScanContext& context, const FileToScan& file) const
|
||||
std::span<const std::filesystem::path> ArtistInfoFileScanner::getSupportedExtensions() const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
bool ArtistInfoFileScanner::needsScan(const FileToScan& file) const
|
||||
{
|
||||
// Special case: only files named "artist.nfo" are compatible with this scanner
|
||||
// Hack here since the scanner framework only handle extensions (the discover count is not accurate)
|
||||
if (!core::stringUtils::stringCaseInsensitiveEqual(file.file.stem().string(), "artist"))
|
||||
if (!core::stringUtils::stringCaseInsensitiveEqual(file.filePath.stem().string(), "artist"))
|
||||
return false;
|
||||
|
||||
const Wt::WDateTime lastWriteTime{ utils::retrieveFileGetLastWrite(file.file) };
|
||||
// Should rarely fail as we are currently iterating it
|
||||
if (!lastWriteTime.isValid())
|
||||
{
|
||||
context.stats.skips++;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (context.scanOptions.fullScan)
|
||||
return true;
|
||||
|
||||
db::Session& dbSession{ _db.getTLSSession() };
|
||||
auto transaction{ dbSession.createReadTransaction() };
|
||||
db::ArtistInfo::pointer artistInfo{ db::ArtistInfo::find(dbSession, file.file) };
|
||||
if (artistInfo
|
||||
&& artistInfo->getLastWriteTime() == lastWriteTime
|
||||
&& artistInfo->getScanVersion() == _settings.artistInfoScanVersion)
|
||||
{
|
||||
context.stats.skips++;
|
||||
return false;
|
||||
}
|
||||
db::ArtistInfo::pointer artistInfo{ db::ArtistInfo::find(dbSession, file.filePath) };
|
||||
|
||||
return true;
|
||||
return !artistInfo
|
||||
|| artistInfo->getLastWriteTime() != file.lastWriteTime
|
||||
|| artistInfo->getScanVersion() != _settings.artistInfoScanVersion;
|
||||
}
|
||||
|
||||
std::unique_ptr<IFileScanOperation> ArtistInfoFileScanner::createScanOperation(const FileToScan& fileToScan) const
|
||||
std::unique_ptr<IFileScanOperation> ArtistInfoFileScanner::createScanOperation(FileToScan&& fileToScan) const
|
||||
{
|
||||
return std::make_unique<ArtistInfoFileScanOperation>(fileToScan, _settings, _db);
|
||||
return std::make_unique<ArtistInfoFileScanOperation>(std::move(fileToScan), _db, _settings);
|
||||
}
|
||||
} // namespace lms::scanner
|
||||
Reference in New Issue
Block a user