✨ feat(core): 引入垃圾回收管理功能

* 新增垃圾回收管理逻辑,支持批量管理与清理功能
* 在数据模型中增加过期时间与保留天数验证逻辑
* 在 FileService 中集成垃圾回收管理模块
* 调整任务调度逻辑以适配新的垃圾回收规则
* 为垃圾回收功能补充相应的单元测试覆盖
This commit is contained in:
orion committed 2026-09-18 09:39:27 +08:00
1 parent 356f23bae1
commit b10113e5ab
6 files changed
+388 -46

No files matched your search

+19 -40
View File
@@ -26,7 +26,10 @@ from core.settings import data_root
from core.utils import get_now, hash_password, is_password_hashed
class FileService:
from apps.admin.trash_management import TrashManagementMixin
class FileService(TrashManagementMixin):
FILE_METADATA_KEY_PREFIX = "admin_file_metadata:"
FILE_VIEW_PRESETS_KEY = "admin_file_view_presets"
ADMIN_ACTIVITY_KEY = "admin_activity_events"
@@ -107,8 +110,8 @@ class FileService:
deduped.append(stat)
return deduped
async def _delete_file_code(self, file_code: FileCodes):
await self.move_to_trash(file_code, reason="manual")
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:
@@ -181,11 +184,18 @@ class FileService:
)
file_code.file_path = destination_dir
original_code = file_code.code
await ShareCodeReservations.get_or_create(
code=original_code,
defaults={"file_code_id": file_code.id, "reason": "restored"},
)
file_code.code = await get_random_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
@@ -248,37 +258,6 @@ class FileService:
meta={"auditOnly": True},
)
async def list_trash(self, page: int, size: int) -> dict[str, Any]:
page = max(page, 1)
size = min(max(size, 1), 100)
query = FileCodes.filter(is_trashed=True).order_by("-trashed_at", "-id")
total = await query.count()
files = await query.offset((page - 1) * size).limit(size)
now = await get_now()
items = []
for file_code in files:
item = await self._build_admin_file_item(file_code, now=now)
item.update(
{
"trashedAt": file_code.trashed_at,
"trashExpiresAt": file_code.trash_expires_at,
"trashReason": file_code.trash_reason,
"trashPurgedAt": file_code.trash_purged_at,
"isTrashPermanent": file_code.trash_expires_at is None and file_code.trash_purged_at is None,
"isTrashPurged": file_code.trash_purged_at is not None,
}
)
items.append(item)
return {"page": page, "size": size, "total": total, "data": items}
async def set_trash_retention(self, file_id: int, retention_days: int) -> FileCodes:
file_code = await FileCodes.filter(id=file_id, is_trashed=True).first()
if not file_code:
raise HTTPException(status_code=404, detail="回收站文件不存在")
file_code.trash_expires_at = self._trash_expiry(await get_now(), retention_days)
await file_code.save(update_fields=["trash_expires_at"])
return file_code
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)
@@ -304,7 +283,7 @@ class FileService:
continue
try:
await self._delete_file_code(file_code)
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)})