SongLoader: new class that merges duplicate code
There was quite a lot of duplicate code for loading DetachedSong objects, with different semantics for "securely" loading local files.
This commit is contained in:
@@ -32,7 +32,7 @@ PlaylistResult
|
||||
playlist_load_into_queue(const char *uri, SongEnumerator &e,
|
||||
unsigned start_index, unsigned end_index,
|
||||
playlist &dest, PlayerControl &pc,
|
||||
bool secure)
|
||||
const SongLoader &loader)
|
||||
{
|
||||
const std::string base_uri = uri != nullptr
|
||||
? PathTraitsUTF8::GetParent(uri)
|
||||
@@ -49,7 +49,7 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e,
|
||||
}
|
||||
|
||||
if (!playlist_check_translate_song(*song, base_uri.c_str(),
|
||||
secure)) {
|
||||
loader)) {
|
||||
delete song;
|
||||
continue;
|
||||
}
|
||||
@@ -67,7 +67,7 @@ PlaylistResult
|
||||
playlist_open_into_queue(const char *uri,
|
||||
unsigned start_index, unsigned end_index,
|
||||
playlist &dest, PlayerControl &pc,
|
||||
bool secure)
|
||||
const SongLoader &loader)
|
||||
{
|
||||
Mutex mutex;
|
||||
Cond cond;
|
||||
@@ -80,7 +80,7 @@ playlist_open_into_queue(const char *uri,
|
||||
PlaylistResult result =
|
||||
playlist_load_into_queue(uri, *playlist,
|
||||
start_index, end_index,
|
||||
dest, pc, secure);
|
||||
dest, pc, loader);
|
||||
delete playlist;
|
||||
|
||||
if (is != nullptr)
|
||||
|
@@ -26,6 +26,7 @@
|
||||
|
||||
#include "PlaylistError.hxx"
|
||||
|
||||
class SongLoader;
|
||||
class SongEnumerator;
|
||||
struct playlist;
|
||||
struct PlayerControl;
|
||||
@@ -43,7 +44,7 @@ PlaylistResult
|
||||
playlist_load_into_queue(const char *uri, SongEnumerator &e,
|
||||
unsigned start_index, unsigned end_index,
|
||||
playlist &dest, PlayerControl &pc,
|
||||
bool secure);
|
||||
const SongLoader &loader);
|
||||
|
||||
/**
|
||||
* Opens a playlist with a playlist plugin and append to the specified
|
||||
@@ -53,7 +54,7 @@ PlaylistResult
|
||||
playlist_open_into_queue(const char *uri,
|
||||
unsigned start_index, unsigned end_index,
|
||||
playlist &dest, PlayerControl &pc,
|
||||
bool secure);
|
||||
const SongLoader &loader);
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -20,8 +20,7 @@
|
||||
#include "config.h"
|
||||
#include "PlaylistSong.hxx"
|
||||
#include "Mapper.hxx"
|
||||
#include "db/DatabaseSong.hxx"
|
||||
#include "ls.hxx"
|
||||
#include "SongLoader.hxx"
|
||||
#include "tag/Tag.hxx"
|
||||
#include "tag/TagBuilder.hxx"
|
||||
#include "fs/AllocatedPath.hxx"
|
||||
@@ -65,44 +64,22 @@ apply_song_metadata(DetachedSong &dest, const DetachedSong &src)
|
||||
}
|
||||
|
||||
static bool
|
||||
playlist_check_load_song(DetachedSong &song)
|
||||
playlist_check_load_song(DetachedSong &song, const SongLoader &loader)
|
||||
{
|
||||
const char *const uri = song.GetURI();
|
||||
|
||||
if (uri_has_scheme(uri)) {
|
||||
return true;
|
||||
} else if (PathTraitsUTF8::IsAbsolute(uri)) {
|
||||
DetachedSong tmp(uri);
|
||||
if (!tmp.Update())
|
||||
return false;
|
||||
|
||||
apply_song_metadata(song, tmp);
|
||||
return true;
|
||||
} else {
|
||||
#ifdef ENABLE_DATABASE
|
||||
DetachedSong *tmp = DatabaseDetachSong(uri, IgnoreError());
|
||||
if (tmp == nullptr)
|
||||
return false;
|
||||
|
||||
apply_song_metadata(song, *tmp);
|
||||
delete tmp;
|
||||
return true;
|
||||
#else
|
||||
DetachedSong *tmp = loader.LoadSong(song.GetURI(), IgnoreError());
|
||||
if (tmp == nullptr)
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
song.SetURI(tmp->GetURI());
|
||||
apply_song_metadata(song, *tmp);
|
||||
delete tmp;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
playlist_check_translate_song(DetachedSong &song, const char *base_uri,
|
||||
bool secure)
|
||||
const SongLoader &loader)
|
||||
{
|
||||
const char *const uri = song.GetURI();
|
||||
|
||||
if (uri_has_scheme(uri))
|
||||
/* valid remote song? */
|
||||
return uri_supported_scheme(uri);
|
||||
|
||||
if (base_uri != nullptr && strcmp(base_uri, ".") == 0)
|
||||
/* PathTraitsUTF8::GetParent() returns "." when there
|
||||
is no directory name in the given path; clear that
|
||||
@@ -110,26 +87,10 @@ playlist_check_translate_song(DetachedSong &song, const char *base_uri,
|
||||
functions */
|
||||
base_uri = nullptr;
|
||||
|
||||
if (PathTraitsUTF8::IsAbsolute(uri)) {
|
||||
/* XXX fs_charset vs utf8? */
|
||||
const char *suffix = map_to_relative_path(uri);
|
||||
assert(suffix != nullptr);
|
||||
|
||||
if (suffix != uri)
|
||||
song.SetURI(std::string(suffix));
|
||||
else if (!secure)
|
||||
/* local files must be relative to the music
|
||||
directory when "secure" is enabled */
|
||||
return false;
|
||||
|
||||
base_uri = nullptr;
|
||||
}
|
||||
|
||||
if (base_uri != nullptr) {
|
||||
const char *uri = song.GetURI();
|
||||
if (base_uri != nullptr && !uri_has_scheme(uri) &&
|
||||
!PathTraitsUTF8::IsAbsolute(uri))
|
||||
song.SetURI(PathTraitsUTF8::Build(base_uri, uri));
|
||||
/* repeat the above checks */
|
||||
return playlist_check_translate_song(song, nullptr, secure);
|
||||
}
|
||||
|
||||
return playlist_check_load_song(song);
|
||||
return playlist_check_load_song(song, loader);
|
||||
}
|
||||
|
@@ -20,18 +20,17 @@
|
||||
#ifndef MPD_PLAYLIST_SONG_HXX
|
||||
#define MPD_PLAYLIST_SONG_HXX
|
||||
|
||||
class SongLoader;
|
||||
class DetachedSong;
|
||||
|
||||
/**
|
||||
* Verifies the song, returns false if it is unsafe. Translate the
|
||||
* song to a song within the database, if it is a local file.
|
||||
*
|
||||
* @param secure if true, then local files are only allowed if they
|
||||
* are relative to base_uri
|
||||
* @return true on success, false if the song should not be used
|
||||
*/
|
||||
bool
|
||||
playlist_check_translate_song(DetachedSong &song, const char *base_uri,
|
||||
bool secure);
|
||||
const SongLoader &loader);
|
||||
|
||||
#endif
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include "SongPrint.hxx"
|
||||
#include "input/InputStream.hxx"
|
||||
#include "DetachedSong.hxx"
|
||||
#include "SongLoader.hxx"
|
||||
#include "fs/Traits.hxx"
|
||||
#include "thread/Cond.hxx"
|
||||
|
||||
@@ -36,10 +37,12 @@ playlist_provider_print(Client &client, const char *uri,
|
||||
? PathTraitsUTF8::GetParent(uri)
|
||||
: std::string(".");
|
||||
|
||||
const SongLoader loader(client);
|
||||
|
||||
DetachedSong *song;
|
||||
while ((song = e.NextSong()) != nullptr) {
|
||||
if (playlist_check_translate_song(*song, base_uri.c_str(),
|
||||
false)) {
|
||||
loader)) {
|
||||
if (detail)
|
||||
song_print_info(client, *song);
|
||||
else
|
||||
|
Reference in New Issue
Block a user