File Upload Base implementation

This commit is contained in:
Tina_Azure
2023-06-12 22:33:28 +02:00
parent 62a8471fc1
commit d322905b35
3 changed files with 80 additions and 0 deletions

View File

@ -1046,6 +1046,63 @@ int main(int argc, char *argv[]) {
});
CROW_ROUTE(app, "/fileuploadtest")
([&, configuration](const crow::request& getRequest ) {
auto& cookieCtx = app.get_context<crow::CookieParser>(getRequest);
crow::mustache::context ctx;
if (Utilities::checkCookieLoginState(configuration, cookieCtx)) {
ctx[MUSTACHE_COOKIE_LOGGED_IN] = true;
}
auto page = crow::mustache::load("TEST_UPLOAD.html");
return page.render(ctx);
});
CROW_ROUTE(app, "/fileuploadtestExecution")
.methods(crow::HTTPMethod::Post)([](const crow::request& postRequest) {
crow::multipart::message multipartMessage(postRequest);
for (const auto& part : multipartMessage.part_map)
{
const auto& mustacheSubmissionNameValue = part.first;
const auto& fileSubmission = part.second;
if (MUSTACHE_FREELANCER_SUBMISSION_NAME == mustacheSubmissionNameValue)
{
auto contentDispositionIt = fileSubmission.headers.find("Content-Disposition");
if (contentDispositionIt == fileSubmission.headers.end())
{
//ERROR no content disposition found
return crow::response(400);
}
auto filenameIt = contentDispositionIt->second.params.find("filename");
if (filenameIt == contentDispositionIt->second.params.end())
{
//ERROR no filename found
return crow::response(400);
}
const std::string outputFilename = filenameIt->second;
std::ofstream outputFileStream(outputFilename);
if (!outputFileStream)
{
//ERROR Write to file failed
}
outputFileStream << fileSubmission.body;
outputFileStream.close();
}
else
{
//ERROR Mustache Submission Name can not be found
}
}
return crow::response(200);
});

View File

@ -86,6 +86,7 @@ namespace TemplateConstCollection {
const static std::string MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_VIEW = "OPERATION_VIEW";
const static std::string MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_EDIT = "OPERATION_EDIT";
const static std::string MUSTACHE_FREELANCER_TEMPLATE_OPERATION_FULFILMENT_DELETE = "OPERATION_DELETE";
const static std::string MUSTACHE_FREELANCER_SUBMISSION_NAME = "FILE_SUBMISSION";
//Cookie names
const static std::string COOKIE_LOGIN_KEY = "loginKey";