Files
YaoXiang/backend/app/storage/local.py
T
2026-06-15 14:50:15 +08:00

29 lines
895 B
Python

import os
import aiofiles
from app.storage.base import StorageProvider
from app.config import settings
class LocalStorageProvider(StorageProvider):
def __init__(self):
self.upload_dir = settings.UPLOAD_DIR
os.makedirs(self.upload_dir, exist_ok=True)
async def save(self, file_path: str, data: bytes) -> str:
full_path = os.path.join(self.upload_dir, file_path)
os.makedirs(os.path.dirname(full_path), exist_ok=True)
async with aiofiles.open(full_path, 'wb') as f:
await f.write(data)
return full_path
async def delete(self, file_path: str) -> bool:
full_path = os.path.join(self.upload_dir, file_path)
if os.path.exists(full_path):
os.remove(full_path)
return True
return False
async def get_url(self, file_path: str) -> str:
return f"/uploads/{file_path}"