✨ 配置回收站默认保留期
This commit is contained in:
1 parent
5427c187d4
commit
07e362d341
4 files changed
+76
-8
No files matched your search
+36
-2
@@ -80,7 +80,7 @@ class FileService:
|
|||||||
"code",
|
"code",
|
||||||
}
|
}
|
||||||
|
|
||||||
TRASH_DEFAULT_RETENTION_DAYS = 7
|
TRASH_DEFAULT_RETENTION_DAYS = 30
|
||||||
TRASH_MAX_RETENTION_DAYS = 365
|
TRASH_MAX_RETENTION_DAYS = 365
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -118,7 +118,9 @@ class FileService:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def _trash_expiry(cls, now: datetime, retention_days: Optional[int]):
|
def _trash_expiry(cls, now: datetime, retention_days: Optional[int]):
|
||||||
if retention_days is None:
|
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:
|
if retention_days == 0:
|
||||||
return None
|
return None
|
||||||
if not 1 <= retention_days <= cls.TRASH_MAX_RETENTION_DAYS:
|
if not 1 <= retention_days <= cls.TRASH_MAX_RETENTION_DAYS:
|
||||||
@@ -1726,6 +1728,7 @@ class FileService:
|
|||||||
class ConfigService:
|
class ConfigService:
|
||||||
INT_FIELDS = {
|
INT_FIELDS = {
|
||||||
"adminSessionExpire",
|
"adminSessionExpire",
|
||||||
|
"trashRetentionDays",
|
||||||
"enableChunk",
|
"enableChunk",
|
||||||
"errorCount",
|
"errorCount",
|
||||||
"errorMinute",
|
"errorMinute",
|
||||||
@@ -1804,6 +1807,28 @@ class ConfigService:
|
|||||||
)
|
)
|
||||||
next_config["adminSessionExpire"] = session_expire
|
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:
|
if int(next_config.get("storageLimit", 0)) < 0:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
@@ -1816,6 +1841,15 @@ class ConfigService:
|
|||||||
await KeyValue.update_or_create(key="settings", defaults={"value": next_config})
|
await KeyValue.update_or_create(key="settings", defaults={"value": next_config})
|
||||||
await refresh_settings()
|
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:
|
class LocalFileService:
|
||||||
async def list_files(self):
|
async def list_files(self):
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ SETUP_CONFIG_FIELDS = {
|
|||||||
"max_save_seconds",
|
"max_save_seconds",
|
||||||
"name",
|
"name",
|
||||||
"openUpload",
|
"openUpload",
|
||||||
|
"trashRetentionDays",
|
||||||
"uploadCount",
|
"uploadCount",
|
||||||
"uploadMinute",
|
"uploadMinute",
|
||||||
"uploadSize",
|
"uploadSize",
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ DEFAULT_CONFIG = {
|
|||||||
"admin_token": "",
|
"admin_token": "",
|
||||||
"jwt_secret": "",
|
"jwt_secret": "",
|
||||||
"adminSessionExpire": ADMIN_SESSION_EXPIRE_DEFAULT,
|
"adminSessionExpire": ADMIN_SESSION_EXPIRE_DEFAULT,
|
||||||
|
"trashRetentionDays": 30,
|
||||||
"openUpload": 1,
|
"openUpload": 1,
|
||||||
"uploadSize": 1024 * 1024 * 10,
|
"uploadSize": 1024 * 1024 * 10,
|
||||||
"allowed_file_types": ["*"],
|
"allowed_file_types": ["*"],
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ from tortoise import Tortoise
|
|||||||
|
|
||||||
from apps.base.models import FileCodes, ShareCodeReservations
|
from apps.base.models import FileCodes, ShareCodeReservations
|
||||||
from apps.base.utils import CODE_LENGTH, get_random_code
|
from apps.base.utils import CODE_LENGTH, get_random_code
|
||||||
from apps.admin.services import FileService
|
from apps.admin.services import ConfigService, FileService
|
||||||
from core.settings import settings
|
from core.settings import settings
|
||||||
|
from core.utils import get_now
|
||||||
|
|
||||||
|
|
||||||
class FakeStorage:
|
class FakeStorage:
|
||||||
@@ -40,13 +41,44 @@ class RecycleBinTests(unittest.TestCase):
|
|||||||
item = await FileCodes.create(code="old", prefix="demo", suffix=".txt", file_path="share/data/demo", uuid_file_name="demo.txt", expired_count=-1)
|
item = await FileCodes.create(code="old", prefix="demo", suffix=".txt", file_path="share/data/demo", uuid_file_name="demo.txt", expired_count=-1)
|
||||||
service = FileService()
|
service = FileService()
|
||||||
service._file_storage = FakeStorage()
|
service._file_storage = FakeStorage()
|
||||||
trashed = await service.move_to_trash(item, reason="manual", retention_days=7)
|
settings.trashRetentionDays = 30
|
||||||
self.assertTrue(trashed.is_trashed)
|
default_now = await get_now()
|
||||||
self.assertEqual(trashed.trash_reason, "manual")
|
trashed_default = await service.move_to_trash(item, reason="manual")
|
||||||
self.assertIsNotNone(trashed.trash_expires_at)
|
self.assertTrue(trashed_default.is_trashed)
|
||||||
|
self.assertEqual(trashed_default.trash_reason, "manual")
|
||||||
|
self.assertIsNotNone(trashed_default.trash_expires_at)
|
||||||
|
self.assertAlmostEqual(
|
||||||
|
(trashed_default.trash_expires_at - default_now).total_seconds(),
|
||||||
|
30 * 86400,
|
||||||
|
delta=5,
|
||||||
|
)
|
||||||
self.assertTrue(FakeStorage.moved)
|
self.assertTrue(FakeStorage.moved)
|
||||||
|
|
||||||
await service.purge_trashed_file(trashed)
|
permanent = await FileCodes.create(code="perm", prefix="perm", suffix=".txt", file_path="share/data/perm", uuid_file_name="perm.txt", expired_count=-1)
|
||||||
|
await service.move_to_trash(permanent, reason="manual", retention_days=0)
|
||||||
|
purged = await FileCodes.create(code="purged", prefix="purged", suffix=".txt", file_path="share/data/purged", uuid_file_name="purged.txt", expired_count=-1, is_trashed=True, trash_purged_at=await get_now())
|
||||||
|
|
||||||
|
original_refresh = __import__("apps.admin.services", fromlist=["refresh_settings"]).refresh_settings
|
||||||
|
async def fake_refresh():
|
||||||
|
return None
|
||||||
|
module = __import__("apps.admin.services", fromlist=["refresh_settings"])
|
||||||
|
module.refresh_settings = fake_refresh
|
||||||
|
try:
|
||||||
|
await ConfigService().update_config({"trashRetentionDays": 14})
|
||||||
|
finally:
|
||||||
|
module.refresh_settings = original_refresh
|
||||||
|
|
||||||
|
await trashed_default.refresh_from_db()
|
||||||
|
await permanent.refresh_from_db()
|
||||||
|
await purged.refresh_from_db()
|
||||||
|
self.assertAlmostEqual(
|
||||||
|
(trashed_default.trash_expires_at - await get_now()).total_seconds(),
|
||||||
|
14 * 86400,
|
||||||
|
delta=5,
|
||||||
|
)
|
||||||
|
self.assertIsNone(permanent.trash_expires_at)
|
||||||
|
self.assertIsNotNone(purged.trash_purged_at)
|
||||||
|
await service.purge_trashed_file(trashed_default)
|
||||||
audit_record = await FileCodes.get(id=item.id)
|
audit_record = await FileCodes.get(id=item.id)
|
||||||
self.assertTrue(audit_record.is_trashed)
|
self.assertTrue(audit_record.is_trashed)
|
||||||
self.assertIsNotNone(audit_record.trash_purged_at)
|
self.assertIsNotNone(audit_record.trash_purged_at)
|
||||||
|
|||||||
Reference in new issue
Block a user