mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-24 03:48:32 +02:00
Add support for cover raw images
This commit is contained in:
parent
a5f79cdf90
commit
befc8da440
12 changed files with 210 additions and 27 deletions
93
server/image_cache.hpp
Normal file
93
server/image_cache.hpp
Normal file
|
@ -0,0 +1,93 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2021 Johannes Pohl
|
||||
|
||||
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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#ifndef IMAGE_CACHE_HPP
|
||||
#define IMAGE_CACHE_HPP
|
||||
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include <boost/algorithm/hex.hpp>
|
||||
#include <boost/uuid/detail/md5.hpp>
|
||||
|
||||
|
||||
class ImageCache
|
||||
{
|
||||
public:
|
||||
ImageCache() = default;
|
||||
virtual ~ImageCache() = default;
|
||||
|
||||
std::string setImage(const std::string& key, std::string image, const std::string& extension)
|
||||
{
|
||||
if (image.empty())
|
||||
{
|
||||
clear(key);
|
||||
return "";
|
||||
}
|
||||
|
||||
using boost::uuids::detail::md5;
|
||||
md5 hash;
|
||||
md5::digest_type digest;
|
||||
hash.process_bytes(image.data(), image.size());
|
||||
hash.get_digest(digest);
|
||||
std::string filename;
|
||||
const auto intDigest = reinterpret_cast<const int*>(&digest);
|
||||
boost::algorithm::hex_lower(intDigest, intDigest + (sizeof(md5::digest_type) / sizeof(int)), std::back_inserter(filename));
|
||||
auto ext = extension;
|
||||
if (ext.find('.') == 0)
|
||||
ext = ext.substr(1);
|
||||
filename += "." + ext;
|
||||
key_to_url_[key] = filename;
|
||||
url_to_data_[filename] = image;
|
||||
return filename;
|
||||
};
|
||||
|
||||
void clear(const std::string& key)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto iter = key_to_url_.find(key);
|
||||
if (iter != key_to_url_.end())
|
||||
{
|
||||
auto url = *iter;
|
||||
auto url_iter = url_to_data_.find(url.second);
|
||||
if (url_iter != url_to_data_.end())
|
||||
url_to_data_.erase(url_iter);
|
||||
key_to_url_.erase(iter);
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> getImage(const std::string& url)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto iter = url_to_data_.find(url);
|
||||
if (iter == url_to_data_.end())
|
||||
return std::nullopt;
|
||||
else
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, std::string> key_to_url_;
|
||||
std::map<std::string, std::string> url_to_data_;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue