LocateUri: new library to classify URIs in a standard way

This commit is contained in:
Max Kellermann
2015-10-22 09:29:02 +02:00
parent 1a5b66b78d
commit f072cbbba7
10 changed files with 477 additions and 189 deletions

View File

@@ -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),
@@ -87,6 +76,27 @@ SongLoader::LoadFile(const char *path_utf8, Error &error) const
return song;
}
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
{
@@ -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;
}
const auto located_uri = LocateUri(uri_utf8, client,
#ifdef ENABLE_DATABASE
storage,
#endif
error);
if (located_uri.IsUnknown())
return nullptr;
return new DetachedSong(uri_utf8);
} else {
/* URI relative to the music directory */
return LoadFromDatabase(uri_utf8, error);
}
return LoadSong(located_uri, error);
}