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

Media: Ensure external media's MRL uniqueness

parent e9fb1cbb
......@@ -247,6 +247,14 @@ std::shared_ptr<File> File::create( MediaLibraryPtr ml, int64_t mediaId, Type ty
std::shared_ptr<File> File::create( MediaLibraryPtr ml, int64_t mediaId, IFile::Type type, const std::string& mrl )
{
// Sqlite won't ensure uniqueness for (folder_id, mrl) when folder_id is null, so we have to ensure
// of it ourselves
static const std::string existingReq = "SELECT * FROM " + policy::FileTable::Name +
" WHERE folder_id IS NULL AND mrl = ?";
auto existing = fetch( ml, existingReq, mrl );
if ( existing != nullptr )
return nullptr;
auto self = std::make_shared<File>( ml, mediaId, type, mrl );
static const std::string req = "INSERT INTO " + policy::FileTable::Name +
"(media_id, mrl, type, folder_id, is_removable, is_external, is_active) VALUES(?, ?, ?, NULL, 0, 1, ?)";
......
......@@ -302,10 +302,13 @@ MediaPtr MediaLibrary::media( const std::string& mrl ) const
MediaPtr MediaLibrary::addMedia( const std::string& mrl )
{
auto t = m_dbConnection->newTransaction();
auto media = Media::create( this, IMedia::Type::Unknown, utils::file::fileName( mrl ) );
if ( media == nullptr )
return nullptr;
media->addExternalMrl( mrl, IFile::Type::Main );
if ( media->addExternalMrl( mrl, IFile::Type::Main ) == nullptr )
return nullptr;
t->commit();
return media;
}
......
......@@ -511,6 +511,14 @@ TEST_F( Medias, ExternalMrl )
ASSERT_EQ( File::Type::Main, f->type() );
}
TEST_F( Medias, DuplicatedExternalMrl )
{
auto m = ml->addMedia( "http://foo.bar" );
auto m2 = ml->addMedia( "http://foo.bar" );
ASSERT_NE( nullptr, m );
ASSERT_EQ( nullptr, m2 );
}
class FetchMedia : public Tests
{
protected:
......
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