File: image_cache.hpp

package info (click to toggle)
snapcast 0.31.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: cpp: 37,729; python: 2,543; sh: 455; makefile: 16
file content (106 lines) | stat: -rw-r--r-- 3,390 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/***
    This file is part of snapcast
    Copyright (C) 2014-2024  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/>.
***/

#pragma once


// 3rd party headers
#include <boost/algorithm/hex.hpp>
#include <boost/uuid/detail/md5.hpp>

// standard headers
#include <map>
#include <mutex>
#include <optional>
#include <string>


/// Image cache, used to store current album art per stream
class ImageCache
{
public:
    /// @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())
        {
            clear(key);
            return "";
        }

        using boost::uuids::detail::md5;
        md5 hash;
        md5::digest_type digest;
        hash.process_bytes(key.data(), key.size());
        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;
        std::lock_guard<std::mutex> lock(mutex_);
        key_to_url_[key] = filename;
        url_to_data_[filename] = std::move(image);
        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_);
        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);
        }
    }

    /// @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_);
        auto iter = url_to_data_.find(url);
        if (iter == url_to_data_.end())
            return std::nullopt;
        else
            return iter->second;
    }

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_;
};