23 lines
682 B
Python
23 lines
682 B
Python
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
from fastapi import UploadFile
|
|
|
|
from app.config import get_settings
|
|
|
|
|
|
class StorageClient:
|
|
def save_upload(self, file: UploadFile) -> str:
|
|
return self.save_bytes(file.file.read(), suffix=Path(file.filename or "upload.bin").suffix)
|
|
|
|
def save_bytes(self, raw: bytes, suffix: str = "") -> str:
|
|
settings = get_settings()
|
|
storage_dir = Path(settings.local_storage_dir)
|
|
storage_dir.mkdir(parents=True, exist_ok=True)
|
|
key = f"{uuid4().hex}{suffix}"
|
|
(storage_dir / key).write_bytes(raw)
|
|
return key
|
|
|
|
def temporary_url(self, key: str) -> str:
|
|
return f"local://{key}"
|