init
This commit is contained in:
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user