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

Media: Store mrl relative to the containing folder

As a result, the Media.Duplicate test is obsolete, since a filename
isn't unique in itself. Only the couple (mrl, folder_id) is UNIQUE.
However, UNIQUE doesn't apply when the folder_id foreign key is NULL
Uniqueness has been manually tested for a real folder/mrl couple
parent c06e8b44
......@@ -70,7 +70,7 @@ Media::Media( const fs::IFile* file, unsigned int folderId, const std::string& t
, m_duration( -1 )
, m_playCount( 0 )
, m_showEpisodeId( 0 )
, m_mrl( file->fullPath() )
, m_mrl( file->name() )
, m_movieId( 0 )
, m_folderId( folderId )
, m_lastModificationDate( file->lastModificationDate() )
......@@ -92,6 +92,7 @@ std::shared_ptr<Media> Media::create( DBConnection dbConnection, Type type, cons
self->m_lastModificationDate, self->m_insertionDate, self->m_title) == false )
return nullptr;
self->m_dbConnection = dbConnection;
self->m_fullPath = file->fullPath();
return self;
}
......@@ -177,7 +178,14 @@ void Media::increasePlayCount()
const std::string& Media::mrl() const
{
return m_mrl;
auto lock = m_fullPath.lock();
if ( m_fullPath.isCached() )
return m_fullPath;
auto folder = Folder::fetch( m_dbConnection, m_folderId );
if ( folder == nullptr )
return m_mrl;
m_fullPath = folder->path() + m_mrl;
return m_fullPath;
}
MoviePtr Media::movie()
......@@ -323,7 +331,7 @@ bool Media::createTable( DBConnection connection )
"duration INTEGER DEFAULT -1,"
"play_count UNSIGNED INTEGER,"
"show_episode_id UNSIGNED INTEGER,"
"mrl TEXT UNIQUE ON CONFLICT FAIL,"
"mrl TEXT,"
"artist TEXT,"
"movie_id UNSIGNED INTEGER,"
"folder_id UNSIGNED INTEGER,"
......@@ -338,7 +346,8 @@ bool Media::createTable( DBConnection connection )
"FOREIGN KEY (movie_id) REFERENCES " + policy::MovieTable::Name
+ "(id_movie) ON DELETE CASCADE,"
"FOREIGN KEY (folder_id) REFERENCES " + policy::FolderTable::Name
+ "(id_folder) ON DELETE CASCADE"
+ "(id_folder) ON DELETE CASCADE,"
"UNIQUE( mrl, folder_id ) ON CONFLICT FAIL"
")";
std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_folder_present AFTER UPDATE OF is_present ON "
+ policy::FolderTable::Name +
......
......@@ -28,7 +28,7 @@
#include "IMedia.h"
#include "database/DatabaseHelpers.h"
#include "utils/Cache.h"
class Album;
class ShowEpisode;
......@@ -125,6 +125,7 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
ShowEpisodePtr m_showEpisode;
MoviePtr m_movie;
bool m_changed;
mutable Cache<std::string> m_fullPath;
friend struct policy::MediaTable;
};
......
......@@ -181,9 +181,22 @@ std::vector<MediaPtr> MediaLibrary::videoFiles()
MediaPtr MediaLibrary::file( const std::string& path )
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
" WHERE mrl = ? AND is_present = 1";
return Media::fetch( m_dbConnection.get(), req, path );
auto folderPath = utils::file::directory( path );
auto folder = Folder::fromPath( m_dbConnection.get(), folderPath );
auto folderId = folder != nullptr ? folder->id() : 0;
if ( folderId != 0 )
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
" WHERE mrl = ? AND folder_id = ? AND is_present = 1";
auto fileName = utils::file::fileName( path );
return Media::fetch( m_dbConnection.get(), req, fileName, folderId );
}
else
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
" WHERE mrl = ? AND folder_id IS NULL AND is_present = 1";
return Media::fetch( m_dbConnection.get(), req, path );
}
}
std::shared_ptr<Media> MediaLibrary::addFile( const std::string& path, Folder* parentFolder )
......
......@@ -83,17 +83,6 @@ TEST_F( Medias, Delete )
ASSERT_EQ( f2, nullptr );
}
TEST_F( Medias, Duplicate )
{
auto f = ml->addFile( "media.avi", nullptr );
ASSERT_NE( f, nullptr );
ASSERT_THROW( ml->addFile( "media.avi", nullptr ), sqlite::errors::ConstraintViolation );
auto f2 = ml->file( "media.avi" );
ASSERT_EQ( f, f2 );
}
TEST_F( Medias, LastModificationDate )
{
auto f = ml->addFile( "media.avi", nullptr );
......
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