瀏覽代碼

mod text2zip

tomoya 1 天之前
父節點
當前提交
50b3284539
共有 1 個文件被更改,包括 4 次插入2 次删除
  1. 4 2
      backend/app/app/api/api_v1/endpoints/text2zip.py

+ 4 - 2
backend/app/app/api/api_v1/endpoints/text2zip.py

@@ -1,6 +1,6 @@
 from typing import Any, List, Optional
 from datetime import datetime
-from fastapi import APIRouter, Body, Depends, HTTPException, Form, status, Response
+from fastapi import APIRouter, Body, Depends, HTTPException, Form, status, Response, BackgroundTasks
 from fastapi.encoders import jsonable_encoder
 from pydantic.networks import EmailStr, HttpUrl
 from sqlalchemy.orm import Session
@@ -93,6 +93,7 @@ punctuation = r"[.,!?;:。 、!?,;:]"
 @router.post('/gen-zip')
 def generate_zip(
       *,
+    background_tasks: BackgroundTasks,
     current_user: models.User = Depends(deps.get_current_active_user),
     model:Optional[str], 
     texts:List[str]):
@@ -129,5 +130,6 @@ def generate_zip(
         def remove_zip():
             if os.path.exists(f'{output_dir}/{dir.name}.zip'):
                 os.remove(f'{output_dir}/{dir.name}.zip')
-    return FileResponse(f'{output_dir}/{dir.name}.zip', background=remove_zip)
+        background_tasks.add_task(remove_zip)
+    return FileResponse(f'{output_dir}/{dir.name}.zip', media_type="application/zip")