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

Media: Make type immutable

parent e455e0da
......@@ -398,11 +398,11 @@ bool Media::setThumbnail( const std::string& thumbnailMrl )
bool Media::save()
{
static const std::string req = "UPDATE " + Media::Table::Name + " SET "
"type = ?, subtype = ?, duration = ?, release_date = ?,"
"subtype = ?, duration = ?, release_date = ?,"
"title = ? WHERE id_media = ?";
if ( m_changed == false )
return true;
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_type, m_subType, m_duration,
if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, m_subType, m_duration,
m_releaseDate, m_title, m_id ) == false )
{
return false;
......@@ -522,14 +522,6 @@ IMedia::SubType Media::subType() const
return m_subType;
}
void Media::setType( Type type )
{
if ( m_type == type )
return;
m_type = type;
m_changed = true;
}
const std::string& Media::title() const
{
return m_title;
......
......@@ -68,7 +68,6 @@ class Media : public IMedia, public DatabaseHelpers<Media>
virtual int64_t id() const override;
virtual Type type() const override;
virtual SubType subType() const override;
void setType( Type type );
virtual const std::string& title() const override;
virtual bool setTitle( const std::string& title ) override;
///
......@@ -156,7 +155,7 @@ private:
// DB fields:
int64_t m_id;
Type m_type;
const Type m_type;
SubType m_subType;
int64_t m_duration;
unsigned int m_playCount;
......
......@@ -44,6 +44,7 @@
#include "discoverer/probe/PathProbe.h"
#include <cstdlib>
#include <algorithm>
namespace medialibrary
{
......@@ -105,6 +106,16 @@ Status MetadataAnalyzer::run( IItem& item )
return Status::Completed;
}
const auto& tracks = item.tracks();
if ( tracks.empty() == true )
{
LOG_WARN( "Failed to analyze ", item.mrl(), ": no tracks found" );
return Status::Fatal;
}
bool isAudio;
if ( item.file() == nullptr )
{
assert( item.media() == nullptr );
......@@ -112,9 +123,13 @@ Status MetadataAnalyzer::run( IItem& item )
auto mrl = item.mrl();
try
{
isAudio = std::find_if( begin( tracks ), end( tracks ), [](const Task::Item::Track& t) {
return t.type == Task::Item::Track::Type::Video;
}) == end( tracks );
auto t = m_ml->getConn()->newTransaction();
LOG_INFO( "Adding ", mrl );
auto m = Media::create( m_ml, IMedia::Type::Unknown, utils::file::fileName( mrl ) );
auto m = Media::create( m_ml, isAudio ? IMedia::Type::Audio : IMedia::Type::Video,
utils::file::fileName( mrl ) );
if ( m == nullptr )
{
LOG_ERROR( "Failed to add media ", mrl, " to the media library" );
......@@ -166,6 +181,8 @@ Status MetadataAnalyzer::run( IItem& item )
assert( false );
return Status::Fatal;
}
else
isAudio = item.media()->type() == IMedia::Type::Audio;
auto media = std::static_pointer_cast<Media>( item.media() );
if ( item.parentPlaylist() != nullptr )
......@@ -174,15 +191,6 @@ Status MetadataAnalyzer::run( IItem& item )
if ( alreadyInParser == true )
return Status::Discarded;
const auto& tracks = item.tracks();
if ( tracks.empty() == true )
{
LOG_WARN( "Failed to analyze ", item.mrl(), ": no tracks found" );
return Status::Fatal;
}
bool isAudio = true;
{
using TracksT = decltype( tracks );
sqlite::Tools::withRetries( 3, [this, &isAudio, &item, &media]( TracksT tracks ) {
......@@ -376,7 +384,6 @@ void MetadataAnalyzer::addPlaylistElement( IItem& item,
bool MetadataAnalyzer::parseVideoFile( IItem& item ) const
{
auto media = static_cast<Media*>( item.media().get() );
media->setType( IMedia::Type::Video );
const auto& title = item.meta( IItem::Metadata::Title );
if ( title.length() == 0 )
return true;
......@@ -433,7 +440,6 @@ bool MetadataAnalyzer::parseVideoFile( IItem& item ) const
bool MetadataAnalyzer::parseAudioFile( IItem& item )
{
auto media = static_cast<Media*>( item.media().get() );
media->setType( IMedia::Type::Audio );
auto artworkMrl = item.meta( IItem::Metadata::ArtworkUrl );
if ( artworkMrl.empty() == false )
......
......@@ -490,22 +490,6 @@ TEST_F( Medias, SortByFilename )
ASSERT_EQ( m1->id(), media[0]->id() );
}
TEST_F( Medias, SetType )
{
auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "media1.mp3" ) );
ASSERT_EQ( IMedia::Type::External, m1->type() );
m1->setType( IMedia::Type::Video );
m1->save();
ASSERT_EQ( IMedia::Type::Video, m1->type() );
Reload();
auto m2 = ml->media( m1->id() );
ASSERT_EQ( IMedia::Type::Video, m2->type() );
}
TEST_F( Medias, Metadata )
{
auto m = ml->addMedia( "media.mp3" );
......
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