'Generic Commit'
All checks were successful
Build-Test-Publish / build (linux/amd64) (push) Successful in 1m21s
Build-Test-Publish / build (linux/arm64) (push) Successful in 2m20s
Build-Test-Publish / create-manifest (push) Successful in 17s

This commit is contained in:
Your Name
2025-06-15 16:32:29 +12:00
parent eedd39a533
commit a4d9e3ebb5
6 changed files with 56 additions and 26 deletions

View File

@@ -46,6 +46,25 @@ void PutHandler::handle_put_object(const drogon::HttpRequestPtr& req, std::funct
return;
}
// Check content length first
auto contentLengthHeader = req->getHeader("content-length");
if (!contentLengthHeader.empty()) {
try {
size_t contentLength = std::stoull(contentLengthHeader);
const size_t MAX_UPLOAD_SIZE = 2ULL * 1024 * 1024 * 1024; // 2GB
if (contentLength > MAX_UPLOAD_SIZE) {
resp->setStatusCode(drogon::k413RequestEntityTooLarge);
nlohmann::json response = {{"result", "error"}, {"error", "File too large"}};
resp->setBody(response.dump());
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
callback(resp);
return;
}
} catch (const std::exception&) {
// Invalid content-length header, continue with parsing
}
}
// Parse the multipart form data
drogon::MultiPartParser fileParser;
if (fileParser.parse(req) != 0) {
@@ -169,20 +188,20 @@ void PutHandler::handle_put_object(const drogon::HttpRequestPtr& req, std::funct
return;
}
// Write file content to temporary file
auto fileContent = fileData->fileContent();
if (!temp_file.write(fileContent.data(), fileContent.size())) {
temp_file.close();
// Use Drogon's built-in file saving for large files
try {
fileData->saveAs(temp_path.string());
} catch (const std::exception& e) {
resp->setStatusCode(drogon::k500InternalServerError);
nlohmann::json response = {{"result", "error"}, {"error", "Failed to write to temporary file"}};
nlohmann::json response = {{"result", "error"}, {"error", "Failed to write to temporary file: " + std::string(e.what())}};
resp->setBody(response.dump());
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
temp_file.close();
std::filesystem::remove(temp_path);
callback(resp);
return;
}
temp_file.close();
// Ensure the temporary file is removed even if errors occur
ScopeFileDeleter temp_file_deleter(temp_path);