LocateUri.cxx 3.45 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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"
25
#include "util/ASCII.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "util/UriExtract.hxx"
27 28 29 30 31

#ifdef ENABLE_DATABASE
#include "storage/StorageInterface.hxx"
#endif

32 33
#include <stdexcept>

34
static LocatedUri
35
LocateFileUri(const char *uri, const Client *client
36
#ifdef ENABLE_DATABASE
37
	      , const Storage *storage
38
#endif
39
	      )
40
{
41
	auto path = AllocatedPath::FromUTF8Throw(uri);
42 43 44

#ifdef ENABLE_DATABASE
	if (storage != nullptr) {
45 46
		const auto suffix = storage->MapToRelativeUTF8(uri);
		if (suffix.data() != nullptr)
47 48
			/* this path was relative to the music
			   directory */
49 50 51
			// TODO: don't use suffix.data() (ok for now because we know it's null-terminated)
			return LocatedUri(LocatedUri::Type::RELATIVE,
					  suffix.data());
52 53 54
	}
#endif

55 56
	if (client != nullptr)
		client->AllowFile(path);
57 58 59 60 61

	return LocatedUri(LocatedUri::Type::PATH, uri, std::move(path));
}

static LocatedUri
62
LocateAbsoluteUri(UriPluginKind kind, const char *uri
63
#ifdef ENABLE_DATABASE
64
		  , const Storage *storage
65
#endif
66
		  )
67
{
68 69 70 71 72 73 74 75 76 77 78 79 80 81
	switch (kind) {
	case UriPluginKind::INPUT:
	case UriPluginKind::STORAGE: // TODO: separate check for storage plugins
		if (!uri_supported_scheme(uri))
			throw std::runtime_error("Unsupported URI scheme");
		break;

	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;
	}
82 83 84

#ifdef ENABLE_DATABASE
	if (storage != nullptr) {
85 86 87 88 89
		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());
90 91 92 93 94 95 96
	}
#endif

	return LocatedUri(LocatedUri::Type::ABSOLUTE, uri);
}

LocatedUri
97 98
LocateUri(UriPluginKind kind,
	  const char *uri, const Client *client
99
#ifdef ENABLE_DATABASE
100
	  , const Storage *storage
101
#endif
102
	  )
103 104
{
	/* skip the obsolete "file://" prefix */
105
	const char *path_utf8 = StringAfterPrefixCaseASCII(uri, "file://");
106
	if (path_utf8 != nullptr) {
107 108
		if (!PathTraitsUTF8::IsAbsolute(path_utf8))
			throw std::runtime_error("Malformed file:// URI");
109

110
		return LocateFileUri(path_utf8, client
111
#ifdef ENABLE_DATABASE
112
				     , storage
113
#endif
114
				     );
115
	} else if (PathTraitsUTF8::IsAbsolute(uri))
116
		return LocateFileUri(uri, client
117
#ifdef ENABLE_DATABASE
118
				     , storage
119
#endif
120
				     );
121
	else if (uri_has_scheme(uri))
122
		return LocateAbsoluteUri(kind, uri
123
#ifdef ENABLE_DATABASE
124
					 , storage
125
#endif
126
					 );
127 128 129
	else
		return LocatedUri(LocatedUri::Type::RELATIVE, uri);
}