PlaylistStream.cxx 2.36 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "PlaylistStream.hxx"
#include "PlaylistRegistry.hxx"
22
#include "SongEnumerator.hxx"
23
#include "input/InputStream.hxx"
24
#include "input/LocalOpen.hxx"
25
#include "fs/Path.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "util/UriExtract.hxx"
27 28
#include "Log.hxx"

29
#include <cassert>
30
#include <exception>
31

32
static std::unique_ptr<SongEnumerator>
33
playlist_open_path_suffix(Path path, Mutex &mutex)
34
try {
35
	assert(!path.IsNull());
36

37 38 39 40
	const auto *suffix = path.GetSuffix();
	if (suffix == nullptr)
		return nullptr;

41
	const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8Throw();
42
	if (!playlist_suffix_supported(suffix_utf8.c_str()))
43 44
		return nullptr;

45
	auto is = OpenLocalInputStream(path, mutex);
46 47
	return playlist_list_open_stream_suffix(std::move(is),
						suffix_utf8.c_str());
48 49
} catch (...) {
	LogError(std::current_exception());
50
	return nullptr;
51 52
}

53
std::unique_ptr<SongEnumerator>
54
playlist_open_path(Path path, Mutex &mutex)
55
try {
56 57
	assert(!path.IsNull());

58 59
	const std::string uri_utf8 = path.ToUTF8Throw();
	auto playlist = playlist_list_open_uri(uri_utf8.c_str(), mutex);
60
	if (playlist == nullptr)
61
		playlist = playlist_open_path_suffix(path, mutex);
62 63

	return playlist;
64 65
} catch (...) {
	LogError(std::current_exception());
66
	return nullptr;
67 68
}

69
std::unique_ptr<SongEnumerator>
70
playlist_open_remote(const char *uri, Mutex &mutex)
71
try {
72 73
	assert(uri_has_scheme(uri));

74
	auto playlist = playlist_list_open_uri(uri, mutex);
75 76 77
	if (playlist != nullptr)
		return playlist;

78
	auto is = InputStream::OpenReady(uri, mutex);
79
	return playlist_list_open_stream(std::move(is), uri);
80 81
} catch (...) {
	LogError(std::current_exception());
82
	return nullptr;
83
}