Deduplicate embedded images, ref #628

This commit is contained in:
emeric
2025-03-15 14:48:37 +01:00
parent 744abefc8b
commit 05000d8a1a
65 changed files with 1307 additions and 319 deletions
+53 -48
View File
@@ -19,32 +19,46 @@
#include "CoverArtId.hpp"
#include "SubsonicId.hpp"
#include "core/String.hpp"
#include "database/ImageId.hpp"
#include "database/TrackEmbeddedImageId.hpp"
namespace lms::api::subsonic
{
namespace
{
constexpr char timestampSeparatorChar{ ':' };
}
constexpr char timestampSeparatorChar{ '-' };
std::string idToString(db::ImageId id)
{
return "im-" + id.toString();
}
std::string idToString(db::ImageId id)
{
return "im-" + id.toString();
}
std::string idToString(db::TrackEmbeddedImageId id)
{
return "trim-" + id.toString();
}
} // namespace
std::string idToString(CoverArtId coverId)
{
// produce "id:timestamp"
std::string res{ std::visit([](auto&& id) {
return idToString(id);
},
coverId.id) };
std::string res;
res += timestampSeparatorChar;
res += std::to_string(coverId.timestamp);
// produce "type-id[-timestamp]"
if (db::TrackEmbeddedImageId * imageId{ std::get_if<db::TrackEmbeddedImageId>(&coverId.id) })
{
res = idToString(*imageId);
assert(!coverId.timestamp.has_value());
}
else if (db::ImageId * imageId{ std::get_if<db::ImageId>(&coverId.id) })
{
res = idToString(*imageId);
res += timestampSeparatorChar;
assert(coverId.timestamp.has_value());
res += std::to_string(*coverId.timestamp);
}
assert(!res.empty());
return res;
}
} // namespace lms::api::subsonic
@@ -52,46 +66,37 @@ namespace lms::api::subsonic
// Used to parse parameters
namespace lms::core::stringUtils
{
template<>
std::optional<db::ImageId> readAs(std::string_view str)
{
std::vector<std::string_view> values{ core::stringUtils::splitString(str, '-') };
if (values.size() != 2)
return std::nullopt;
if (values[0] != "im")
return std::nullopt;
if (const auto value{ core::stringUtils::readAs<db::ReleaseId::ValueType>(values[1]) })
return db::ImageId{ *value };
return std::nullopt;
}
template<>
std::optional<api::subsonic::CoverArtId> readAs(std::string_view str)
{
// expect "id:timestamp"
auto timeStampSeparator{ str.find_last_of(api::subsonic::timestampSeparatorChar) };
if (timeStampSeparator == std::string_view::npos)
std::optional<api::subsonic::CoverArtId> res;
std::vector<std::string_view> values{ core::stringUtils::splitString(str, '-') };
if (values.size() <= 1)
return std::nullopt;
std::string_view strId{ str.substr(0, timeStampSeparator) };
std::string_view strTimestamp{ str.substr(timeStampSeparator + 1) };
if (values[0] == "trim")
{
// expect "trim-id"
if (values.size() == 2)
{
if (const auto value{ core::stringUtils::readAs<db::TrackEmbeddedImageId::ValueType>(values[1]) })
res.emplace(db::TrackEmbeddedImageId{ *value });
}
}
else if (values[0] == "im")
{
// expect "im-id-timestamp"
if (values.size() == 3)
{
const auto imageId{ core::stringUtils::readAs<db::ImageId::ValueType>(values[1]) };
const auto timestamp{ core::stringUtils::readAs<std::time_t>(values[2]) };
api::subsonic::CoverArtId cover;
if (const auto imagetId{ readAs<db::ImageId>(strId) })
cover.id = *imagetId;
else if (const auto trackId{ readAs<db::TrackId>(strId) })
cover.id = *trackId;
else
return std::nullopt;
if (imageId && timestamp)
res.emplace(db::ImageId{ *imageId }, *timestamp);
}
}
if (const auto timestamp{ readAs<std::time_t>(strTimestamp) })
cover.timestamp = *timestamp;
else
return std::nullopt;
return cover;
return res;
}
} // namespace lms::core::stringUtils