From c69db9d98ef47b7355b3a80bf7f8d87bb9b14562 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 16 Sep 2022 23:31:38 +0200 Subject: [PATCH] add resource download --- Utils/WebUtils.h | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Utils/WebUtils.h diff --git a/Utils/WebUtils.h b/Utils/WebUtils.h new file mode 100644 index 0000000..e531626 --- /dev/null +++ b/Utils/WebUtils.h @@ -0,0 +1,63 @@ +/** + * Karaka + * + * @package Utils + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef UTILS_WEB_UTILS_H +#define UTILS_WEB_UTILS_H + +#include +#include + +#include + +#include "FileUtils.h" + +namespace Utils { + class WebUtils { + private: + static + int write_download_data (void *ptr, size_t size, size_t nmeb, void *stream) + { + Utils::FileUtils::file_body *out = (Utils::FileUtils::file_body *) stream; + int outSize = size * nmeb; + + out->content = (char *) malloc(outSize + 1); + if (!out->content) { + fprintf(stderr, "CRITICAL: malloc failed"); + } + + if (out->content) { + memcpy(out->content, ptr, outSize); + + out->size = outSize; + out->content[out->size] = 0; + } + + return out->size; + } + + public: + static + Utils::FileUtils::file_body download (char *url) + { + file_body page = {0}; + + CURL *h = curl_easy_init(); + curl_easy_setopt(h, CURLOPT_URL, url); + + curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, write_download_data); + curl_easy_setopt(h, CURLOPT_WRITEDATA, &page); + curl_easy_perform(h); + curl_easy_cleanup(h); + + return page; + } + }; +} + +#endif \ No newline at end of file