优化http文件上传相关代码 (#4796)

This commit is contained in:
YuLi
2026-08-01 22:04:57 -07:00
committed by GitHub
parent 79d795a767
commit 006eece147
3 changed files with 10 additions and 4 deletions

View File

@@ -226,6 +226,8 @@ static std::map<std::string, HttpBodyHandler, StrCaseCompare> s_map_file_handler
* Register a handler for a URL path. When a PUT/POST request arrives at that path,
* the handler is invoked to create an HttpBody (e.g. HttpFileStorage) that receives
* the request body stream directly. CHECK_SECRET() can be used inside the handler.
* File publication policy belongs to the registered application: use a temporary
* storage path here and move it to the public path in the corresponding API handler.
*
* @param url_path the HTTP URL path to match
* @param handler callback that sets body; may throw AuthException to reject

View File

@@ -147,7 +147,7 @@ static std::shared_ptr<char> getSharedMmap(const string &file_path, int64_t &fil
return nullptr;
}
LARGE_INTEGER FileSize;
LARGE_INTEGER FileSize;
GetFileSizeEx(hfile, &FileSize); //GetFileSize函数的拓展,可用于获取大于4G的文件大小
file_size = FileSize.QuadPart;
@@ -202,7 +202,7 @@ HttpFileBody::HttpFileBody(const string &file_path, bool use_mmap) {
}
if (use_mmap ) {
_map_addr = getSharedMmap(file_path, _read_to);
_map_addr = getSharedMmap(file_path, _read_to);
}
if (!_map_addr && _read_to != -1) {

View File

@@ -211,11 +211,15 @@ public:
using Ptr = std::shared_ptr<HttpFileStorage>;
/**
* @param file_path 文件路径,文件以二进制写入模式打开(覆盖已有内容)
* @param file_path File path, opened in binary write mode (truncates existing content)
* @param file_path 直接以二进制覆盖写入的路径;未完成的上传会删除该路径。需要保护正式文件时,调用方应
* 传入临时路径,并在请求处理完成后自行发布到正式路径。
* @param file_path Path opened directly in binary truncate mode and removed after an incomplete upload. To protect
* a published file, pass a temporary path and publish it from the completed-request handler.
*/
HttpFileStorage(std::string file_path);
~HttpFileStorage() override;
HttpFileStorage(const HttpFileStorage &) = delete;
HttpFileStorage &operator=(const HttpFileStorage &) = delete;
void writeData(const char *data, size_t size, uint64_t content_size) override;
int64_t remainSize() override;