✨ 新增回收站审计与八位取件码

This commit is contained in:
orion committed 2026-08-07 10:28:22 +08:00
1 parent c25c495657
commit 4ee06e2d29
15 files changed
+534 -132

No files matched your search

@@ -0,0 +1,24 @@
from tortoise import connections
async def migrate():
conn = connections.get("default")
result = await conn.execute_query("PRAGMA table_info(filecodes)")
columns = {row[1] for row in (result[1] if result and len(result) > 1 else [])}
additions = [
("is_trashed", "BOOLEAN NOT NULL DEFAULT 0"),
("trashed_at", "TIMESTAMP NULL"),
("trash_expires_at", "TIMESTAMP NULL"),
("trash_reason", "VARCHAR(32) NULL"),
("trash_original_path", "VARCHAR(1024) NULL"),
("trash_purged_at", "TIMESTAMP NULL"),
]
for name, definition in additions:
if name not in columns:
await conn.execute_query(f"ALTER TABLE filecodes ADD COLUMN {name} {definition}")
await conn.execute_script(
"""
CREATE INDEX IF NOT EXISTS idx_filecodes_is_trashed ON filecodes(is_trashed);
CREATE INDEX IF NOT EXISTS idx_filecodes_trash_expires_at ON filecodes(trash_expires_at);
"""
)
+6
View File
@@ -28,6 +28,12 @@ class FileCodes(models.Model):
file_hash = fields.CharField(max_length=64, null=True)
is_chunked = fields.BooleanField(default=False)
upload_id = fields.CharField(max_length=36, null=True)
is_trashed = fields.BooleanField(default=False, index=True)
trashed_at = fields.DatetimeField(null=True, index=True)
trash_expires_at = fields.DatetimeField(null=True, index=True)
trash_reason = fields.CharField(max_length=32, null=True)
trash_original_path = fields.CharField(max_length=1024, null=True)
trash_purged_at = fields.DatetimeField(null=True, index=True)
async def is_expired(self):
if self.expired_at is None:
+10 -3
View File
@@ -96,6 +96,11 @@ async def get_expire_info(
return expired_at, expired_count, used_count, code
CODE_ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"
CODE_LENGTH = 8
CODE_MAX_ATTEMPTS = 50
def get_code_generate_type() -> str:
code_generate_type = getattr(settings, "code_generate_type", "secret")
if code_generate_type in {"secret", "string"}:
@@ -104,20 +109,22 @@ def get_code_generate_type() -> str:
async def get_random_code(style: str | None = None) -> str:
"""生成新的 8 位取件码,并避开所有仍保留的历史审计记录。"""
code_style = style or get_code_generate_type()
if code_style == "num":
code_style = "number"
if code_style == "string":
code_style = "secret"
while True:
for _ in range(CODE_MAX_ATTEMPTS):
code = (
await get_random_num()
await get_random_num(CODE_LENGTH)
if code_style == "number"
else await get_random_string()
else await get_random_string(CODE_LENGTH)
)
if not await FileCodes.filter(code=code).exists():
return str(code)
raise HTTPException(status_code=503, detail="取件码生成暂时繁忙,请重试")
async def calculate_file_hash(file: UploadFile, chunk_size=1024 * 1024) -> str:
+1 -1
View File
@@ -238,7 +238,7 @@ async def get_code_file_by_code(
normalized_code = normalize_share_code(code)
if not normalized_code:
return False, "文件不存在"
file_code = await FileCodes.filter(code=normalized_code).first()
file_code = await FileCodes.filter(code=normalized_code, is_trashed=False).first()
if not file_code:
return False, "文件不存在"
if await file_code.is_expired() and check: