/* This file is part of FediBlock-backend. * Copyright © 2020 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ #include "cgi.hpp" #include "files.hpp" #include "fs-compat.hpp" #include "time.hpp" #include #include #include #include #include #include #include #include #include namespace FediBlock::cgi { 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; using std::chrono::system_clock; entry_type parse_formdata() { entry_type entry; cgicc::Cgicc cgi; entry.instance = cgi("instance"); entry.tags = string_to_vector(cgi("tags")); entry.receipts = string_to_vector(cgi("receipts")); entry.description = cgi("description"); entry.report_time = time::to_string(system_clock::now()); const auto screenshot = cgi.getFile("screenshot"); if (screenshot != cgi.getFiles().end()) { constexpr size_t size_limit{1024 * 1024 * 2}; // 2 MiB. if (screenshot->getDataLength() > size_limit) { throw runtime_error{"Filesize too big"}; } const string filepath{files::get_tmpdir() / screenshot->getFilename()}; ofstream file{filepath, ios::binary}; if (!file.good()) { throw runtime_error{"Could not open temporary file: " + filepath}; } screenshot->writeToStream(file); entry.screenshot_filepath = filepath; } return entry; } vector string_to_vector(const string_view str) { vector vec; stringstream input{str.data()}; string element; while (getline(input, element, ',')) { if (!element.empty()) { vec.push_back(element); } } return vec; } } // namespace FediBlock::cgi