PlaylistStream.cxx 2.35 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 24
#include "util/UriUtil.hxx"
#include "input/InputStream.hxx"
25
#include "input/LocalOpen.hxx"
26
#include "fs/Path.hxx"
27 28
#include "Log.hxx"

29
#include <exception>
30

31 32
#include <assert.h>

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

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

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

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

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

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

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

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

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

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