/* * 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 . */ #pragma once #include #include #include #include #include #include #include #include #include #include "services/cover/ICoverService.hpp" #include "image/IEncodedImage.hpp" #include "services/database/Types.hpp" namespace Database { class Session; } namespace Av { class IAudioFile; } namespace Cover { struct CacheEntryDesc { std::variant id; std::size_t size; bool operator==(const CacheEntryDesc& other) const { return id == other.id && size == other.size; } }; } // ns Cover namespace std { template<> class hash { public: size_t operator()(const Cover::CacheEntryDesc& e) const { size_t h {}; std::visit([&](auto id) { using IdType = std::decay_t; h ^= std::hash()(id); }, e.id); h ^= std::hash()(e.size) << 1; return h; } }; } // ns std namespace Cover { class CoverService : public ICoverService { public: CoverService(Database::Db& db, const std::filesystem::path& execPath, const std::filesystem::path& defaultCoverPath); CoverService(const CoverService&) = delete; CoverService& operator=(const CoverService&) = delete; CoverService(CoverService&&) = delete; CoverService& operator=(CoverService&&) = delete; private: std::shared_ptr getFromTrack(Database::TrackId trackId, Image::ImageSize width) override; std::shared_ptr getFromRelease(Database::ReleaseId releaseId, Image::ImageSize width) override; void flushCache() override; void setJpegQuality(unsigned quality) override; std::shared_ptr getFromTrack(Database::Session& dbSession, Database::TrackId trackId, Image::ImageSize width, bool allowReleaseFallback); std::unique_ptr getFromAvMediaFile(const Av::IAudioFile& input, Image::ImageSize width) const; std::unique_ptr getFromCoverFile(const std::filesystem::path& p, Image::ImageSize width) const; std::unique_ptr getFromTrack(const std::filesystem::path& path, Image::ImageSize width) const; std::multimap getCoverPaths(const std::filesystem::path& directoryPath) const; std::unique_ptr getFromDirectory(const std::filesystem::path& directory, Image::ImageSize width) const; std::unique_ptr getFromSameNamedFile(const std::filesystem::path& filePath, Image::ImageSize width) const; std::shared_ptr getDefault(Image::ImageSize width); bool checkCoverFile(const std::filesystem::path& directoryPath) const; Database::Db& _db; std::shared_mutex _cacheMutex; std::unordered_map> _cache; std::unordered_map> _defaultCoverCache; std::atomic _cacheMisses {}; std::atomic _cacheHits {}; std::size_t _cacheSize {}; void saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr image); std::shared_ptr loadFromCache(const CacheEntryDesc& entryDesc); const std::filesystem::path _defaultCoverPath; const std::size_t _maxCacheSize; static inline const std::vector _fileExtensions {".jpg", ".jpeg", ".png", ".bmp"}; // TODO parametrize const std::size_t _maxFileSize; static inline const std::vector _preferredFileNames {"cover", "front"}; // TODO parametrize unsigned _jpegQuality; }; } // namespace Cover