Add support for screenshots.
Some checks reported errors
continuous-integration/drone/push Build was killed
Some checks reported errors
continuous-integration/drone/push Build was killed
This commit is contained in:
parent
e4abfdbc0c
commit
4ce4fdd4fd
33
src/cgi.cpp
33
src/cgi.cpp
|
@ -19,8 +19,12 @@
|
||||||
#include <cgicc/Cgicc.h>
|
#include <cgicc/Cgicc.h>
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <ios>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -30,11 +34,16 @@ namespace FediBlock
|
||||||
using std::cerr;
|
using std::cerr;
|
||||||
using std::exception;
|
using std::exception;
|
||||||
using std::getline;
|
using std::getline;
|
||||||
|
using std::ios;
|
||||||
|
using std::ofstream;
|
||||||
|
using std::runtime_error;
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::string_view;
|
using std::string_view;
|
||||||
using std::stringstream;
|
using std::stringstream;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
entry_type parse_formdata()
|
entry_type parse_formdata()
|
||||||
{
|
{
|
||||||
entry_type entry;
|
entry_type entry;
|
||||||
|
@ -45,6 +54,30 @@ entry_type parse_formdata()
|
||||||
entry.tags = string_to_vector(cgi("tags"));
|
entry.tags = string_to_vector(cgi("tags"));
|
||||||
entry.receipts = string_to_vector(cgi("receipts"));
|
entry.receipts = string_to_vector(cgi("receipts"));
|
||||||
entry.description = cgi("description");
|
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)
|
catch (const exception &e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -34,6 +34,7 @@ struct entry_type
|
||||||
vector<string> tags;
|
vector<string> tags;
|
||||||
vector<string> receipts;
|
vector<string> receipts;
|
||||||
string description;
|
string description;
|
||||||
|
string screenshot_filepath;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Read form data from QUERY_STRING or stdin and return it as an object.
|
// Read form data from QUERY_STRING or stdin and return it as an object.
|
||||||
|
|
|
@ -26,7 +26,8 @@ string to_json(const entry_type &entry)
|
||||||
const nlohmann::json json{{"instance", entry.instance},
|
const nlohmann::json json{{"instance", entry.instance},
|
||||||
{"tags", entry.tags},
|
{"tags", entry.tags},
|
||||||
{"receipts", entry.receipts},
|
{"receipts", entry.receipts},
|
||||||
{"description", entry.description}};
|
{"description", entry.description},
|
||||||
|
{"screenshot", entry.screenshot_filepath}};
|
||||||
return json.dump(4);
|
return json.dump(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
src/main.cpp
13
src/main.cpp
|
@ -17,18 +17,18 @@
|
||||||
#include "cgi.hpp"
|
#include "cgi.hpp"
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
using std::cout;
|
using std::cout;
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
using namespace FediBlock;
|
using namespace FediBlock;
|
||||||
|
|
||||||
void print_debug()
|
void print_debug(const entry_type &entry)
|
||||||
{
|
{
|
||||||
cout << "\r\n\r\nDEBUG:\r\n";
|
cout << "\r\n\r\nDEBUG:\r\n";
|
||||||
|
|
||||||
const entry_type entry{parse_formdata()};
|
|
||||||
|
|
||||||
cout << " Instance: " << entry.instance << "\r\n";
|
cout << " Instance: " << entry.instance << "\r\n";
|
||||||
cout << " Tags:";
|
cout << " Tags:";
|
||||||
for (const auto &tag : entry.tags)
|
for (const auto &tag : entry.tags)
|
||||||
|
@ -43,6 +43,7 @@ void print_debug()
|
||||||
}
|
}
|
||||||
cout << "\r\n";
|
cout << "\r\n";
|
||||||
cout << " Description: " << entry.description << "\r\n";
|
cout << " Description: " << entry.description << "\r\n";
|
||||||
|
cout << " Screenshot: " << entry.screenshot_filepath << "\r\n";
|
||||||
|
|
||||||
cout << "\r\n\r\nJSON:\r\n";
|
cout << "\r\n\r\nJSON:\r\n";
|
||||||
cout << to_json(entry) << "\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 << "Content-Type: text/plain; charset=utf-8\r\n\r\n";
|
||||||
cout << "Received and filed to /dev/null. 😝\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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user