3333#include < tbox/base/scope_exit.hpp>
3434#include < tbox/event/signal_event.h>
3535#include < tbox/event/loop.h>
36+ #include < tbox/eventx/work_thread.h>
3637#include < tbox/network/sockaddr.h>
3738#include < tbox/http/server/server.h>
3839#include < tbox/http/server/middlewares/router_middleware.h>
@@ -164,6 +165,8 @@ int main(int argc, char **argv)
164165 }
165166 );
166167
168+ tbox::eventx::WorkThread worker (sp_loop);
169+
167170 // 初始化信号处理
168171 sp_sig_event->initialize ({SIGINT, SIGTERM}, Event::Mode::kPersist );
169172 sp_sig_event->enable ();
@@ -190,9 +193,10 @@ int main(int argc, char **argv)
190193 });
191194
192195 // 添加文件上传处理
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) {
194197 // 获取表单字段
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>();
196200
197201 if (!form_data.getField (" name" , name))
198202 name = " 未提供" ;
@@ -204,27 +208,25 @@ int main(int argc, char **argv)
204208 description = " 未提供" ;
205209
206210 // 获取上传的文件
207- if (form_data.getFile (" file" , filename, file_content )) {
211+ if (form_data.getFile (" file" , filename, *sp_file_content )) {
208212 // 保存文件
209213 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+ );
228230 } else {
229231 LogNotice (" Filename is empty" );
230232 ctx->res ().status_code = StatusCode::k400_BadRequest;
0 commit comments