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

parser: Store subitems in the parent item

parent 594810da
......@@ -91,11 +91,11 @@ int MetadataParser::toInt( parser::Task& task, parser::Task::Item::Metadata meta
parser::Task::Status MetadataParser::run( parser::Task& task )
{
bool alreadyInParser = false;
int nbSubitem = task.vlcMedia.subitems()->count();
int nbSubitem = task.item().subItems().size();
// Assume that file containing subitem(s) is a Playlist
if ( nbSubitem > 0 )
{
auto res = addPlaylistMedias( task, nbSubitem );
auto res = addPlaylistMedias( task );
if ( res == false ) // playlist addition may fail due to constraint violation
return parser::Task::Status::Fatal;
......@@ -227,7 +227,7 @@ parser::Task::Status MetadataParser::run( parser::Task& task )
/* Playlist files */
bool MetadataParser::addPlaylistMedias( parser::Task& task, int nbSubitem ) const
bool MetadataParser::addPlaylistMedias( parser::Task& task ) const
{
auto t = m_ml->getConn()->newTransaction();
const auto& mrl = task.item().mrl();
......@@ -248,19 +248,17 @@ bool MetadataParser::addPlaylistMedias( parser::Task& task, int nbSubitem ) cons
return false;
}
t->commit();
auto subitems = task.vlcMedia.subitems();
for ( int i = 0; i < nbSubitem; ++i ) // FIXME: Interrupt loop if paused
addPlaylistElement( task, playlistPtr, subitems->itemAtIndex( i ), static_cast<unsigned int>( i ) + 1 );
auto subitems = task.item().subItems();
for ( auto i = 0u; i < subitems.size(); ++i ) // FIXME: Interrupt loop if paused
addPlaylistElement( task, playlistPtr, subitems[i], static_cast<unsigned int>( i ) + 1 );
return true;
}
void MetadataParser::addPlaylistElement( parser::Task& task, const std::shared_ptr<Playlist>& playlistPtr,
VLC::MediaPtr subitem, unsigned int index ) const
parser::Task::Item& subitem, unsigned int index ) const
{
if ( subitem == nullptr )
return;
const auto& mrl = subitem->mrl();
const auto& mrl = subitem.mrl();
LOG_INFO( "Try to add ", mrl, " to the playlist ", mrl );
auto media = m_ml->media( mrl );
if ( media != nullptr )
......@@ -276,16 +274,16 @@ void MetadataParser::addPlaylistElement( parser::Task& task, const std::shared_p
{
auto t2 = m_ml->getConn()->newTransaction();
auto externalMedia = Media::create( m_ml, IMedia::Type::Unknown, utils::url::encode(
subitem->meta( libvlc_meta_Title ) ) );
subitem.meta( parser::Task::Item::Metadata::Title ) ) );
if ( externalMedia == nullptr )
{
LOG_ERROR( "Failed to create external media for ", subitem->mrl(), " in the playlist ", mrl );
LOG_ERROR( "Failed to create external media for ", mrl, " in the playlist ", task.item().mrl() );
return;
}
// Assuming that external mrl present in playlist file is a main media resource
auto externalFile = externalMedia->addExternalMrl( mrl, IFile::Type::Main );
if ( externalFile == nullptr )
LOG_ERROR( "Failed to create external file for ", subitem->mrl(), " in the playlist ", mrl );
LOG_ERROR( "Failed to create external file for ", mrl, " in the playlist ", task.item().mrl() );
playlistPtr->add( externalMedia->id(), index );
t2->commit();
return;
......@@ -300,7 +298,7 @@ void MetadataParser::addPlaylistElement( parser::Task& task, const std::shared_p
LOG_ERROR( ex.what() );
return;
}
LOG_INFO( "Importing ", isDirectory ? "folder " : "file ", subitem->mrl(), " in the playlist ", mrl );
LOG_INFO( "Importing ", isDirectory ? "folder " : "file ", mrl, " in the playlist ", task.item().mrl() );
auto directoryMrl = utils::file::directory( mrl );
auto parentFolder = Folder::fromMrl( m_ml, directoryMrl );
bool parentKnown = parentFolder != nullptr;
......
......@@ -45,9 +45,9 @@ protected:
virtual void onRestarted() override;
bool isCompleted( const parser::Task& task ) const override;
bool addPlaylistMedias( parser::Task& task, int nbSubitem ) const;
bool addPlaylistMedias( parser::Task& task ) const;
void addPlaylistElement( parser::Task& task, const std::shared_ptr<Playlist>& playlistPtr,
VLC::MediaPtr subitem, unsigned int index ) const;
parser::Task::Item& subitem, unsigned int index ) const;
bool parseAudioFile(parser::Task& task);
bool parseVideoFile(parser::Task& task) const;
std::pair<std::shared_ptr<Artist>, std::shared_ptr<Artist>> findOrCreateArtist( parser::Task& vlcMedia ) const;
......
......@@ -91,30 +91,7 @@ parser::Task::Status VLCMetadataService::run( parser::Task& task )
if ( res == false )
return parser::Task::Status::Fatal;
}
task.item().setMeta( parser::Task::Item::Metadata::Title,
task.vlcMedia.meta( libvlc_meta_Title ) );
task.item().setMeta( parser::Task::Item::Metadata::ArtworkUrl,
task.vlcMedia.meta( libvlc_meta_ArtworkURL ) );
task.item().setMeta( parser::Task::Item::Metadata::ShowName,
task.vlcMedia.meta( libvlc_meta_ShowName ) );
task.item().setMeta( parser::Task::Item::Metadata::Episode,
task.vlcMedia.meta( libvlc_meta_Episode ) );
task.item().setMeta( parser::Task::Item::Metadata::Album,
task.vlcMedia.meta( libvlc_meta_Album ) );
task.item().setMeta( parser::Task::Item::Metadata::Genre,
task.vlcMedia.meta( libvlc_meta_Genre ) );
task.item().setMeta( parser::Task::Item::Metadata::Date,
task.vlcMedia.meta( libvlc_meta_Date ) );
task.item().setMeta( parser::Task::Item::Metadata::AlbumArtist,
task.vlcMedia.meta( libvlc_meta_AlbumArtist ) );
task.item().setMeta( parser::Task::Item::Metadata::Artist,
task.vlcMedia.meta( libvlc_meta_Artist ) );
task.item().setMeta( parser::Task::Item::Metadata::TrackNumber,
task.vlcMedia.meta( libvlc_meta_TrackNumber ) );
task.item().setMeta( parser::Task::Item::Metadata::DiscNumber,
task.vlcMedia.meta( libvlc_meta_DiscNumber ) );
task.item().setMeta( parser::Task::Item::Metadata::DiscTotal,
task.vlcMedia.meta( libvlc_meta_DiscTotal ) );
mediaToItem( task.vlcMedia, task.item() );
// Don't save the file parsing step yet, since all data are just in memory. Just mark
// the extraction as done.
task.markStepCompleted( parser::Task::ParserStep::MetadataExtraction );
......@@ -145,4 +122,45 @@ void VLCMetadataService::onRestarted()
{
}
void VLCMetadataService::mediaToItem( VLC::Media& media, parser::Task::Item& item )
{
item.setMeta( parser::Task::Item::Metadata::Title,
media.meta( libvlc_meta_Title ) );
item.setMeta( parser::Task::Item::Metadata::ArtworkUrl,
media.meta( libvlc_meta_ArtworkURL ) );
item.setMeta( parser::Task::Item::Metadata::ShowName,
media.meta( libvlc_meta_ShowName ) );
item.setMeta( parser::Task::Item::Metadata::Episode,
media.meta( libvlc_meta_Episode ) );
item.setMeta( parser::Task::Item::Metadata::Album,
media.meta( libvlc_meta_Album ) );
item.setMeta( parser::Task::Item::Metadata::Genre,
media.meta( libvlc_meta_Genre ) );
item.setMeta( parser::Task::Item::Metadata::Date,
media.meta( libvlc_meta_Date ) );
item.setMeta( parser::Task::Item::Metadata::AlbumArtist,
media.meta( libvlc_meta_AlbumArtist ) );
item.setMeta( parser::Task::Item::Metadata::Artist,
media.meta( libvlc_meta_Artist ) );
item.setMeta( parser::Task::Item::Metadata::TrackNumber,
media.meta( libvlc_meta_TrackNumber ) );
item.setMeta( parser::Task::Item::Metadata::DiscNumber,
media.meta( libvlc_meta_DiscNumber ) );
item.setMeta( parser::Task::Item::Metadata::DiscTotal,
media.meta( libvlc_meta_DiscTotal ) );
auto subItems = media.subitems();
if ( subItems != nullptr )
{
for ( auto i = 0; i < subItems->count(); ++i )
{
auto vlcMedia = subItems->itemAtIndex( i );
assert( vlcMedia != nullptr );
parser::Task::Item subItem{ vlcMedia->mrl() };
mediaToItem( *vlcMedia, subItem );
item.addSubItem( std::move( subItem ) );
}
}
}
}
......@@ -50,6 +50,8 @@ private:
virtual void onFlushing() override;
virtual void onRestarted() override;
void mediaToItem( VLC::Media& media, parser::Task::Item& item );
private:
MediaLibrary* m_ml;
......
......@@ -160,6 +160,16 @@ const std::string& Task::Item::mrl() const
return m_mrl;
}
const std::vector<Task::Item>& Task::Item::subItems() const
{
return m_subItems;
}
void Task::Item::addSubItem( Task::Item item )
{
m_subItems.emplace_back( std::move( item ) );
}
bool Task::restoreLinkedEntities()
{
LOG_INFO("Restoring linked entities of task ", m_id);
......
......@@ -114,11 +114,17 @@ public:
const std::string& mrl() const;
const std::vector<Item>& subItems() const;
void addSubItem( Item mrl );
private:
std::string m_mrl;
std::unordered_map<Metadata, std::string> m_metadata;
std::vector<Item> m_subItems;
};
static_assert( std::is_move_assignable<Item>::value, "Item must be move assignable" );
/*
* Constructs a task to be resumed.
......
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