From 76bc3b3677062c0c1a6b9fa08ff20d12e470159c Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 4 Nov 2024 02:19:09 -0500 Subject: Some old refactoring + some new refactoring --- imagestore.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 imagestore.cpp (limited to 'imagestore.cpp') diff --git a/imagestore.cpp b/imagestore.cpp new file mode 100644 index 0000000..09bef92 --- /dev/null +++ b/imagestore.cpp @@ -0,0 +1,52 @@ +#include "imagestore.h" + +#include + +#include +#include + +Magick::Image imagestore::get(std::string key, std::string url) const { + std::string filename = path_ + "/" + key; + + Magick::Image pic; + + if (std::ifstream(filename)) { + pic.read(filename); + } else { + std::ostringstream imgbuf; + curl::curl_ios imgios(imgbuf); + curl::curl_easy imghandle(imgios); + + imghandle.add(url.c_str()); + imghandle.add(30); + imghandle.add(300); + + imghandle.perform(); + + if (imghandle.get_info().get() != 200) { + throw std::runtime_error("Could not download image"); + } + + std::string content_type = + imghandle.get_info().get(); + if (content_type.substr(0, 6) != "image/") { + throw std::runtime_error("Could not download image"); + } + + std::string imgstr = imgbuf.str(); + Magick::Blob img(imgstr.c_str(), imgstr.length()); + + try { + pic.read(img); + } catch (const Magick::ErrorOption& e) { + // Occurs when the the data downloaded from the server is malformed + std::cout << "Magick: " << e.what() << std::endl; + + throw std::runtime_error("Could not download image"); + } + + pic.write(filename); + } + + return pic; +} -- cgit 1.4.1