Lan
2026-01-07 20:04:44 +08:00
parent e80be564a5
commit e87bcfcce5
3 changed files with 58 additions and 56 deletions
+33 -31
View File
@@ -17,6 +17,7 @@ from core.response import APIResponse
from apps.base.models import FileCodes, KeyValue
from apps.admin.dependencies import create_token
from core.settings import settings
from core.utils import get_now
admin_api = APIRouter(prefix="/admin", tags=["管理"])
@@ -37,16 +38,13 @@ async def dashboard(admin: bool = Depends(admin_required)):
all_codes = await FileCodes.all()
all_size = str(sum([code.size for code in all_codes]))
sys_start = await KeyValue.filter(key="sys_start").first()
# 获取当前日期时间
now = datetime.datetime.now()
now = await get_now()
today_start = now.replace(hour=0, minute=0, second=0, microsecond=0)
yesterday_start = today_start - datetime.timedelta(days=1)
yesterday_end = today_start - datetime.timedelta(microseconds=1)
# 统计昨天一整天的记录数(从昨天0点到23:59:59)
yesterday_codes = FileCodes.filter(
created_at__gte=yesterday_start, created_at__lte=yesterday_end
)
# 统计今天到现在的记录数(从今天0点到现在)
today_codes = FileCodes.filter(created_at__gte=today_start)
return APIResponse(
detail={
@@ -63,9 +61,9 @@ async def dashboard(admin: bool = Depends(admin_required)):
@admin_api.delete("/file/delete")
async def file_delete(
data: IDData,
file_service: FileService = Depends(get_file_service),
admin: bool = Depends(admin_required),
data: IDData,
file_service: FileService = Depends(get_file_service),
admin: bool = Depends(admin_required),
):
await file_service.delete_file(data.id)
return APIResponse()
@@ -73,11 +71,11 @@ async def file_delete(
@admin_api.get("/file/list")
async def file_list(
page: int = 1,
size: int = 10,
keyword: str = "",
file_service: FileService = Depends(get_file_service),
admin: bool = Depends(admin_required),
page: int = 1,
size: int = 10,
keyword: str = "",
file_service: FileService = Depends(get_file_service),
admin: bool = Depends(admin_required),
):
files, total = await file_service.list_files(page, size, keyword)
return APIResponse(
@@ -92,17 +90,17 @@ async def file_list(
@admin_api.get("/config/get")
async def get_config(
config_service: ConfigService = Depends(get_config_service),
admin: bool = Depends(admin_required),
config_service: ConfigService = Depends(get_config_service),
admin: bool = Depends(admin_required),
):
return APIResponse(detail=config_service.get_config())
@admin_api.patch("/config/update")
async def update_config(
data: dict,
config_service: ConfigService = Depends(get_config_service),
admin: bool = Depends(admin_required),
data: dict,
config_service: ConfigService = Depends(get_config_service),
admin: bool = Depends(admin_required),
):
data.pop("themesChoices")
await config_service.update_config(data)
@@ -111,9 +109,9 @@ async def update_config(
@admin_api.get("/file/download")
async def file_download(
id: int,
file_service: FileService = Depends(get_file_service),
admin: bool = Depends(admin_required),
id: int,
file_service: FileService = Depends(get_file_service),
admin: bool = Depends(admin_required),
):
file_content = await file_service.download_file(id)
return file_content
@@ -121,8 +119,8 @@ async def file_download(
@admin_api.get("/local/lists")
async def get_local_lists(
local_file_service: LocalFileService = Depends(get_local_file_service),
admin: bool = Depends(admin_required),
local_file_service: LocalFileService = Depends(get_local_file_service),
admin: bool = Depends(admin_required),
):
files = await local_file_service.list_files()
return APIResponse(detail=files)
@@ -130,9 +128,9 @@ async def get_local_lists(
@admin_api.delete("/local/delete")
async def delete_local_file(
item: DeleteItem,
local_file_service: LocalFileService = Depends(get_local_file_service),
admin: bool = Depends(admin_required),
item: DeleteItem,
local_file_service: LocalFileService = Depends(get_local_file_service),
admin: bool = Depends(admin_required),
):
result = await local_file_service.delete_file(item.filename)
return APIResponse(detail=result)
@@ -140,9 +138,9 @@ async def delete_local_file(
@admin_api.post("/local/share")
async def share_local_file(
item: ShareItem,
file_service: FileService = Depends(get_file_service),
admin: bool = Depends(admin_required),
item: ShareItem,
file_service: FileService = Depends(get_file_service),
admin: bool = Depends(admin_required),
):
share_info = await file_service.share_local_file(item)
return APIResponse(detail=share_info)
@@ -150,8 +148,8 @@ async def share_local_file(
@admin_api.patch("/file/update")
async def update_file(
data: UpdateFileData,
admin: bool = Depends(admin_required),
data: UpdateFileData,
admin: bool = Depends(admin_required),
):
file_code = await FileCodes.filter(id=data.id).first()
if not file_code:
@@ -167,7 +165,11 @@ async def update_file(
update_data["prefix"] = data.prefix
if data.suffix is not None and data.suffix != file_code.suffix:
update_data["suffix"] = data.suffix
if data.expired_at is not None and data.expired_at != "" and data.expired_at != file_code.expired_at:
if (
data.expired_at is not None
and data.expired_at != ""
and data.expired_at != file_code.expired_at
):
update_data["expired_at"] = data.expired_at
if data.expired_count is not None and data.expired_count != file_code.expired_count:
update_data["expired_count"] = data.expired_count