新增存储引擎统一文件读写
This commit is contained in:
+45
@@ -0,0 +1,45 @@
|
||||
import os
|
||||
import asyncio
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
|
||||
import settings
|
||||
|
||||
|
||||
class FileSystemStorage:
|
||||
DATA_ROOT = Path(settings.DATA_ROOT)
|
||||
STATIC_URL = settings.STATIC_URL
|
||||
NAME = "filesystem"
|
||||
|
||||
async def get_filepath(self, path):
|
||||
return self.DATA_ROOT / path.lstrip(self.STATIC_URL + '/')
|
||||
|
||||
def _save(self, filepath, file_bytes):
|
||||
with open(filepath, 'wb') as f:
|
||||
f.write(file_bytes)
|
||||
|
||||
async def save_file(self, file, file_bytes, key):
|
||||
now = datetime.datetime.now()
|
||||
path = self.DATA_ROOT / f"upload/{now.year}/{now.month}/{now.day}/"
|
||||
ext = file.filename.split('.')[-1]
|
||||
name = f'{key}.{ext}'
|
||||
if not path.exists():
|
||||
path.mkdir(parents=True)
|
||||
filepath = path / name
|
||||
await asyncio.to_thread(self._save, filepath, file_bytes)
|
||||
text = f"{self.STATIC_URL}/{filepath.relative_to(self.DATA_ROOT)}"
|
||||
return text
|
||||
|
||||
async def delete_file(self, file):
|
||||
filepath = self.DATA_ROOT / file['text'].lstrip(self.STATIC_URL + '/')
|
||||
await asyncio.to_thread(os.remove, filepath)
|
||||
|
||||
async def delete_files(self, files):
|
||||
for file in files:
|
||||
if file['type'] != 'text':
|
||||
await self.delete_file(file)
|
||||
|
||||
|
||||
STORAGE_ENGINE = {
|
||||
"filesystem": FileSystemStorage
|
||||
}
|
||||
Reference in New Issue
Block a user