PlaylistStream.cxx 2.37 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
 * 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 "PlaylistStream.hxx"
#include "PlaylistRegistry.hxx"
23
#include "SongEnumerator.hxx"
24 25
#include "util/UriUtil.hxx"
#include "input/InputStream.hxx"
26
#include "input/LocalOpen.hxx"
27
#include "fs/Path.hxx"
28 29
#include "Log.hxx"

30
#include <exception>
31

32 33
#include <assert.h>

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

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

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

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

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

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

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

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

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

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