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

Media: Fix vacuuming for media that are inserted but never played

parent ef651de2
......@@ -817,12 +817,18 @@ void Media::clearHistory( MediaLibraryPtr ml )
void Media::removeOldMedia( MediaLibraryPtr ml, std::chrono::seconds maxLifeTime )
{
// Media that were never played have a real_last_played_date = NULL, so they
// won't match for real_last_played_date < X
// However we need to take care about media that were inserted but never played
const std::string req = "DELETE FROM " + Media::Table::Name + " "
"WHERE real_last_played_date < ? AND ( type = ? OR type = ? ) "
"WHERE ( real_last_played_date < ? OR "
"( real_last_played_date IS NULL AND insertion_date < ? ) )"
"AND ( type = ? OR type = ? ) "
"AND nb_playlists = 0";
auto deadline = std::chrono::duration_cast<std::chrono::seconds>(
(std::chrono::system_clock::now() - maxLifeTime).time_since_epoch() );
sqlite::Tools::executeDelete( ml->getConn(), req, deadline.count(),
deadline.count(),
IMedia::Type::External, IMedia::Type::Stream );
}
......
......@@ -221,6 +221,13 @@ void MediaLibraryTester::outdateAllDevices()
sqlite::Tools::executeUpdate( getConn(), req );
}
void MediaLibraryTester::setMediaInsertionDate( int64_t mediaId, time_t t )
{
std::string req = "UPDATE " + Media::Table::Name + " SET insertion_date = ? "
"WHERE id_media = ?";
sqlite::Tools::executeUpdate( getConn(), req, mediaId, t );
}
void MediaLibraryTester::outdateAllExternalMedia()
{
std::string req = "UPDATE " + Media::Table::Name + " SET real_last_played_date = 1 "
......
......@@ -78,6 +78,7 @@ public:
MediaPtr addMedia( const std::string& mrl );
void deleteMedia( int64_t mediaId );
void outdateAllDevices();
void setMediaInsertionDate( int64_t mediaId, time_t t );
void outdateAllExternalMedia();
private:
......
......@@ -734,6 +734,22 @@ TEST_F( Medias, VacuumOldExternal )
auto m2 = ml->addExternalMedia( "bar.mp3" );
auto s1 = ml->addStream( "http://baz.mkv" );
ASSERT_NE( nullptr, m1 );
ASSERT_NE( nullptr, m2 );
ASSERT_NE( nullptr, s1 );
// Check that they will not be vacuumed even if they haven't been played yet.
Reload();
m1 = ml->media( m1->id() );
m2 = ml->media( m2->id() );
s1 = ml->media( s1->id() );
ASSERT_NE( nullptr, m1 );
ASSERT_NE( nullptr, m2 );
ASSERT_NE( nullptr, s1 );
auto playlist = ml->createPlaylist( "playlist" );
playlist->append( *m1 );
......@@ -750,6 +766,29 @@ TEST_F( Medias, VacuumOldExternal )
ASSERT_EQ( nullptr, s1 );
}
TEST_F( Medias, VacuumNeverPlayedMedia )
{
auto m1 = ml->addExternalMedia( "foo.avi" );
auto m2 = ml->addExternalMedia( "bar.mp3" );
auto s1 = ml->addStream( "http://baz.mkv" );
ASSERT_NE( nullptr, m1 );
ASSERT_NE( nullptr, m2 );
ASSERT_NE( nullptr, s1 );
ml->setMediaInsertionDate( m1->id(), 1 );
Reload();
m1 = ml->media( m1->id() );
m2 = ml->media( m2->id() );
s1 = ml->media( s1->id() );
ASSERT_EQ( nullptr, m1 );
ASSERT_NE( nullptr, m2 );
ASSERT_NE( nullptr, s1 );
}
TEST_F( Medias, NbPlaylists )
{
auto m = std::static_pointer_cast<Media>( ml->addExternalMedia( "media.mkv" ) );
......
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