33
33
#include < tbox/base/scope_exit.hpp>
34
34
#include < tbox/event/signal_event.h>
35
35
#include < tbox/event/loop.h>
36
+ #include < tbox/eventx/work_thread.h>
36
37
#include < tbox/network/sockaddr.h>
37
38
#include < tbox/http/server/server.h>
38
39
#include < tbox/http/server/middlewares/router_middleware.h>
@@ -164,6 +165,8 @@ int main(int argc, char **argv)
164
165
}
165
166
);
166
167
168
+ tbox::eventx::WorkThread worker (sp_loop);
169
+
167
170
// 初始化信号处理
168
171
sp_sig_event->initialize ({SIGINT, SIGTERM}, Event::Mode::kPersist );
169
172
sp_sig_event->enable ();
@@ -190,9 +193,10 @@ int main(int argc, char **argv)
190
193
});
191
194
192
195
// 添加文件上传处理
193
- form_data.post (" /upload" , [&upload_dir] (ContextSptr ctx, const FormData& form_data, const NextFunc& next) {
196
+ form_data.post (" /upload" , [&upload_dir, &worker] (ContextSptr ctx, const FormData& form_data, const NextFunc& next) {
194
197
// 获取表单字段
195
- std::string name, email, description, filename, file_content;
198
+ std::string name, email, description, filename;
199
+ auto sp_file_content = std::make_shared<std::string>();
196
200
197
201
if (!form_data.getField (" name" , name))
198
202
name = " 未提供" ;
@@ -204,27 +208,25 @@ int main(int argc, char **argv)
204
208
description = " 未提供" ;
205
209
206
210
// 获取上传的文件
207
- if (form_data.getFile (" file" , filename, file_content )) {
211
+ if (form_data.getFile (" file" , filename, *sp_file_content )) {
208
212
// 保存文件
209
213
if (!filename.empty ()) {
210
- std::string file_path = upload_dir + " /" + filename;
211
- std::ofstream outfile (file_path, std::ios::binary);
212
-
213
- if (outfile.is_open ()) {
214
- outfile.write (file_content.c_str (), file_content.size ());
215
- outfile.close ();
216
-
217
- LogInfo (" File saved: %s" , file_path.c_str ());
218
-
219
- // 返回成功页面
220
- ctx->res ().status_code = StatusCode::k200_OK;
221
- ctx->res ().headers [" Content-Type" ] = " text/html; charset=UTF-8" ;
222
- ctx->res ().body = GenSuccessHtml (name, email, filename, description);
223
- } else {
224
- LogWarn (" Cannot create file: %s" , file_path.c_str ());
225
- ctx->res ().status_code = StatusCode::k500_InternalServerError;
226
- ctx->res ().body = " Server Error: Cannot save file" ;
227
- }
214
+ worker.execute (
215
+ [ctx, upload_dir, name, email, filename, description, sp_file_content] {
216
+ std::string file_path = upload_dir + " /" + filename;
217
+ if (tbox::util::fs::WriteBinaryToFile (file_path, *sp_file_content)) {
218
+ // 返回成功页面
219
+ ctx->res ().status_code = StatusCode::k200_OK;
220
+ ctx->res ().headers [" Content-Type" ] = " text/html; charset=UTF-8" ;
221
+ ctx->res ().body = GenSuccessHtml (name, email, filename, description);
222
+ } else {
223
+ LogWarn (" Cannot create file: %s" , file_path.c_str ());
224
+ ctx->res ().status_code = StatusCode::k500_InternalServerError;
225
+ ctx->res ().body = " Server Error: Cannot save file" ;
226
+ }
227
+ },
228
+ [ctx] { }
229
+ );
228
230
} else {
229
231
LogNotice (" Filename is empty" );
230
232
ctx->res ().status_code = StatusCode::k400_BadRequest;
0 commit comments