summaryrefslogtreecommitdiff
path: root/filecache.hpp
blob: 0eacbbd8d5e945efb4d7ce1116ed8b06a97e9fb7 (plain)
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
#pragma once
#include <functional>
#include <string>
#include <filesystem>
#include <vector>
#include <unordered_map>
#include <future>
#include "util.hpp"
#include "http.hpp"

class Cache {
public:
    Cache();
    ~Cache();

    using callback_type = std::function<void(std::string)>;
    void GetFileFromURL(std::string url, callback_type cb);
    std::string GetPathIfCached(std::string url);
    void ClearCache();

private:
    std::string GetCachedName(std::string str);
    void CleanupFutures();
    void RespondFromPath(std::filesystem::path path, callback_type cb);
    void OnResponse(const http::response_type &r);

    std::unique_ptr<Semaphore> m_semaphore;

    std::unordered_map<std::string, std::vector<callback_type>> m_callbacks;
    std::vector<std::future<void>> m_futures;
    std::filesystem::path m_tmp_path;

    bool m_canceled = false;
};