diff --git a/src/av/AvInfo.cpp b/src/av/AvInfo.cpp index 68390e30..ba320c0e 100644 --- a/src/av/AvInfo.cpp +++ b/src/av/AvInfo.cpp @@ -79,7 +79,7 @@ MediaFile::open(void) int error = avformat_open_input(&_context, _p.string().c_str(), nullptr, nullptr); if (error < 0) { - LMS_LOG(AV, ERROR) << "Cannot open '" << _p.string() << "', avformat_open_input returned " << averror_to_string(error); + LMS_LOG(AV, ERROR) << "Cannot open '" << _p.string() << "': " << averror_to_string(error); return false; } @@ -95,7 +95,7 @@ MediaFile::scan(void) int error = avformat_find_stream_info(_context, nullptr); if (error < 0) { - LMS_LOG(AV, ERROR) << "Cannot find stream information: '" << averror_to_string(error); + LMS_LOG(AV, ERROR) << "Cannot find stream information on '" << _p.string() << "': " << averror_to_string(error); return false; } diff --git a/src/database-updater/DatabaseUpdater.cpp b/src/database-updater/DatabaseUpdater.cpp index f6746ca4..c747713d 100644 --- a/src/database-updater/DatabaseUpdater.cpp +++ b/src/database-updater/DatabaseUpdater.cpp @@ -84,6 +84,8 @@ isFileSupported(const boost::filesystem::path& file, const std::vector getRootDirectoriesByType(Wt::Dbo::Session& session, Database::MediaDirectory::Type type) { + Wt::Dbo::Transaction transaction(session); + std::vector res; std::vector rootDirs = Database::MediaDirectory::getByType(session, type); @@ -246,6 +248,9 @@ Updater::process(boost::system::error_code err) for (RootDirectory rootDirectory : rootDirectories) { + if (!_running) + break; + LMS_LOG(DBUPDATER, INFO) << "Processing root directory '" << rootDirectory.path << "'..."; processRootDirectory(rootDirectory, stats); LMS_LOG(DBUPDATER, INFO) << "Processing root directory '" << rootDirectory.path << "' DONE"; @@ -376,53 +381,59 @@ Updater::getGenres( const std::list& names) void Updater::processAudioFile( const boost::filesystem::path& file, Stats& stats) { - try { - // Check last update time - boost::posix_time::ptime lastWriteTime (boost::posix_time::from_time_t( boost::filesystem::last_write_time( file ) ) ); + boost::posix_time::ptime lastWriteTime (boost::posix_time::from_time_t( boost::filesystem::last_write_time( file ) ) ); + // Skip file if last write is the same + { Wt::Dbo::Transaction transaction(_db.getSession()); Wt::Dbo::ptr track = Track::getByPath(_db.getSession(), file); - // Skip file if last write is the same if (track && track->getLastWriteTime() == lastWriteTime) return; + } - MetaData::Items items; - if (!_metadataParser.parse(file, items)) - return; + MetaData::Items items; + if (!_metadataParser.parse(file, items)) + return; - // We estimate this is a audio file if: - // - we found a least one audio stream - // - the duration is not null - if (items.find(MetaData::Type::AudioStreams) == items.end() - || boost::any_cast >(items[MetaData::Type::AudioStreams]).empty()) + Wt::Dbo::Transaction transaction(_db.getSession()); + + Wt::Dbo::ptr track = Track::getByPath(_db.getSession(), file); + + // We estimate this is a audio file if: + // - we found a least one audio stream + // - the duration is not null + if (items.find(MetaData::Type::AudioStreams) == items.end() + || boost::any_cast >(items[MetaData::Type::AudioStreams]).empty()) + { + LMS_LOG(DBUPDATER, DEBUG) << "Skipped '" << file << "' (no audio stream found)"; + + // If Track exists here, delete it! + if (track) { - LMS_LOG(DBUPDATER, DEBUG) << "Skipped '" << file << "' (no audio stream found)"; - - // If Track exists here, delete it! - if (track) { - track.remove(); - stats.nbRemoved++; - } - return; + track.remove(); + stats.nbRemoved++; } - if (items.find(MetaData::Type::Duration) == items.end() - || boost::any_cast(items[MetaData::Type::Duration]).total_seconds() <= 0) + return; + } + if (items.find(MetaData::Type::Duration) == items.end() + || boost::any_cast(items[MetaData::Type::Duration]).total_seconds() <= 0) + { + LMS_LOG(DBUPDATER, DEBUG) << "Skipped '" << file << "' (no duration or duration <= 0)"; + + // If Track exists here, delete it! + if (track) { - LMS_LOG(DBUPDATER, DEBUG) << "Skipped '" << file << "' (no duration or duration <= 0)"; - - // If Track exists here, delete it! - if (track) { - track.remove(); - stats.nbRemoved++; - } - return; + track.remove(); + stats.nbRemoved++; } + return; + } - // ***** Title - std::string title; - if (items.find(MetaData::Type::Title) != items.end()) { + // ***** Title + std::string title; + if (items.find(MetaData::Type::Title) != items.end()) { title = boost::any_cast(items[MetaData::Type::Title]); } else @@ -541,20 +552,12 @@ Updater::processAudioFile( const boost::filesystem::path& file, Stats& stats) } transaction.commit(); - } - catch( std::exception& e ) - { - LMS_LOG(DBUPDATER, ERROR) << "Exception while parsing audio file : '" << file << "': '" << e.what() << "' => skipping!"; - stats.nbRemoved++; - } } void Updater::processRootDirectory(RootDirectory rootDirectory, Stats& stats) { - if (!_running) - return; if (!boost::filesystem::exists(rootDirectory.path) || !boost::filesystem::is_directory(rootDirectory.path)) return; @@ -641,49 +644,68 @@ Updater::checkFile(const boost::filesystem::path& p, const std::vector trackPaths = Track::getAllPaths(_db.getSession());; std::vector rootDirs = getRootDirectoriesByType(_db.getSession(), Database::MediaDirectory::Audio); LMS_LOG(DBUPDATER, DEBUG) << "Checking tracks..."; - auto tracks = Track::getAll(_db.getSession()); - for (auto track : tracks) + for (auto& trackPath : trackPaths) { - if (!checkFile(track->getPath(), rootDirs, _audioExtensions)) + if (!_running) + return; + + if (!checkFile(trackPath, rootDirs, _audioExtensions)) { - track.remove(); - stats.nbRemoved++; + Wt::Dbo::Transaction transaction(_db.getSession()); + + Track::pointer track = Track::getByPath(_db.getSession(), trackPath); + if (track) + { + track.remove(); + stats.nbRemoved++; + } } } - // Now process orphan Genre (no track) LMS_LOG(DBUPDATER, DEBUG) << "Checking Genres..."; - auto genres = Genre::getAll(_db.getSession()); - for (auto genre : genres) { - if (genre->getTracks().size() == 0) + Wt::Dbo::Transaction transaction(_db.getSession()); + + // Now process orphan Genre (no track) + auto genres = Genre::getAll(_db.getSession()); + for (auto genre : genres) { - LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan genre '" << genre->getName() << "'"; - genre.remove(); + if (genre->getTracks().size() == 0) + { + LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan genre '" << genre->getName() << "'"; + genre.remove(); + } } } LMS_LOG(DBUPDATER, DEBUG) << "Checking artists..."; - auto artists = Artist::getAllOrphans(_db.getSession()); - for (auto artist : artists) { - LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan artist '" << artist->getName() << "'"; - artist.remove(); + Wt::Dbo::Transaction transaction(_db.getSession()); + + auto artists = Artist::getAllOrphans(_db.getSession()); + for (auto artist : artists) + { + LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan artist '" << artist->getName() << "'"; + artist.remove(); + } } LMS_LOG(DBUPDATER, DEBUG) << "Checking releases..."; - auto releases = Release::getAllOrphans(_db.getSession()); - for (auto release : releases) { - LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan release '" << release->getName() << "'"; - release.remove(); + Wt::Dbo::Transaction transaction(_db.getSession()); + + auto releases = Release::getAllOrphans(_db.getSession()); + for (auto release : releases) + { + LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan release '" << release->getName() << "'"; + release.remove(); + } } LMS_LOG(DBUPDATER, INFO) << "Check audio files done!"; @@ -692,23 +714,25 @@ Updater::checkAudioFiles( Stats& stats ) void Updater::checkVideoFiles( Stats& stats ) { - LMS_LOG(DBUPDATER, DEBUG) << "Checking video files..."; - Wt::Dbo::Transaction transaction(_db.getSession()); - std::vector rootDirs = getRootDirectoriesByType(_db.getSession(), Database::MediaDirectory::Video); + std::vector videoPaths = Video::getAllPaths(_db.getSession()); LMS_LOG(DBUPDATER, DEBUG) << "Checking videos..."; - typedef Wt::Dbo::collection< Wt::Dbo::ptr