diff options
-rw-r--r-- | imagestore.cpp | 15 | ||||
-rw-r--r-- | imagestore.h | 5 | ||||
-rw-r--r-- | server_main.cpp | 7 |
3 files changed, 25 insertions, 2 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 | } | ||
diff --git a/imagestore.h b/imagestore.h index 8c3fecf..2c3495d 100644 --- a/imagestore.h +++ b/imagestore.h | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | #include <Magick++.h> | 4 | #include <Magick++.h> |
5 | 5 | ||
6 | #include <mutex> | ||
6 | #include <string> | 7 | #include <string> |
7 | 8 | ||
8 | class imagestore { | 9 | class imagestore { |
@@ -11,8 +12,12 @@ class imagestore { | |||
11 | 12 | ||
12 | Magick::Image get(std::string key, std::string url) const; | 13 | Magick::Image get(std::string key, std::string url) const; |
13 | 14 | ||
15 | void cleanup() const; | ||
16 | |||
14 | private: | 17 | private: |
15 | std::string path_; | 18 | std::string path_; |
19 | |||
20 | mutable std::mutex mutex_; | ||
16 | }; | 21 | }; |
17 | 22 | ||
18 | #endif /* end of include guard: IMAGESTORE_H_80B1E49F */ | 23 | #endif /* end of include guard: IMAGESTORE_H_80B1E49F */ |
diff --git a/server_main.cpp b/server_main.cpp index 8d2c4aa..34cb50c 100644 --- a/server_main.cpp +++ b/server_main.cpp | |||
@@ -36,7 +36,8 @@ class server { | |||
36 | socket_.listen(port_); | 36 | socket_.listen(port_); |
37 | socket_.start_accept(); | 37 | socket_.start_accept(); |
38 | 38 | ||
39 | asio::post(std::bind(&server::cleanup_thread, this)); | 39 | std::thread cleanup(std::bind(&server::cleanup_thread, this)); |
40 | cleanup.detach(); | ||
40 | 41 | ||
41 | std::cout << "Listening on port " << port_ << "..." << std::endl; | 42 | std::cout << "Listening on port " << port_ << "..." << std::endl; |
42 | 43 | ||
@@ -145,7 +146,9 @@ class server { | |||
145 | 146 | ||
146 | void cleanup_thread() { | 147 | void cleanup_thread() { |
147 | for (;;) { | 148 | for (;;) { |
148 | // sleep | 149 | std::this_thread::sleep_for(std::chrono::hours(24)); |
150 | |||
151 | images_.cleanup(); | ||
149 | } | 152 | } |
150 | } | 153 | } |
151 | 154 | ||