14using namespace std::filesystem;
27 const auto designName = path(filename).stem().string();
39 CROW_LOG_INFO <<
"Invalidated cached design \"" << designName <<
"\" for re-parse of the replaced archive";
44 CROW_LOG_INFO <<
"Auto-warm load kicked for uploaded design \"" << designName <<
"\"";
48 CROW_LOG_DEBUG <<
"Auto-warm for \"" << designName <<
"\" skipped: a load is already queued or in progress";
51 catch (
const std::exception& e)
53 CROW_LOG_ERROR <<
"Auto-warm load failed to start for \"" << designName <<
"\": " << e.what();
68 .methods(crow::HTTPMethod::POST)
69 ([&](
const crow::request& req, std::string filename)
73 if (authResp.code != crow::status::OK)
78 const auto& contentType = req.get_header_value(CONTENT_TYPE_HEADER_NAME);
79 if (contentType != CONTENT_TYPE_APPLICATION_OCTET_STREAM)
81 return crow::response(crow::status::BAD_REQUEST, std::string(
"unsupported content type: this endpoint only accepts '") + CONTENT_TYPE_APPLICATION_OCTET_STREAM +
"'");
84 return handleOctetStreamUpload(filename, req);
88 .methods(crow::HTTPMethod::POST)
89 ([&](
const crow::request& req)
93 if (authResp.code != crow::status::OK)
98 const auto& contentType = req.get_header_value(CONTENT_TYPE_HEADER_NAME);
99 if (contentType.find(CONTENT_TYPE_MULTIPART_FORM_DATA) != 0)
102 return crow::response(crow::status::BAD_REQUEST, std::string(
"unsupported content type: this endpoint only accepts '") + CONTENT_TYPE_MULTIPART_FORM_DATA +
"'");
105 return handleMultipartFormUpload(req);
109 .methods(crow::HTTPMethod::GET)
110 ([&](
const crow::request& req)
114 if (authResp.code != crow::status::OK)
147 crow::response FileUploadController::handleOctetStreamUpload(
const std::string& filename,
const crow::request& req)
155 if (!safeName.has_value())
157 return crow::response(crow::status::BAD_REQUEST,
"invalid filename");
160 std::string tempName;
163 return crow::response(crow::status::INTERNAL_SERVER_ERROR,
"failed to generate temp file name");
165 const auto tempPath = temp_directory_path() / tempName;
166 std::ofstream outfile(tempPath, std::ofstream::binary);
169 return crow::response(crow::status::INTERNAL_SERVER_ERROR,
"failed handling new file");
177 std::error_code removeEc;
178 remove(tempPath, removeEc);
179 return crow::response(crow::status::INTERNAL_SERVER_ERROR,
"failed handling new file");
183 finalPath /= safeName.value();
187 if (ec == std::errc::file_exists)
189 std::error_code removeEc;
190 remove(tempPath, removeEc);
191 return crow::response(crow::status::CONFLICT,
"a design with that filename already exists");
197 std::error_code removeEc;
198 remove(tempPath, removeEc);
199 return crow::response(crow::status::INTERNAL_SERVER_ERROR,
"failed handling new file");
207 crow::response FileUploadController::handleMultipartFormUpload(
const crow::request& req)
209 crow::multipart::message file_message(req);
221 const crow::multipart::part* pPart;
222 std::string safeName;
225 std::vector<ValidatedPart> validatedParts;
227 for (
const auto& part : file_message.part_map)
229 const auto& part_name = part.first;
230 const auto& part_value = part.second;
232 CROW_LOG_DEBUG <<
"Part: " << part_name;
233 if (MULTIPART_FORM_DATA_PART_NAME != part_name)
236 CROW_LOG_DEBUG <<
" Value: " << part_value.body <<
'\n';
238 <<
"multipart/form-data POST failed! Part name was: [" << part_name
239 <<
"], which is not supported. Part name should be [" << MULTIPART_FORM_DATA_PART_NAME
245 auto headers_it = part_value.headers.find(CONTENT_DISPOSITION_HEADER_NAME);
246 if (headers_it == part_value.headers.end())
248 CROW_LOG_ERROR <<
"No " << CONTENT_DISPOSITION_HEADER_NAME <<
" found";
249 return crow::response(crow::status::BAD_REQUEST);
251 auto params_it = headers_it->second.params.find(MULTIPART_FORM_DATA_PART_FILENAME);
252 if (params_it == headers_it->second.params.end())
254 CROW_LOG_ERROR <<
"Part with name \"" <<
"MULTIPART_FORMDATA_PART_NAME" <<
"\" should have a \"" <<
"MULTIPART_FORMDATA_PART_FILENAME" <<
"\" parameter";
255 return crow::response(crow::status::BAD_REQUEST);
257 const std::string outfile_name = params_it->second;
259 for (
const auto& part_header : part_value.headers)
261 const auto& part_header_name = part_header.first;
262 const auto& part_header_val = part_header.second;
263 CROW_LOG_DEBUG <<
"Header: " << part_header_name <<
'=' << part_header_val.value;
264 for (
const auto& param : part_header_val.params)
266 const auto& param_key = param.first;
267 const auto& param_val = param.second;
268 CROW_LOG_DEBUG <<
" Param: " << param_key <<
',' << param_val;
275 auto safeName = sanitizeFilename(outfile_name);
276 if (!safeName.has_value())
278 return crow::response(crow::status::BAD_REQUEST,
"invalid filename");
282 finalPath /= safeName.value();
284 std::error_code existEc;
285 if (exists(finalPath, existEc))
287 return crow::response(crow::status::CONFLICT,
"a design with that filename already exists");
290 validatedParts.push_back({ &part_value, safeName.value(), std::move(finalPath) });
296 std::vector<std::string> committedNames;
297 const auto rollbackCommitted = [&]()
299 for (
const auto& name : committedNames)
302 committedPath /= name;
303 std::error_code removeEc;
304 remove(committedPath, removeEc);
309 CROW_LOG_ERROR <<
"rollback could not remove committed upload ["
310 << committedPath.string() <<
"]: " << removeEc.message();
317 for (
const auto& validated : validatedParts)
326 std::string tempName;
329 CROW_LOG_ERROR <<
" Failed to generate temp file name\n";
331 return crow::response(crow::status::INTERNAL_SERVER_ERROR,
"failed handling new file");
333 const auto tempPath = temp_directory_path() / tempName;
334 std::ofstream out_file(tempPath, std::ofstream::binary);
337 CROW_LOG_ERROR <<
" Write to file failed\n";
339 return crow::response(crow::status::INTERNAL_SERVER_ERROR,
"failed handling new file");
341 out_file << validated.pPart->body;
345 CROW_LOG_ERROR <<
" Write to file failed (write/close)\n";
346 std::error_code removeEc;
347 remove(tempPath, removeEc);
349 return crow::response(crow::status::INTERNAL_SERVER_ERROR,
"failed handling new file");
354 if (ec == std::errc::file_exists)
356 std::error_code removeEc;
357 remove(tempPath, removeEc);
359 return crow::response(crow::status::CONFLICT,
"a design with that filename already exists");
363 std::error_code removeEc;
364 remove(tempPath, removeEc);
366 return crow::response(crow::status::INTERNAL_SERVER_ERROR,
"failed handling new file");
370 committedNames.push_back(validated.safeName);
372 CROW_LOG_INFO <<
" Contents written to " << validated.safeName <<
'\n';
378 std::optional<std::string> FileUploadController::sanitizeFilename(
const std::string& filename)
const
390 std::string printable;
391 printable.reserve(filename.size());
392 for (
const auto c : filename)
394 const auto uc =
static_cast<unsigned char>(c);
395 if (uc < 0x20 || uc == 0x7F)
398 const char hex[] =
"0123456789ABCDEF";
399 printable += hex[uc >> 4];
400 printable += hex[uc & 0x0F];
407 CROW_LOG_ERROR <<
"Rejected unsafe upload filename: [" << printable <<
"]";
void register_routes() override
FileUploadController(Odb::Lib::App::IOdbServerApp &serverApp)
void InvalidateDesign(const std::string &designName)
bool LoadDesignAsync(const std::string &designName)
LoadState GetLoadState(const std::string &designName) const
virtual const OdbDesignArgs & args() const =0
virtual DesignCache & designs()=0
virtual RequestAuthenticationBase & request_auth()=0
virtual CrowApp & crow_app()=0
std::string designsDir() const
virtual crow::response AuthenticateRequest(const crow::request &req)
IOdbServerApp & m_serverApp
crow::response makeLoadedFileModelsResponse(bool) const
static std::string decode(const std::string &encoded)
void fastmove_file(const path &source, const path &dest, bool overwriteExisting, std::error_code &ec)
bool is_safe_filename_component(const std::string &name)