blob: 394b8cad243e365ff73a6ceca14c7b9fef44f093 (
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 <cpr/cpr.h>
#include <functional>
#include <string>
#include <filesystem>
#include <vector>
#include <unordered_map>
#include "util.hpp"
// todo throttle requests and keep track of active requests to stop redundant requests
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);
private:
std::string GetCachedName(std::string str);
void CleanupFutures();
void RespondFromPath(std::filesystem::path path, callback_type cb);
void OnResponse(const cpr::Response &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;
};
|