Commit 08357df3 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen Committed by Hugo Beauzée-Luyssen
Browse files

Media: Specify the duration during the insertion

We already know it, so we won't need a later update. This fixes an
issue where a video media without a title wouldn't get
save()'d, causing the duration to be silently dropped.
parent 3836edc6
Pipeline #7262 passed with stage
in 11 minutes and 9 seconds
......@@ -88,12 +88,13 @@ Media::Media( MediaLibraryPtr ml, sqlite::Row& row )
{
}
Media::Media( MediaLibraryPtr ml, const std::string& title, Type type )
Media::Media( MediaLibraryPtr ml, const std::string& title, Type type,
int64_t duration )
: m_ml( ml )
, m_id( 0 )
, m_type( type )
, m_subType( SubType::Unknown )
, m_duration( -1 )
, m_duration( duration )
, m_playCount( 0 )
, m_lastPlayedDate( 0 )
, m_insertionDate( time( nullptr ) )
......@@ -111,15 +112,16 @@ Media::Media( MediaLibraryPtr ml, const std::string& title, Type type )
std::shared_ptr<Media> Media::create( MediaLibraryPtr ml, Type type,
int64_t deviceId, int64_t folderId,
const std::string& fileName )
const std::string& fileName,
int64_t duration )
{
auto self = std::make_shared<Media>( ml, fileName, type );
auto self = std::make_shared<Media>( ml, fileName, type, duration );
static const std::string req = "INSERT INTO " + Media::Table::Name +
"(type, insertion_date, title, filename, device_id, folder_id) "
"VALUES(?, ?, ?, ?, ?, ?)";
"(type, duration, 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 },
if ( insert( ml, self, req, type, self->m_duration, self->m_insertionDate,
self->m_title, self->m_filename, sqlite::ForeignKey{ deviceId },
sqlite::ForeignKey{ folderId } ) == false )
return nullptr;
return self;
......
......@@ -60,11 +60,13 @@ class Media : public IMedia,
// 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...
Media( MediaLibraryPtr ml , sqlite::Row& row );
Media( MediaLibraryPtr ml, const std::string& title, Type type);
Media( MediaLibraryPtr ml, const std::string& title, Type type,
int64_t duration );
static std::shared_ptr<Media> create( MediaLibraryPtr ml, Type type,
int64_t deviceId, int64_t folderId,
const std::string& fileName );
const std::string& fileName,
int64_t duration );
static void createTable( sqlite::Connection* connection, uint32_t modelVersion );
static void createTriggers( sqlite::Connection* connection, uint32_t modelVersion );
......
......@@ -413,7 +413,7 @@ MediaPtr MediaLibrary::addExternalMedia( const std::string& mrl, IMedia::Type ty
auto t = m_dbConnection->newTransaction();
auto fileName = utils::file::fileName( mrl );
auto media = Media::create( this, type, 0, 0,
utils::url::decode( fileName ) );
utils::url::decode( fileName ), -1 );
if ( media == nullptr )
return nullptr;
if ( media->addExternalMrl( mrl, IFile::Type::Main ) == nullptr )
......
......@@ -262,7 +262,7 @@ void MetadataAnalyzer::addPlaylistElement( IItem& item,
{
auto t2 = m_ml->getConn()->newTransaction();
auto externalMedia = Media::create( m_ml, IMedia::Type::External, 0, 0,
subitem.meta( IItem::Metadata::Title ) );
subitem.meta( IItem::Metadata::Title ), -1 );
if ( externalMedia == nullptr )
{
LOG_ERROR( "Failed to create external media for ", mrl, " in the playlist ", playlistMrl );
......@@ -411,7 +411,8 @@ std::tuple<Status, bool> MetadataAnalyzer::createFileAndMedia( IItem& item ) con
auto folder = static_cast<Folder*>( item.parentFolder().get() );
auto m = Media::create( m_ml, isAudio ? IMedia::Type::Audio : IMedia::Type::Video,
folder->deviceId(), folder->id(),
utils::url::decode( utils::file::fileName( mrl ) ) );
utils::url::decode( utils::file::fileName( mrl ) ),
item.duration() );
if ( m == nullptr )
{
LOG_ERROR( "Failed to add media ", mrl, " to the media library" );
......@@ -431,7 +432,6 @@ std::tuple<Status, bool> MetadataAnalyzer::createFileAndMedia( IItem& item ) con
return std::make_tuple( Status::Fatal, false );
}
createTracks( *m, tracks );
m->setDuration( item.duration() );
item.setMedia( std::move( m ) );
// Will invoke ITaskCb::updateFileId to upadte m_fileId & its
......
......@@ -115,7 +115,7 @@ std::shared_ptr<Media> MediaLibraryTester::addFile( std::shared_ptr<fs::IFile> f
{
LOG_INFO( "Adding ", fileFs->mrl() );
auto mptr = Media::create( this, type, parentFolder->deviceId(), parentFolder->id(),
fileFs->name() );
fileFs->name(), -1 );
if ( mptr == nullptr )
{
LOG_ERROR( "Failed to add media ", fileFs->mrl(), " to the media library" );
......
Supports Markdown
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