Commit c915e04e authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

MediaLibrary: Watch for new/deleted files in directory

parent 53e2d902
......@@ -142,11 +142,7 @@ FolderPtr MediaLibrary::addFolder( const std::string& path )
for ( auto& f : dir->files() )
{
if ( std::find( begin( supportedExtensions ), end( supportedExtensions ),
utils::file::extension( f ) ) == end( supportedExtensions ) )
continue;
if ( File::create( m_dbConnection, f, folder->id() ) == nullptr )
std::cerr << "Failed to add file " << f << " to the media library" << std::endl;
addFile( f, folder->id() );
}
for ( auto& f : dir->dirs() )
folders.emplace( f, folder->id() );
......@@ -305,6 +301,7 @@ bool MediaLibrary::checkSubfolders( fs::IDirectory* folder, unsigned int parentI
std::cout << "Changes detected, checking its children" << std::endl;
// This folder was modified, let's recurse
checkSubfolders( subFolder.get(), (*it)->id() );
checkFiles( subFolder.get(), (*it)->id() );
subFoldersInDB.erase( it );
}
// Now all folders we had in DB but haven't seen from the FS must have been deleted.
......@@ -316,3 +313,47 @@ bool MediaLibrary::checkSubfolders( fs::IDirectory* folder, unsigned int parentI
return true;
}
void MediaLibrary::checkFiles( fs::IDirectory* folder, unsigned int parentId )
{
static const std::string req = "SELECT * FROM " + policy::FileTable::Name
+ " WHERE folder_id = ?";
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;
});
if ( it == end( files ) )
{
addFile( filePath, parentId );
continue;
}
auto file = m_fsFactory->createFile( filePath );
if ( file->lastModificationDate() == (*it)->lastModificationDate() )
{
// Unchanged file
files.erase( it );
continue;
}
//FIXME: What should we do when a file is modified?! Delete & re-add?
files.erase( it );
}
for ( auto file : files )
{
deleteFile( file );
}
}
bool MediaLibrary::addFile( const std::string& filePath, unsigned int folderId )
{
if ( std::find( begin( supportedExtensions ), end( supportedExtensions ),
utils::file::extension( filePath ) ) == end( supportedExtensions ) )
return false;
if ( File::create( m_dbConnection, filePath, folderId ) == nullptr )
{
std::cerr << "Failed to add file " << filePath << " to the media library" << std::endl;
return false;
}
return true;
}
......@@ -46,6 +46,8 @@ class MediaLibrary : public IMediaLibrary
private:
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 );
private:
std::shared_ptr<sqlite3> m_dbConnection;
......
......@@ -336,7 +336,7 @@ TEST_F( Folders, LastModificationDate )
ASSERT_NE( 0u, subFolders[0]->lastModificationDate() );
}
TEST_F( Folders, NewFile )
TEST_F( Folders, NewFolderWithFile )
{
ml->addFolder( "." );
......@@ -353,3 +353,22 @@ TEST_F( Folders, NewFile )
auto file = ml->file( newFolder + "newfile.avi" );
ASSERT_NE( nullptr, file );
}
TEST_F( Folders, NewFileInSubFolder )
{
ml->addFolder( "." );
ASSERT_EQ( 3u, ml->files().size() );
// Do not watch for live changes
ml.reset();
fsMock->addFile( mock::FileSystemFactory::SubFolder, "newfile.avi" );
Reload();
ASSERT_EQ( 4u, ml->files().size() );
auto file = ml->file( std::string( mock::FileSystemFactory::SubFolder ) + "newfile.avi" );
auto f = ml->folder( mock::FileSystemFactory::SubFolder );
ASSERT_EQ( 2u, f->files().size() );
ASSERT_NE( nullptr, file );
ASSERT_FALSE( file->isStandAlone() );
}
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