修复小文件上传问题

This commit is contained in:
lnk
2026-07-17 15:40:35 +08:00
parent 439b22243a
commit c0405b5e48
4 changed files with 52 additions and 14 deletions

View File

@@ -7876,7 +7876,11 @@ bool SendFileWebAuto(const std::string& id,
<< " -> use default upload URL (cloud path=" << file_cloudpath << ")\n";
// 实际上传调用
SendFileWeb(WEB_FILEUPLOAD, local_path, file_cloudpath, out_filename,2);
if (!SendFileWeb(WEB_FILEUPLOAD, local_path, file_cloudpath, out_filename,2)) {
std::cerr << "[SendFileWebAuto][ERROR] File upload failed: "
<< local_path << std::endl;
return false;
}
std::cout << "[SendFileWebAuto] File upload complete: " << out_filename << std::endl;

View File

@@ -219,7 +219,7 @@ bool DownloadFileAPI_web(const std::string& strUrl, // 接口路径
////////////////////////////////////////////////////////////////////////////////////////////////////////上传文件接口
//处理文件上传响应
void handleUploadResponse(const std::string& response, std::string& wavepath, int type) {
bool handleUploadResponse(const std::string& response, std::string& wavepath, int type) {
using nlohmann::json; //把 nlohmann::json 这个名字带到当前作用域
@@ -231,27 +231,34 @@ void handleUploadResponse(const std::string& response, std::string& wavepath, in
catch (const json::parse_error& e) {
std::cerr << "Error parsing response: " << e.what() << std::endl;
DIY_ERRORLOG_CODE("process",0,LOG_CODE_JSON, "文件上传接口响应异常,上传文件失败");
return;
return false;
}
// 提取字段
if (!json_data.contains("code") || !json_data.contains("data")) {
std::cerr << "Error: Missing expected fields in JSON response." << std::endl;
DIY_ERRORLOG_CODE("process",0,LOG_CODE_JSON, "文件上传接口响应异常,上传文件失败");
return;
return false;
}
std::string code = json_data["code"].get<std::string>();
std::cout << "Response Code: " << code << std::endl;
std::string msg = json_data.value("msg", std::string{"not found"});
std::string msg = json_data.value("message",
json_data.value("msg", std::string{"not found"}));
std::cout << "Message: " << msg << std::endl;
if (code != "A0000") {
std::cerr << "File upload failed, response code=" << code
<< ", message=" << msg << std::endl;
return false;
}
auto& data = json_data["data"];
if (!data.contains("name") || !data.contains("fileName") || !data.contains("url")) {
std::cerr << "Error: Missing expected fields in JSON data object." << std::endl;
DIY_ERRORLOG_CODE("process",0,LOG_CODE_JSON, "文件上传接口响应异常,上传文件失败");
return;
return false;
}
std::string name = data["name"].get<std::string>();
@@ -288,20 +295,24 @@ void handleUploadResponse(const std::string& response, std::string& wavepath, in
std::cout << "wavepath: " << wavepath << std::endl;
DIY_INFOLOG_CODE("process",0,LOG_CODE_FILE, "上传文件成功,远端文件名:%s", wavepath.c_str());
return true;
}
//上传文件
void SendFileWeb(const std::string& strUrl, const std::string& localpath, const std::string& cloudpath, std::string& wavepath, int type) {
bool SendFileWeb(const std::string& strUrl, const std::string& localpath, const std::string& cloudpath, std::string& wavepath, int type) {
wavepath.clear();
// 基本存在性检查
if (access(localpath.c_str(), F_OK) != 0) {
std::cerr << "Local file does not exist: " << localpath << std::endl;
return;
return false;
}
// ★新增stat 打印大小,便于快速确认读源
struct stat st {};
if (stat(localpath.c_str(), &st) != 0) {
perror("stat");
return false;
} else {
std::cout << "[debug] upload file: " << localpath
<< ", size=" << static_cast<long long>(st.st_size) << " bytes\n";
@@ -359,6 +370,7 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply_web);
// 执行请求
bool upload_ok = false;
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
const char* em = errbuf[0] ? errbuf : curl_easy_strerror(res);
@@ -366,16 +378,18 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
DIY_ERRORLOG_CODE("process",0,LOG_CODE_CONFIG, "通过文件接口上传文件 %s 失败",localpath.c_str());
} else {
std::cout << "http web success, response: " << resPost0 << std::endl;
handleUploadResponse(resPost0, wavepath, type); // 处理响应
upload_ok = handleUploadResponse(resPost0, wavepath, type); // 处理响应
}
// 清理
curl_formfree(formpost); // 释放表单数据
//curl_slist_free_all(header_list); // 释放头部列表
curl_easy_cleanup(curl);
return upload_ok;
} else {
std::cerr << ">>> curl init failed" << std::endl;
}
return false;
}
//上传暂态文件
@@ -1851,4 +1865,4 @@ void process_qvvr_async_tasks()
<< std::endl;
}
}
}
}

View File

@@ -797,7 +797,7 @@ bool save_internal_value(const std::string &dev_id, const std::vector<ushort> &f
bool save_set_value(const std::string &dev_id, unsigned char mp_index, const std::vector<float> &fabsf);
//发送文件
void SendFileWeb(const std::string& strUrl, const std::string& localpath, const std::string& cloudpath, std::string& wavepath,int Type);
bool SendFileWeb(const std::string& strUrl, const std::string& localpath, const std::string& cloudpath, std::string& wavepath,int Type);
//状态翻转
void connect_status_update(const std::string& id, int status);
@@ -1148,4 +1148,4 @@ bool runninginfo_cache_take(const std::string& dev_id, RunningInformation& out);
void versioninfo_cache_put(const std::string& dev_id, const DeviceVersionInfo& info);
bool versioninfo_cache_take(const std::string& dev_id, DeviceVersionInfo& out);
#endif
#endif

View File

@@ -846,7 +846,16 @@ void process_received_message(string mac, string id,const char* data, size_t len
if (out_file) {
out_file.write(reinterpret_cast<const char*>(file_data.data()),
file_data.size());
std::cout << "File saved: " << file_path << std::endl;
// SendFileWebAuto reopens this path. Close first so data buffered by
// ofstream (especially for small files) is visible to stat/libcurl.
out_file.close();
if (!out_file) {
std::cerr << "Failed to write/close file: " << file_path << std::endl;
ClientManager::instance().change_device_state(id, DeviceState::IDLE);
return;
}
std::cout << "File saved: " << file_path
<< ", size=" << file_data.size() << " bytes" << std::endl;
//lnk20250805文件保存成功调用录波文件上送接口
//update_qvvr_file_download(file_path, id);
@@ -1171,7 +1180,18 @@ void process_received_message(string mac, string id,const char* data, size_t len
if (out_file) {
out_file.write(reinterpret_cast<const char*>(file_data.data()),
file_data.size());
std::cout << "File saved: " << file_path << std::endl;
// The uploader reopens this path. Close first so buffered data is
// visible to stat/libcurl; otherwise small files can upload as 0 bytes.
out_file.close();
if (!out_file) {
std::cerr << "Failed to write/close file: " << file_path << std::endl;
on_device_response_minimal(static_cast<int>(ResponseCode::INTERNAL_ERROR),
id, 0, static_cast<int>(DeviceState::READING_FILEDATA));
ClientManager::instance().change_device_state(id, DeviceState::IDLE);
return;
}
std::cout << "File saved: " << file_path
<< ", size=" << file_data.size() << " bytes" << std::endl;
//调试用
// 若是 .cfg先查看并打印内容限长