diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2024-11-04 02:19:09 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2024-11-04 02:19:09 -0500 |
commit | 76bc3b3677062c0c1a6b9fa08ff20d12e470159c (patch) | |
tree | 390897784acdec4f002df4a13e5d63f1f641fb40 /imagestore.cpp | |
parent | 5a65625cb589b2cb5b336e1fa5748df8dcdb8f6a (diff) | |
download | wizard-76bc3b3677062c0c1a6b9fa08ff20d12e470159c.tar.gz wizard-76bc3b3677062c0c1a6b9fa08ff20d12e470159c.tar.bz2 wizard-76bc3b3677062c0c1a6b9fa08ff20d12e470159c.zip |
Some old refactoring + some new refactoring
Diffstat (limited to 'imagestore.cpp')
-rw-r--r-- | imagestore.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/imagestore.cpp b/imagestore.cpp new file mode 100644 index 0000000..09bef92 --- /dev/null +++ b/imagestore.cpp | |||
@@ -0,0 +1,52 @@ | |||
1 | #include "imagestore.h" | ||
2 | |||
3 | #include <curlcpp/curl_easy.h> | ||
4 | |||
5 | #include <fstream> | ||
6 | #include <sstream> | ||
7 | |||
8 | Magick::Image imagestore::get(std::string key, std::string url) const { | ||
9 | std::string filename = path_ + "/" + key; | ||
10 | |||
11 | Magick::Image pic; | ||
12 | |||
13 | if (std::ifstream(filename)) { | ||
14 | pic.read(filename); | ||
15 | } else { | ||
16 | std::ostringstream imgbuf; | ||
17 | curl::curl_ios<std::ostringstream> imgios(imgbuf); | ||
18 | curl::curl_easy imghandle(imgios); | ||
19 | |||
20 | imghandle.add<CURLOPT_URL>(url.c_str()); | ||
21 | imghandle.add<CURLOPT_CONNECTTIMEOUT>(30); | ||
22 | imghandle.add<CURLOPT_TIMEOUT>(300); | ||
23 | |||
24 | imghandle.perform(); | ||
25 | |||
26 | if (imghandle.get_info<CURLINFO_RESPONSE_CODE>().get() != 200) { | ||
27 | throw std::runtime_error("Could not download image"); | ||
28 | } | ||
29 | |||
30 | std::string content_type = | ||
31 | imghandle.get_info<CURLINFO_CONTENT_TYPE>().get(); | ||
32 | if (content_type.substr(0, 6) != "image/") { | ||
33 | throw std::runtime_error("Could not download image"); | ||
34 | } | ||
35 | |||
36 | std::string imgstr = imgbuf.str(); | ||
37 | Magick::Blob img(imgstr.c_str(), imgstr.length()); | ||
38 | |||
39 | try { | ||
40 | pic.read(img); | ||
41 | } catch (const Magick::ErrorOption& e) { | ||
42 | // Occurs when the the data downloaded from the server is malformed | ||
43 | std::cout << "Magick: " << e.what() << std::endl; | ||
44 | |||
45 | throw std::runtime_error("Could not download image"); | ||
46 | } | ||
47 | |||
48 | pic.write(filename); | ||
49 | } | ||
50 | |||
51 | return pic; | ||
52 | } | ||