diff options
Diffstat (limited to 'imagestore.cpp')
-rw-r--r-- | imagestore.cpp | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/imagestore.cpp b/imagestore.cpp index 09bef92..5094569 100644 --- a/imagestore.cpp +++ b/imagestore.cpp | |||
@@ -2,10 +2,14 @@ | |||
2 | 2 | ||
3 | #include <curlcpp/curl_easy.h> | 3 | #include <curlcpp/curl_easy.h> |
4 | 4 | ||
5 | #include <chrono> | ||
5 | #include <fstream> | 6 | #include <fstream> |
6 | #include <sstream> | 7 | #include <sstream> |
8 | #include <thread> | ||
7 | 9 | ||
8 | Magick::Image imagestore::get(std::string key, std::string url) const { | 10 | Magick::Image imagestore::get(std::string key, std::string url) const { |
11 | std::lock_guard lock(mutex_); | ||
12 | |||
9 | std::string filename = path_ + "/" + key; | 13 | std::string filename = path_ + "/" + key; |
10 | 14 | ||
11 | Magick::Image pic; | 15 | Magick::Image pic; |
@@ -13,6 +17,9 @@ Magick::Image imagestore::get(std::string key, std::string url) const { | |||
13 | if (std::ifstream(filename)) { | 17 | if (std::ifstream(filename)) { |
14 | pic.read(filename); | 18 | pic.read(filename); |
15 | } else { | 19 | } else { |
20 | // This is to avoid getting rate limited. | ||
21 | std::this_thread::sleep_for(std::chrono::milliseconds(50)); | ||
22 | |||
16 | std::ostringstream imgbuf; | 23 | std::ostringstream imgbuf; |
17 | curl::curl_ios<std::ostringstream> imgios(imgbuf); | 24 | curl::curl_ios<std::ostringstream> imgios(imgbuf); |
18 | curl::curl_easy imghandle(imgios); | 25 | curl::curl_easy imghandle(imgios); |
@@ -50,3 +57,11 @@ Magick::Image imagestore::get(std::string key, std::string url) const { | |||
50 | 57 | ||
51 | return pic; | 58 | return pic; |
52 | } | 59 | } |
60 | |||
61 | void imagestore::cleanup() const { | ||
62 | std::lock_guard lock(mutex_); | ||
63 | |||
64 | for (const auto& entry : std::filesystem::directory_iterator(path_)) { | ||
65 | std::filesystem::remove_all(entry); | ||
66 | } | ||
67 | } | ||