This commit is contained in:
cjw
2026-03-15 13:33:47 +08:00
parent 5a665242a3
commit 191ac77d15
7 changed files with 31 additions and 26 deletions
+5 -4
View File
@@ -2,6 +2,7 @@
import aiofiles
from pathlib import Path
from fastapi import UploadFile
from typing import Tuple
class FileHandler:
@@ -9,15 +10,15 @@ class FileHandler:
self.upload_dir = Path(upload_dir)
self.upload_dir.mkdir(exist_ok=True)
async def save_uploaded_file(self, file: UploadFile) -> Path:
async def save_uploaded_file(self, file: UploadFile) -> Tuple[Path, int]:
"""保存上传的文件"""
file_path = self.upload_dir / file.filename
content = await file.read()
async with aiofiles.open(file_path, 'wb') as f:
content = await file.read()
await f.write(content)
return file_path
return file_path, len(content)
def cleanup_file(self, file_path: Path):
"""清理文件"""
@@ -25,4 +26,4 @@ class FileHandler:
if file_path.exists():
file_path.unlink()
except Exception as e:
print(f"文件清理失败: {e}")
print(f"文件清理失败: {e}")