tastytea
f522c9ae45
All checks were successful
continuous-integration/drone/push Build is passing
* More efficient. * More readable. * Errors are reported to the user.
97 lines
2.4 KiB
C++
97 lines
2.4 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 <cgicc/Cgicc.h>
|
|
|
|
#include <exception>
|
|
#include <fstream>
|
|
#include <ios>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace FediBlock::cgi
|
|
{
|
|
|
|
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;
|
|
|
|
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");
|
|
|
|
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())
|
|
{
|
|
vec.push_back(element);
|
|
}
|
|
}
|
|
|
|
return vec;
|
|
}
|
|
|
|
} // namespace FediBlock::cgi
|