aboutsummaryrefslogtreecommitdiffstats
path: root/src/SongLoader.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2015-10-22 09:29:02 +0200
committerMax Kellermann <max@duempel.org>2015-10-22 09:39:28 +0200
commitf072cbbba765e3b972655880970d65760a208843 (patch)
treeb2065cf07d599f7f44c39bb3c3537f8f72691a73 /src/SongLoader.cxx
parent1a5b66b78dc767a4e0b721a2325957fa9ae815c2 (diff)
downloadmpd-f072cbbba765e3b972655880970d65760a208843.tar.gz
mpd-f072cbbba765e3b972655880970d65760a208843.tar.xz
mpd-f072cbbba765e3b972655880970d65760a208843.zip
LocateUri: new library to classify URIs in a standard way
Diffstat (limited to 'src/SongLoader.cxx')
-rw-r--r--src/SongLoader.cxx66
1 files changed, 32 insertions, 34 deletions
diff --git a/src/SongLoader.cxx b/src/SongLoader.cxx
index 9f7359a31..14f9f4dd6 100644
--- a/src/SongLoader.cxx
+++ b/src/SongLoader.cxx
@@ -19,19 +19,15 @@
#include "config.h"
#include "SongLoader.hxx"
+#include "LocateUri.hxx"
#include "client/Client.hxx"
#include "db/DatabaseSong.hxx"
#include "storage/StorageInterface.hxx"
-#include "ls.hxx"
-#include "fs/AllocatedPath.hxx"
-#include "fs/Traits.hxx"
-#include "util/UriUtil.hxx"
#include "util/Error.hxx"
#include "DetachedSong.hxx"
#include "PlaylistError.hxx"
#include <assert.h>
-#include <string.h>
#ifdef ENABLE_DATABASE
@@ -57,7 +53,7 @@ SongLoader::LoadFromDatabase(const char *uri, Error &error) const
}
DetachedSong *
-SongLoader::LoadFile(const char *path_utf8, Error &error) const
+SongLoader::LoadFile(const char *path_utf8, Path path_fs, Error &error) const
{
#ifdef ENABLE_DATABASE
if (storage != nullptr) {
@@ -69,13 +65,6 @@ SongLoader::LoadFile(const char *path_utf8, Error &error) const
}
#endif
- const auto path_fs = AllocatedPath::FromUTF8(path_utf8, error);
- if (path_fs.IsNull())
- return nullptr;
-
- if (client != nullptr && !client->AllowFile(path_fs, error))
- return nullptr;
-
DetachedSong *song = new DetachedSong(path_utf8);
if (!song->LoadFile(path_fs)) {
error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_SONG),
@@ -88,6 +77,27 @@ SongLoader::LoadFile(const char *path_utf8, Error &error) const
}
DetachedSong *
+SongLoader::LoadSong(const LocatedUri &located_uri, Error &error) const
+{
+ switch (located_uri.type) {
+ case LocatedUri::Type::UNKNOWN:
+ gcc_unreachable();
+
+ case LocatedUri::Type::ABSOLUTE:
+ return new DetachedSong(located_uri.canonical_uri);
+
+ case LocatedUri::Type::RELATIVE:
+ return LoadFromDatabase(located_uri.canonical_uri, error);
+
+ case LocatedUri::Type::PATH:
+ return LoadFile(located_uri.canonical_uri, located_uri.path,
+ error);
+ }
+
+ gcc_unreachable();
+}
+
+DetachedSong *
SongLoader::LoadSong(const char *uri_utf8, Error &error) const
{
#if !CLANG_CHECK_VERSION(3,6)
@@ -95,25 +105,13 @@ SongLoader::LoadSong(const char *uri_utf8, Error &error) const
assert(uri_utf8 != nullptr);
#endif
- if (memcmp(uri_utf8, "file:///", 8) == 0)
- /* absolute path */
- return LoadFile(uri_utf8 + 7, error);
- else if (PathTraitsUTF8::IsAbsolute(uri_utf8))
- /* absolute path */
- return LoadFile(uri_utf8, error);
- else if (uri_has_scheme(uri_utf8)) {
- /* remove URI */
- if (!uri_supported_scheme(uri_utf8)) {
- error.Set(playlist_domain,
- int(PlaylistResult::NO_SUCH_SONG),
- "Unsupported URI scheme");
- return nullptr;
- }
-
- return new DetachedSong(uri_utf8);
- } else {
- /* URI relative to the music directory */
-
- return LoadFromDatabase(uri_utf8, error);
- }
+ const auto located_uri = LocateUri(uri_utf8, client,
+#ifdef ENABLE_DATABASE
+ storage,
+#endif
+ error);
+ if (located_uri.IsUnknown())
+ return nullptr;
+
+ return LoadSong(located_uri, error);
}