'Generic Commit'
This commit is contained in:
@@ -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);
|
||||
|
Reference in New Issue
Block a user