Big.
This commit is contained in:
@@ -153,11 +153,18 @@ void Server::setup_routes() {
|
||||
handle_get_directory(req, res);
|
||||
});
|
||||
|
||||
// Upload object
|
||||
server_.Put("/(.*)", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
// Upload object with streaming support
|
||||
server_.Put("/upload", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
handle_put_object(req, res);
|
||||
});
|
||||
|
||||
// Handle PUT requests to other paths
|
||||
server_.Put("/(.*)", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
res.status = 404;
|
||||
nlohmann::json response = {{"result", "error"}, {"error", "Not found - put requests must be to /upload"}};
|
||||
res.set_content(response.dump(), "application/json");
|
||||
});
|
||||
|
||||
// Get metadata for label:tag
|
||||
server_.Get("/meta/(.*)", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
handle_get_metadata(req, res);
|
||||
@@ -310,8 +317,31 @@ void Server::handle_put_object(const httplib::Request& req, httplib::Response& r
|
||||
return;
|
||||
}
|
||||
|
||||
// Write request body to temporary file
|
||||
temp_file.write(req.body.data(), req.body.size());
|
||||
// Write request body to temporary file in chunks to handle large files better
|
||||
// This improves memory usage even though the entire body is still loaded
|
||||
// by httplib - a proper streaming solution would require changes to httplib
|
||||
const size_t CHUNK_SIZE = 1024 * 1024; // 1MB chunks
|
||||
size_t remaining = req.body.size();
|
||||
size_t offset = 0;
|
||||
|
||||
while (remaining > 0) {
|
||||
size_t write_size = std::min(CHUNK_SIZE, remaining);
|
||||
if (!temp_file.write(req.body.data() + offset, write_size)) {
|
||||
res.status = 500;
|
||||
nlohmann::json response = {{"result", "error"}, {"error", "Failed to write to temporary file"}};
|
||||
res.set_content(response.dump(), "application/json");
|
||||
temp_file.close();
|
||||
std::filesystem::remove(temp_path);
|
||||
return;
|
||||
}
|
||||
|
||||
offset += write_size;
|
||||
remaining -= write_size;
|
||||
|
||||
// Periodically flush to disk
|
||||
temp_file.flush();
|
||||
}
|
||||
|
||||
temp_file.close();
|
||||
|
||||
// Ensure the temporary file is removed even if errors occur
|
||||
|
Reference in New Issue
Block a user