2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2014-02-02 14:37:52 +01:00
|
|
|
|
|
|
|
#include "SongLoader.hxx"
|
2015-10-22 09:29:02 +02:00
|
|
|
#include "LocateUri.hxx"
|
2023-11-25 23:06:24 +01:00
|
|
|
#include "Partition.hxx"
|
|
|
|
#include "client/IClient.hxx"
|
2014-02-02 14:37:52 +01:00
|
|
|
#include "db/DatabaseSong.hxx"
|
2014-02-07 19:01:06 +01:00
|
|
|
#include "storage/StorageInterface.hxx"
|
2018-08-02 13:45:43 +02:00
|
|
|
#include "song/DetachedSong.hxx"
|
2014-02-02 14:37:52 +01:00
|
|
|
#include "PlaylistError.hxx"
|
2018-11-19 12:49:45 +01:00
|
|
|
#include "config.h"
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2014-02-01 00:26:34 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
|
2023-11-25 23:06:24 +01:00
|
|
|
SongLoader::SongLoader(const IClient &_client) noexcept
|
|
|
|
:client(&_client),
|
|
|
|
db(_client.GetDatabase()),
|
2014-02-07 00:29:07 +01:00
|
|
|
storage(_client.GetStorage()) {}
|
2014-02-01 00:26:34 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
DetachedSong
|
2016-03-19 00:13:57 +01:00
|
|
|
SongLoader::LoadFromDatabase(const char *uri) const
|
2015-10-22 09:34:13 +02:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
if (db != nullptr)
|
2017-02-08 09:47:43 +01:00
|
|
|
return DatabaseDetachSong(*db, storage, uri);
|
2015-10-22 09:34:13 +02:00
|
|
|
#else
|
|
|
|
(void)uri;
|
|
|
|
#endif
|
|
|
|
|
2016-02-28 11:15:20 +01:00
|
|
|
throw PlaylistError(PlaylistResult::NO_SUCH_SONG, "No database");
|
2015-10-22 09:34:13 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
DetachedSong
|
2016-03-19 00:13:57 +01:00
|
|
|
SongLoader::LoadFile(const char *path_utf8, Path path_fs) const
|
2014-02-02 14:37:52 +01:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_DATABASE
|
2014-02-07 19:01:06 +01:00
|
|
|
if (storage != nullptr) {
|
2020-04-03 16:22:39 +02:00
|
|
|
const auto suffix = storage->MapToRelativeUTF8(path_utf8);
|
|
|
|
if (suffix.data() != nullptr)
|
2014-02-07 19:01:06 +01:00
|
|
|
/* this path was relative to the music
|
|
|
|
directory - obtain it from the database */
|
2020-04-03 16:22:39 +02:00
|
|
|
return LoadFromDatabase(std::string(suffix).c_str());
|
2014-02-07 19:01:06 +01:00
|
|
|
}
|
2014-02-02 14:37:52 +01:00
|
|
|
#endif
|
|
|
|
|
2016-02-28 11:08:00 +01:00
|
|
|
DetachedSong song(path_utf8);
|
2016-02-28 11:15:20 +01:00
|
|
|
if (!song.LoadFile(path_fs))
|
|
|
|
throw PlaylistError::NoSuchSong();
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
return song;
|
2014-02-02 14:37:52 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
DetachedSong
|
2016-03-19 00:13:57 +01:00
|
|
|
SongLoader::LoadSong(const LocatedUri &located_uri) const
|
2015-10-22 09:29:02 +02:00
|
|
|
{
|
|
|
|
switch (located_uri.type) {
|
|
|
|
case LocatedUri::Type::ABSOLUTE:
|
2017-02-08 10:02:08 +01:00
|
|
|
return DetachedSong(located_uri.canonical_uri);
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
case LocatedUri::Type::RELATIVE:
|
2016-03-19 00:13:57 +01:00
|
|
|
return LoadFromDatabase(located_uri.canonical_uri);
|
2015-10-22 09:29:02 +02:00
|
|
|
|
|
|
|
case LocatedUri::Type::PATH:
|
2016-03-19 00:13:57 +01:00
|
|
|
return LoadFile(located_uri.canonical_uri, located_uri.path);
|
2015-10-22 09:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gcc_unreachable();
|
|
|
|
}
|
|
|
|
|
2017-02-08 10:02:08 +01:00
|
|
|
DetachedSong
|
2016-10-27 21:59:17 +02:00
|
|
|
SongLoader::LoadSong(const char *uri_utf8) const
|
2014-02-02 14:37:52 +01:00
|
|
|
{
|
2014-12-26 14:28:01 +01:00
|
|
|
#if !CLANG_CHECK_VERSION(3,6)
|
|
|
|
/* disabled on clang due to -Wtautological-pointer-compare */
|
2014-02-02 14:37:52 +01:00
|
|
|
assert(uri_utf8 != nullptr);
|
2014-12-26 14:28:01 +01:00
|
|
|
#endif
|
2014-02-02 14:37:52 +01:00
|
|
|
|
2019-04-18 10:03:15 +02:00
|
|
|
const auto located_uri = LocateUri(UriPluginKind::INPUT,
|
|
|
|
uri_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
|
|
|
);
|
2016-03-19 00:13:57 +01:00
|
|
|
return LoadSong(located_uri);
|
2014-02-02 14:37:52 +01:00
|
|
|
}
|