25 lines
685 B
Python
25 lines
685 B
Python
import os
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
import aiofiles
|
|
from fastapi import UploadFile
|
|
|
|
from app.config import settings
|
|
|
|
|
|
async def save_upload_file(file: UploadFile, sub_dir: str = "reports") -> str:
|
|
"""Save uploaded file and return the relative URL path"""
|
|
upload_path = Path(settings.upload_dir) / sub_dir
|
|
upload_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
ext = Path(file.filename).suffix if file.filename else ".jpg"
|
|
filename = f"{uuid.uuid4().hex}{ext}"
|
|
file_path = upload_path / filename
|
|
|
|
content = await file.read()
|
|
async with aiofiles.open(file_path, "wb") as f:
|
|
await f.write(content)
|
|
|
|
return f"/uploads/{sub_dir}/{filename}"
|