2023-08-14 17:52:53 +08:00
|
|
|
|
# @Time : 2023/8/14 14:38
|
|
|
|
|
|
# @Author : Lan
|
|
|
|
|
|
# @File : views.py
|
|
|
|
|
|
# @Software: PyCharm
|
2024-11-23 16:43:58 +08:00
|
|
|
|
import datetime
|
2026-09-20 02:01:08 +08:00
|
|
|
|
from typing import Any
|
2026-06-03 03:32:12 +08:00
|
|
|
|
from collections import Counter
|
2026-06-03 07:36:48 +08:00
|
|
|
|
from typing import Optional
|
2023-08-29 22:12:04 +08:00
|
|
|
|
|
2025-02-09 22:57:21 +08:00
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
2024-10-06 17:14:16 +08:00
|
|
|
|
from apps.admin.services import FileService, ConfigService, LocalFileService
|
2025-02-15 22:00:53 +08:00
|
|
|
|
from apps.admin.dependencies import (
|
|
|
|
|
|
admin_required,
|
2026-06-03 02:44:42 +08:00
|
|
|
|
get_admin_session,
|
2025-02-15 22:00:53 +08:00
|
|
|
|
get_file_service,
|
|
|
|
|
|
get_config_service,
|
|
|
|
|
|
get_local_file_service,
|
|
|
|
|
|
)
|
2026-06-03 04:38:30 +08:00
|
|
|
|
from apps.admin.schemas import (
|
|
|
|
|
|
IDData,
|
|
|
|
|
|
IDsData,
|
|
|
|
|
|
BatchUpdateFileData,
|
2026-06-03 06:22:56 +08:00
|
|
|
|
BatchFilePolicyActionData,
|
2026-06-03 05:45:02 +08:00
|
|
|
|
FilePolicyActionData,
|
2026-06-03 06:37:56 +08:00
|
|
|
|
FileMetadataData,
|
2026-06-03 07:01:38 +08:00
|
|
|
|
FileViewPresetData,
|
|
|
|
|
|
FileViewPresetDeleteData,
|
2026-06-03 04:38:30 +08:00
|
|
|
|
ShareItem,
|
|
|
|
|
|
DeleteItem,
|
|
|
|
|
|
LoginData,
|
|
|
|
|
|
UpdateFileData,
|
|
|
|
|
|
)
|
2023-08-15 00:16:26 +08:00
|
|
|
|
from core.response import APIResponse
|
2024-11-23 16:43:58 +08:00
|
|
|
|
from apps.base.models import FileCodes, KeyValue
|
2026-09-16 18:31:00 +08:00
|
|
|
|
from apps.base.quota import owned_storage_queryset
|
2026-09-09 19:04:12 +08:00
|
|
|
|
from tortoise.expressions import Q
|
|
|
|
|
|
from tortoise.functions import Count, Sum
|
2026-07-10 19:21:00 +08:00
|
|
|
|
from apps.admin.dependencies import (
|
|
|
|
|
|
create_token,
|
|
|
|
|
|
get_admin_session_expire_seconds,
|
|
|
|
|
|
verify_token,
|
|
|
|
|
|
)
|
2026-09-09 16:33:07 +08:00
|
|
|
|
from core.logger import logger
|
2025-02-09 22:57:21 +08:00
|
|
|
|
from core.settings import settings
|
2026-09-09 16:33:07 +08:00
|
|
|
|
from core.utils import get_now, hash_password, password_needs_rehash, verify_password
|
2026-07-23 19:49:48 +08:00
|
|
|
|
from apps.base.utils import ip_limit
|
2023-08-15 00:16:26 +08:00
|
|
|
|
|
2026-01-25 23:40:15 +08:00
|
|
|
|
admin_api = APIRouter(
|
|
|
|
|
|
prefix="/admin", tags=["管理"], dependencies=[Depends(admin_required)]
|
|
|
|
|
|
)
|
2023-08-15 00:16:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 07:36:48 +08:00
|
|
|
|
def _pick_query_text(*values: Optional[str]) -> Optional[str]:
|
|
|
|
|
|
for value in values:
|
|
|
|
|
|
normalized_value = str(value or "").strip()
|
|
|
|
|
|
if normalized_value:
|
|
|
|
|
|
return normalized_value
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.post("/login")
|
2026-07-23 19:49:48 +08:00
|
|
|
|
async def login(data: LoginData, ip: str = Depends(ip_limit["login"])):
|
2026-09-09 18:09:02 +08:00
|
|
|
|
# 登录失败计入 IP 频率限制,超过 login_count/login_minute 后暂时锁定
|
2026-01-07 20:36:59 +08:00
|
|
|
|
if not verify_password(data.password, settings.admin_token):
|
2026-07-23 19:49:48 +08:00
|
|
|
|
ip_limit["login"].add_ip(ip)
|
2025-02-09 22:57:21 +08:00
|
|
|
|
raise HTTPException(status_code=401, detail="密码错误")
|
2025-02-15 22:00:53 +08:00
|
|
|
|
|
2026-09-09 16:33:07 +08:00
|
|
|
|
# 透明重哈希:存量 sha256/明文口令在登录成功(已证明持有口令)时升级为 scrypt
|
|
|
|
|
|
if password_needs_rehash(settings.admin_token):
|
|
|
|
|
|
try:
|
|
|
|
|
|
record = await KeyValue.filter(key="settings").first()
|
|
|
|
|
|
if record:
|
|
|
|
|
|
stored = dict(record.value or {})
|
|
|
|
|
|
stored["admin_token"] = hash_password(data.password)
|
|
|
|
|
|
record.value = stored
|
|
|
|
|
|
await record.save()
|
|
|
|
|
|
settings.admin_token = stored["admin_token"]
|
|
|
|
|
|
logger.info("管理员口令已升级为 scrypt 哈希")
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
logger.warning("口令哈希升级失败,保持旧格式", exc_info=True)
|
|
|
|
|
|
|
2026-07-10 19:21:00 +08:00
|
|
|
|
expires_in = get_admin_session_expire_seconds()
|
|
|
|
|
|
token = create_token({"is_admin": True}, expires_in=expires_in)
|
|
|
|
|
|
return APIResponse(
|
|
|
|
|
|
detail={
|
|
|
|
|
|
"id": "admin",
|
|
|
|
|
|
"username": "admin",
|
|
|
|
|
|
"token": token,
|
|
|
|
|
|
"token_type": "Bearer",
|
|
|
|
|
|
"expires_at": verify_token(token)["exp"],
|
|
|
|
|
|
"expires_in": expires_in,
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2023-08-15 20:18:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 02:44:42 +08:00
|
|
|
|
@admin_api.get("/verify")
|
|
|
|
|
|
async def verify_admin(session: dict = Depends(get_admin_session)):
|
|
|
|
|
|
return APIResponse(detail=session)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.post("/logout")
|
|
|
|
|
|
async def logout_admin():
|
|
|
|
|
|
return APIResponse(detail={"ok": True})
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 03:32:12 +08:00
|
|
|
|
async def build_dashboard_recent_file(file_code: FileCodes) -> dict:
|
|
|
|
|
|
is_expired = await file_code.is_expired()
|
|
|
|
|
|
return {
|
|
|
|
|
|
"id": file_code.id,
|
|
|
|
|
|
"code": file_code.code,
|
|
|
|
|
|
"name": file_code.prefix + file_code.suffix,
|
|
|
|
|
|
"suffix": file_code.suffix,
|
|
|
|
|
|
"size": file_code.size,
|
|
|
|
|
|
"text": file_code.text is not None,
|
2026-09-11 03:10:04 +08:00
|
|
|
|
"expired_at": file_code.expired_at,
|
|
|
|
|
|
"expired_count": file_code.expired_count,
|
|
|
|
|
|
"used_count": file_code.used_count,
|
|
|
|
|
|
"created_at": file_code.created_at,
|
|
|
|
|
|
"is_expired": is_expired,
|
2026-06-03 03:32:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-09-09 19:04:12 +08:00
|
|
|
|
def _expired_predicate(now: datetime.datetime) -> Q:
|
|
|
|
|
|
"""SQL version of FileCodes.is_expired:
|
|
|
|
|
|
expired_at IS NOT NULL AND ((expired_count < 0 AND expired_at < now) OR expired_count = 0)
|
|
|
|
|
|
"""
|
|
|
|
|
|
return Q(expired_at__isnull=False) & (
|
|
|
|
|
|
Q(expired_count__lt=0, expired_at__lt=now) | Q(expired_count=0)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _sum_size(queryset) -> int:
|
|
|
|
|
|
rows = await queryset.annotate(total=Sum("size")).values("total")
|
|
|
|
|
|
return rows[0]["total"] or 0
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.get("/dashboard")
|
2026-06-03 06:15:15 +08:00
|
|
|
|
async def dashboard(file_service: FileService = Depends(get_file_service)):
|
2025-02-15 22:00:53 +08:00
|
|
|
|
sys_start = await KeyValue.filter(key="sys_start").first()
|
2026-01-07 20:04:44 +08:00
|
|
|
|
now = await get_now()
|
2024-11-23 16:43:58 +08:00
|
|
|
|
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)
|
2026-06-03 03:32:12 +08:00
|
|
|
|
|
2026-09-09 19:04:12 +08:00
|
|
|
|
# 简单计数全部走 SQL 聚合,避免整表载入内存(D3)。
|
|
|
|
|
|
# 健康摘要依赖逐行 rules engine(_build_file_status_insights),保留一次遍历。
|
|
|
|
|
|
total_files = await FileCodes.all().count()
|
|
|
|
|
|
expired_count = await FileCodes.filter(_expired_predicate(now)).count()
|
2026-09-16 18:31:00 +08:00
|
|
|
|
all_size = await _sum_size(owned_storage_queryset())
|
2026-09-09 19:04:12 +08:00
|
|
|
|
used_count = (
|
|
|
|
|
|
await FileCodes.all().annotate(total=Sum("used_count")).values("total")
|
|
|
|
|
|
)[0]["total"] or 0
|
|
|
|
|
|
text_count = await FileCodes.filter(text__isnull=False).count()
|
|
|
|
|
|
chunked_count = await FileCodes.filter(is_chunked=True).count()
|
|
|
|
|
|
yesterday_count = await FileCodes.filter(
|
|
|
|
|
|
created_at__gte=yesterday_start, created_at__lte=yesterday_end
|
|
|
|
|
|
).count()
|
|
|
|
|
|
yesterday_size = await _sum_size(
|
2026-09-16 18:31:00 +08:00
|
|
|
|
owned_storage_queryset(
|
|
|
|
|
|
FileCodes.filter(created_at__gte=yesterday_start, created_at__lte=yesterday_end)
|
|
|
|
|
|
)
|
2026-09-09 19:04:12 +08:00
|
|
|
|
)
|
|
|
|
|
|
today_count = await FileCodes.filter(created_at__gte=today_start).count()
|
2026-09-16 18:31:00 +08:00
|
|
|
|
today_size = await _sum_size(
|
|
|
|
|
|
owned_storage_queryset(FileCodes.filter(created_at__gte=today_start))
|
|
|
|
|
|
)
|
2026-09-09 19:04:12 +08:00
|
|
|
|
|
|
|
|
|
|
suffix_rows = (
|
|
|
|
|
|
await FileCodes.filter(text__isnull=True)
|
|
|
|
|
|
.annotate(count=Count("id"))
|
|
|
|
|
|
.values("suffix", "count")
|
|
|
|
|
|
)
|
2026-06-03 03:32:12 +08:00
|
|
|
|
suffix_counter = Counter(
|
2026-09-09 19:04:12 +08:00
|
|
|
|
{((row["suffix"] or "file") or "file"): row["count"] for row in suffix_rows}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
recent_file_codes = await FileCodes.all().order_by("-created_at").limit(8)
|
|
|
|
|
|
health_summary = await file_service.build_file_health_summary(
|
|
|
|
|
|
await FileCodes.all(), now=now
|
2026-06-03 03:32:12 +08:00
|
|
|
|
)
|
2026-06-03 07:19:56 +08:00
|
|
|
|
recent_activities = await file_service.list_admin_activities(limit=8)
|
2025-02-15 22:00:53 +08:00
|
|
|
|
return APIResponse(
|
|
|
|
|
|
detail={
|
2026-09-11 03:10:04 +08:00
|
|
|
|
"total_files": total_files,
|
|
|
|
|
|
"storage_used": str(all_size),
|
|
|
|
|
|
"sys_uptime": sys_start.value if sys_start else None,
|
|
|
|
|
|
"yesterday_count": yesterday_count,
|
|
|
|
|
|
"yesterday_size": str(yesterday_size),
|
|
|
|
|
|
"today_count": today_count,
|
|
|
|
|
|
"today_size": str(today_size),
|
|
|
|
|
|
"active_count": total_files - expired_count,
|
|
|
|
|
|
"expired_count": expired_count,
|
|
|
|
|
|
"text_count": text_count,
|
|
|
|
|
|
"file_count": total_files - text_count,
|
|
|
|
|
|
"chunked_count": chunked_count,
|
|
|
|
|
|
"used_count": used_count,
|
|
|
|
|
|
"storage_backend": settings.file_storage,
|
|
|
|
|
|
"upload_size_limit": settings.upload_size,
|
2026-09-09 18:09:02 +08:00
|
|
|
|
"open_upload": settings.open_upload,
|
|
|
|
|
|
"enable_chunk": settings.enable_chunk,
|
2026-09-11 03:10:04 +08:00
|
|
|
|
"max_save_seconds": settings.max_save_seconds,
|
2026-06-03 06:15:15 +08:00
|
|
|
|
**health_summary,
|
2026-09-11 03:10:04 +08:00
|
|
|
|
"health_summary": health_summary,
|
|
|
|
|
|
"top_suffixes": [
|
2026-06-03 03:32:12 +08:00
|
|
|
|
{"suffix": suffix, "count": count}
|
|
|
|
|
|
for suffix, count in suffix_counter.most_common(8)
|
|
|
|
|
|
],
|
2026-09-11 03:10:04 +08:00
|
|
|
|
"recent_files": [
|
2026-06-03 03:32:12 +08:00
|
|
|
|
await build_dashboard_recent_file(file_code)
|
|
|
|
|
|
for file_code in recent_file_codes
|
|
|
|
|
|
],
|
2026-06-03 07:19:56 +08:00
|
|
|
|
"recent_activities": recent_activities["activities"],
|
2025-02-15 22:00:53 +08:00
|
|
|
|
}
|
2024-11-23 16:43:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 07:19:56 +08:00
|
|
|
|
@admin_api.get("/activities")
|
|
|
|
|
|
async def admin_activities(
|
|
|
|
|
|
limit: int = 20,
|
2026-06-03 07:36:48 +08:00
|
|
|
|
action: Optional[str] = None,
|
|
|
|
|
|
targetType: Optional[str] = None,
|
|
|
|
|
|
target_type: Optional[str] = None,
|
|
|
|
|
|
keyword: Optional[str] = None,
|
2026-06-03 07:19:56 +08:00
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
2026-06-03 07:36:48 +08:00
|
|
|
|
result = await file_service.list_admin_activities(
|
|
|
|
|
|
limit=limit,
|
|
|
|
|
|
action=action,
|
|
|
|
|
|
target_type=_pick_query_text(targetType, target_type),
|
|
|
|
|
|
keyword=keyword,
|
|
|
|
|
|
)
|
2026-06-03 07:19:56 +08:00
|
|
|
|
return APIResponse(detail=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.delete("/file/delete")
|
2024-10-06 17:14:16 +08:00
|
|
|
|
async def file_delete(
|
2026-01-07 20:04:44 +08:00
|
|
|
|
data: IDData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
2024-10-06 17:14:16 +08:00
|
|
|
|
):
|
|
|
|
|
|
await file_service.delete_file(data.id)
|
2023-08-15 00:16:26 +08:00
|
|
|
|
return APIResponse()
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 04:24:16 +08:00
|
|
|
|
async def batch_delete_files(
|
|
|
|
|
|
data: IDsData,
|
|
|
|
|
|
file_service: FileService,
|
|
|
|
|
|
):
|
|
|
|
|
|
if not data.ids:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="请选择要删除的文件")
|
|
|
|
|
|
result = await file_service.delete_files(data.ids)
|
|
|
|
|
|
return APIResponse(detail=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.delete("/file/batch-delete")
|
|
|
|
|
|
async def file_batch_delete(
|
|
|
|
|
|
data: IDsData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await batch_delete_files(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.post("/file/batch-delete")
|
|
|
|
|
|
async def file_batch_delete_post(
|
|
|
|
|
|
data: IDsData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await batch_delete_files(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 04:38:30 +08:00
|
|
|
|
async def batch_update_files(
|
|
|
|
|
|
data: BatchUpdateFileData,
|
|
|
|
|
|
file_service: FileService,
|
|
|
|
|
|
):
|
|
|
|
|
|
if not data.ids:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="请选择要更新的文件")
|
|
|
|
|
|
|
2026-09-20 02:01:08 +08:00
|
|
|
|
update_data: dict[str, Any] = {}
|
2026-06-03 04:38:30 +08:00
|
|
|
|
fields_set = data.model_fields_set
|
2026-09-11 03:10:04 +08:00
|
|
|
|
should_clear_expired_at = bool(data.clear_expired_at)
|
2026-06-03 04:38:30 +08:00
|
|
|
|
|
|
|
|
|
|
if should_clear_expired_at:
|
|
|
|
|
|
update_data["expired_at"] = None
|
|
|
|
|
|
update_data["expired_count"] = -1
|
|
|
|
|
|
elif "expired_at" in fields_set and data.expired_at != "":
|
2026-09-20 02:01:08 +08:00
|
|
|
|
# schema 声明 Union[datetime, str]——透传两种形态,tortoise 均可序列化
|
2026-06-03 04:38:30 +08:00
|
|
|
|
update_data["expired_at"] = data.expired_at
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
not should_clear_expired_at
|
|
|
|
|
|
and "expired_count" in fields_set
|
|
|
|
|
|
and data.expired_count is not None
|
|
|
|
|
|
):
|
|
|
|
|
|
update_data["expired_count"] = data.expired_count
|
|
|
|
|
|
|
|
|
|
|
|
if not update_data:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="请选择要更新的字段")
|
|
|
|
|
|
|
|
|
|
|
|
result = await file_service.update_files(data.ids, update_data)
|
|
|
|
|
|
return APIResponse(detail=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.patch("/file/batch-update")
|
|
|
|
|
|
async def file_batch_update(
|
|
|
|
|
|
data: BatchUpdateFileData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await batch_update_files(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.post("/file/batch-update")
|
|
|
|
|
|
async def file_batch_update_post(
|
|
|
|
|
|
data: BatchUpdateFileData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await batch_update_files(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 05:45:02 +08:00
|
|
|
|
async def apply_file_policy_action(
|
|
|
|
|
|
data: FilePolicyActionData,
|
|
|
|
|
|
file_service: FileService,
|
|
|
|
|
|
):
|
2026-09-11 03:10:04 +08:00
|
|
|
|
download_limit = data.download_limit
|
2026-06-03 05:45:02 +08:00
|
|
|
|
|
|
|
|
|
|
detail = await file_service.apply_file_policy_action(
|
|
|
|
|
|
file_id=data.id,
|
|
|
|
|
|
action=data.action,
|
|
|
|
|
|
download_limit=download_limit,
|
|
|
|
|
|
)
|
|
|
|
|
|
return APIResponse(detail=detail)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.patch("/file/policy-action")
|
|
|
|
|
|
async def file_policy_action(
|
|
|
|
|
|
data: FilePolicyActionData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await apply_file_policy_action(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.post("/file/policy-action")
|
|
|
|
|
|
async def file_policy_action_post(
|
|
|
|
|
|
data: FilePolicyActionData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await apply_file_policy_action(data, file_service)
|
2026-06-03 06:22:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def apply_batch_file_policy_action(
|
|
|
|
|
|
data: BatchFilePolicyActionData,
|
|
|
|
|
|
file_service: FileService,
|
|
|
|
|
|
):
|
|
|
|
|
|
if not data.ids:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="请选择要更新的文件")
|
|
|
|
|
|
|
2026-09-11 03:10:04 +08:00
|
|
|
|
download_limit = data.download_limit
|
2026-06-03 06:22:56 +08:00
|
|
|
|
|
|
|
|
|
|
result = await file_service.apply_files_policy_action(
|
|
|
|
|
|
file_ids=data.ids,
|
|
|
|
|
|
action=data.action,
|
|
|
|
|
|
download_limit=download_limit,
|
|
|
|
|
|
)
|
|
|
|
|
|
return APIResponse(detail=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.patch("/file/batch-policy-action")
|
|
|
|
|
|
async def file_batch_policy_action(
|
|
|
|
|
|
data: BatchFilePolicyActionData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await apply_batch_file_policy_action(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.post("/file/batch-policy-action")
|
|
|
|
|
|
async def file_batch_policy_action_post(
|
|
|
|
|
|
data: BatchFilePolicyActionData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await apply_batch_file_policy_action(data, file_service)
|
2026-06-03 05:45:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.get("/file/list")
|
2024-10-06 17:14:16 +08:00
|
|
|
|
async def file_list(
|
2026-01-07 20:04:44 +08:00
|
|
|
|
page: int = 1,
|
|
|
|
|
|
size: int = 10,
|
|
|
|
|
|
keyword: str = "",
|
2026-06-03 03:45:30 +08:00
|
|
|
|
status: str = "",
|
|
|
|
|
|
type: str = "",
|
2026-06-03 05:55:18 +08:00
|
|
|
|
health: str = "",
|
2026-09-11 03:10:04 +08:00
|
|
|
|
sort_by: str = "created_at",
|
|
|
|
|
|
sort_order: str = "desc",
|
2026-01-07 20:04:44 +08:00
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
2024-10-06 17:14:16 +08:00
|
|
|
|
):
|
2026-06-03 03:45:30 +08:00
|
|
|
|
page = max(page, 1)
|
|
|
|
|
|
size = min(max(size, 1), 100)
|
2026-06-03 10:24:07 +08:00
|
|
|
|
files, total, summary = await file_service.list_files(
|
2026-06-03 03:45:30 +08:00
|
|
|
|
page,
|
|
|
|
|
|
size,
|
|
|
|
|
|
keyword,
|
|
|
|
|
|
status=status,
|
|
|
|
|
|
file_type=type,
|
2026-06-03 05:55:18 +08:00
|
|
|
|
health=health,
|
2026-09-11 03:10:04 +08:00
|
|
|
|
sort_by=sort_by,
|
|
|
|
|
|
sort_order=sort_order,
|
2026-06-03 03:45:30 +08:00
|
|
|
|
)
|
2025-02-15 22:00:53 +08:00
|
|
|
|
return APIResponse(
|
|
|
|
|
|
detail={
|
|
|
|
|
|
"page": page,
|
|
|
|
|
|
"size": size,
|
|
|
|
|
|
"data": files,
|
|
|
|
|
|
"total": total,
|
2026-06-03 03:45:30 +08:00
|
|
|
|
"summary": summary,
|
2025-02-15 22:00:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
)
|
2023-08-15 17:23:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 04:58:19 +08:00
|
|
|
|
@admin_api.get("/file/detail")
|
|
|
|
|
|
async def file_detail(
|
|
|
|
|
|
id: int,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
detail = await file_service.get_file_detail(id)
|
|
|
|
|
|
return APIResponse(detail=detail)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.post("/file/detail")
|
|
|
|
|
|
async def file_detail_post(
|
|
|
|
|
|
data: IDData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
detail = await file_service.get_file_detail(data.id)
|
|
|
|
|
|
return APIResponse(detail=detail)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 06:37:56 +08:00
|
|
|
|
async def update_file_metadata(
|
|
|
|
|
|
data: FileMetadataData,
|
|
|
|
|
|
file_service: FileService,
|
|
|
|
|
|
):
|
|
|
|
|
|
fields_set = data.model_fields_set
|
|
|
|
|
|
update_note = "note" in fields_set
|
|
|
|
|
|
update_tags = "tags" in fields_set
|
|
|
|
|
|
if not update_note and not update_tags:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="请选择要更新的元数据")
|
|
|
|
|
|
|
|
|
|
|
|
detail = await file_service.update_file_metadata(
|
|
|
|
|
|
file_id=data.id,
|
|
|
|
|
|
note=data.note,
|
|
|
|
|
|
tags=data.tags,
|
|
|
|
|
|
update_note=update_note,
|
|
|
|
|
|
update_tags=update_tags,
|
|
|
|
|
|
)
|
|
|
|
|
|
return APIResponse(detail=detail)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.patch("/file/metadata")
|
|
|
|
|
|
async def file_metadata(
|
|
|
|
|
|
data: FileMetadataData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await update_file_metadata(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.post("/file/metadata")
|
|
|
|
|
|
async def file_metadata_post(
|
|
|
|
|
|
data: FileMetadataData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await update_file_metadata(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 07:01:38 +08:00
|
|
|
|
@admin_api.get("/file/view-presets")
|
|
|
|
|
|
async def file_view_presets(
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
result = await file_service.list_file_view_presets()
|
|
|
|
|
|
return APIResponse(detail=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def save_file_view_preset(
|
|
|
|
|
|
data: FileViewPresetData,
|
|
|
|
|
|
file_service: FileService,
|
|
|
|
|
|
):
|
|
|
|
|
|
filters = data.filters if data.filters is not None else data.params
|
|
|
|
|
|
preset = await file_service.save_file_view_preset(
|
|
|
|
|
|
preset_id=data.id,
|
|
|
|
|
|
name=data.name,
|
|
|
|
|
|
filters=filters or {},
|
|
|
|
|
|
)
|
|
|
|
|
|
return APIResponse(detail=preset)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.post("/file/view-presets")
|
|
|
|
|
|
async def file_view_presets_save(
|
|
|
|
|
|
data: FileViewPresetData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await save_file_view_preset(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.patch("/file/view-presets")
|
|
|
|
|
|
async def file_view_presets_patch(
|
|
|
|
|
|
data: FileViewPresetData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await save_file_view_preset(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def delete_file_view_preset(
|
|
|
|
|
|
data: FileViewPresetDeleteData,
|
|
|
|
|
|
file_service: FileService,
|
|
|
|
|
|
):
|
|
|
|
|
|
result = await file_service.delete_file_view_preset(data.id)
|
|
|
|
|
|
return APIResponse(detail=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.delete("/file/view-presets")
|
|
|
|
|
|
async def file_view_presets_delete(
|
|
|
|
|
|
data: FileViewPresetDeleteData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await delete_file_view_preset(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.post("/file/view-presets/delete")
|
|
|
|
|
|
async def file_view_presets_delete_post(
|
|
|
|
|
|
data: FileViewPresetDeleteData,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
|
|
|
|
|
return await delete_file_view_preset(data, file_service)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.get("/config/get")
|
2024-10-06 17:14:16 +08:00
|
|
|
|
async def get_config(
|
2026-01-07 20:04:44 +08:00
|
|
|
|
config_service: ConfigService = Depends(get_config_service),
|
2024-10-06 17:14:16 +08:00
|
|
|
|
):
|
|
|
|
|
|
return APIResponse(detail=config_service.get_config())
|
2023-08-15 17:23:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.patch("/config/update")
|
2024-10-06 17:14:16 +08:00
|
|
|
|
async def update_config(
|
2026-01-07 20:04:44 +08:00
|
|
|
|
data: dict,
|
|
|
|
|
|
config_service: ConfigService = Depends(get_config_service),
|
2026-06-03 07:19:56 +08:00
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
2024-10-06 17:14:16 +08:00
|
|
|
|
):
|
2026-09-09 18:09:02 +08:00
|
|
|
|
data.pop("themes_choices", None)
|
2024-10-06 17:14:16 +08:00
|
|
|
|
await config_service.update_config(data)
|
2026-06-03 07:19:56 +08:00
|
|
|
|
await file_service.record_admin_activity(
|
|
|
|
|
|
action="config.update",
|
|
|
|
|
|
target_type="config",
|
|
|
|
|
|
target_name="system",
|
|
|
|
|
|
count=1,
|
|
|
|
|
|
meta={"fields": sorted(data.keys())},
|
|
|
|
|
|
)
|
2023-08-15 17:23:30 +08:00
|
|
|
|
return APIResponse()
|
2024-03-29 17:55:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.get("/file/download")
|
2024-10-06 17:14:16 +08:00
|
|
|
|
async def file_download(
|
2026-01-07 20:04:44 +08:00
|
|
|
|
id: int,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
2024-10-06 17:14:16 +08:00
|
|
|
|
):
|
|
|
|
|
|
file_content = await file_service.download_file(id)
|
|
|
|
|
|
return file_content
|
2024-03-29 17:55:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-06-03 04:05:52 +08:00
|
|
|
|
@admin_api.get("/file/preview")
|
|
|
|
|
|
async def file_preview(
|
|
|
|
|
|
id: int,
|
2026-09-11 03:10:04 +08:00
|
|
|
|
max_chars: int = 4000,
|
2026-06-03 04:05:52 +08:00
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
|
|
|
|
|
):
|
2026-09-11 03:10:04 +08:00
|
|
|
|
preview = await file_service.preview_file(id, max_chars)
|
2026-06-03 04:05:52 +08:00
|
|
|
|
return APIResponse(detail=preview)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.get("/local/lists")
|
2024-10-06 17:14:16 +08:00
|
|
|
|
async def get_local_lists(
|
2026-09-16 18:31:00 +08:00
|
|
|
|
path: str = "",
|
2026-01-07 20:04:44 +08:00
|
|
|
|
local_file_service: LocalFileService = Depends(get_local_file_service),
|
2024-10-06 17:14:16 +08:00
|
|
|
|
):
|
2026-09-16 18:31:00 +08:00
|
|
|
|
files = await local_file_service.list_files(path)
|
2024-09-24 23:00:43 +08:00
|
|
|
|
return APIResponse(detail=files)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.delete("/local/delete")
|
2024-10-06 17:14:16 +08:00
|
|
|
|
async def delete_local_file(
|
2026-01-07 20:04:44 +08:00
|
|
|
|
item: DeleteItem,
|
|
|
|
|
|
local_file_service: LocalFileService = Depends(get_local_file_service),
|
2026-06-03 07:19:56 +08:00
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
2024-10-06 17:14:16 +08:00
|
|
|
|
):
|
|
|
|
|
|
result = await local_file_service.delete_file(item.filename)
|
2026-06-03 07:19:56 +08:00
|
|
|
|
await file_service.record_admin_activity(
|
|
|
|
|
|
action="local_file.delete",
|
|
|
|
|
|
target_type="local_file",
|
|
|
|
|
|
target_name=item.filename,
|
|
|
|
|
|
count=1,
|
|
|
|
|
|
meta={"success": bool(result)},
|
|
|
|
|
|
)
|
2024-10-06 17:14:16 +08:00
|
|
|
|
return APIResponse(detail=result)
|
2024-09-24 23:00:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 22:00:53 +08:00
|
|
|
|
@admin_api.post("/local/share")
|
2024-10-06 17:14:16 +08:00
|
|
|
|
async def share_local_file(
|
2026-01-07 20:04:44 +08:00
|
|
|
|
item: ShareItem,
|
|
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
2024-10-06 17:14:16 +08:00
|
|
|
|
):
|
|
|
|
|
|
share_info = await file_service.share_local_file(item)
|
2026-06-03 07:19:56 +08:00
|
|
|
|
await file_service.record_admin_activity(
|
|
|
|
|
|
action="local_file.share",
|
|
|
|
|
|
target_type="file",
|
|
|
|
|
|
target_id=share_info.get("id") if isinstance(share_info, dict) else None,
|
|
|
|
|
|
target_name=item.filename,
|
|
|
|
|
|
count=1,
|
|
|
|
|
|
meta={
|
2026-09-11 03:10:04 +08:00
|
|
|
|
"expire_value": item.expire_value,
|
2026-09-09 18:09:02 +08:00
|
|
|
|
"expire_style": item.expire_style,
|
2026-06-03 07:19:56 +08:00
|
|
|
|
},
|
|
|
|
|
|
)
|
2024-10-06 17:14:16 +08:00
|
|
|
|
return APIResponse(detail=share_info)
|
2025-03-04 00:09:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_api.patch("/file/update")
|
|
|
|
|
|
async def update_file(
|
2026-01-07 20:04:44 +08:00
|
|
|
|
data: UpdateFileData,
|
2026-06-03 07:19:56 +08:00
|
|
|
|
file_service: FileService = Depends(get_file_service),
|
2025-03-04 00:09:06 +08:00
|
|
|
|
):
|
2026-09-11 02:56:02 +08:00
|
|
|
|
await file_service.update_file(
|
|
|
|
|
|
file_id=data.id,
|
|
|
|
|
|
code=data.code,
|
|
|
|
|
|
prefix=data.prefix,
|
|
|
|
|
|
suffix=data.suffix,
|
|
|
|
|
|
expired_at=data.expired_at,
|
|
|
|
|
|
expired_count=data.expired_count,
|
|
|
|
|
|
)
|
2025-03-04 00:09:06 +08:00
|
|
|
|
return APIResponse(detail="更新成功")
|