Remove ImageCache instance from ServerSettings

This commit is contained in:
badaix 2024-07-01 23:29:26 +02:00
parent 2530a347c2
commit 78cbd3cf5b
6 changed files with 20 additions and 12 deletions

View file

@ -24,6 +24,7 @@
#include "common/aixlog.hpp"
#include "common/utils/file_utils.hpp"
#include "control_session_ws.hpp"
#include "image_cache.hpp"
#include "stream_session_ws.hpp"
// 3rd party headers
@ -288,7 +289,7 @@ void ControlSessionHttp::handle_request(http::request<Body, http::basic_fields<A
{
pos += image_cache_target.size();
target = target.substr(pos);
auto image = settings_.http.image_cache.getImage(std::string(target));
auto image = ImageCache::instance().getImage(std::string(target));
LOG(DEBUG, LOG_TAG) << "image cache: " << target << ", found: " << image.has_value() << "\n";
if (image.has_value())
{

View file

@ -30,12 +30,19 @@
#include <string>
/// Image cache, used to store current album art per stream
class ImageCache
{
public:
ImageCache() = default;
virtual ~ImageCache() = default;
/// @return singleton to the image cache
static ImageCache& instance()
{
static ImageCache instance_;
return instance_;
}
/// Store the base64 encoded @p image for @p key (the session that stores the image) in the cache
/// @return url of the cached image (md5 of key + image data) appended with @p extension
std::string setImage(const std::string& key, std::string image, const std::string& extension)
{
if (image.empty())
@ -51,7 +58,7 @@ public:
hash.process_bytes(image.data(), image.size());
hash.get_digest(digest);
std::string filename;
const auto intDigest = reinterpret_cast<const int*>(&digest);
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)
@ -63,6 +70,7 @@ public:
return filename;
};
/// Clear image for @p key (the stream session's name)
void clear(const std::string& key)
{
std::lock_guard<std::mutex> lock(mutex_);
@ -77,6 +85,7 @@ public:
}
}
/// @return base64 encoded image for url (the one returned by "setImage")
std::optional<std::string> getImage(const std::string& url)
{
std::lock_guard<std::mutex> lock(mutex_);
@ -88,6 +97,9 @@ public:
}
private:
ImageCache() = default;
~ImageCache() = default;
std::map<std::string, std::string> key_to_url_;
std::map<std::string, std::string> url_to_data_;
std::mutex mutex_;

View file

@ -21,7 +21,6 @@
// local headers
#include "common/utils/string_utils.hpp"
#include "image_cache.hpp"
// standard headers
#include <string>
@ -73,7 +72,6 @@ struct ServerSettings
std::vector<std::string> ssl_bind_to_address{{"0.0.0.0"}};
std::string doc_root{""};
std::string host{"<hostname>"};
inline static ImageCache image_cache;
};
struct Tcp

View file

@ -20,8 +20,6 @@
// prototype/interface header file
#include "jack_stream.hpp"
#include <jack/jack.h>
// local headers
#include "common/aixlog.hpp"
#include "common/snap_exception.hpp"
@ -59,7 +57,6 @@ void float_to_s24(char* dst, jack_default_audio_sample_t* src, unsigned long nsa
while (nsamples--)
{
// float to S24 conversion
if (*src <= -1.0f)
{

View file

@ -22,7 +22,6 @@
// local headers
#include "pcm_stream.hpp"
#include <server/server_settings.hpp>
// 3rd party headers
#include <boost/asio/io_context.hpp>

View file

@ -28,6 +28,7 @@
#include "common/utils/string_utils.hpp"
#include "control_error.hpp"
#include "encoder/encoder_factory.hpp"
#include "image_cache.hpp"
// 3rd party headers
#include <boost/asio/ip/host_name.hpp>
@ -537,7 +538,7 @@ void PcmStream::setProperties(const Properties& properties)
if (props.metadata.has_value() && props.metadata->art_data.has_value() && !props.metadata->art_url.has_value())
{
auto data = base64_decode(props.metadata->art_data->data);
auto md5 = ServerSettings::Http::image_cache.setImage(getName(), std::move(data), props.metadata->art_data->extension);
auto md5 = ImageCache::instance().setImage(getName(), std::move(data), props.metadata->art_data->extension);
std::stringstream url;
url << "http://" << server_settings_.http.host << ":" << server_settings_.http.port << "/__image_cache?name=" << md5;
@ -545,7 +546,7 @@ void PcmStream::setProperties(const Properties& properties)
}
else if (!props.metadata.has_value() || !props.metadata->art_data.has_value())
{
ServerSettings::Http::image_cache.clear(getName());
ImageCache::instance().clear(getName());
}
if (props == properties_)