2015-10-22 09:29:02 +02:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2015-10-22 09:29:02 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "LocateUri.hxx"
|
|
|
|
#include "client/Client.hxx"
|
|
|
|
#include "fs/AllocatedPath.hxx"
|
|
|
|
#include "ls.hxx"
|
2021-10-06 20:15:20 +02:00
|
|
|
#include "storage/Registry.hxx"
|
2018-08-02 10:38:20 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2019-08-09 15:54:13 +02:00
|
|
|
#include "util/UriExtract.hxx"
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
#include "storage/StorageInterface.hxx"
|
|
|
|
#endif
|
|
|
|
|
2019-12-16 22:52:50 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2015-10-22 09:29:02 +02:00
|
|
|
static LocatedUri
|
2016-10-27 21:59:17 +02:00
|
|
|
LocateFileUri(const char *uri, const Client *client
|
2015-10-22 09:29:02 +02:00
|
|
|
#ifdef ENABLE_DATABASE
|
2016-10-27 21:59:17 +02:00
|
|
|
, const Storage *storage
|
2015-10-22 09:29:02 +02:00
|
|
|
#endif
|
2016-10-27 21:59:17 +02:00
|
|
|
)
|
2015-10-22 09:29:02 +02:00
|
|
|
{
|
2016-10-27 21:59:17 +02:00
|
|
|
auto path = AllocatedPath::FromUTF8Throw(uri);
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
if (storage != nullptr) {
|
2020-04-03 16:22:39 +02:00
|
|
|
const auto suffix = storage->MapToRelativeUTF8(uri);
|
|
|
|
if (suffix.data() != nullptr)
|
2015-10-22 09:29:02 +02:00
|
|
|
/* this path was relative to the music
|
|
|
|
directory */
|
2020-04-03 16:22:39 +02:00
|
|
|
// TODO: don't use suffix.data() (ok for now because we know it's null-terminated)
|
|
|
|
return LocatedUri(LocatedUri::Type::RELATIVE,
|
|
|
|
suffix.data());
|
2015-10-22 09:29:02 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-27 21:59:17 +02:00
|
|
|
if (client != nullptr)
|
|
|
|
client->AllowFile(path);
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
return LocatedUri(LocatedUri::Type::PATH, uri, std::move(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
static LocatedUri
|
2019-04-18 10:03:15 +02:00
|
|
|
LocateAbsoluteUri(UriPluginKind kind, const char *uri
|
2015-10-22 09:29:02 +02:00
|
|
|
#ifdef ENABLE_DATABASE
|
2016-10-27 21:59:17 +02:00
|
|
|
, const Storage *storage
|
2015-10-22 09:29:02 +02:00
|
|
|
#endif
|
2016-10-27 21:59:17 +02:00
|
|
|
)
|
2015-10-22 09:29:02 +02:00
|
|
|
{
|
2019-04-18 10:03:15 +02:00
|
|
|
switch (kind) {
|
|
|
|
case UriPluginKind::INPUT:
|
|
|
|
if (!uri_supported_scheme(uri))
|
2021-10-06 19:26:09 +02:00
|
|
|
throw std::invalid_argument("Unsupported URI scheme");
|
2019-04-18 10:03:15 +02:00
|
|
|
break;
|
|
|
|
|
2021-10-06 20:15:20 +02:00
|
|
|
case UriPluginKind::STORAGE:
|
|
|
|
/* plugin support will be checked after the
|
|
|
|
Storage::MapToRelativeUTF8() call */
|
|
|
|
break;
|
|
|
|
|
2019-04-18 10:03:15 +02:00
|
|
|
case UriPluginKind::PLAYLIST:
|
|
|
|
/* for now, no validation for playlist URIs; this is
|
|
|
|
more complicated because there are three ways to
|
|
|
|
identify which plugin to use: URI scheme, filename
|
|
|
|
suffix and MIME type */
|
|
|
|
break;
|
|
|
|
}
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
if (storage != nullptr) {
|
2020-04-03 16:22:39 +02:00
|
|
|
const auto suffix = storage->MapToRelativeUTF8(uri);
|
|
|
|
if (suffix.data() != nullptr)
|
|
|
|
// TODO: don't use suffix.data() (ok for now because we know it's null-terminated)
|
|
|
|
return LocatedUri(LocatedUri::Type::RELATIVE,
|
|
|
|
suffix.data());
|
2015-10-22 09:29:02 +02:00
|
|
|
}
|
2021-10-06 20:15:20 +02:00
|
|
|
|
|
|
|
if (kind == UriPluginKind::STORAGE &&
|
|
|
|
GetStoragePluginByUri(uri) == nullptr)
|
|
|
|
throw std::invalid_argument("Unsupported URI scheme");
|
2015-10-22 09:29:02 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return LocatedUri(LocatedUri::Type::ABSOLUTE, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
LocatedUri
|
2019-04-18 10:03:15 +02:00
|
|
|
LocateUri(UriPluginKind kind,
|
|
|
|
const char *uri, const Client *client
|
2015-10-22 09:29:02 +02:00
|
|
|
#ifdef ENABLE_DATABASE
|
2016-10-27 21:59:17 +02:00
|
|
|
, const Storage *storage
|
2015-10-22 09:29:02 +02:00
|
|
|
#endif
|
2016-10-27 21:59:17 +02:00
|
|
|
)
|
2015-10-22 09:29:02 +02:00
|
|
|
{
|
|
|
|
/* skip the obsolete "file://" prefix */
|
2018-08-02 10:38:20 +02:00
|
|
|
const char *path_utf8 = StringAfterPrefixCaseASCII(uri, "file://");
|
2015-10-22 09:29:02 +02:00
|
|
|
if (path_utf8 != nullptr) {
|
2016-10-27 21:59:17 +02:00
|
|
|
if (!PathTraitsUTF8::IsAbsolute(path_utf8))
|
2021-10-06 19:26:09 +02:00
|
|
|
throw std::invalid_argument("Malformed file:// URI");
|
2015-10-22 09:29:02 +02:00
|
|
|
|
2016-10-27 21:59:17 +02:00
|
|
|
return LocateFileUri(path_utf8, client
|
2015-10-22 09:29:02 +02:00
|
|
|
#ifdef ENABLE_DATABASE
|
2016-10-27 21:59:17 +02:00
|
|
|
, storage
|
2015-10-22 09:29:02 +02:00
|
|
|
#endif
|
2016-10-27 21:59:17 +02:00
|
|
|
);
|
2015-10-22 09:29:02 +02:00
|
|
|
} else if (PathTraitsUTF8::IsAbsolute(uri))
|
2016-10-27 21:59:17 +02:00
|
|
|
return LocateFileUri(uri, client
|
2015-10-22 09:29:02 +02:00
|
|
|
#ifdef ENABLE_DATABASE
|
2016-10-27 21:59:17 +02:00
|
|
|
, storage
|
2015-10-22 09:29:02 +02:00
|
|
|
#endif
|
2016-10-27 21:59:17 +02:00
|
|
|
);
|
2015-10-22 09:29:02 +02:00
|
|
|
else if (uri_has_scheme(uri))
|
2019-04-18 10:03:15 +02:00
|
|
|
return LocateAbsoluteUri(kind, uri
|
2015-10-22 09:29:02 +02:00
|
|
|
#ifdef ENABLE_DATABASE
|
2016-10-27 21:59:17 +02:00
|
|
|
, storage
|
2015-10-22 09:29:02 +02:00
|
|
|
#endif
|
2016-10-27 21:59:17 +02:00
|
|
|
);
|
2015-10-22 09:29:02 +02:00
|
|
|
else
|
|
|
|
return LocatedUri(LocatedUri::Type::RELATIVE, uri);
|
|
|
|
}
|