100 lines
2.6 KiB
C++
100 lines
2.6 KiB
C++
/* This file is part of FediBlock-backend.
|
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
|
*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "cgi.hpp"
|
|
#include "files.hpp"
|
|
#include "fs-compat.hpp"
|
|
#include "time.hpp"
|
|
|
|
#include <cgicc/Cgicc.h>
|
|
|
|
#include <chrono>
|
|
#include <fstream>
|
|
#include <ios>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
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> string_to_vector(const string_view str)
|
|
{
|
|
vector<string> vec;
|
|
|
|
stringstream input{str.data()};
|
|
string element;
|
|
|
|
while (getline(input, element, ','))
|
|
{
|
|
if (!element.empty())
|
|
{
|
|
const size_t startpos{element.find_first_not_of(' ')};
|
|
const size_t length{element.find_last_not_of(' ') - startpos + 1};
|
|
vec.push_back(element.substr(startpos, length));
|
|
}
|
|
}
|
|
|
|
return vec;
|
|
}
|
|
|
|
} // namespace FediBlock::cgi
|