首次提交by MimoCode

This commit is contained in:
tang1219
2026-06-15 14:50:15 +08:00
parent 584b31da50
commit ead13f863c
166 changed files with 14908 additions and 1 deletions
View File
+15
View File
@@ -0,0 +1,15 @@
from abc import ABC, abstractmethod
class StorageProvider(ABC):
@abstractmethod
async def save(self, file_path: str, data: bytes) -> str:
pass
@abstractmethod
async def delete(self, file_path: str) -> bool:
pass
@abstractmethod
async def get_url(self, file_path: str) -> str:
pass
+29
View File
@@ -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}"
+33
View File
@@ -0,0 +1,33 @@
from app.storage.base import StorageProvider
from app.storage.local import LocalStorageProvider
class StorageManager:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if self._initialized:
return
self.provider: StorageProvider = LocalStorageProvider()
self._initialized = True
def set_provider(self, provider: StorageProvider):
self.provider = provider
async def save(self, file_path: str, data: bytes) -> str:
return await self.provider.save(file_path, data)
async def delete(self, file_path: str) -> bool:
return await self.provider.delete(file_path)
async def get_url(self, file_path: str) -> str:
return await self.provider.get_url(file_path)
storage_manager = StorageManager()