g_solution / app /services /storage.py
minhvtt's picture
Upload 23 files
5b977f1 verified
Raw
History Blame Contribute Delete
928 Bytes
from pathlib import Path
from uuid import uuid4
from fastapi import UploadFile
from app.core.config import settings
class StorageService:
def __init__(self, base_dir: str) -> None:
self.base_dir = Path(base_dir)
self.base_dir.mkdir(parents=True, exist_ok=True)
async def save_upload(self, file: UploadFile) -> tuple[str, str, str]:
extension = Path(file.filename or "capture.png").suffix or ".png"
image_id = str(uuid4())
stored_name = f"{image_id}{extension}"
target_path = self.base_dir / stored_name
content = await file.read()
target_path.write_bytes(content)
return image_id, str(target_path), stored_name
def delete(self, absolute_path: str) -> None:
target = Path(absolute_path)
if target.exists():
target.unlink()
storage_service = StorageService(settings.storage_dir)