Commit 94ed95e3 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Require a fs::IFile to create a File instance

This prevents us from taking shortcuts with the file creation time, and
will prove itself helpfull when we need even more informations about
file
parent 7f47e4e4
......@@ -13,6 +13,7 @@
#include "ShowEpisode.h"
#include "database/SqliteTools.h"
#include "VideoTrack.h"
#include "filesystem/IFile.h"
const std::string policy::FileTable::Name = "File";
const std::string policy::FileTable::CacheColumn = "mrl";
......@@ -35,28 +36,28 @@ File::File( DBConnection dbConnection, sqlite3_stmt* stmt )
m_isReady = m_type != UnknownType;
}
File::File( const std::string& mrl, unsigned int folderId )
File::File( const fs::IFile* file, unsigned int folderId )
: m_id( 0 )
, m_type( UnknownType )
, m_duration( 0 )
, m_albumTrackId( 0 )
, m_playCount( 0 )
, m_showEpisodeId( 0 )
, m_mrl( mrl )
, m_mrl( file->fullPath() )
, m_movieId( 0 )
, m_folderId( folderId )
, m_lastModificationDate( time( nullptr ) )
, m_lastModificationDate( file->lastModificationDate() )
, m_isReady( false )
{
}
FilePtr File::create( DBConnection dbConnection, const std::string& mrl, unsigned int folderId )
FilePtr File::create( DBConnection dbConnection, const fs::IFile* file, unsigned int folderId )
{
auto self = std::make_shared<File>( mrl, folderId );
auto self = std::make_shared<File>( file, folderId );
static const std::string req = "INSERT INTO " + policy::FileTable::Name +
"(mrl, folder_id, last_modification_date) VALUES(?, ?, ?)";
if ( _Cache::insert( dbConnection, self, req, mrl, sqlite::ForeignKey( folderId ), self->m_lastModificationDate ) == false )
if ( _Cache::insert( dbConnection, self, req, self->m_mrl, sqlite::ForeignKey( folderId ), self->m_lastModificationDate ) == false )
return nullptr;
self->m_dbConnection = dbConnection;
return self;
......
......@@ -40,9 +40,9 @@ class File : public IFile, public Cache<File, IFile, policy::FileTable, policy::
// shall be well-formed, and private constructor would prevent that.
// There might be a way with a user-defined allocator, but we'll see that later...
File(DBConnection dbConnection , sqlite3_stmt* stmt);
File(const std::string& mrl , unsigned int folderId);
File(const fs::IFile* file , unsigned int folderId);
static FilePtr create(DBConnection dbConnection, const std::string& mrl , unsigned int folderId);
static FilePtr create(DBConnection dbConnection, const fs::IFile* file , unsigned int folderId);
static bool createTable( DBConnection connection );
virtual unsigned int id() const;
......
......@@ -103,7 +103,8 @@ FilePtr MediaLibrary::file( const std::string& path )
FilePtr MediaLibrary::addFile( const std::string& path )
{
auto file = File::create( m_dbConnection, path, 0 );
auto fsFile = m_fsFactory->createFile( path );
auto file = File::create( m_dbConnection, fsFile.get(), 0 );
if ( file == nullptr )
return nullptr;
return file;
......@@ -142,7 +143,8 @@ FolderPtr MediaLibrary::addFolder( const std::string& path )
for ( auto& f : dir->files() )
{
addFile( f, folder->id() );
auto fsFile = m_fsFactory->createFile( f );
addFile( fsFile.get(), folder->id() );
}
for ( auto& f : dir->dirs() )
folders.emplace( f, folder->id() );
......@@ -238,6 +240,7 @@ void MediaLibrary::parse(FilePtr file , IParserCb* cb)
bool MediaLibrary::loadFolders()
{
//FIXME: This should probably be in a sql transaction
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE id_parent IS NULL";
auto rootFolders = sqlite::Tools::fetchAll<Folder, IFolder>( m_dbConnection, req );
......@@ -322,15 +325,15 @@ void MediaLibrary::checkFiles( fs::IDirectory* folder, unsigned int parentId )
auto files = sqlite::Tools::fetchAll<File, IFile>( m_dbConnection, req, parentId );
for ( const auto& filePath : folder->files() )
{
auto it = std::find_if( begin( files ), end( files ), [filePath](const std::shared_ptr<IFile>& file) {
return file->mrl() == filePath;
auto file = m_fsFactory->createFile( filePath );
auto it = std::find_if( begin( files ), end( files ), [filePath](const std::shared_ptr<IFile>& f) {
return f->mrl() == filePath;
});
if ( it == end( files ) )
{
addFile( filePath, parentId );
addFile( file.get(), parentId );
continue;
}
auto file = m_fsFactory->createFile( filePath );
if ( file->lastModificationDate() == (*it)->lastModificationDate() )
{
// Unchanged file
......@@ -346,14 +349,14 @@ void MediaLibrary::checkFiles( fs::IDirectory* folder, unsigned int parentId )
}
}
bool MediaLibrary::addFile( const std::string& filePath, unsigned int folderId )
bool MediaLibrary::addFile( const fs::IFile* file, unsigned int folderId )
{
if ( std::find( begin( supportedExtensions ), end( supportedExtensions ),
utils::file::extension( filePath ) ) == end( supportedExtensions ) )
file->extension() ) == end( supportedExtensions ) )
return false;
if ( File::create( m_dbConnection, filePath, folderId ) == nullptr )
if ( File::create( m_dbConnection, file, folderId ) == nullptr )
{
std::cerr << "Failed to add file " << filePath << " to the media library" << std::endl;
std::cerr << "Failed to add file " << file->fullPath() << " to the media library" << std::endl;
return false;
}
return true;
......
......@@ -47,7 +47,7 @@ class MediaLibrary : public IMediaLibrary
bool loadFolders();
bool checkSubfolders( fs::IDirectory* folder, unsigned int parentId );
void checkFiles( fs::IDirectory* folder, unsigned int parentId );
bool addFile( const std::string& filePath, unsigned int folderId );
bool addFile( const fs::IFile* file, unsigned int folderId );
private:
std::shared_ptr<sqlite3> m_dbConnection;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment