* 校验变更的出站地址并保留现有内部 CloudDrive 配置 * 修复未知长度上传、API 404 语义及下载令牌比较 * 合并 S3 小分片为符合最小尺寸要求的 multipart 部分 * 同步官方运行依赖、版本元数据和已推送前端版本 * 验证单元集成、覆盖率及定制上传下载兼容并记录整合范围
1992 lines
75 KiB
Python
1992 lines
75 KiB
Python
import asyncio
|
||
import hashlib
|
||
import json
|
||
from pathlib import Path
|
||
|
||
import os
|
||
from datetime import datetime, timedelta
|
||
from typing import Any, Dict, List, Optional, Tuple
|
||
|
||
from apps.base.local_share import (
|
||
LOCAL_REF_MARKER, MAX_LIST_ENTRIES, build_local_ref_download,
|
||
format_local_ctime, get_local_root, is_local_ref, normalize_local_relpath,
|
||
resolve_under_local, should_skip_storage_delete,
|
||
)
|
||
from core.response import APIResponse
|
||
from core.storage import FileStorageInterface, storages
|
||
from core.settings import (
|
||
ADMIN_SESSION_EXPIRE_MAX,
|
||
ADMIN_SESSION_EXPIRE_MIN,
|
||
settings,
|
||
)
|
||
from core.config import refresh_settings
|
||
from core.security import (INTERNAL_CONFIG_KEYS, generate_jwt_secret,
|
||
OUTBOUND_ENDPOINT_CONFIG_KEYS, validate_outbound_endpoint, validate_outbound_hostname)
|
||
from apps.base.models import FileCodes, KeyValue, ShareCodeReservations, TransferStats, file_codes_pydantic
|
||
|
||
from apps.base.utils import get_expire_info, get_random_code, validate_expire_style
|
||
from fastapi import HTTPException
|
||
from core.utils import get_now, hash_password, is_password_hashed, validate_background_url
|
||
|
||
|
||
from apps.admin.trash_management import TrashManagementMixin
|
||
|
||
|
||
# Single-worker deployments serialize JSON read/modify/write operations.
|
||
keyvalue_write_lock = asyncio.Lock()
|
||
|
||
|
||
class FileService(TrashManagementMixin):
|
||
FILE_METADATA_KEY_PREFIX = "admin_file_metadata:"
|
||
FILE_VIEW_PRESETS_KEY = "admin_file_view_presets"
|
||
ADMIN_ACTIVITY_KEY = "admin_activity_events"
|
||
MAX_METADATA_NOTE_LENGTH = 2000
|
||
MAX_METADATA_TAGS = 12
|
||
MAX_METADATA_TAG_LENGTH = 24
|
||
MAX_VIEW_PRESETS = 24
|
||
MAX_VIEW_PRESET_NAME_LENGTH = 32
|
||
MAX_VIEW_PRESET_KEYWORD_LENGTH = 80
|
||
MAX_ADMIN_ACTIVITIES = 5000
|
||
MAX_ADMIN_ACTIVITY_TEXT_LENGTH = 120
|
||
|
||
POLICY_ACTIONS = {
|
||
"extend_24h",
|
||
"extend_7d",
|
||
"make_permanent",
|
||
"reset_download_limit",
|
||
}
|
||
|
||
SORT_FIELDS = {
|
||
"created_at",
|
||
"createdat",
|
||
"expired_at",
|
||
"expiredat",
|
||
"name",
|
||
"size",
|
||
"used_count",
|
||
"usedcount",
|
||
"code",
|
||
}
|
||
VIEW_PRESET_STATUS_VALUES = {"all", "active", "expired"}
|
||
VIEW_PRESET_TYPE_VALUES = {"all", "file", "text", "chunked"}
|
||
VIEW_PRESET_HEALTH_VALUES = {
|
||
"all",
|
||
"attention",
|
||
"danger",
|
||
"warning",
|
||
"healthy",
|
||
"expired",
|
||
"expiring_soon",
|
||
"storage_issue",
|
||
"never_retrieved",
|
||
"permanent",
|
||
}
|
||
VIEW_PRESET_SORT_FIELDS = {
|
||
"created_at",
|
||
"expired_at",
|
||
"name",
|
||
"size",
|
||
"used_count",
|
||
"code",
|
||
}
|
||
|
||
TRASH_DEFAULT_RETENTION_DAYS = 30
|
||
TRASH_MAX_RETENTION_DAYS = 365
|
||
|
||
def __init__(self):
|
||
self._file_storage: Optional[FileStorageInterface] = None
|
||
|
||
@property
|
||
def file_storage(self) -> FileStorageInterface:
|
||
if self._file_storage is None:
|
||
self._file_storage = storages[settings.file_storage]()
|
||
return self._file_storage
|
||
|
||
def _file_metadata_key(self, file_id: int) -> str:
|
||
return f"{self.FILE_METADATA_KEY_PREFIX}{file_id}"
|
||
|
||
def _dedupe_download_stats(self, stats: List[TransferStats]) -> List[TransferStats]:
|
||
deduped: List[TransferStats] = []
|
||
recent_downloads: Dict[Tuple[Optional[int], str, int], datetime] = {}
|
||
for stat in stats:
|
||
key = (stat.file_code_id, stat.ip or "", int(stat.size or 0))
|
||
previous = recent_downloads.get(key)
|
||
if previous and (stat.created_at - previous).total_seconds() <= 5:
|
||
continue
|
||
recent_downloads[key] = stat.created_at
|
||
deduped.append(stat)
|
||
return deduped
|
||
|
||
async def _delete_file_code(self, file_code: FileCodes, reason: str = "manual"):
|
||
await self.move_to_trash(file_code, reason=reason)
|
||
|
||
@staticmethod
|
||
def _trash_path(file_code: FileCodes) -> str:
|
||
filename = file_code.uuid_file_name or f"{file_code.prefix}{file_code.suffix}"
|
||
return f"trash/{file_code.id}/{filename}"
|
||
|
||
@classmethod
|
||
def _trash_expiry(cls, now: datetime, retention_days: Optional[int]):
|
||
if retention_days is None:
|
||
retention_days = int(
|
||
getattr(settings, "trashRetentionDays", cls.TRASH_DEFAULT_RETENTION_DAYS)
|
||
)
|
||
if retention_days == 0:
|
||
return None
|
||
if not 1 <= retention_days <= cls.TRASH_MAX_RETENTION_DAYS:
|
||
raise HTTPException(status_code=400, detail="回收站保留期限必须为 1 到 365 天,或设为永久")
|
||
return now + timedelta(days=retention_days)
|
||
|
||
async def move_to_trash(
|
||
self,
|
||
file_code: FileCodes,
|
||
reason: str,
|
||
retention_days: Optional[int] = None,
|
||
) -> FileCodes:
|
||
if file_code.is_trashed:
|
||
return file_code
|
||
now = await get_now()
|
||
original_path = await file_code.get_file_path() if file_code.text is None else ""
|
||
if not should_skip_storage_delete(file_code):
|
||
await self.file_storage.move_file(file_code, self._trash_path(file_code))
|
||
await file_code.update_from_dict(
|
||
{
|
||
"is_trashed": True,
|
||
"trashed_at": now,
|
||
"trash_expires_at": self._trash_expiry(now, retention_days),
|
||
"trash_reason": reason,
|
||
"trash_original_path": original_path,
|
||
}
|
||
).save()
|
||
await TransferStats.create(
|
||
action="trash",
|
||
file_code_id=file_code.id,
|
||
code=file_code.code,
|
||
name=f"{file_code.prefix or ''}{file_code.suffix or ''}" or file_code.code,
|
||
size=int(file_code.size or 0),
|
||
ip=f"system-{reason}",
|
||
file_path=original_path,
|
||
expired_at=file_code.expired_at,
|
||
deleted_at=now,
|
||
meta=json.dumps({"reason": reason, "trash_path": self._trash_path(file_code)}, ensure_ascii=False),
|
||
)
|
||
return file_code
|
||
|
||
async def restore_from_trash(self, file_id: int) -> FileCodes:
|
||
file_code = await FileCodes.filter(id=file_id, is_trashed=True).first()
|
||
if not file_code:
|
||
raise HTTPException(status_code=404, detail="回收站文件不存在")
|
||
if file_code.trash_purged_at is not None:
|
||
raise HTTPException(status_code=409, detail="文件本体已永久清理,仅保留审计记录")
|
||
original_path = file_code.trash_original_path or ""
|
||
if not should_skip_storage_delete(file_code):
|
||
if not original_path:
|
||
raise HTTPException(status_code=409, detail="缺少原始文件路径,无法恢复")
|
||
destination_dir, filename = os.path.split(original_path)
|
||
if filename != (file_code.uuid_file_name or filename):
|
||
raise HTTPException(status_code=409, detail="回收站文件名不一致,无法恢复")
|
||
await self.file_storage.move_file(
|
||
FileCodes(file_path=os.path.dirname(self._trash_path(file_code)), uuid_file_name=os.path.basename(self._trash_path(file_code))),
|
||
original_path,
|
||
)
|
||
file_code.file_path = destination_dir
|
||
original_code = file_code.code
|
||
active_collision = await FileCodes.filter(
|
||
code=original_code, is_trashed=False
|
||
).exclude(id=file_code.id).exists()
|
||
if active_collision:
|
||
raise HTTPException(status_code=409, detail="原取件码已被其他文件占用,无法复用")
|
||
reservations = await ShareCodeReservations.filter(code=original_code).all()
|
||
if any(reservation.file_code_id != file_code.id for reservation in reservations):
|
||
raise HTTPException(status_code=409, detail="原取件码已被历史记录占用,无法复用")
|
||
await ShareCodeReservations.filter(
|
||
code=original_code, file_code_id=file_code.id
|
||
).delete()
|
||
file_code.code = original_code
|
||
file_code.is_trashed = False
|
||
file_code.trashed_at = None
|
||
file_code.trash_expires_at = None
|
||
file_code.trash_reason = None
|
||
file_code.trash_original_path = None
|
||
file_code.trash_purged_at = None
|
||
await file_code.save()
|
||
await TransferStats.create(
|
||
action="restore",
|
||
file_code_id=file_code.id,
|
||
code=file_code.code,
|
||
name=f"{file_code.prefix or ''}{file_code.suffix or ''}" or file_code.code,
|
||
size=int(file_code.size or 0),
|
||
ip="admin-restore",
|
||
file_path=file_code.file_path or "",
|
||
expired_at=file_code.expired_at,
|
||
meta=json.dumps({"new_code": file_code.code}, ensure_ascii=False),
|
||
)
|
||
await self.record_admin_activity(
|
||
action="trash.restore",
|
||
target_type="file",
|
||
target_id=file_code.id,
|
||
target_name=self._build_file_activity_name(file_code),
|
||
meta={"newCode": file_code.code},
|
||
)
|
||
return file_code
|
||
|
||
async def purge_trashed_file(self, file_code: FileCodes) -> None:
|
||
if file_code.trash_purged_at is not None:
|
||
return
|
||
if not should_skip_storage_delete(file_code):
|
||
trash_path = self._trash_path(file_code)
|
||
await self.file_storage.delete_file(
|
||
FileCodes(
|
||
file_path=os.path.dirname(trash_path),
|
||
uuid_file_name=os.path.basename(trash_path),
|
||
)
|
||
)
|
||
purged_at = await get_now()
|
||
file_code.trash_purged_at = purged_at
|
||
file_code.trash_expires_at = None
|
||
await file_code.save(update_fields=["trash_purged_at", "trash_expires_at"])
|
||
await TransferStats.create(
|
||
action="purge",
|
||
file_code_id=file_code.id,
|
||
code=file_code.code,
|
||
name=f"{file_code.prefix or ''}{file_code.suffix or ''}" or file_code.code,
|
||
size=int(file_code.size or 0),
|
||
ip="system-purge",
|
||
file_path=file_code.trash_original_path or "",
|
||
expired_at=file_code.expired_at,
|
||
deleted_at=purged_at,
|
||
meta=json.dumps({"reason": "trash_purge"}, ensure_ascii=False),
|
||
)
|
||
await self.record_admin_activity(
|
||
action="trash.purge",
|
||
target_type="file",
|
||
target_id=file_code.id,
|
||
target_name=self._build_file_activity_name(file_code),
|
||
meta={"auditOnly": True},
|
||
)
|
||
|
||
async def delete_file(self, file_id: int):
|
||
file_code = await FileCodes.get(id=file_id)
|
||
target_name = self._build_file_activity_name(file_code)
|
||
await self._delete_file_code(file_code)
|
||
await self.record_admin_activity(
|
||
action="file.delete",
|
||
target_type="file",
|
||
target_id=file_id,
|
||
target_name=target_name,
|
||
count=1,
|
||
)
|
||
|
||
async def delete_files(self, file_ids: list[int]):
|
||
unique_ids = list(dict.fromkeys(file_ids))
|
||
deleted = []
|
||
failed = []
|
||
missing = []
|
||
|
||
for file_id in unique_ids:
|
||
file_code = await FileCodes.filter(id=file_id).first()
|
||
if not file_code:
|
||
missing.append(file_id)
|
||
continue
|
||
|
||
try:
|
||
await self._delete_file_code(file_code, reason="batch_delete")
|
||
deleted.append(file_id)
|
||
except Exception as exc:
|
||
failed.append({"id": file_id, "reason": str(exc)})
|
||
|
||
if deleted:
|
||
await self.record_admin_activity(
|
||
action="files.batch_delete",
|
||
target_type="file",
|
||
count=len(deleted),
|
||
meta={
|
||
"requestedCount": len(file_ids),
|
||
"uniqueCount": len(unique_ids),
|
||
"deleted": deleted,
|
||
"missing": missing,
|
||
"failedCount": len(failed),
|
||
},
|
||
)
|
||
|
||
return {
|
||
"requestedCount": len(file_ids),
|
||
"requested_count": len(file_ids),
|
||
"uniqueCount": len(unique_ids),
|
||
"unique_count": len(unique_ids),
|
||
"deletedCount": len(deleted),
|
||
"deleted_count": len(deleted),
|
||
"missingCount": len(missing),
|
||
"missing_count": len(missing),
|
||
"failedCount": len(failed),
|
||
"failed_count": len(failed),
|
||
"deleted": deleted,
|
||
"missing": missing,
|
||
"failed": failed,
|
||
}
|
||
|
||
async def update_files(self, file_ids: list[int], update_data: dict[str, Any]):
|
||
unique_ids = list(dict.fromkeys(file_ids))
|
||
updated = []
|
||
failed = []
|
||
missing = []
|
||
|
||
for file_id in unique_ids:
|
||
file_code = await FileCodes.filter(id=file_id).first()
|
||
if not file_code:
|
||
missing.append(file_id)
|
||
continue
|
||
|
||
try:
|
||
await file_code.update_from_dict(update_data).save()
|
||
updated.append(file_id)
|
||
except Exception as exc:
|
||
failed.append({"id": file_id, "reason": str(exc)})
|
||
|
||
if updated:
|
||
await self.record_admin_activity(
|
||
action="files.batch_update",
|
||
target_type="file",
|
||
count=len(updated),
|
||
meta={
|
||
"fields": sorted(update_data.keys()),
|
||
"requestedCount": len(file_ids),
|
||
"uniqueCount": len(unique_ids),
|
||
"updated": updated,
|
||
"missing": missing,
|
||
"failedCount": len(failed),
|
||
},
|
||
)
|
||
|
||
return {
|
||
"requestedCount": len(file_ids),
|
||
"requested_count": len(file_ids),
|
||
"uniqueCount": len(unique_ids),
|
||
"unique_count": len(unique_ids),
|
||
"updatedCount": len(updated),
|
||
"updated_count": len(updated),
|
||
"missingCount": len(missing),
|
||
"missing_count": len(missing),
|
||
"failedCount": len(failed),
|
||
"failed_count": len(failed),
|
||
"updated": updated,
|
||
"missing": missing,
|
||
"failed": failed,
|
||
}
|
||
|
||
async def apply_file_policy_action(
|
||
self,
|
||
file_id: int,
|
||
action: str,
|
||
download_limit: Optional[int] = None,
|
||
) -> dict[str, Any]:
|
||
file_code = await FileCodes.filter(id=file_id).first()
|
||
if not file_code:
|
||
raise HTTPException(status_code=404, detail="文件不存在")
|
||
|
||
action = action.strip().lower()
|
||
now = await get_now()
|
||
update_data = self._build_policy_action_update(
|
||
file_code=file_code,
|
||
action=action,
|
||
now=now,
|
||
download_limit=download_limit,
|
||
)
|
||
|
||
await file_code.update_from_dict(update_data).save()
|
||
await self.record_admin_activity(
|
||
action="file.policy_action",
|
||
target_type="file",
|
||
target_id=file_id,
|
||
target_name=self._build_file_activity_name(file_code),
|
||
count=1,
|
||
meta={"policyAction": action},
|
||
)
|
||
return await self.get_file_detail(file_id)
|
||
|
||
async def get_file_metadata(self, file_id: int) -> dict[str, Any]:
|
||
record = await KeyValue.filter(key=self._file_metadata_key(file_id)).first()
|
||
return self._normalize_file_metadata(record.value if record else None)
|
||
|
||
async def update_file_metadata(
|
||
self,
|
||
file_id: int,
|
||
note: Optional[str],
|
||
tags: Optional[list[str]],
|
||
update_note: bool,
|
||
update_tags: bool,
|
||
) -> dict[str, Any]:
|
||
file_code = await FileCodes.filter(id=file_id).first()
|
||
if not file_code:
|
||
raise HTTPException(status_code=404, detail="文件不存在")
|
||
|
||
current_metadata = await self.get_file_metadata(file_id)
|
||
next_metadata = dict(current_metadata)
|
||
if update_note:
|
||
next_metadata["note"] = self._normalize_metadata_note(note)
|
||
if update_tags:
|
||
next_metadata["tags"] = self._normalize_metadata_tags(tags)
|
||
|
||
now = await get_now()
|
||
updated_at = now.isoformat()
|
||
next_metadata["updatedAt"] = updated_at
|
||
next_metadata["updated_at"] = updated_at
|
||
await KeyValue.update_or_create(
|
||
key=self._file_metadata_key(file_id),
|
||
defaults={"value": next_metadata},
|
||
)
|
||
await self.record_admin_activity(
|
||
action="file.metadata_update",
|
||
target_type="file",
|
||
target_id=file_id,
|
||
target_name=self._build_file_activity_name(file_code),
|
||
count=1,
|
||
meta={
|
||
"updateNote": update_note,
|
||
"updateTags": update_tags,
|
||
"tagCount": len(next_metadata["tags"]),
|
||
},
|
||
)
|
||
return await self.get_file_detail(file_id)
|
||
|
||
async def list_file_view_presets(self) -> dict[str, Any]:
|
||
presets = await self._get_file_view_presets()
|
||
return {
|
||
"presets": presets,
|
||
"items": presets,
|
||
"total": len(presets),
|
||
}
|
||
|
||
async def save_file_view_preset(
|
||
self,
|
||
preset_id: Optional[str],
|
||
name: str,
|
||
filters: dict[str, Any],
|
||
) -> dict[str, Any]:
|
||
async with keyvalue_write_lock:
|
||
presets = await self._get_file_view_presets()
|
||
normalized_name = self._normalize_file_view_preset_name(name)
|
||
normalized_filters = self._normalize_file_view_preset_filters(filters)
|
||
now = await get_now()
|
||
updated_at = now.isoformat()
|
||
|
||
target_index = next(
|
||
(index for index, preset in enumerate(presets) if preset["id"] == preset_id),
|
||
-1,
|
||
)
|
||
is_update = target_index >= 0
|
||
if is_update:
|
||
preset = presets[target_index]
|
||
next_preset = {
|
||
**preset,
|
||
"name": normalized_name,
|
||
"filters": normalized_filters,
|
||
"params": normalized_filters,
|
||
"updatedAt": updated_at,
|
||
"updated_at": updated_at,
|
||
}
|
||
presets[target_index] = next_preset
|
||
else:
|
||
if len(presets) >= self.MAX_VIEW_PRESETS:
|
||
raise HTTPException(status_code=400, detail="视图预设数量已达上限")
|
||
next_preset = {
|
||
"id": preset_id or self._build_file_view_preset_id(normalized_name, now),
|
||
"name": normalized_name,
|
||
"filters": normalized_filters,
|
||
"params": normalized_filters,
|
||
"createdAt": updated_at,
|
||
"created_at": updated_at,
|
||
"updatedAt": updated_at,
|
||
"updated_at": updated_at,
|
||
}
|
||
presets.append(next_preset)
|
||
|
||
await self._save_file_view_presets(presets)
|
||
await self.record_admin_activity(
|
||
action="file.view_preset_update" if is_update else "file.view_preset_create",
|
||
target_type="view_preset",
|
||
target_id=next_preset["id"],
|
||
target_name=next_preset["name"],
|
||
count=1,
|
||
meta={"filters": normalized_filters},
|
||
)
|
||
return next_preset
|
||
|
||
async def delete_file_view_preset(self, preset_id: str) -> dict[str, Any]:
|
||
preset_id = str(preset_id).strip()
|
||
if not preset_id:
|
||
raise HTTPException(status_code=400, detail="请选择要删除的视图预设")
|
||
|
||
async with keyvalue_write_lock:
|
||
presets = await self._get_file_view_presets()
|
||
deleted_preset = next(
|
||
(preset for preset in presets if preset["id"] == preset_id),
|
||
None,
|
||
)
|
||
next_presets = [preset for preset in presets if preset["id"] != preset_id]
|
||
if len(next_presets) == len(presets):
|
||
raise HTTPException(status_code=404, detail="视图预设不存在")
|
||
|
||
await self._save_file_view_presets(next_presets)
|
||
await self.record_admin_activity(
|
||
action="file.view_preset_delete",
|
||
target_type="view_preset",
|
||
target_id=preset_id,
|
||
target_name=(deleted_preset or {}).get("name", ""),
|
||
count=1,
|
||
)
|
||
return {
|
||
"deleted": preset_id,
|
||
"deletedPresetId": preset_id,
|
||
"deleted_preset_id": preset_id,
|
||
"total": len(next_presets),
|
||
}
|
||
|
||
async def apply_files_policy_action(
|
||
self,
|
||
file_ids: list[int],
|
||
action: str,
|
||
download_limit: Optional[int] = None,
|
||
) -> dict[str, Any]:
|
||
unique_ids = list(dict.fromkeys(file_ids))
|
||
updated = []
|
||
failed = []
|
||
missing = []
|
||
action = action.strip().lower()
|
||
|
||
if action not in self.POLICY_ACTIONS:
|
||
raise HTTPException(status_code=400, detail="不支持的策略动作")
|
||
|
||
if action == "reset_download_limit":
|
||
next_limit = download_limit if download_limit is not None else 5
|
||
if next_limit < 1:
|
||
raise HTTPException(status_code=400, detail="取件次数必须大于 0")
|
||
|
||
now = await get_now()
|
||
for file_id in unique_ids:
|
||
file_code = await FileCodes.filter(id=file_id).first()
|
||
if not file_code:
|
||
missing.append(file_id)
|
||
continue
|
||
|
||
try:
|
||
update_data = self._build_policy_action_update(
|
||
file_code=file_code,
|
||
action=action,
|
||
now=now,
|
||
download_limit=download_limit,
|
||
)
|
||
await file_code.update_from_dict(update_data).save()
|
||
updated.append(file_id)
|
||
except Exception as exc:
|
||
failed.append({"id": file_id, "reason": str(exc)})
|
||
|
||
if updated:
|
||
await self.record_admin_activity(
|
||
action="files.batch_policy_action",
|
||
target_type="file",
|
||
count=len(updated),
|
||
meta={
|
||
"policyAction": action,
|
||
"requestedCount": len(file_ids),
|
||
"uniqueCount": len(unique_ids),
|
||
"updated": updated,
|
||
"missing": missing,
|
||
"failedCount": len(failed),
|
||
},
|
||
)
|
||
|
||
return {
|
||
"requestedCount": len(file_ids),
|
||
"requested_count": len(file_ids),
|
||
"uniqueCount": len(unique_ids),
|
||
"unique_count": len(unique_ids),
|
||
"updatedCount": len(updated),
|
||
"updated_count": len(updated),
|
||
"missingCount": len(missing),
|
||
"missing_count": len(missing),
|
||
"failedCount": len(failed),
|
||
"failed_count": len(failed),
|
||
"action": action,
|
||
"updated": updated,
|
||
"missing": missing,
|
||
"failed": failed,
|
||
}
|
||
|
||
async def list_files(
|
||
self,
|
||
page: int,
|
||
size: int,
|
||
keyword: str = "",
|
||
status: str = "",
|
||
file_type: str = "",
|
||
health: str = "",
|
||
sort_by: str = "created_at",
|
||
sort_order: str = "desc",
|
||
):
|
||
page = max(page, 1)
|
||
size = min(max(size, 1), 100)
|
||
keyword = keyword.strip().lower()
|
||
status = status.strip().lower()
|
||
file_type = file_type.strip().lower()
|
||
health = health.strip().lower()
|
||
sort_by = self._normalize_sort_by(sort_by)
|
||
reverse = sort_order.strip().lower() != "asc"
|
||
|
||
all_files = await FileCodes.filter(is_trashed=False).all()
|
||
now = await get_now()
|
||
enriched_files = []
|
||
summary = {
|
||
"totalFiles": len(all_files),
|
||
"activeCount": 0,
|
||
"expiredCount": 0,
|
||
"textCount": 0,
|
||
"fileCount": 0,
|
||
"chunkedCount": 0,
|
||
**self._empty_health_summary(),
|
||
"storageUsed": sum(file_code.size for file_code in all_files if not is_local_ref(file_code)),
|
||
"usedCount": sum(file_code.used_count for file_code in all_files),
|
||
}
|
||
|
||
for file_code in all_files:
|
||
item = await self._build_admin_file_item(file_code, now=now)
|
||
if item["isExpired"]:
|
||
summary["expiredCount"] += 1
|
||
else:
|
||
summary["activeCount"] += 1
|
||
if item["isText"]:
|
||
summary["textCount"] += 1
|
||
else:
|
||
summary["fileCount"] += 1
|
||
if item["isChunked"]:
|
||
summary["chunkedCount"] += 1
|
||
self._accumulate_health_summary(summary, item)
|
||
|
||
if not self._match_admin_file(item, keyword, status, file_type, health):
|
||
continue
|
||
enriched_files.append(item)
|
||
|
||
enriched_files.sort(
|
||
key=lambda item: self._get_sort_value(item, sort_by),
|
||
reverse=reverse,
|
||
)
|
||
offset = (page - 1) * size
|
||
return enriched_files[offset : offset + size], len(enriched_files), summary
|
||
|
||
def _empty_health_summary(self) -> dict[str, int]:
|
||
return {
|
||
"healthAttentionCount": 0,
|
||
"healthDangerCount": 0,
|
||
"healthWarningCount": 0,
|
||
"expiringSoonCount": 0,
|
||
"storageIssueCount": 0,
|
||
"neverRetrievedCount": 0,
|
||
"healthyCount": 0,
|
||
"permanentCount": 0,
|
||
}
|
||
|
||
def _accumulate_health_summary(self, summary: dict[str, Any], item: dict[str, Any]) -> None:
|
||
status_insights = item.get("statusInsights") or {}
|
||
reasons = status_insights.get("reasons") or []
|
||
severity = status_insights.get("severity")
|
||
state = status_insights.get("state")
|
||
|
||
if severity in {"danger", "warning"}:
|
||
summary["healthAttentionCount"] += 1
|
||
if severity == "danger":
|
||
summary["healthDangerCount"] += 1
|
||
if severity == "warning":
|
||
summary["healthWarningCount"] += 1
|
||
if severity == "success":
|
||
summary["healthyCount"] += 1
|
||
if state == "permanent":
|
||
summary["permanentCount"] += 1
|
||
if "expires_soon" in reasons:
|
||
summary["expiringSoonCount"] += 1
|
||
if "storage_metadata_incomplete" in reasons:
|
||
summary["storageIssueCount"] += 1
|
||
if "never_retrieved" in reasons:
|
||
summary["neverRetrievedCount"] += 1
|
||
|
||
async def build_file_health_summary(
|
||
self, file_codes: list[FileCodes], now: Optional[datetime] = None
|
||
) -> dict[str, int]:
|
||
if now is None:
|
||
now = await get_now()
|
||
summary = self._empty_health_summary()
|
||
for file_code in file_codes:
|
||
item = await self._build_admin_file_item(file_code, now=now)
|
||
self._accumulate_health_summary(summary, item)
|
||
return summary
|
||
|
||
async def _build_admin_file_item(
|
||
self, file_code: FileCodes, now: Optional[datetime] = None
|
||
) -> dict[str, Any]:
|
||
if now is None:
|
||
now = await get_now()
|
||
is_text = file_code.text is not None
|
||
is_expired = await file_code.is_expired()
|
||
name = f"{file_code.prefix}{file_code.suffix}"
|
||
has_download_limit = file_code.expired_count >= 0
|
||
is_permanent = file_code.expired_at is None and file_code.expired_count < 0
|
||
can_download = is_text or bool(file_code.file_path or file_code.uuid_file_name)
|
||
remaining_downloads = (
|
||
max(file_code.expired_count, 0) if file_code.expired_count >= 0 else None
|
||
)
|
||
data = {
|
||
"id": file_code.id,
|
||
"code": file_code.code,
|
||
"prefix": file_code.prefix,
|
||
"suffix": file_code.suffix,
|
||
"uuid_file_name": file_code.uuid_file_name,
|
||
"file_path": file_code.file_path,
|
||
"size": file_code.size or 0,
|
||
"text": file_code.text,
|
||
"expired_at": file_code.expired_at,
|
||
"expired_count": file_code.expired_count,
|
||
"used_count": file_code.used_count,
|
||
"created_at": file_code.created_at,
|
||
"file_hash": file_code.file_hash,
|
||
"is_chunked": file_code.is_chunked,
|
||
"upload_id": file_code.upload_id,
|
||
}
|
||
data.update(
|
||
{
|
||
"name": name,
|
||
"type": "text" if is_text else "file",
|
||
"status": "expired" if is_expired else "active",
|
||
"isText": is_text,
|
||
"is_text": is_text,
|
||
"isExpired": is_expired,
|
||
"is_expired": is_expired,
|
||
"isChunked": file_code.is_chunked,
|
||
"is_chunked": file_code.is_chunked,
|
||
"remainingDownloads": remaining_downloads,
|
||
"remaining_downloads": remaining_downloads,
|
||
"usedCount": file_code.used_count,
|
||
"used_count": file_code.used_count,
|
||
"createdAt": file_code.created_at,
|
||
"created_at": file_code.created_at,
|
||
"expiredAt": file_code.expired_at,
|
||
"expired_at": file_code.expired_at,
|
||
"fileHash": file_code.file_hash,
|
||
"file_hash": file_code.file_hash,
|
||
}
|
||
)
|
||
status_insights = self._build_file_status_insights(
|
||
file_code=file_code,
|
||
detail=data,
|
||
now=now,
|
||
has_download_limit=has_download_limit,
|
||
is_permanent=is_permanent,
|
||
can_download=can_download,
|
||
)
|
||
data.update(
|
||
{
|
||
"statusInsights": status_insights,
|
||
"status_insights": status_insights,
|
||
}
|
||
)
|
||
return data
|
||
|
||
async def get_file_detail(self, file_id: int):
|
||
file_code = await FileCodes.filter(id=file_id).first()
|
||
if not file_code:
|
||
raise HTTPException(status_code=404, detail="文件不存在")
|
||
|
||
now = await get_now()
|
||
detail = await self._build_admin_file_item(file_code, now=now)
|
||
is_text = file_code.text is not None
|
||
has_download_limit = file_code.expired_count >= 0
|
||
is_permanent = file_code.expired_at is None and file_code.expired_count < 0
|
||
text_length = len(file_code.text) if file_code.text else 0
|
||
can_download = is_text or bool(file_code.file_path or file_code.uuid_file_name)
|
||
status_insights = self._build_file_status_insights(
|
||
file_code=file_code,
|
||
detail=detail,
|
||
now=now,
|
||
has_download_limit=has_download_limit,
|
||
is_permanent=is_permanent,
|
||
can_download=can_download,
|
||
)
|
||
timeline = await self._build_file_timeline(
|
||
file_code=file_code,
|
||
detail=detail,
|
||
now=now,
|
||
has_download_limit=has_download_limit,
|
||
is_permanent=is_permanent,
|
||
is_text=is_text,
|
||
)
|
||
|
||
detail.update(
|
||
{
|
||
"filename": detail["name"],
|
||
"displayName": detail["name"],
|
||
"display_name": detail["name"],
|
||
"isPermanent": is_permanent,
|
||
"is_permanent": is_permanent,
|
||
"hasDownloadLimit": has_download_limit,
|
||
"has_download_limit": has_download_limit,
|
||
"hasExpirationTime": file_code.expired_at is not None,
|
||
"has_expiration_time": file_code.expired_at is not None,
|
||
"textLength": text_length,
|
||
"text_length": text_length,
|
||
"canPreviewText": is_text,
|
||
"can_preview_text": is_text,
|
||
"canDownload": can_download,
|
||
"can_download": can_download,
|
||
"storageBackend": settings.file_storage,
|
||
"storage_backend": settings.file_storage,
|
||
"filePath": file_code.file_path,
|
||
"file_path": file_code.file_path,
|
||
"uuidFileName": file_code.uuid_file_name,
|
||
"uuid_file_name": file_code.uuid_file_name,
|
||
"uploadId": file_code.upload_id,
|
||
"upload_id": file_code.upload_id,
|
||
"policy": {
|
||
"expiredAt": file_code.expired_at,
|
||
"expired_at": file_code.expired_at,
|
||
"expiredCount": file_code.expired_count,
|
||
"expired_count": file_code.expired_count,
|
||
"remainingDownloads": detail["remainingDownloads"],
|
||
"remaining_downloads": detail["remaining_downloads"],
|
||
"isExpired": detail["isExpired"],
|
||
"is_expired": detail["is_expired"],
|
||
"isPermanent": is_permanent,
|
||
"is_permanent": is_permanent,
|
||
},
|
||
"storage": {
|
||
"backend": settings.file_storage,
|
||
"filePath": file_code.file_path,
|
||
"file_path": file_code.file_path,
|
||
"uuidFileName": file_code.uuid_file_name,
|
||
"uuid_file_name": file_code.uuid_file_name,
|
||
"fileHash": file_code.file_hash,
|
||
"file_hash": file_code.file_hash,
|
||
"isChunked": file_code.is_chunked,
|
||
"is_chunked": file_code.is_chunked,
|
||
"uploadId": file_code.upload_id,
|
||
"upload_id": file_code.upload_id,
|
||
},
|
||
"statusInsights": status_insights,
|
||
"status_insights": status_insights,
|
||
"timeline": timeline,
|
||
}
|
||
)
|
||
metadata = await self.get_file_metadata(file_id)
|
||
detail.update(
|
||
{
|
||
"metadata": metadata,
|
||
"meta": metadata,
|
||
"note": metadata["note"],
|
||
"tags": metadata["tags"],
|
||
"metadataUpdatedAt": metadata["updatedAt"],
|
||
"metadata_updated_at": metadata["updated_at"],
|
||
}
|
||
)
|
||
return detail
|
||
|
||
def _normalize_metadata_note(self, note: Optional[str]) -> str:
|
||
if note is None:
|
||
return ""
|
||
return str(note).strip()[: self.MAX_METADATA_NOTE_LENGTH]
|
||
|
||
def _normalize_metadata_tags(self, tags: Any) -> list[str]:
|
||
if not tags:
|
||
return []
|
||
if isinstance(tags, str):
|
||
tags = [tags]
|
||
elif not isinstance(tags, list):
|
||
return []
|
||
|
||
normalized_tags = []
|
||
seen_tags = set()
|
||
for raw_tag in tags:
|
||
tag = str(raw_tag).strip()
|
||
if not tag:
|
||
continue
|
||
tag = tag[: self.MAX_METADATA_TAG_LENGTH]
|
||
dedupe_key = tag.lower()
|
||
if dedupe_key in seen_tags:
|
||
continue
|
||
seen_tags.add(dedupe_key)
|
||
normalized_tags.append(tag)
|
||
if len(normalized_tags) >= self.MAX_METADATA_TAGS:
|
||
break
|
||
return normalized_tags
|
||
|
||
def _normalize_file_metadata(self, metadata: Any) -> dict[str, Any]:
|
||
if not isinstance(metadata, dict):
|
||
metadata = {}
|
||
|
||
updated_at = metadata.get("updatedAt") or metadata.get("updated_at")
|
||
return {
|
||
"note": self._normalize_metadata_note(metadata.get("note")),
|
||
"tags": self._normalize_metadata_tags(metadata.get("tags")),
|
||
"updatedAt": updated_at,
|
||
"updated_at": updated_at,
|
||
}
|
||
|
||
async def list_admin_activities(
|
||
self,
|
||
page: int = 1,
|
||
size: int = 20,
|
||
limit: Optional[int] = None,
|
||
action: Optional[str] = None,
|
||
target_type: Optional[str] = None,
|
||
keyword: Optional[str] = None,
|
||
) -> dict[str, Any]:
|
||
if limit is not None:
|
||
size = limit
|
||
try:
|
||
page = max(int(page or 1), 1)
|
||
except (TypeError, ValueError):
|
||
page = 1
|
||
try:
|
||
size = int(size or 20)
|
||
except (TypeError, ValueError):
|
||
size = 20
|
||
size = min(max(size, 1), 100)
|
||
activities = await self._get_admin_activities()
|
||
normalized_action = self._normalize_admin_activity_text(action).lower()
|
||
normalized_target_type = self._normalize_admin_activity_text(target_type).lower()
|
||
normalized_keyword = self._normalize_admin_activity_text(keyword).lower()
|
||
filtered_activities = self._filter_admin_activities(
|
||
activities,
|
||
action=normalized_action,
|
||
target_type=normalized_target_type,
|
||
keyword=normalized_keyword,
|
||
)
|
||
offset = (page - 1) * size
|
||
visible_activities = filtered_activities[offset : offset + size]
|
||
action_options = self._build_admin_activity_options(activities, "action")
|
||
target_type_options = self._build_admin_activity_options(activities, "targetType")
|
||
return {
|
||
"activities": visible_activities,
|
||
"items": visible_activities,
|
||
"page": page,
|
||
"size": size,
|
||
"total": len(filtered_activities),
|
||
"storedTotal": len(activities),
|
||
"stored_total": len(activities),
|
||
"filters": {
|
||
"action": normalized_action,
|
||
"targetType": normalized_target_type,
|
||
"target_type": normalized_target_type,
|
||
"keyword": normalized_keyword,
|
||
},
|
||
"actionOptions": action_options,
|
||
"action_options": action_options,
|
||
"targetTypeOptions": target_type_options,
|
||
"target_type_options": target_type_options,
|
||
}
|
||
|
||
async def record_admin_activity(
|
||
self,
|
||
action: str,
|
||
target_type: str,
|
||
target_id: Optional[Any] = None,
|
||
target_name: str = "",
|
||
count: int = 1,
|
||
meta: Optional[dict[str, Any]] = None,
|
||
) -> Optional[dict[str, Any]]:
|
||
try:
|
||
now = await get_now()
|
||
created_at = now.isoformat()
|
||
activity = self._normalize_admin_activity(
|
||
{
|
||
"id": self._build_admin_activity_id(
|
||
action=action,
|
||
target_type=target_type,
|
||
target_id=target_id,
|
||
target_name=target_name,
|
||
timestamp=now,
|
||
),
|
||
"action": action,
|
||
"targetType": target_type,
|
||
"target_type": target_type,
|
||
"targetId": target_id,
|
||
"target_id": target_id,
|
||
"targetName": target_name,
|
||
"target_name": target_name,
|
||
"count": count,
|
||
"meta": meta or {},
|
||
"createdAt": created_at,
|
||
"created_at": created_at,
|
||
}
|
||
)
|
||
if not activity:
|
||
return None
|
||
|
||
async with keyvalue_write_lock:
|
||
activities = await self._get_admin_activities()
|
||
next_activities = [
|
||
activity,
|
||
*[item for item in activities if item["id"] != activity["id"]],
|
||
][: self.MAX_ADMIN_ACTIVITIES]
|
||
await self._save_admin_activities(next_activities)
|
||
return activity
|
||
except Exception:
|
||
return None
|
||
|
||
async def _get_admin_activities(self) -> list[dict[str, Any]]:
|
||
record = await KeyValue.filter(key=self.ADMIN_ACTIVITY_KEY).first()
|
||
raw_activities = record.value if record else []
|
||
if isinstance(raw_activities, dict):
|
||
raw_activities = (
|
||
raw_activities.get("activities") or raw_activities.get("items") or []
|
||
)
|
||
if not isinstance(raw_activities, list):
|
||
return []
|
||
|
||
activities = []
|
||
seen_ids = set()
|
||
for raw_activity in raw_activities:
|
||
activity = self._normalize_admin_activity(raw_activity)
|
||
if not activity or activity["id"] in seen_ids:
|
||
continue
|
||
seen_ids.add(activity["id"])
|
||
activities.append(activity)
|
||
if len(activities) >= self.MAX_ADMIN_ACTIVITIES:
|
||
break
|
||
|
||
activities.sort(key=lambda item: item.get("createdAt") or "", reverse=True)
|
||
return activities
|
||
|
||
async def _save_admin_activities(self, activities: list[dict[str, Any]]) -> None:
|
||
await KeyValue.update_or_create(
|
||
key=self.ADMIN_ACTIVITY_KEY,
|
||
defaults={"value": {"activities": activities}},
|
||
)
|
||
|
||
def _normalize_admin_activity(self, activity: Any) -> Optional[dict[str, Any]]:
|
||
if not isinstance(activity, dict):
|
||
return None
|
||
|
||
action = self._normalize_admin_activity_text(activity.get("action"))
|
||
target_type = self._normalize_admin_activity_text(
|
||
activity.get("targetType") or activity.get("target_type") or "system"
|
||
)
|
||
if not action:
|
||
return None
|
||
|
||
target_name = self._normalize_admin_activity_text(
|
||
activity.get("targetName") or activity.get("target_name")
|
||
)
|
||
created_at = activity.get("createdAt") or activity.get("created_at")
|
||
if isinstance(created_at, datetime):
|
||
created_at = created_at.isoformat()
|
||
created_at = str(created_at or "")
|
||
if not created_at:
|
||
return None
|
||
|
||
target_id = activity.get("targetId")
|
||
if target_id is None:
|
||
target_id = activity.get("target_id")
|
||
|
||
count = activity.get("count", 1)
|
||
try:
|
||
count = max(int(count), 1)
|
||
except (TypeError, ValueError):
|
||
count = 1
|
||
|
||
meta = activity.get("meta")
|
||
if not isinstance(meta, dict):
|
||
meta = {}
|
||
|
||
activity_id = self._normalize_admin_activity_text(activity.get("id"))
|
||
if not activity_id:
|
||
activity_id = self._build_admin_activity_id(
|
||
action=action,
|
||
target_type=target_type,
|
||
target_id=target_id,
|
||
target_name=target_name,
|
||
timestamp=None,
|
||
seed=created_at,
|
||
)
|
||
|
||
return {
|
||
"id": activity_id,
|
||
"action": action,
|
||
"targetType": target_type,
|
||
"target_type": target_type,
|
||
"targetId": target_id,
|
||
"target_id": target_id,
|
||
"targetName": target_name,
|
||
"target_name": target_name,
|
||
"count": count,
|
||
"meta": meta,
|
||
"createdAt": created_at,
|
||
"created_at": created_at,
|
||
}
|
||
|
||
def _normalize_admin_activity_text(self, value: Any) -> str:
|
||
return str(value or "").strip()[: self.MAX_ADMIN_ACTIVITY_TEXT_LENGTH]
|
||
|
||
def _filter_admin_activities(
|
||
self,
|
||
activities: list[dict[str, Any]],
|
||
action: str,
|
||
target_type: str,
|
||
keyword: str,
|
||
) -> list[dict[str, Any]]:
|
||
filtered_activities = []
|
||
for activity in activities:
|
||
if action and str(activity.get("action") or "").lower() != action:
|
||
continue
|
||
if target_type and str(activity.get("targetType") or "").lower() != target_type:
|
||
continue
|
||
if keyword and not self._activity_matches_keyword(activity, keyword):
|
||
continue
|
||
filtered_activities.append(activity)
|
||
return filtered_activities
|
||
|
||
def _activity_matches_keyword(self, activity: dict[str, Any], keyword: str) -> bool:
|
||
searchable_values = [
|
||
activity.get("action"),
|
||
activity.get("targetType"),
|
||
activity.get("target_type"),
|
||
activity.get("targetId"),
|
||
activity.get("target_id"),
|
||
activity.get("targetName"),
|
||
activity.get("target_name"),
|
||
]
|
||
meta = activity.get("meta")
|
||
if isinstance(meta, dict):
|
||
searchable_values.extend(meta.values())
|
||
|
||
return any(keyword in str(value or "").lower() for value in searchable_values)
|
||
|
||
def _build_admin_activity_options(
|
||
self,
|
||
activities: list[dict[str, Any]],
|
||
field: str,
|
||
) -> list[dict[str, Any]]:
|
||
counters: dict[str, dict[str, Any]] = {}
|
||
for activity in activities:
|
||
raw_value = self._normalize_admin_activity_text(activity.get(field))
|
||
if not raw_value:
|
||
continue
|
||
value = raw_value.lower()
|
||
if value not in counters:
|
||
counters[value] = {"label": raw_value, "count": 0}
|
||
counters[value]["count"] += 1
|
||
|
||
return [
|
||
{
|
||
"value": value,
|
||
"label": option["label"],
|
||
"count": option["count"],
|
||
}
|
||
for value, option in sorted(
|
||
counters.items(),
|
||
key=lambda item: (-item[1]["count"], item[0]),
|
||
)
|
||
]
|
||
|
||
def _build_admin_activity_id(
|
||
self,
|
||
action: str,
|
||
target_type: str,
|
||
target_id: Optional[Any],
|
||
target_name: str,
|
||
timestamp: Optional[datetime],
|
||
seed: Optional[str] = None,
|
||
) -> str:
|
||
timestamp_seed = (
|
||
str(int(timestamp.timestamp() * 1000)) if timestamp else str(seed or "activity")
|
||
)
|
||
digest = hashlib.sha1(
|
||
f"{timestamp_seed}:{action}:{target_type}:{target_id}:{target_name}".encode("utf-8")
|
||
).hexdigest()[:10]
|
||
return f"act_{timestamp_seed}_{digest}"
|
||
|
||
def _build_file_activity_name(self, file_code: FileCodes) -> str:
|
||
return (file_code.prefix + file_code.suffix) or file_code.code
|
||
|
||
async def _get_file_view_presets(self) -> list[dict[str, Any]]:
|
||
record = await KeyValue.filter(key=self.FILE_VIEW_PRESETS_KEY).first()
|
||
raw_presets = record.value if record else []
|
||
if isinstance(raw_presets, dict):
|
||
raw_presets = raw_presets.get("presets") or raw_presets.get("items") or []
|
||
if not isinstance(raw_presets, list):
|
||
return []
|
||
|
||
presets = []
|
||
seen_ids = set()
|
||
for raw_preset in raw_presets:
|
||
try:
|
||
preset = self._normalize_file_view_preset(raw_preset)
|
||
except HTTPException:
|
||
continue
|
||
if not preset or preset["id"] in seen_ids:
|
||
continue
|
||
seen_ids.add(preset["id"])
|
||
presets.append(preset)
|
||
if len(presets) >= self.MAX_VIEW_PRESETS:
|
||
break
|
||
return presets
|
||
|
||
async def _save_file_view_presets(self, presets: list[dict[str, Any]]) -> None:
|
||
await KeyValue.update_or_create(
|
||
key=self.FILE_VIEW_PRESETS_KEY,
|
||
defaults={"value": {"presets": presets}},
|
||
)
|
||
|
||
def _normalize_file_view_preset(self, preset: Any) -> Optional[dict[str, Any]]:
|
||
if not isinstance(preset, dict):
|
||
return None
|
||
|
||
preset_id = str(preset.get("id") or "").strip()
|
||
raw_name = str(preset.get("name") or "").strip()
|
||
if not raw_name:
|
||
return None
|
||
name = self._normalize_file_view_preset_name(raw_name)
|
||
if not preset_id:
|
||
preset_id = self._build_file_view_preset_id(name)
|
||
|
||
filters = preset.get("filters") or preset.get("params") or {}
|
||
normalized_filters = self._normalize_file_view_preset_filters(filters)
|
||
created_at = preset.get("createdAt") or preset.get("created_at")
|
||
updated_at = preset.get("updatedAt") or preset.get("updated_at")
|
||
|
||
return {
|
||
"id": preset_id,
|
||
"name": name,
|
||
"filters": normalized_filters,
|
||
"params": normalized_filters,
|
||
"createdAt": created_at,
|
||
"created_at": created_at,
|
||
"updatedAt": updated_at,
|
||
"updated_at": updated_at,
|
||
}
|
||
|
||
def _normalize_file_view_preset_name(self, name: Any) -> str:
|
||
normalized_name = str(name or "").strip()
|
||
if not normalized_name:
|
||
raise HTTPException(status_code=400, detail="请输入视图名称")
|
||
return normalized_name[: self.MAX_VIEW_PRESET_NAME_LENGTH]
|
||
|
||
def _normalize_file_view_preset_filters(self, filters: Any) -> dict[str, Any]:
|
||
if not isinstance(filters, dict):
|
||
filters = {}
|
||
|
||
sort_by = str(filters.get("sortBy") or filters.get("sort_by") or "created_at")
|
||
sort_by = sort_by.replace("-", "_").strip().lower()
|
||
if sort_by not in self.VIEW_PRESET_SORT_FIELDS:
|
||
sort_by = "created_at"
|
||
|
||
sort_order = str(filters.get("sortOrder") or filters.get("sort_order") or "desc")
|
||
sort_order = sort_order.strip().lower()
|
||
if sort_order not in {"asc", "desc"}:
|
||
sort_order = "desc"
|
||
|
||
size = filters.get("size", 10)
|
||
try:
|
||
size = int(size)
|
||
except (TypeError, ValueError):
|
||
size = 10
|
||
|
||
return {
|
||
"keyword": str(filters.get("keyword") or "").strip()[
|
||
: self.MAX_VIEW_PRESET_KEYWORD_LENGTH
|
||
],
|
||
"status": self._normalize_file_view_preset_choice(
|
||
filters.get("status"), self.VIEW_PRESET_STATUS_VALUES
|
||
),
|
||
"type": self._normalize_file_view_preset_choice(
|
||
filters.get("type"), self.VIEW_PRESET_TYPE_VALUES
|
||
),
|
||
"health": self._normalize_file_view_preset_choice(
|
||
filters.get("health"), self.VIEW_PRESET_HEALTH_VALUES
|
||
),
|
||
"sortBy": sort_by,
|
||
"sortOrder": sort_order,
|
||
"size": min(max(size, 1), 100),
|
||
}
|
||
|
||
def _normalize_file_view_preset_choice(self, value: Any, allowed_values: set[str]) -> str:
|
||
normalized_value = str(value or "all").strip().lower()
|
||
if normalized_value not in allowed_values:
|
||
return "all"
|
||
return normalized_value
|
||
|
||
def _build_file_view_preset_id(
|
||
self, name: str, timestamp: Optional[datetime] = None
|
||
) -> str:
|
||
seed = int(timestamp.timestamp() * 1000) if timestamp else "saved"
|
||
digest = hashlib.sha1(name.encode("utf-8")).hexdigest()[:8]
|
||
return f"view_{seed}_{digest}"
|
||
|
||
def _build_file_status_insights(
|
||
self,
|
||
file_code: FileCodes,
|
||
detail: dict[str, Any],
|
||
now: datetime,
|
||
has_download_limit: bool,
|
||
is_permanent: bool,
|
||
can_download: bool,
|
||
) -> dict[str, Any]:
|
||
remaining_downloads = detail["remainingDownloads"]
|
||
seconds_until_expiration = self._seconds_between(now, file_code.expired_at)
|
||
age_seconds = self._seconds_between(file_code.created_at, now)
|
||
reasons = []
|
||
|
||
if detail["isExpired"]:
|
||
reasons.append("expired")
|
||
if has_download_limit and remaining_downloads == 0:
|
||
reasons.append("download_limit_exhausted")
|
||
if seconds_until_expiration is not None and 0 < seconds_until_expiration <= 86400:
|
||
reasons.append("expires_soon")
|
||
if file_code.used_count == 0:
|
||
reasons.append("never_retrieved")
|
||
if not can_download:
|
||
reasons.append("storage_metadata_incomplete")
|
||
if file_code.is_chunked:
|
||
reasons.append("chunked_upload")
|
||
|
||
severity = "success"
|
||
state = "available"
|
||
next_action = "monitor"
|
||
if detail["isExpired"] or (has_download_limit and remaining_downloads == 0):
|
||
severity = "danger"
|
||
state = "expired"
|
||
next_action = "extend_or_delete"
|
||
elif not can_download:
|
||
severity = "danger"
|
||
state = "storage_incomplete"
|
||
next_action = "inspect_storage"
|
||
elif "expires_soon" in reasons:
|
||
severity = "warning"
|
||
state = "expiring_soon"
|
||
next_action = "extend_expiration"
|
||
elif is_permanent:
|
||
state = "permanent"
|
||
next_action = "monitor"
|
||
|
||
return {
|
||
"severity": severity,
|
||
"state": state,
|
||
"nextAction": next_action,
|
||
"next_action": next_action,
|
||
"reasons": reasons,
|
||
"metrics": {
|
||
"ageSeconds": max(age_seconds or 0, 0),
|
||
"age_seconds": max(age_seconds or 0, 0),
|
||
"secondsUntilExpiration": seconds_until_expiration,
|
||
"seconds_until_expiration": seconds_until_expiration,
|
||
"remainingDownloads": remaining_downloads,
|
||
"remaining_downloads": remaining_downloads,
|
||
"usedCount": file_code.used_count,
|
||
"used_count": file_code.used_count,
|
||
},
|
||
}
|
||
|
||
async def _build_file_timeline(
|
||
self,
|
||
file_code: FileCodes,
|
||
detail: dict[str, Any],
|
||
now: datetime,
|
||
has_download_limit: bool,
|
||
is_permanent: bool,
|
||
is_text: bool,
|
||
) -> list[dict[str, Any]]:
|
||
remaining_downloads = detail["remainingDownloads"]
|
||
seconds_until_expiration = self._seconds_between(now, file_code.expired_at)
|
||
timeline = [
|
||
{
|
||
"key": "created",
|
||
"status": "done",
|
||
"severity": "success",
|
||
"timestamp": file_code.created_at,
|
||
},
|
||
{
|
||
"key": "content_ready",
|
||
"status": "done",
|
||
"severity": "success",
|
||
"timestamp": file_code.created_at,
|
||
"detail": "text" if is_text else "file",
|
||
},
|
||
]
|
||
|
||
if file_code.upload_id:
|
||
timeline.append(
|
||
{
|
||
"key": "upload_session",
|
||
"status": "done",
|
||
"severity": "info",
|
||
"timestamp": file_code.created_at,
|
||
"detail": file_code.upload_id,
|
||
}
|
||
)
|
||
|
||
if is_permanent:
|
||
timeline.append(
|
||
{
|
||
"key": "expiration_policy",
|
||
"status": "unlimited",
|
||
"severity": "success",
|
||
"timestamp": file_code.created_at,
|
||
}
|
||
)
|
||
elif file_code.expired_at is not None:
|
||
expired = seconds_until_expiration is not None and seconds_until_expiration <= 0
|
||
timeline.append(
|
||
{
|
||
"key": "expiration_policy",
|
||
"status": "expired" if expired else "pending",
|
||
"severity": "danger" if expired else "warning",
|
||
"timestamp": file_code.expired_at,
|
||
"value": seconds_until_expiration,
|
||
}
|
||
)
|
||
|
||
if has_download_limit:
|
||
exhausted = remaining_downloads == 0
|
||
timeline.append(
|
||
{
|
||
"key": "download_limit",
|
||
"status": "exhausted" if exhausted else "active",
|
||
"severity": "danger" if exhausted else "info",
|
||
"timestamp": file_code.created_at,
|
||
"value": remaining_downloads,
|
||
}
|
||
)
|
||
else:
|
||
timeline.append(
|
||
{
|
||
"key": "download_limit",
|
||
"status": "unlimited",
|
||
"severity": "success",
|
||
"timestamp": file_code.created_at,
|
||
"value": None,
|
||
}
|
||
)
|
||
|
||
download_stats = await TransferStats.filter(
|
||
action="download", file_code_id=file_code.id
|
||
).order_by("created_at")
|
||
download_event_count = len(download_stats)
|
||
display_download_count = max(int(file_code.used_count or 0), download_event_count)
|
||
timeline.append(
|
||
{
|
||
"key": "retrieved",
|
||
"status": "done" if display_download_count > 0 else "pending",
|
||
"severity": "success" if display_download_count > 0 else "neutral",
|
||
"timestamp": (
|
||
download_stats[-1].created_at if download_stats else file_code.created_at
|
||
),
|
||
"value": display_download_count,
|
||
"event_count": download_event_count,
|
||
}
|
||
)
|
||
lookup_stats = await TransferStats.filter(
|
||
action="lookup", file_code_id=file_code.id
|
||
).order_by("created_at", "id")
|
||
events = []
|
||
for index, stat in enumerate(lookup_stats, start=1):
|
||
events.append({
|
||
"key": "queried_event",
|
||
"status": "done",
|
||
"severity": "info",
|
||
"timestamp": stat.created_at,
|
||
"value": index,
|
||
"detail": stat.ip or "",
|
||
})
|
||
for index, stat in enumerate(download_stats, start=1):
|
||
events.append(
|
||
{
|
||
"key": "retrieved_event",
|
||
"status": "done",
|
||
"severity": "success",
|
||
"timestamp": stat.created_at,
|
||
"value": index,
|
||
"detail": stat.ip or "",
|
||
}
|
||
)
|
||
timeline.extend(sorted(events, key=lambda event: event["timestamp"]))
|
||
return timeline
|
||
|
||
def _seconds_between(
|
||
self, start: Optional[datetime], end: Optional[datetime]
|
||
) -> Optional[int]:
|
||
if start is None or end is None:
|
||
return None
|
||
if start.tzinfo is None and end.tzinfo is not None:
|
||
end = end.replace(tzinfo=None)
|
||
elif start.tzinfo is not None and end.tzinfo is None:
|
||
start = start.replace(tzinfo=None)
|
||
return int((end - start).total_seconds())
|
||
|
||
def _build_policy_action_update(
|
||
self,
|
||
file_code: FileCodes,
|
||
action: str,
|
||
now: datetime,
|
||
download_limit: Optional[int],
|
||
) -> dict[str, Any]:
|
||
if action == "extend_24h":
|
||
return {"expired_at": self._extended_expiration(file_code.expired_at, now, hours=24)}
|
||
if action == "extend_7d":
|
||
return {"expired_at": self._extended_expiration(file_code.expired_at, now, days=7)}
|
||
if action == "make_permanent":
|
||
return {"expired_at": None, "expired_count": -1}
|
||
if action == "reset_download_limit":
|
||
next_limit = download_limit if download_limit is not None else 5
|
||
if next_limit < 1:
|
||
raise HTTPException(status_code=400, detail="取件次数必须大于 0")
|
||
return {"expired_count": next_limit}
|
||
|
||
raise HTTPException(status_code=400, detail="不支持的策略动作")
|
||
|
||
def _extended_expiration(
|
||
self,
|
||
expired_at: Optional[datetime],
|
||
now: datetime,
|
||
**duration: int,
|
||
) -> datetime:
|
||
base_time = now
|
||
if expired_at is not None:
|
||
comparable_expired_at = self._align_datetime(expired_at, now)
|
||
if comparable_expired_at > now:
|
||
base_time = comparable_expired_at
|
||
return base_time + timedelta(**duration)
|
||
|
||
def _align_datetime(self, value: datetime, reference: datetime) -> datetime:
|
||
if value.tzinfo is None and reference.tzinfo is not None:
|
||
return value.replace(tzinfo=reference.tzinfo)
|
||
if value.tzinfo is not None and reference.tzinfo is None:
|
||
return value.replace(tzinfo=None)
|
||
return value
|
||
|
||
def _match_admin_file(
|
||
self,
|
||
item: dict[str, Any],
|
||
keyword: str,
|
||
status: str,
|
||
file_type: str,
|
||
health: str,
|
||
) -> bool:
|
||
if status == "active" and item["isExpired"]:
|
||
return False
|
||
if status == "expired" and not item["isExpired"]:
|
||
return False
|
||
if file_type == "text" and not item["isText"]:
|
||
return False
|
||
if file_type == "file" and item["isText"]:
|
||
return False
|
||
if file_type == "chunked" and not item["isChunked"]:
|
||
return False
|
||
if not self._match_admin_file_health(item, health):
|
||
return False
|
||
if not keyword:
|
||
return True
|
||
|
||
search_values = [
|
||
item.get("code"),
|
||
item.get("name"),
|
||
item.get("prefix"),
|
||
item.get("suffix"),
|
||
item.get("fileHash"),
|
||
item.get("text"),
|
||
]
|
||
return any(keyword in str(value).lower() for value in search_values if value)
|
||
|
||
def _match_admin_file_health(self, item: dict[str, Any], health: str) -> bool:
|
||
if not health or health == "all":
|
||
return True
|
||
|
||
status_insights = item.get("statusInsights") or {}
|
||
severity = status_insights.get("severity")
|
||
state = status_insights.get("state")
|
||
reasons = set(status_insights.get("reasons") or [])
|
||
|
||
if health == "attention":
|
||
return severity in {"danger", "warning"}
|
||
if health == "danger":
|
||
return severity == "danger"
|
||
if health == "warning":
|
||
return severity == "warning"
|
||
if health == "expired":
|
||
return state == "expired" or item.get("isExpired") is True
|
||
if health == "expiring_soon":
|
||
return "expires_soon" in reasons
|
||
if health == "storage_issue":
|
||
return state == "storage_incomplete" or "storage_metadata_incomplete" in reasons
|
||
if health == "never_retrieved":
|
||
return "never_retrieved" in reasons
|
||
if health == "healthy":
|
||
return severity == "success"
|
||
if health == "permanent":
|
||
return state == "permanent"
|
||
|
||
return True
|
||
|
||
def _normalize_sort_by(self, sort_by: str) -> str:
|
||
normalized = sort_by.replace("-", "_").strip().lower()
|
||
if normalized not in self.SORT_FIELDS:
|
||
return "created_at"
|
||
return normalized
|
||
|
||
def _get_sort_value(self, item: dict[str, Any], sort_by: str):
|
||
def date_value(value: Any) -> float:
|
||
if value is None:
|
||
return 0
|
||
if isinstance(value, datetime):
|
||
return value.timestamp()
|
||
return 0
|
||
|
||
sort_map = {
|
||
"created_at": date_value(item.get("createdAt")),
|
||
"createdat": date_value(item.get("createdAt")),
|
||
"expired_at": date_value(item.get("expiredAt")),
|
||
"expiredat": date_value(item.get("expiredAt")),
|
||
"name": item.get("name") or "",
|
||
"size": item.get("size") or 0,
|
||
"used_count": item.get("usedCount") or 0,
|
||
"usedcount": item.get("usedCount") or 0,
|
||
"code": item.get("code") or "",
|
||
}
|
||
return sort_map.get(sort_by)
|
||
|
||
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:
|
||
if is_local_ref(file_code):
|
||
return build_local_ref_download(file_code)
|
||
return await self.file_storage.get_file_response(file_code)
|
||
|
||
async def preview_file(self, file_id: int, max_chars: int = 4000):
|
||
max_chars = min(max(max_chars, 1), 20000)
|
||
file_code = await FileCodes.filter(id=file_id).first()
|
||
if not file_code:
|
||
raise HTTPException(status_code=404, detail="文件不存在")
|
||
if file_code.text is None:
|
||
raise HTTPException(status_code=400, detail="仅文本分享支持预览")
|
||
|
||
content = file_code.text
|
||
preview = content[:max_chars]
|
||
return {
|
||
"id": file_code.id,
|
||
"code": file_code.code,
|
||
"name": f"{file_code.prefix}{file_code.suffix}",
|
||
"type": "text",
|
||
"content": preview,
|
||
"length": len(content),
|
||
"previewLength": len(preview),
|
||
"preview_length": len(preview),
|
||
"truncated": len(content) > max_chars,
|
||
"maxChars": max_chars,
|
||
"max_chars": max_chars,
|
||
"createdAt": file_code.created_at,
|
||
"created_at": file_code.created_at,
|
||
"expiredAt": file_code.expired_at,
|
||
"expired_at": file_code.expired_at,
|
||
}
|
||
|
||
async def share_local_file(self, item):
|
||
validate_expire_style(item.expire_style)
|
||
local_file = LocalFileClass(item.filename)
|
||
if not await local_file.exists():
|
||
raise HTTPException(status_code=404, detail="文件不存在")
|
||
|
||
expired_at, expired_count, used_count, code = await get_expire_info(
|
||
item.expire_value, item.expire_style
|
||
)
|
||
name = local_file.name
|
||
suffix = Path(name).suffix
|
||
prefix = name[: len(name) - len(suffix)] if suffix else name
|
||
record = await FileCodes.create(
|
||
code=code,
|
||
prefix=prefix,
|
||
suffix=suffix,
|
||
uuid_file_name=local_file.file,
|
||
file_path=LOCAL_REF_MARKER,
|
||
size=local_file.size or 0,
|
||
expired_at=expired_at,
|
||
expired_count=expired_count,
|
||
used_count=used_count,
|
||
)
|
||
return {
|
||
"code": code,
|
||
"name": name,
|
||
"path": local_file.file,
|
||
"id": record.id,
|
||
}
|
||
|
||
|
||
class ConfigService:
|
||
INT_FIELDS = {
|
||
"adminSessionExpire",
|
||
"trashRetentionDays",
|
||
"enableChunk",
|
||
"chunkSizeMB",
|
||
"downloadCheckInterval",
|
||
"errorCount",
|
||
"errorMinute",
|
||
"loginCount",
|
||
"loginMinute",
|
||
"max_save_seconds",
|
||
"onedrive_proxy",
|
||
"openUpload",
|
||
"port",
|
||
"s3_proxy",
|
||
"serverPort",
|
||
"serverWorkers",
|
||
"showAdminAddr",
|
||
"storageLimit",
|
||
"uploadCount",
|
||
"uploadMinute",
|
||
"uploadSize",
|
||
"webdav_proxy",
|
||
}
|
||
FLOAT_FIELDS = {"opacity"}
|
||
|
||
def get_config(self):
|
||
config = dict(settings.items())
|
||
config["admin_token"] = ""
|
||
for key in INTERNAL_CONFIG_KEYS:
|
||
config.pop(key, None)
|
||
return config
|
||
|
||
async def update_config(self, data: dict):
|
||
async with keyvalue_write_lock:
|
||
await refresh_settings(force=True)
|
||
current_config = dict(settings.items())
|
||
next_config = dict(current_config)
|
||
update_data = {
|
||
key: value
|
||
for key, value in data.items()
|
||
if key in settings.default_config and key not in INTERNAL_CONFIG_KEYS
|
||
}
|
||
|
||
if "downloadCheckInterval" in update_data:
|
||
interval = update_data["downloadCheckInterval"]
|
||
if isinstance(interval, bool) or not isinstance(interval, int) or (
|
||
interval != 0 and not 30 <= interval <= 86400
|
||
):
|
||
raise HTTPException(400, "检测间隔必须是 0(关闭)或 30–86400 秒的整数")
|
||
|
||
if "chunkSizeMB" in update_data:
|
||
raw_size = update_data["chunkSizeMB"]
|
||
if isinstance(raw_size, bool) or not (
|
||
isinstance(raw_size, int) or
|
||
isinstance(raw_size, str) and raw_size.strip().isdigit()
|
||
):
|
||
raise HTTPException(400, "分片大小必须是 1–100 MB 的整数")
|
||
if not 1 <= int(raw_size) <= 100:
|
||
raise HTTPException(400, "分片大小必须是 1–100 MB 的整数")
|
||
|
||
admin_token = update_data.get("admin_token")
|
||
admin_password_changed = False
|
||
if admin_token is None or admin_token == "":
|
||
update_data.pop("admin_token", None)
|
||
elif not is_password_hashed(admin_token):
|
||
update_data["admin_token"] = hash_password(admin_token)
|
||
admin_password_changed = True
|
||
else:
|
||
admin_password_changed = True
|
||
|
||
for key, value in update_data.items():
|
||
if value == "" and key in self.INT_FIELDS | self.FLOAT_FIELDS:
|
||
continue
|
||
|
||
try:
|
||
if key in self.INT_FIELDS:
|
||
next_config[key] = int(value)
|
||
elif key in self.FLOAT_FIELDS:
|
||
next_config[key] = float(value)
|
||
else:
|
||
next_config[key] = value
|
||
except (TypeError, ValueError):
|
||
raise HTTPException(status_code=400, detail=f"{key} 配置值格式错误")
|
||
|
||
try:
|
||
session_expire = int(next_config.get("adminSessionExpire"))
|
||
except (TypeError, ValueError):
|
||
raise HTTPException(
|
||
status_code=400,
|
||
detail="adminSessionExpire 配置值格式错误",
|
||
)
|
||
if (
|
||
not ADMIN_SESSION_EXPIRE_MIN <= session_expire <= ADMIN_SESSION_EXPIRE_MAX
|
||
or session_expire % ADMIN_SESSION_EXPIRE_MIN != 0
|
||
):
|
||
raise HTTPException(
|
||
status_code=400,
|
||
detail="adminSessionExpire 必须是 1 到 365 个整天",
|
||
)
|
||
next_config["adminSessionExpire"] = session_expire
|
||
|
||
try:
|
||
trash_retention_days = int(
|
||
next_config.get("trashRetentionDays", FileService.TRASH_DEFAULT_RETENTION_DAYS)
|
||
)
|
||
except (TypeError, ValueError):
|
||
raise HTTPException(
|
||
status_code=400,
|
||
detail="trashRetentionDays 配置值格式错误",
|
||
)
|
||
if not 1 <= trash_retention_days <= FileService.TRASH_MAX_RETENTION_DAYS:
|
||
raise HTTPException(
|
||
status_code=400,
|
||
detail="trashRetentionDays 必须是 1 到 365 个整天",
|
||
)
|
||
next_config["trashRetentionDays"] = trash_retention_days
|
||
retention_changed = (
|
||
"trashRetentionDays" in update_data
|
||
and trash_retention_days != int(
|
||
current_config.get("trashRetentionDays", FileService.TRASH_DEFAULT_RETENTION_DAYS)
|
||
)
|
||
)
|
||
|
||
if int(next_config.get("storageLimit", 0)) < 0:
|
||
raise HTTPException(
|
||
status_code=400,
|
||
detail="storageLimit 不能小于 0",
|
||
)
|
||
|
||
if str(next_config.get("background") or "") != str(current_config.get("background") or ""):
|
||
try:
|
||
validate_background_url(next_config.get("background"))
|
||
except ValueError as exc:
|
||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||
|
||
# Existing internal NAS/CloudDrive endpoints remain valid for unrelated saves.
|
||
# Validate only changed destinations, matching upstream 2.7.1 semantics.
|
||
for endpoint_key in OUTBOUND_ENDPOINT_CONFIG_KEYS:
|
||
candidate = str(next_config.get(endpoint_key) or "")
|
||
current = str(current_config.get(endpoint_key) or "")
|
||
if candidate == current:
|
||
continue
|
||
validator = validate_outbound_hostname if endpoint_key == "s3_hostname" else validate_outbound_endpoint
|
||
try:
|
||
next_config[endpoint_key] = await asyncio.to_thread(validator, candidate)
|
||
except ValueError as exc:
|
||
raise HTTPException(400, str(exc)) from exc
|
||
|
||
if admin_password_changed:
|
||
next_config["jwt_secret"] = generate_jwt_secret()
|
||
|
||
await KeyValue.update_or_create(key="settings", defaults={"value": next_config})
|
||
await refresh_settings(force=True)
|
||
|
||
if retention_changed:
|
||
retention_now = await get_now()
|
||
retention_expiry = FileService._trash_expiry(retention_now, trash_retention_days)
|
||
await FileCodes.filter(
|
||
is_trashed=True,
|
||
trash_purged_at__isnull=True,
|
||
trash_expires_at__isnull=False,
|
||
).update(trash_expires_at=retention_expiry)
|
||
|
||
class LocalFileService:
|
||
async def list_files(self, path: str = "", *, legacy: bool = False, page: int | None = None, size: int = 20, keyword: str = ""):
|
||
# NAS metadata access may block; keep the request event loop responsive.
|
||
return await asyncio.to_thread(self._list_files, path, legacy=legacy, page=page, size=size, keyword=keyword)
|
||
|
||
def _list_files(self, path: str, *, legacy: bool, page: int | None, size: int, keyword: str):
|
||
if page is not None and (page < 1 or not 1 <= size <= 100):
|
||
raise HTTPException(status_code=400, detail="分页参数无效")
|
||
relpath = normalize_local_relpath(path, allow_empty=True)
|
||
directory = resolve_under_local(relpath)
|
||
if not directory.exists() or not directory.is_dir():
|
||
raise HTTPException(status_code=404, detail="目录不存在")
|
||
|
||
root = get_local_root()
|
||
items = []
|
||
try:
|
||
children = list(directory.iterdir())
|
||
except OSError as exc:
|
||
raise HTTPException(status_code=500, detail="无法读取目录") from exc
|
||
|
||
keyword = keyword.strip().casefold()
|
||
truncated = False
|
||
for child in children:
|
||
try:
|
||
resolved = child.resolve()
|
||
resolved.relative_to(root)
|
||
except (OSError, ValueError, RuntimeError):
|
||
continue
|
||
if keyword and keyword not in child.name.casefold():
|
||
continue
|
||
if not resolved.is_file() and not resolved.is_dir():
|
||
continue
|
||
if legacy and not resolved.is_file():
|
||
continue
|
||
child_rel = child.name if not relpath else f"{relpath}/{child.name}"
|
||
is_dir = resolved.is_dir()
|
||
items.append(
|
||
{
|
||
"file": child.name,
|
||
"name": child.name,
|
||
"path": child_rel,
|
||
"type": "dir" if is_dir else "file",
|
||
"ctime": format_local_ctime(resolved),
|
||
"size": None if is_dir else resolved.stat().st_size,
|
||
}
|
||
)
|
||
|
||
items.sort(key=lambda item: (item["type"] != "dir", item["name"].casefold(), item["name"]))
|
||
total = len(items)
|
||
if page is not None:
|
||
page = min(page, max(1, (total + size - 1) // size))
|
||
items = items[(page - 1) * size:page * size]
|
||
elif not legacy:
|
||
truncated = total > MAX_LIST_ENTRIES
|
||
items = items[:MAX_LIST_ENTRIES]
|
||
parent = ""
|
||
if relpath:
|
||
parent_path = Path(relpath).parent.as_posix()
|
||
parent = "" if parent_path == "." else parent_path
|
||
return {
|
||
"path": relpath,
|
||
"parent": parent,
|
||
"truncated": truncated,
|
||
"items": items,
|
||
"total": total,
|
||
"page": page,
|
||
"size": size,
|
||
}
|
||
|
||
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):
|
||
relpath = normalize_local_relpath(file)
|
||
self.file = relpath
|
||
self.name = Path(relpath).name
|
||
self.path = resolve_under_local(relpath)
|
||
if self.path.is_file():
|
||
self.ctime = format_local_ctime(self.path)
|
||
self.size = self.path.stat().st_size
|
||
else:
|
||
self.ctime = None
|
||
self.size = None
|
||
|
||
async def read(self) -> bytes:
|
||
with open(self.path, "rb") as fh:
|
||
return fh.read()
|
||
|
||
async def write(self, data):
|
||
with open(self.path, "wb") as f:
|
||
f.write(data)
|
||
|
||
async def delete(self):
|
||
if not self.path.is_file():
|
||
raise HTTPException(status_code=404, detail="文件不存在")
|
||
self.path.unlink()
|
||
|
||
async def exists(self):
|
||
return self.path.is_file()
|