From 9f400f6e6c340d651101d819ebeae98ff9adc79b Mon Sep 17 00:00:00 2001 From: emeric Date: Tue, 2 Sep 2014 14:06:34 +0200 Subject: [PATCH] DB, scan only well known extensions --- config/ConfigReader.cpp | 27 +++++++++-- database-updater/DatabaseUpdater.cpp | 69 ++++++++++++++++++++-------- database-updater/DatabaseUpdater.hpp | 17 +++++-- etc/lms.conf.sample | 8 +++- service/DatabaseUpdateService.cpp | 2 + service/DatabaseUpdateService.hpp | 2 + 6 files changed, 98 insertions(+), 27 deletions(-) diff --git a/config/ConfigReader.cpp b/config/ConfigReader.cpp index 1996a6ad..b8310b5d 100644 --- a/config/ConfigReader.cpp +++ b/config/ConfigReader.cpp @@ -1,5 +1,20 @@ +#include + #include "ConfigReader.hpp" +namespace { + + void splitStrings(const std::string& source, std::vector& res) + { + std::istringstream oss(source); + + std::string str; + while(oss >> str) + res.push_back(str); + } + +} + ConfigReader::ConfigReader(boost::filesystem::path p) { _config.readFile(p.string().c_str()); @@ -29,7 +44,7 @@ ConfigReader::getUserInterfaceConfig(Service::UserInterfaceService::Config& conf config.sslPrivateKeyPath = _config.lookup("ui.ssl-crypto.key"); config.sslTempDhPath = _config.lookup("ui.ssl-crypto.dh"); - config.dbPath = _config.lookup("main.db"); + config.dbPath = _config.lookup("main.database.path"); } void @@ -45,7 +60,7 @@ ConfigReader::getRemoteServerConfig(Service::RemoteServerService::Config& config config.sslPrivateKeyPath = _config.lookup("remote.ssl-crypto.key"); config.sslTempDhPath = _config.lookup("remote.ssl-crypto.dh"); - config.dbPath = _config.lookup("main.db"); + config.dbPath = _config.lookup("main.database.path"); } void @@ -53,6 +68,12 @@ ConfigReader::getDatabaseUpdateConfig(Service::DatabaseUpdateService::Config& co { config.enable = true; - config.dbPath = _config.lookup("main.db"); + config.dbPath = _config.lookup("main.database.path"); + + std::string audioExtensions = _config.lookup("main.database.audio_extensions"); + std::string videoExtensions = _config.lookup("main.database.audio_extensions"); + + splitStrings(audioExtensions, config.audioExtensions); + splitStrings(videoExtensions, config.videoExtensions); } diff --git a/database-updater/DatabaseUpdater.cpp b/database-updater/DatabaseUpdater.cpp index fc141082..b32f118a 100644 --- a/database-updater/DatabaseUpdater.cpp +++ b/database-updater/DatabaseUpdater.cpp @@ -48,29 +48,40 @@ getNextFirstOfMonth(const boost::gregorian::date& current) } +bool +isFileSupported(const boost::filesystem::path& file, const std::vector extensions) +{ + + boost::filesystem::path fileExtension = file.extension(); + + BOOST_FOREACH(const boost::filesystem::path extension, extensions) + { + if (extension == fileExtension) + return true; + } + + return false; } +std::vector +getRootDirectoriesByType(Wt::Dbo::Session& session, Database::MediaDirectory::Type type) +{ + std::vector res; + std::vector rootDirs = Database::MediaDirectory::getByType(session, type); + + BOOST_FOREACH(Database::MediaDirectory::pointer rootDir, rootDirs) + res.push_back(rootDir->getPath()); + + return res; +} + +} // namespace + namespace DatabaseUpdater { using namespace Database; -namespace { - - std::vector - getRootDirectoriesByType(Wt::Dbo::Session& session, Database::MediaDirectory::Type type) - { - std::vector res; - std::vector rootDirs = Database::MediaDirectory::getByType(session, type); - - BOOST_FOREACH(Database::MediaDirectory::pointer rootDir, rootDirs) - res.push_back(rootDir->getPath()); - - return res; - } - -} - Updater::Updater(boost::filesystem::path dbPath, MetaData::Parser& parser) : _running(false), @@ -81,6 +92,20 @@ _metadataParser(parser) _ioService.setThreadCount(1); } +void +Updater::setAudioExtensions(const std::vector& extensions) +{ + BOOST_FOREACH(const std::string& extension, extensions) + _audioExtensions.push_back("." + extension); +} + +void +Updater::setVideoExtensions(const std::vector& extensions) +{ + BOOST_FOREACH(const std::string& extension, extensions) + _videoExtensions.push_back("." + extension); +} + void Updater::start(void) { @@ -405,7 +430,8 @@ Updater::processDirectory(const boost::filesystem::path& rootDirectory, switch( type ) { case Database::MediaDirectory::Audio: - processAudioFile( *itPath, stats ); + if (isFileSupported(*itPath, _audioExtensions)) + processAudioFile( *itPath, stats ); break; @@ -420,7 +446,7 @@ Updater::processDirectory(const boost::filesystem::path& rootDirectory, } bool -Updater::checkFile(const boost::filesystem::path& p, const std::vector& rootDirs) +Updater::checkFile(const boost::filesystem::path& p, const std::vector& rootDirs, const std::vector& extensions) { bool status = true; @@ -449,6 +475,11 @@ Updater::checkFile(const boost::filesystem::path& p, const std::vectorgetPath(), rootDirs)) + if (!checkFile(track->getPath(), rootDirs, _audioExtensions)) { track.remove(); stats.nbRemoved++; diff --git a/database-updater/DatabaseUpdater.hpp b/database-updater/DatabaseUpdater.hpp index 4940e425..1708efdf 100644 --- a/database-updater/DatabaseUpdater.hpp +++ b/database-updater/DatabaseUpdater.hpp @@ -1,5 +1,5 @@ -#ifndef DB_UPDATER_UPDATER_HPP -#define DB_UPDATER_UPDATER_HPP +#ifndef DB_UPDATER_HPP +#define DB_UPDATER_HPP #include #include @@ -18,10 +18,12 @@ class Updater public: Updater(boost::filesystem::path db, MetaData::Parser& parser); + void setAudioExtensions(const std::vector& extensions); + void setVideoExtensions(const std::vector& extensions); + void start(); void stop(); - private: struct Stats @@ -44,7 +46,10 @@ class Updater void process(boost::system::error_code ec); // Check if a file exists and is still in a root directory - static bool checkFile(const boost::filesystem::path& p, const std::vector& rootDirectories); + static bool checkFile(const boost::filesystem::path& p, + const std::vector& rootDirectories, + const std::vector& extensions); + // Video @@ -68,8 +73,12 @@ class Updater Database::Handler _db; + std::vector _audioExtensions; + std::vector _videoExtensions; + MetaData::Parser& _metadataParser; + }; // class Updater } // DatabaseUpdater diff --git a/etc/lms.conf.sample b/etc/lms.conf.sample index 7308214e..2ca863b0 100644 --- a/etc/lms.conf.sample +++ b/etc/lms.conf.sample @@ -7,9 +7,15 @@ main = { level = 7; } - db = "/var/lms/lms.db"; + database = { + path = "/var/lms/lms.db"; + + audio_extensions = "mp3 ogg oga aac m4a flac wav wma aif aiff ape mpc shn"; + video_extensions = "flv avi mpg mpeg mp4 m4v mkv mov wmv ogv divx m2ts" + } } + ui = { enable = true; diff --git a/service/DatabaseUpdateService.cpp b/service/DatabaseUpdateService.cpp index 9956610e..7386d6ea 100644 --- a/service/DatabaseUpdateService.cpp +++ b/service/DatabaseUpdateService.cpp @@ -10,6 +10,8 @@ DatabaseUpdateService::DatabaseUpdateService(const Config& config) : _metadataParser(), _databaseUpdater( config.dbPath, _metadataParser) { + _databaseUpdater.setAudioExtensions(config.audioExtensions); + _databaseUpdater.setVideoExtensions(config.videoExtensions); } void diff --git a/service/DatabaseUpdateService.hpp b/service/DatabaseUpdateService.hpp index 1490ae57..bb478543 100644 --- a/service/DatabaseUpdateService.hpp +++ b/service/DatabaseUpdateService.hpp @@ -20,6 +20,8 @@ class DatabaseUpdateService : public Service struct Config { bool enable; boost::filesystem::path dbPath; + std::vector audioExtensions; + std::vector videoExtensions; }; DatabaseUpdateService(const Config& config);