update:存储改成单例模式,修复读取问题
This commit is contained in:
+3
-1
@@ -11,7 +11,7 @@ from apps.admin.pydantics import IDData
|
|||||||
from apps.base.models import FileCodes, KeyValue
|
from apps.base.models import FileCodes, KeyValue
|
||||||
from core.response import APIResponse
|
from core.response import APIResponse
|
||||||
from core.settings import settings
|
from core.settings import settings
|
||||||
from core.storage import file_storage
|
from core.storage import FileStorageInterface, storages
|
||||||
|
|
||||||
admin_api = APIRouter(
|
admin_api = APIRouter(
|
||||||
prefix='/admin',
|
prefix='/admin',
|
||||||
@@ -26,6 +26,7 @@ async def login():
|
|||||||
|
|
||||||
@admin_api.delete('/file/delete', dependencies=[Depends(admin_required)])
|
@admin_api.delete('/file/delete', dependencies=[Depends(admin_required)])
|
||||||
async def file_delete(data: IDData):
|
async def file_delete(data: IDData):
|
||||||
|
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
||||||
file_code = await FileCodes.get(id=data.id)
|
file_code = await FileCodes.get(id=data.id)
|
||||||
await file_storage.delete_file(file_code)
|
await file_storage.delete_file(file_code)
|
||||||
await file_code.delete()
|
await file_code.delete()
|
||||||
@@ -79,6 +80,7 @@ async def get_file_by_id(id):
|
|||||||
|
|
||||||
@admin_api.get('/file/download', dependencies=[Depends(admin_required)])
|
@admin_api.get('/file/download', dependencies=[Depends(admin_required)])
|
||||||
async def file_download(id: int):
|
async def file_download(id: int):
|
||||||
|
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
||||||
has, file_code = await get_file_by_id(id)
|
has, file_code = await get_file_by_id(id)
|
||||||
# 检查文件是否存在
|
# 检查文件是否存在
|
||||||
if not has:
|
if not has:
|
||||||
|
|||||||
+5
-1
@@ -10,7 +10,7 @@ from apps.base.pydantics import SelectFileModel
|
|||||||
from apps.base.utils import get_expire_info, get_file_path_name, error_ip_limit, upload_ip_limit
|
from apps.base.utils import get_expire_info, get_file_path_name, error_ip_limit, upload_ip_limit
|
||||||
from core.response import APIResponse
|
from core.response import APIResponse
|
||||||
from core.settings import settings
|
from core.settings import settings
|
||||||
from core.storage import file_storage
|
from core.storage import storages, FileStorageInterface
|
||||||
from core.utils import get_select_token
|
from core.utils import get_select_token
|
||||||
|
|
||||||
# 创建一个API路由
|
# 创建一个API路由
|
||||||
@@ -56,6 +56,7 @@ async def share_file(expire_value: int = Form(default=1, gt=0), expire_style: st
|
|||||||
# 获取文件路径和名称
|
# 获取文件路径和名称
|
||||||
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(file)
|
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(file)
|
||||||
# 保存文件
|
# 保存文件
|
||||||
|
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
||||||
await file_storage.save_file(file, save_path)
|
await file_storage.save_file(file, save_path)
|
||||||
# 创建一个新的FileCodes实例
|
# 创建一个新的FileCodes实例
|
||||||
await FileCodes.create(
|
await FileCodes.create(
|
||||||
@@ -94,6 +95,7 @@ async def get_code_file_by_code(code, check=True):
|
|||||||
# 获取文件的API
|
# 获取文件的API
|
||||||
@share_api.get('/select/')
|
@share_api.get('/select/')
|
||||||
async def get_code_file(code: str, ip: str = Depends(error_ip_limit)):
|
async def get_code_file(code: str, ip: str = Depends(error_ip_limit)):
|
||||||
|
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
||||||
# 获取文件
|
# 获取文件
|
||||||
has, file_code = await get_code_file_by_code(code)
|
has, file_code = await get_code_file_by_code(code)
|
||||||
# 检查文件是否存在
|
# 检查文件是否存在
|
||||||
@@ -115,6 +117,7 @@ async def get_code_file(code: str, ip: str = Depends(error_ip_limit)):
|
|||||||
# 选择文件的API
|
# 选择文件的API
|
||||||
@share_api.post('/select/')
|
@share_api.post('/select/')
|
||||||
async def select_file(data: SelectFileModel, ip: str = Depends(error_ip_limit)):
|
async def select_file(data: SelectFileModel, ip: str = Depends(error_ip_limit)):
|
||||||
|
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
||||||
# 获取文件
|
# 获取文件
|
||||||
has, file_code = await get_code_file_by_code(data.code)
|
has, file_code = await get_code_file_by_code(data.code)
|
||||||
# 检查文件是否存在
|
# 检查文件是否存在
|
||||||
@@ -141,6 +144,7 @@ async def select_file(data: SelectFileModel, ip: str = Depends(error_ip_limit)):
|
|||||||
# 下载文件的API
|
# 下载文件的API
|
||||||
@share_api.get('/download')
|
@share_api.get('/download')
|
||||||
async def download_file(key: str, code: str, ip: str = Depends(error_ip_limit)):
|
async def download_file(key: str, code: str, ip: str = Depends(error_ip_limit)):
|
||||||
|
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
||||||
# 检查token是否有效
|
# 检查token是否有效
|
||||||
is_valid = await get_select_token(code) == key
|
is_valid = await get_select_token(code) == key
|
||||||
if not is_valid:
|
if not is_valid:
|
||||||
|
|||||||
+9
-3
@@ -2,6 +2,8 @@
|
|||||||
# @Author : Lan
|
# @Author : Lan
|
||||||
# @File : storage.py
|
# @File : storage.py
|
||||||
# @Software: PyCharm
|
# @Software: PyCharm
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import asyncio
|
import asyncio
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -20,6 +22,12 @@ from fastapi.responses import FileResponse
|
|||||||
|
|
||||||
|
|
||||||
class FileStorageInterface:
|
class FileStorageInterface:
|
||||||
|
_instance: Optional['FileStorageInterface'] = None
|
||||||
|
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
if cls._instance is None:
|
||||||
|
cls._instance = super(FileStorageInterface, cls).__new__(cls, *args, **kwargs)
|
||||||
|
return cls._instance
|
||||||
|
|
||||||
async def save_file(self, file: UploadFile, save_path: str):
|
async def save_file(self, file: UploadFile, save_path: str):
|
||||||
"""
|
"""
|
||||||
@@ -79,7 +87,7 @@ class SystemFileStorage(FileStorageInterface):
|
|||||||
return await get_file_url(file_code.code)
|
return await get_file_url(file_code.code)
|
||||||
|
|
||||||
async def get_file_response(self, file_code: FileCodes):
|
async def get_file_response(self, file_code: FileCodes):
|
||||||
file_path = file_storage.root_path / await file_code.get_file_path()
|
file_path = self.root_path / await file_code.get_file_path()
|
||||||
if not file_path.exists():
|
if not file_path.exists():
|
||||||
return APIResponse(code=404, detail='文件已过期删除')
|
return APIResponse(code=404, detail='文件已过期删除')
|
||||||
return FileResponse(file_path, filename=file_code.prefix + file_code.suffix)
|
return FileResponse(file_path, filename=file_code.prefix + file_code.suffix)
|
||||||
@@ -288,5 +296,3 @@ storages = {
|
|||||||
'onedrive': OneDriveFileStorage,
|
'onedrive': OneDriveFileStorage,
|
||||||
'opendal': OpenDALFileStorage,
|
'opendal': OpenDALFileStorage,
|
||||||
}
|
}
|
||||||
|
|
||||||
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
|
||||||
|
|||||||
+3
-1
@@ -8,11 +8,13 @@ from tortoise.expressions import Q
|
|||||||
|
|
||||||
from apps.base.models import FileCodes
|
from apps.base.models import FileCodes
|
||||||
from apps.base.utils import error_ip_limit, upload_ip_limit
|
from apps.base.utils import error_ip_limit, upload_ip_limit
|
||||||
from core.storage import file_storage
|
from core.settings import settings
|
||||||
|
from core.storage import FileStorageInterface, storages
|
||||||
from core.utils import get_now
|
from core.utils import get_now
|
||||||
|
|
||||||
|
|
||||||
async def delete_expire_files():
|
async def delete_expire_files():
|
||||||
|
file_storage: FileStorageInterface = storages[settings.file_storage]()
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
await error_ip_limit.remove_expired_ip()
|
await error_ip_limit.remove_expired_ip()
|
||||||
|
|||||||
Reference in New Issue
Block a user