✨ 配置回收站默认保留期

This commit is contained in:
orion committed 2026-09-11 16:50:05 +08:00
1 parent 6bc1c30797
commit 925c6450ac
4 files changed
+316 -8

No files matched your search

+36 -2
View File
@@ -80,7 +80,7 @@ class FileService:
"code",
}
TRASH_DEFAULT_RETENTION_DAYS = 7
TRASH_DEFAULT_RETENTION_DAYS = 30
TRASH_MAX_RETENTION_DAYS = 365
def __init__(self):
@@ -118,7 +118,9 @@ class FileService:
@classmethod
def _trash_expiry(cls, now: datetime, retention_days: Optional[int]):
if retention_days is None:
retention_days = cls.TRASH_DEFAULT_RETENTION_DAYS
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:
@@ -1721,6 +1723,7 @@ class FileService:
class ConfigService:
INT_FIELDS = {
"adminSessionExpire",
"trashRetentionDays",
"enableChunk",
"errorCount",
"errorMinute",
@@ -1799,6 +1802,28 @@ class ConfigService:
)
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,
@@ -1811,6 +1836,15 @@ class ConfigService:
await KeyValue.update_or_create(key="settings", defaults={"value": next_config})
await refresh_settings()
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):