Changed the default cover image for releases

This commit is contained in:
emeric
2024-03-24 15:15:36 +01:00
parent 339b797312
commit ac69582f4b
16 changed files with 187 additions and 91 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

+11
View File
@@ -0,0 +1,11 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg fill="#c9c9c9" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="800px" height="800px" viewBox="-46.75 -46.75 560.95 560.95" xml:space="preserve" stroke="#c9c9c9" transform="rotate(0)matrix(1, 0, 0, 1, 0, 0)">
<g id="SVGRepo_bgCarrier" stroke-width="0"/>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"/>
<g id="SVGRepo_iconCarrier"> <g> <g id="Layer_7_5_"> <path d="M233.728,0C104.641,0,0,104.642,0,233.727c0,129.084,104.641,233.725,233.727,233.725 c129.083,0,233.724-104.641,233.724-233.725C467.451,104.642,362.811,0,233.728,0z M64.964,250.082h-9.692 c0-52.863,19.316-101.268,51.208-138.639c2.288,2.292,4.578,4.575,6.871,6.87C83.2,153.906,64.964,199.893,64.964,250.082z M98.899,250.082h-9.695c0-43.511,15.522-83.45,41.315-114.595l6.886,6.889C113.355,171.742,98.899,209.244,98.899,250.082z M233.728,326.421c-51.194,0-92.698-41.492-92.698-92.694c0-51.199,41.503-92.696,92.698-92.696 c51.199,0,92.693,41.497,92.693,92.696C326.421,284.929,284.927,326.421,233.728,326.421z M333.519,333.59 c24.055-29.363,38.514-66.885,38.514-107.708h9.689c0,43.51-15.524,83.453-41.313,114.597L333.519,333.59z M364.446,364.523 c-2.293-2.288-4.572-4.575-6.873-6.868c30.14-35.606,48.372-81.593,48.372-131.774h9.697 C415.652,278.749,396.337,327.156,364.446,364.523z M265.021,233.727c0,17.28-14.009,31.293-31.293,31.293 c-17.287,0-31.296-14.013-31.296-31.293c0-17.287,14.009-31.295,31.296-31.295C251.012,202.431,265.021,216.439,265.021,233.727z"/> </g> </g> </g>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

