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

Media: Add a folder_id field

ref #42
parent c6d16f55
......@@ -79,6 +79,8 @@ Media::Media( MediaLibraryPtr ml, sqlite::Row& row )
// Skip is_present
// Skip device_id
, m_nbPlaylists( row.load<unsigned int>( 16 ) )
// Skip folder_id if any field gets added afterward
// End of DB fields extraction
, m_metadata( m_ml, IMetadata::EntityType::Media )
, m_changed( false )
......@@ -108,15 +110,17 @@ Media::Media( MediaLibraryPtr ml, const std::string& title, Type type )
}
std::shared_ptr<Media> Media::create( MediaLibraryPtr ml, Type type,
int64_t deviceId, const std::string& fileName )
int64_t deviceId, int64_t folderId,
const std::string& fileName )
{
auto self = std::make_shared<Media>( ml, fileName, type );
static const std::string req = "INSERT INTO " + Media::Table::Name +
"(type, insertion_date, title, filename, device_id) "
"VALUES(?, ?, ?, ?, ?)";
"(type, insertion_date, title, filename, device_id, folder_id) "
"VALUES(?, ?, ?, ?, ?, ?)";
if ( insert( ml, self, req, type, self->m_insertionDate, self->m_title,
self->m_filename, sqlite::ForeignKey{ deviceId } ) == false )
self->m_filename, sqlite::ForeignKey{ deviceId },
sqlite::ForeignKey{ folderId } ) == false )
return nullptr;
return self;
}
......
......@@ -61,7 +61,7 @@ class Media : public IMedia, public DatabaseHelpers<Media>
Media( MediaLibraryPtr ml, const std::string& title, Type type);
static std::shared_ptr<Media> create( MediaLibraryPtr ml, Type type,
int64_t deviceId,
int64_t deviceId, int64_t folderId,
const std::string& fileName );
static void createTable( sqlite::Connection* connection, uint32_t modelVersion );
static void createTriggers( sqlite::Connection* connection, uint32_t modelVersion );
......
......@@ -404,7 +404,8 @@ MediaPtr MediaLibrary::addExternalMedia( const std::string& mrl, IMedia::Type ty
return sqlite::Tools::withRetries( 3, [this, &mrl, type]() -> MediaPtr {
auto t = m_dbConnection->newTransaction();
auto fileName = utils::file::fileName( mrl );
auto media = Media::create( this, type, 0, utils::url::decode( fileName ) );
auto media = Media::create( this, type, 0, 0,
utils::url::decode( fileName ) );
if ( media == nullptr )
return nullptr;
if ( media->addExternalMrl( mrl, IFile::Type::Main ) == nullptr )
......
......@@ -48,10 +48,10 @@
"(SELECT COUNT(media_id) FROM PlaylistMediaRelation WHERE media_id = id_media )"
"WHERE id_media IN (SELECT media_id FROM PlaylistMediaRelation)",
/********************* Populate new media.device_id ***************************/
/*************** Populate new media.device_id & folder_id *********************/
"UPDATE " + Media::Table::Name + " SET device_id = "
"(SELECT d.id_device FROM " + Device::Table::Name + " d "
"UPDATE " + Media::Table::Name + " SET (device_id, folder_id) = "
"(SELECT d.id_device, f.id_folder FROM " + Device::Table::Name + " d "
"INNER JOIN " + Folder::Table::Name + " f ON d.id_device = f.device_id "
"INNER JOIN " + File::Table::Name + " fi ON fi.folder_id = f.id_folder "
"WHERE fi.type = " +
......
......@@ -18,9 +18,12 @@
"is_present BOOLEAN NOT NULL DEFAULT 1,"
"device_id INTEGER,"
"nb_playlists UNSIGNED INTEGER NOT NULL DEFAULT 0,"
"folder_id UNSIGNED INTEGER,"
"FOREIGN KEY(thumbnail_id) REFERENCES " + Thumbnail::Table::Name
+ "(id_thumbnail)"
+ "(id_thumbnail),"
"FOREIGN KEY(folder_id) REFERENCES " + Folder::Table::Name
+ "(id_folder)"
")",
"CREATE INDEX IF NOT EXISTS media_types_idx ON " + Media::Table::Name +
......
......@@ -257,8 +257,8 @@ void MetadataAnalyzer::addPlaylistElement( IItem& item,
if ( fsFactory == nullptr ) // Media not supported by any FsFactory, registering it as external
{
auto t2 = m_ml->getConn()->newTransaction();
auto externalMedia = Media::create( m_ml, IMedia::Type::External,
0, subitem.meta( IItem::Metadata::Title ) );
auto externalMedia = Media::create( m_ml, IMedia::Type::External, 0, 0,
subitem.meta( IItem::Metadata::Title ) );
if ( externalMedia == nullptr )
{
LOG_ERROR( "Failed to create external media for ", mrl, " in the playlist ", playlistMrl );
......@@ -398,7 +398,8 @@ std::tuple<Status, bool> MetadataAnalyzer::createFileAndMedia( IItem& item ) con
LOG_INFO( "Adding ", mrl );
auto folder = static_cast<Folder*>( item.parentFolder().get() );
auto m = Media::create( m_ml, isAudio ? IMedia::Type::Audio : IMedia::Type::Video,
folder->deviceId(), utils::url::decode( utils::file::fileName( mrl ) ) );
folder->deviceId(), folder->id(),
utils::url::decode( utils::file::fileName( mrl ) ) );
if ( m == nullptr )
{
LOG_ERROR( "Failed to add media ", mrl, " to the media library" );
......
......@@ -71,7 +71,7 @@ std::shared_ptr<Media> MediaLibraryTester::addFile( std::shared_ptr<fs::IFile> f
IMedia::Type type )
{
LOG_INFO( "Adding ", fileFs->mrl() );
auto mptr = Media::create( this, type, parentFolder->deviceId(),
auto mptr = Media::create( this, type, parentFolder->deviceId(), parentFolder->id(),
utils::file::stripExtension( fileFs->name() ) );
if ( mptr == 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