style: code format

This commit is contained in:
Lan
2025-02-15 22:00:53 +08:00
parent 7b0ea804cf
commit 10d136b0c5
14 changed files with 404 additions and 311 deletions
+39 -22
View File
@@ -19,16 +19,18 @@ class FileService:
await self.file_storage.delete_file(file_code)
await file_code.delete()
async def list_files(self, page: int, size: int, keyword: str = ''):
async def list_files(self, page: int, size: int, keyword: str = ""):
offset = (page - 1) * size
files = await FileCodes.filter(prefix__icontains=keyword).limit(size).offset(offset)
files = (
await FileCodes.filter(prefix__icontains=keyword).limit(size).offset(offset)
)
total = await FileCodes.filter(prefix__icontains=keyword).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='文件不存在')
raise HTTPException(status_code=404, detail="文件不存在")
if file_code.text:
return APIResponse(detail=file_code.text)
else:
@@ -37,10 +39,12 @@ class FileService:
async def share_local_file(self, item):
local_file = LocalFileClass(item.filename)
if not await local_file.exists():
raise HTTPException(status_code=404, detail='文件不存在')
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)
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)
@@ -58,8 +62,8 @@ class FileService:
)
return {
'code': code,
'name': local_file.file,
"code": code,
"name": local_file.file,
}
@@ -68,21 +72,32 @@ class ConfigService:
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='管理员密码不能为空')
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']:
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']:
elif key in ["opacity"]:
data[key] = float(value)
else:
data[key] = value
await KeyValue.filter(key='settings').update(value=data)
await KeyValue.filter(key="settings").update(value=data)
for k, v in data.items():
settings.__setattr__(k, v)
@@ -90,9 +105,9 @@ class ConfigService:
class LocalFileService:
async def list_files(self):
files = []
if not os.path.exists(data_root / 'local'):
os.makedirs(data_root / 'local')
for file in os.listdir(data_root / 'local'):
if not os.path.exists(data_root / "local"):
os.makedirs(data_root / "local")
for file in os.listdir(data_root / "local"):
files.append(LocalFileClass(file))
return files
@@ -100,22 +115,24 @@ class LocalFileService:
file = LocalFileClass(filename)
if await file.exists():
await file.delete()
return '删除成功'
raise HTTPException(status_code=404, detail='文件不存在')
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.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')
return open(self.path, "rb")
async def write(self, data):
with open(self.path, 'w') as f:
with open(self.path, "w") as f:
f.write(data)
async def delete(self):