This repository has been archived on 2021-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
backend/src/cgi.cpp

94 lines
2.3 KiB
C++
Raw Normal View History

2020-06-29 06:10:40 +02:00
/* 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/>.
*/
2020-06-29 05:21:23 +02:00
#include "cgi.hpp"
#include "files.hpp"
#include "fs-compat.hpp"
2020-06-29 05:21:23 +02:00
#include <cgicc/Cgicc.h>
2020-06-29 22:37:09 +02:00
#include <fstream>
#include <ios>
#include <iostream>
2020-06-29 05:21:23 +02:00
#include <sstream>
2020-06-29 22:37:09 +02:00
#include <stdexcept>
2020-06-29 05:21:23 +02:00
#include <string>
#include <vector>
2020-07-01 20:51:35 +02:00
namespace FediBlock::cgi
2020-06-29 05:21:23 +02:00
{
using std::getline;
2020-06-29 22:37:09 +02:00
using std::ios;
using std::ofstream;
using std::runtime_error;
2020-06-29 05:21:23 +02:00
using std::string;
using std::string_view;
using std::stringstream;
using std::vector;
entry_type parse_formdata()
{
2020-06-29 07:05:04 +02:00
entry_type entry;
2020-06-29 22:37:09 +02:00
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)
2020-06-29 22:37:09 +02:00
{
throw runtime_error{"Filesize too big"};
}
2020-06-29 22:37:09 +02:00
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};
2020-06-29 22:37:09 +02:00
}
screenshot->writeToStream(file);
entry.screenshot_filepath = filepath;
2020-06-29 05:21:23 +02:00
}
2020-06-29 07:05:04 +02:00
return entry;
2020-06-29 05:21:23 +02:00
}
2020-06-29 07:05:04 +02:00
vector<string> string_to_vector(const string_view str)
2020-06-29 05:21:23 +02:00
{
2020-06-29 07:05:04 +02:00
vector<string> vec;
2020-06-29 05:21:23 +02:00
stringstream input{str.data()};
string element;
while (getline(input, element, ','))
2020-06-29 05:21:23 +02:00
{
2020-06-30 07:33:15 +02:00
if (!element.empty())
{
vec.push_back(element);
}
2020-06-29 05:21:23 +02:00
}
2020-06-29 07:05:04 +02:00
return vec;
2020-06-29 05:21:23 +02:00
}
2020-07-01 20:51:35 +02:00
} // namespace FediBlock::cgi