Add support for screenshots.
continuous-integration/drone/push Build was killed Details

This commit is contained in:
tastytea 2020-06-29 22:37:09 +02:00
parent e4abfdbc0c
commit 4ce4fdd4fd
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 45 additions and 5 deletions

View File

@ -19,8 +19,12 @@
#include <cgicc/Cgicc.h>
#include <exception>
#include <filesystem>
#include <fstream>
#include <ios>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
@ -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)
{

View File

@ -34,6 +34,7 @@ struct entry_type
vector<string> tags;
vector<string> receipts;
string description;
string screenshot_filepath;
};
// Read form data from QUERY_STRING or stdin and return it as an object.

View File

@ -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);
}

View File

@ -17,18 +17,18 @@
#include "cgi.hpp"
#include "json.hpp"
#include <filesystem>
#include <iostream>
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;
}