2015-10-22 09:29:02 +02:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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"
|
|
|
|
#include "util/UriUtil.hxx"
|
2018-08-02 10:38:20 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
#include "storage/StorageInterface.hxx"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const char *suffix = storage->MapToRelativeUTF8(uri);
|
|
|
|
if (suffix != nullptr)
|
|
|
|
/* this path was relative to the music
|
|
|
|
directory */
|
|
|
|
return LocatedUri(LocatedUri::Type::RELATIVE, suffix);
|
|
|
|
}
|
|
|
|
#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
|
2016-10-27 21:59:17 +02:00
|
|
|
LocateAbsoluteUri(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
|
|
|
{
|
2016-10-27 21:59:17 +02:00
|
|
|
if (!uri_supported_scheme(uri))
|
|
|
|
throw std::runtime_error("Unsupported URI scheme");
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
if (storage != nullptr) {
|
|
|
|
const char *suffix = storage->MapToRelativeUTF8(uri);
|
|
|
|
if (suffix != nullptr)
|
|
|
|
return LocatedUri(LocatedUri::Type::RELATIVE, suffix);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return LocatedUri(LocatedUri::Type::ABSOLUTE, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
LocatedUri
|
2016-10-27 21:59:17 +02:00
|
|
|
LocateUri(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))
|
|
|
|
throw std::runtime_error("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))
|
2016-10-27 21:59:17 +02:00
|
|
|
return LocateAbsoluteUri(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);
|
|
|
|
}
|