首次提交by MimoCode
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
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}"
|
||||
Reference in New Issue
Block a user