+1
View File
@@ -1,5 +1,6 @@
add_library(lmsimage SHARED
impl/SvgImage.cpp
)
target_include_directories(lmsimage INTERFACE
+50
View File
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2015 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SvgImage.hpp"
#include <fstream>
#include "image/Exception.hpp"
namespace lms::image
{
std::unique_ptr<IEncodedImage> readSvgFile(const std::filesystem::path& p)
{
if (p.extension() != ".svg")
throw Exception{ "Unexpected file extension: '" + p.extension().string() + "', expected .svg" };
std::ifstream ifs{ p.string(), std::ios::binary };
if (!ifs.is_open())
throw Exception{ "Cannot open file '" + p.string() + "' for reading purpose" };
std::vector<std::byte> data;
// read file content
ifs.seekg(0, std::ios::end);
std::streamsize size = ifs.tellg();
if (size < 0)
throw Exception{ "Cannot determine file size for '" + p.string() + "'" };
ifs.seekg(0, std::ios::beg);
data.resize(size);
if (!ifs.read(reinterpret_cast<char*>(data.data()), size))
throw Exception{ "Cannot read file content for '" + p.string() + "'" };
return std::make_unique<SvgImage>(std::move(data));
}
}
+42
View File
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2015 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <filesystem>
#include <memory>
#include <vector>
#include "image/IEncodedImage.hpp"
namespace lms::image
{
class SvgImage : public IEncodedImage
{
public:
SvgImage(std::vector<std::byte>&& data) : _data{ std::move(data) } {}
const std::byte* getData() const { return &_data.front(); }
std::size_t getDataSize() const { return _data.size(); }
std::string_view getMimeType() const { return "image/svg+xml"; }
private:
const std::vector<std::byte> _data;
};
}
+5 -7
View File
@@ -23,11 +23,9 @@
namespace lms::image
{
class Exception : public core::LmsException
{
public:
using LmsException::LmsException;
};
class Exception : public core::LmsException
{
public:
using LmsException::LmsException;
};
} // namespace lms::cover
+10 -11
View File
@@ -24,16 +24,15 @@
namespace lms::image
{
using ImageSize = std::size_t;
using ImageSize = std::size_t;
class IEncodedImage
{
public:
virtual ~IEncodedImage() = default;
virtual const std::byte* getData() const = 0;
virtual std::size_t getDataSize() const = 0;
virtual std::string_view getMimeType() const = 0;
};
}
class IEncodedImage
{
public:
virtual ~IEncodedImage() = default;
virtual const std::byte* getData() const = 0;
virtual std::size_t getDataSize() const = 0;
virtual std::string_view getMimeType() const = 0;
};
}
+7 -14
View File
@@ -19,23 +19,16 @@
#pragma once
#include <filesystem>
#include <memory>
#include "image/IEncodedImage.hpp"
namespace lms::image
{
class IRawImage
{
public:
virtual ~IRawImage() = default;
virtual void resize(ImageSize width) = 0;
virtual std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const = 0;
};
void init(const std::filesystem::path& path);
std::unique_ptr<IRawImage> decodeImage(const std::byte* encodedData, std::size_t encodedDataSize);
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path);
class IRawImage
{
public:
virtual ~IRawImage() = default;
virtual void resize(ImageSize width) = 0;
virtual std::unique_ptr<IEncodedImage> encodeToJPEG(unsigned quality) const = 0;
};
}
+34
View File
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <filesystem>
#include <memory>
#include "image/IEncodedImage.hpp"
#include "image/IRawImage.hpp"
namespace lms::image
{
void init(const std::filesystem::path& path);
std::unique_ptr<IRawImage> decodeImage(const std::byte* encodedData, std::size_t encodedDataSize);
std::unique_ptr<IRawImage> decodeImage(const std::filesystem::path& path);
std::unique_ptr<IEncodedImage> readSvgFile(const std::filesystem::path& path);
}
+8 -41
View File
@@ -30,7 +30,7 @@
#include "database/Track.hpp"
#include "image/Exception.hpp"
#include "image/IRawImage.hpp"
#include "image/Image.hpp"
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/Path.hpp"
@@ -107,18 +107,16 @@ namespace lms::cover
}
}
std::unique_ptr<ICoverService> createCoverService(db::Db& db, const std::filesystem::path& execPath, const std::filesystem::path& defaultCoverPath)
std::unique_ptr<ICoverService> createCoverService(db::Db& db, const std::filesystem::path& defaultSvgCoverPath)
{
return std::make_unique<CoverService>(db, execPath, defaultCoverPath);
return std::make_unique<CoverService>(db, defaultSvgCoverPath);
}
using namespace image;
CoverService::CoverService(db::Db& db,
const std::filesystem::path& execPath,
const std::filesystem::path& defaultCoverPath)
const std::filesystem::path& defaultSvgCoverPath)
: _db{ db }
, _defaultCoverPath{ defaultCoverPath }
, _maxCacheSize{ core::Service<core::IConfig>::get()->getULong("cover-max-cache-size", 30) * 1000 * 1000 }
, _maxFileSize{ core::Service<core::IConfig>::get()->getULong("cover-max-file-size", 10) * 1000 * 1000 }
, _preferredFileNames{ constructPreferredFileNames() }
@@ -126,25 +124,12 @@ namespace lms::cover
{
setJpegQuality(core::Service<core::IConfig>::get()->getULong("cover-jpeg-quality", 75));
LMS_LOG(COVER, INFO, "Default cover path = '" << _defaultCoverPath.string() << "'");
LMS_LOG(COVER, INFO, "Default cover path = '" << defaultSvgCoverPath.string() << "'");
LMS_LOG(COVER, INFO, "Max cache size = " << _maxCacheSize);
LMS_LOG(COVER, INFO, "Max file size = " << _maxFileSize);
LMS_LOG(COVER, INFO, "Preferred file names: " << core::stringUtils::joinStrings(_preferredFileNames, ","));
#if LMS_SUPPORT_IMAGE_GM
GraphicsMagick::init(execPath);
#else
(void)execPath;
#endif
try
{
getDefault(512);
}
catch (const image::Exception& e)
{
throw core::LmsException("Cannot read default cover file '" + _defaultCoverPath.string() + "': " + e.what());
}
_defaultCover = image::readSvgFile(defaultSvgCoverPath); // may throw
}
std::unique_ptr<IEncodedImage> CoverService::getFromAvMediaFile(const av::IAudioFile& input, ImageSize width) const
@@ -189,27 +174,9 @@ namespace lms::cover
return image;
}
std::shared_ptr<IEncodedImage> CoverService::getDefault(ImageSize width)
std::shared_ptr<IEncodedImage> CoverService::getDefaultSvgCover()
{
{
std::shared_lock lock{ _cacheMutex };
if (auto it{ _defaultCoverCache.find(width) }; it != std::cend(_defaultCoverCache))
return it->second;
}
{
std::unique_lock lock{ _cacheMutex };
if (auto it{ _defaultCoverCache.find(width) }; it != std::cend(_defaultCoverCache))
return it->second;
std::shared_ptr<IEncodedImage> image{ getFromCoverFile(_defaultCoverPath, width) };
_defaultCoverCache[width] = image;
LMS_LOG(COVER, DEBUG, "Default cache entries = " << _defaultCoverCache.size());
return image;
}
return _defaultCover;
}
std::unique_ptr<IEncodedImage> CoverService::getFromDirectory(const std::filesystem::path& directory, ImageSize width, const std::vector<std::string>& preferredFileNames, bool allowPickRandom) const
@@ -84,16 +84,16 @@ namespace lms::cover
class CoverService : public ICoverService
{
public:
CoverService(db::Db& db, const std::filesystem::path& execPath, const std::filesystem::path& defaultCoverPath);
CoverService(db::Db& db, const std::filesystem::path& defaultSvgCoverPath);
private:
CoverService(const CoverService&) = delete;
CoverService& operator=(const CoverService&) = delete;
private:
std::shared_ptr<image::IEncodedImage> getFromTrack(db::TrackId trackId, image::ImageSize width) override;
std::shared_ptr<image::IEncodedImage> getFromRelease(db::ReleaseId releaseId, image::ImageSize width) override;
std::shared_ptr<image::IEncodedImage> getFromArtist(db::ArtistId artistId, image::ImageSize width) override;
std::shared_ptr<image::IEncodedImage> getDefault(image::ImageSize width) override;
std::shared_ptr<image::IEncodedImage> getDefaultSvgCover() override;
void flushCache() override;
void setJpegQuality(unsigned quality) override;
@@ -112,7 +112,7 @@ namespace lms::cover
std::shared_mutex _cacheMutex;
std::unordered_map<CacheEntryDesc, std::shared_ptr<image::IEncodedImage>> _cache;
std::unordered_map<image::ImageSize, std::shared_ptr<image::IEncodedImage>> _defaultCoverCache;
std::shared_ptr<image::IEncodedImage> _defaultCover;
std::atomic<std::size_t> _cacheMisses{};
std::atomic<std::size_t> _cacheHits{};
std::size_t _cacheSize{};
@@ -120,7 +120,6 @@ namespace lms::cover
void saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr<image::IEncodedImage> image);
std::shared_ptr<image::IEncodedImage> loadFromCache(const CacheEntryDesc& entryDesc);
const std::filesystem::path _defaultCoverPath;
const std::size_t _maxCacheSize;
static inline const std::vector<std::filesystem::path> _fileExtensions{ ".jpg", ".jpeg", ".png", ".bmp" }; // TODO parametrize
const std::size_t _maxFileSize;
@@ -43,14 +43,14 @@ namespace lms::cover
virtual std::shared_ptr<image::IEncodedImage> getFromRelease(db::ReleaseId releaseId, image::ImageSize width) = 0;
virtual std::shared_ptr<image::IEncodedImage> getFromArtist(db::ArtistId artistId, image::ImageSize width) = 0;
virtual std::shared_ptr<image::IEncodedImage> getDefault(image::ImageSize width) = 0;
virtual std::shared_ptr<image::IEncodedImage> getDefaultSvgCover() = 0;
virtual void flushCache() = 0;
virtual void setJpegQuality(unsigned quality) = 0; // from 1 to 100
};
std::unique_ptr<ICoverService> createCoverService(db::Db& db, const std::filesystem::path& execPath, const std::filesystem::path& defaultCoverPath);
std::unique_ptr<ICoverService> createCoverService(db::Db& db, const std::filesystem::path& defaultSvgCoverPath);
} // namespace lms::coverArt
@@ -272,7 +272,7 @@ namespace lms::api::subsonic
cover = core::Service<cover::ICoverService>::get()->getFromArtist(*artistId, size);
if (!cover && context.enableDefaultCover && !artistId)
cover = core::Service<cover::ICoverService>::get()->getDefault(size);
cover = core::Service<cover::ICoverService>::get()->getDefaultSvgCover();
if (!cover)
{
+2 -2
View File
@@ -25,7 +25,7 @@
#include <Wt/WServer.h>
#include <Wt/WApplication.h>
#include "image/IRawImage.hpp"
#include "image/Image.hpp"
#include "services/auth/IAuthTokenService.hpp"
#include "services/auth/IPasswordService.hpp"
#include "services/auth/IEnvService.hpp"
@@ -310,7 +310,7 @@ namespace lms
throw core::LmsException{ "Bad value '" + authenticationBackend + "' for 'authentication-backend'" };
image::init(argv[0]);
core::Service<cover::ICoverService> coverService{ cover::createCoverService(database, argv[0], server.appRoot() + "/images/unknown-cover.jpg") };
core::Service<cover::ICoverService> coverService{ cover::createCoverService(database, server.appRoot() + "/images/unknown-cover.svg") };
core::Service<recommendation::IRecommendationService> recommendationService{ recommendation::createRecommendationService(database) };
core::Service<recommendation::IPlaylistGeneratorService> playlistGeneratorService{ recommendation::createPlaylistGeneratorService(database, *recommendationService.get()) };
core::Service<scanner::IScannerService> scannerService{ scanner::createScannerService(database) };
+2 -2
View File
@@ -98,7 +98,7 @@ namespace lms::ui
cover = core::Service<cover::ICoverService>::get()->getFromTrack(*trackId, *size);
if (!cover)
cover = core::Service<cover::ICoverService>::get()->getDefault(*size);
cover = core::Service<cover::ICoverService>::get()->getDefaultSvgCover();
}
else if (releaseIdStr)
{
@@ -110,7 +110,7 @@ namespace lms::ui
cover = core::Service<cover::ICoverService>::get()->getFromRelease(*releaseId, *size);
if (!cover)
cover = core::Service<cover::ICoverService>::get()->getDefault(*size);
cover = core::Service<cover::ICoverService>::get()->getDefaultSvgCover();
}
else
{
+8 -6
View File
@@ -24,15 +24,16 @@
#include <boost/program_options.hpp>
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "services/cover/ICoverService.hpp"
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/Service.hpp"
#include "core/StreamLogger.hpp"
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "image/Image.hpp"
#include "services/cover/ICoverService.hpp"
namespace lms
{
@@ -83,9 +84,10 @@ int main(int argc, char* argv[])
return EXIT_SUCCESS;
}
image::init(argv[0]);
core::Service<core::IConfig> config{ core::createConfig(vm["conf"].as<std::string>()) };
db::Db db{ config->getPath("working-dir") / "lms.db" };
core::Service<cover::ICoverService> coverArtService{ cover::createCoverService(db, argv[0], vm["default-cover"].as<std::string>()) };
core::Service<cover::ICoverService> coverArtService{ cover::createCoverService(db, vm["default-cover"].as<std::string>()) };
coverArtService->setJpegQuality(config->getULong("cover-jpeg-quality", vm["quality"].as<unsigned>()));