Skip to content

Commit

Permalink
refact: some codes
Browse files Browse the repository at this point in the history
  • Loading branch information
vastsa committed Oct 6, 2024
1 parent fd1eea7 commit cc3a521
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 363 deletions.
16 changes: 15 additions & 1 deletion apps/admin/depends.py → apps/admin/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@
from fastapi import Header, HTTPException
from fastapi.requests import Request
from core.settings import settings
from apps.admin.services import FileService, ConfigService, LocalFileService


async def admin_required(authorization: Union[str, None] = Header(default=None), request: Request = None):
async def admin_required(authorization: str = Header(default=None), request: Request = None):
is_admin = authorization == str(settings.admin_token)
if request.url.path.startswith('/share/'):
if not settings.openUpload and not is_admin:
raise HTTPException(status_code=403, detail='本站未开启游客上传,如需上传请先登录后台')
else:
if not is_admin:
raise HTTPException(status_code=401, detail='未授权或授权校验失败')
return is_admin


async def get_file_service():
return FileService()


async def get_config_service():
return ConfigService()


async def get_local_file_service():
return LocalFileService()
5 changes: 0 additions & 5 deletions apps/admin/pydantics.py

This file was deleted.

19 changes: 19 additions & 0 deletions apps/admin/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pydantic import BaseModel


class IDData(BaseModel):
id: int


class ConfigUpdateData(BaseModel):
admin_token: str


class ShareItem(BaseModel):
expire_value: int
expire_style: str = 'day'
filename: str


class DeleteItem(BaseModel):
filename: str
123 changes: 123 additions & 0 deletions apps/admin/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os
import time

from core.response import APIResponse
from core.storage import FileStorageInterface, storages
from core.settings import settings
from apps.base.models import FileCodes, KeyValue
from apps.base.utils import get_expire_info, get_file_path_name
from fastapi import HTTPException
from core.settings import data_root


class FileService:
def __init__(self):
self.file_storage: FileStorageInterface = storages[settings.file_storage]()

async def delete_file(self, file_id: int):
file_code = await FileCodes.get(id=file_id)
await self.file_storage.delete_file(file_code)
await file_code.delete()

async def list_files(self, page: int, size: int):
offset = (page - 1) * size
files = await FileCodes.all().limit(size).offset(offset)
total = await FileCodes.all().count()
return files, total

async def download_file(self, file_id: int):
file_code = await FileCodes.filter(id=file_id).first()
if not file_code:
raise HTTPException(status_code=404, detail='文件不存在')
if file_code.text:
return APIResponse(detail=file_code.text)
else:
return await self.file_storage.get_file_response(file_code)

async def share_local_file(self, item):
local_file = LocalFileClass(item.filename)
if not await local_file.exists():
raise HTTPException(status_code=404, detail='文件不存在')

text = await local_file.read()
expired_at, expired_count, used_count, code = await get_expire_info(item.expire_value, item.expire_style)
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(item)

await self.file_storage.save_file(text, save_path)

await FileCodes.create(
code=code,
prefix=prefix,
suffix=suffix,
uuid_file_name=uuid_file_name,
file_path=path,
size=local_file.size,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
)

return {
'code': code,
'name': local_file.file,
}


class ConfigService:
def get_config(self):
return settings.items()

async def update_config(self, data: dict):
admin_token = data.get('admin_token')
if admin_token is None or admin_token == '':
raise HTTPException(status_code=400, detail='管理员密码不能为空')

for key, value in data.items():
if key not in settings.default_config:
continue
if key in ['errorCount', 'errorMinute', 'max_save_seconds', 'onedrive_proxy', 'openUpload', 'port', 's3_proxy', 'uploadCount', 'uploadMinute', 'uploadSize']:
data[key] = int(value)
elif key in ['opacity']:
data[key] = float(value)
else:
data[key] = value

await KeyValue.filter(key='settings').update(value=data)
for k, v in data.items():
settings.__setattr__(k, v)


class LocalFileService:
async def list_files(self):
files = []
for file in os.listdir(data_root / 'local'):
files.append(LocalFileClass(file))
return files

async def delete_file(self, filename: str):
file = LocalFileClass(filename)
if await file.exists():
await file.delete()
return '删除成功'
raise HTTPException(status_code=404, detail='文件不存在')


class LocalFileClass:
def __init__(self, file):
self.file = file
self.path = data_root / 'local' / file
self.ctime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getctime(self.path)))
self.size = os.path.getsize(self.path)

async def read(self):
return open(self.path, 'rb')

async def write(self, data):
with open(self.path, 'w') as f:
f.write(data)

async def delete(self):
os.remove(self.path)

async def exists(self):
return os.path.exists(self.path)
Loading

0 comments on commit cc3a521

Please sign in to comment.