From 4ce4fdd4fdf1548081ec175e8d5066587f3c29f6 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 29 Jun 2020 22:37:09 +0200 Subject: [PATCH] Add support for screenshots. --- src/cgi.cpp | 33 +++++++++++++++++++++++++++++++++ src/cgi.hpp | 1 + src/json.cpp | 3 ++- src/main.cpp | 13 +++++++++---- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/cgi.cpp b/src/cgi.cpp index 5596bb6..c8091f0 100644 --- a/src/cgi.cpp +++ b/src/cgi.cpp @@ -19,8 +19,12 @@ #include #include +#include +#include +#include #include #include +#include #include #include @@ -30,11 +34,16 @@ namespace FediBlock using std::cerr; using std::exception; using std::getline; +using std::ios; +using std::ofstream; +using std::runtime_error; using std::string; using std::string_view; using std::stringstream; using std::vector; +namespace fs = std::filesystem; + entry_type parse_formdata() { entry_type entry; @@ -45,6 +54,30 @@ entry_type parse_formdata() entry.tags = string_to_vector(cgi("tags")); entry.receipts = string_to_vector(cgi("receipts")); entry.description = cgi("description"); + + const auto screenshot = cgi.getFile("screenshot"); + if (screenshot != cgi.getFiles().end()) + { + string filepath{fs::temp_directory_path() + / "fediblock-backend-XXXXXX"}; + if (mkstemp(&filepath[0]) == -1) // mkstemp() modifies filepath. + { + throw runtime_error{"Could not open temporary file: " + + filepath}; + } + + ofstream file{filepath, ios::binary}; + if (file.good()) + { + screenshot->writeToStream(file); + entry.screenshot_filepath = filepath; + } + else + { + throw runtime_error{"Could not open temporary file: " + + filepath}; + } + } } catch (const exception &e) { diff --git a/src/cgi.hpp b/src/cgi.hpp index ed48583..ea35efc 100644 --- a/src/cgi.hpp +++ b/src/cgi.hpp @@ -34,6 +34,7 @@ struct entry_type vector tags; vector receipts; string description; + string screenshot_filepath; }; // Read form data from QUERY_STRING or stdin and return it as an object. diff --git a/src/json.cpp b/src/json.cpp index adf1d22..407370d 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -26,7 +26,8 @@ string to_json(const entry_type &entry) const nlohmann::json json{{"instance", entry.instance}, {"tags", entry.tags}, {"receipts", entry.receipts}, - {"description", entry.description}}; + {"description", entry.description}, + {"screenshot", entry.screenshot_filepath}}; return json.dump(4); } diff --git a/src/main.cpp b/src/main.cpp index 6263aa3..0052fe8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,18 +17,18 @@ #include "cgi.hpp" #include "json.hpp" +#include #include using std::cout; +namespace fs = std::filesystem; using namespace FediBlock; -void print_debug() +void print_debug(const entry_type &entry) { cout << "\r\n\r\nDEBUG:\r\n"; - const entry_type entry{parse_formdata()}; - cout << " Instance: " << entry.instance << "\r\n"; cout << " Tags:"; for (const auto &tag : entry.tags) @@ -43,6 +43,7 @@ void print_debug() } cout << "\r\n"; cout << " Description: " << entry.description << "\r\n"; + cout << " Screenshot: " << entry.screenshot_filepath << "\r\n"; cout << "\r\n\r\nJSON:\r\n"; cout << to_json(entry) << "\r\n"; @@ -53,7 +54,11 @@ int main() cout << "Content-Type: text/plain; charset=utf-8\r\n\r\n"; cout << "Received and filed to /dev/null. 😝\r\n"; - print_debug(); + const entry_type entry{parse_formdata()}; + + print_debug(entry); + + fs::remove(entry.screenshot_filepath); return 0; }