/*
* Copyright (C) 2013 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 .
*/
#include
#include
#include
#include
#include "logger/Logger.hpp"
#include "database/MediaDirectory.hpp"
#include "database/AudioTypes.hpp"
#include "database/VideoTypes.hpp"
#include "Checksum.hpp"
#include "DatabaseUpdater.hpp"
namespace {
boost::gregorian::date
getNextDay(const boost::gregorian::date& current)
{
boost::gregorian::day_iterator it(current);
return *(++it);
}
boost::gregorian::date
getNextMonday(const boost::gregorian::date& current)
{
boost::gregorian::day_iterator it(current);
++it;
// While it's not monday
while( it->day_of_week() != 1 )
++it;
return *(it);
}
boost::gregorian::date
getNextFirstOfMonth(const boost::gregorian::date& current)
{
boost::gregorian::day_iterator it(current);
++it;
// While it's not the 1st of the month
while( it->day() != 1 )
++it;
return (*it);
}
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;
Updater::Updater(boost::filesystem::path dbPath, MetaData::Parser& parser)
: _running(false),
_scheduleTimer(_ioService),
_db(dbPath),
_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)
{
_running = true;
// post some jobs in the io_service
processNextJob();
_ioService.start();
}
void
Updater::stop(void)
{
_running = false;
// TODO cancel all jobs (timer, ...)
_scheduleTimer.cancel();
_ioService.stop();
}
void
Updater::processNextJob(void)
{
Wt::Dbo::Transaction transaction(_db.getSession());
MediaDirectorySettings::pointer settings = MediaDirectorySettings::get(_db.getSession());
if (settings->getManualScanRequested()) {
LMS_LOG(MOD_DBUPDATER, SEV_NOTICE) << "Manual scan requested!";
scheduleScan( boost::posix_time::seconds(0) );
}
else
{
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
boost::posix_time::time_duration startTime = settings->getUpdateStartTime();
boost::gregorian::date nextScanDate;
switch( settings->getUpdatePeriod() )
{
case Database::MediaDirectorySettings::Never:
// Nothing to do
break;
case Database::MediaDirectorySettings::Daily:
if (now.time_of_day() < startTime)
nextScanDate = now.date();
else
nextScanDate = getNextDay(now.date());
break;
case Database::MediaDirectorySettings::Weekly:
if (now.time_of_day() < startTime && now.date().day_of_week() == 1)
nextScanDate = now.date();
else
nextScanDate = getNextMonday(now.date());
break;
case Database::MediaDirectorySettings::Monthly:
if (now.time_of_day() < startTime && now.date().day() == 1)
nextScanDate = now.date();
else
nextScanDate = getNextFirstOfMonth(now.date());
break;
}
if (!nextScanDate.is_special())
scheduleScan( boost::posix_time::ptime (nextScanDate, settings->getUpdateStartTime() ) );
}
}
void
Updater::scheduleScan( boost::posix_time::time_duration duration)
{
LMS_LOG(MOD_DBUPDATER, SEV_NOTICE) << "Scheduling next scan in " << duration;
_scheduleTimer.expires_from_now(duration);
_scheduleTimer.async_wait( boost::bind( &Updater::process, this, boost::asio::placeholders::error) );
}
void
Updater::scheduleScan( boost::posix_time::ptime time)
{
LMS_LOG(MOD_DBUPDATER, SEV_NOTICE) << "Scheduling next scan at " << time;
_scheduleTimer.expires_at(time);
_scheduleTimer.async_wait( boost::bind( &Updater::process, this, boost::asio::placeholders::error) );
}
void
Updater::process(boost::system::error_code err)
{
if (!err)
{
Stats stats;
checkAudioFiles(stats);
checkVideoFiles(stats);
typedef std::pair RootDirectory;
std::vector rootDirectories;
{
Wt::Dbo::Transaction transaction(_db.getSession());
std::vector mediaDirectories = MediaDirectory::getAll(_db.getSession());
BOOST_FOREACH(MediaDirectory::pointer directory, mediaDirectories)
rootDirectories.push_back( std::make_pair( directory->getPath(), directory->getType() ));
}
BOOST_FOREACH( RootDirectory rootDirectory, rootDirectories)
processDirectory(rootDirectory.first, rootDirectory.first, rootDirectory.second, stats);
LMS_LOG(MOD_DBUPDATER, SEV_INFO) << "Changes = " << stats.nbChanges();
// Update database stats
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
{
Wt::Dbo::Transaction transaction(_db.getSession());
Database::MediaDirectorySettings::pointer settings = Database::MediaDirectorySettings::get(_db.getSession());
if (stats.nbChanges() > 0)
settings.modify()->setLastUpdate(now);
// Save the last scan only if it has been completed
if (_running)
settings.modify()->setLastScan(now);
// If the manual scan was required we can now set it to done
// Update only if the scan is complete!
if (settings->getManualScanRequested() && _running)
settings.modify()->setManualScanRequested(false);
}
if (_running)
processNextJob();
}
}
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 ) ) );
Wt::Dbo::Transaction transaction(_db.getSession());
// Skip file if last write is the same
Wt::Dbo::ptr