Skip to content

Commit

Permalink
update:存储改成单例模式,修复读取问题
Browse files Browse the repository at this point in the history
  • Loading branch information
vastsa committed Jun 18, 2024
1 parent 8afae1f commit e421ab9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
4 changes: 3 additions & 1 deletion apps/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from apps.base.models import FileCodes, KeyValue
from core.response import APIResponse
from core.settings import settings
from core.storage import file_storage
from core.storage import FileStorageInterface, storages

admin_api = APIRouter(
prefix='/admin',
Expand All @@ -26,6 +26,7 @@ async def login():

@admin_api.delete('/file/delete', dependencies=[Depends(admin_required)])
async def file_delete(data: IDData):
file_storage: FileStorageInterface = storages[settings.file_storage]()
file_code = await FileCodes.get(id=data.id)
await file_storage.delete_file(file_code)
await file_code.delete()
Expand Down Expand Up @@ -79,6 +80,7 @@ async def get_file_by_id(id):

@admin_api.get('/file/download', dependencies=[Depends(admin_required)])
async def file_download(id: int):
file_storage: FileStorageInterface = storages[settings.file_storage]()
has, file_code = await get_file_by_id(id)
# 检查文件是否存在
if not has:
Expand Down
6 changes: 5 additions & 1 deletion apps/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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.settings import settings
from core.storage import file_storage
from core.storage import storages, FileStorageInterface
from core.utils import get_select_token

# 创建一个API路由
Expand Down Expand Up @@ -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)
# 保存文件
file_storage: FileStorageInterface = storages[settings.file_storage]()
await file_storage.save_file(file, save_path)
# 创建一个新的FileCodes实例
await FileCodes.create(
Expand Down Expand Up @@ -94,6 +95,7 @@ async def get_code_file_by_code(code, check=True):
# 获取文件的API
@share_api.get('/select/')
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)
# 检查文件是否存在
Expand All @@ -115,6 +117,7 @@ async def get_code_file(code: str, ip: str = Depends(error_ip_limit)):
# 选择文件的API
@share_api.post('/select/')
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)
# 检查文件是否存在
Expand All @@ -141,6 +144,7 @@ async def select_file(data: SelectFileModel, ip: str = Depends(error_ip_limit)):
# 下载文件的API
@share_api.get('/download')
async def download_file(key: str, code: str, ip: str = Depends(error_ip_limit)):
file_storage: FileStorageInterface = storages[settings.file_storage]()
# 检查token是否有效
is_valid = await get_select_token(code) == key
if not is_valid:
Expand Down
12 changes: 9 additions & 3 deletions core/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# @Author : Lan
# @File : storage.py
# @Software: PyCharm
from typing import Optional

import aiohttp
import asyncio
from pathlib import Path
Expand All @@ -20,6 +22,12 @@


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):
"""
Expand Down Expand Up @@ -79,7 +87,7 @@ async def get_file_url(self, file_code: FileCodes):
return await get_file_url(file_code.code)

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():
return APIResponse(code=404, detail='文件已过期删除')
return FileResponse(file_path, filename=file_code.prefix + file_code.suffix)
Expand Down Expand Up @@ -288,5 +296,3 @@ async def get_file_response(self, file_code: FileCodes):
'onedrive': OneDriveFileStorage,
'opendal': OpenDALFileStorage,
}

file_storage: FileStorageInterface = storages[settings.file_storage]()
4 changes: 3 additions & 1 deletion core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

from apps.base.models import FileCodes
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


async def delete_expire_files():
file_storage: FileStorageInterface = storages[settings.file_storage]()
while True:
try:
await error_ip_limit.remove_expired_ip()
Expand Down

0 comments on commit e421ab9

Please sign in to comment.