🐛 永久封存恢复前的旧取件码

This commit is contained in:
orion committed 2026-08-07 10:46:45 +08:00
1 parent 56a86c59cf
commit d75044e884
7 files changed
+92 -10

No files matched your search

+6 -1
View File
@@ -17,7 +17,7 @@ from core.settings import (
)
from core.config import refresh_settings
from core.security import INTERNAL_CONFIG_KEYS, generate_jwt_secret
from apps.base.models import FileCodes, KeyValue, TransferStats, file_codes_pydantic
from apps.base.models import FileCodes, KeyValue, ShareCodeReservations, TransferStats, file_codes_pydantic
from apps.base.utils import get_expire_info, get_file_path_name, get_random_code
from apps.base.quota import release_storage, reserve_storage
@@ -178,6 +178,11 @@ class FileService:
original_path,
)
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()
file_code.is_trashed = False
file_code.trashed_at = None
+6 -4
View File
@@ -31,7 +31,7 @@ from apps.admin.schemas import (
UpdateFileData,
)
from core.response import APIResponse
from apps.base.models import FileCodes, KeyValue, TransferStats
from apps.base.models import FileCodes, KeyValue, ShareCodeReservations, TransferStats
from apps.admin.dependencies import (
create_token,
get_admin_session_expire_seconds,
@@ -859,9 +859,11 @@ async def update_file(
update_data = {}
if data.code is not None and data.code != file_code.code:
# 判断code是否存在
if await FileCodes.filter(code=data.code).first():
raise HTTPException(status_code=400, detail="code已存在")
# 当前记录和全部退役取件码都不可重复使用。
if await FileCodes.filter(code=data.code).first() or await ShareCodeReservations.filter(
code=data.code
).first():
raise HTTPException(status_code=400, detail="取件码已被占用")
update_data["code"] = data.code
if data.prefix is not None and data.prefix != file_code.prefix:
update_data["prefix"] = data.prefix
@@ -0,0 +1,20 @@
from tortoise import connections
async def migrate():
conn = connections.get("default")
await conn.execute_script(
"""
CREATE TABLE IF NOT EXISTS share_code_reservations (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
code VARCHAR(255) NOT NULL UNIQUE,
file_code_id INT NULL,
reason VARCHAR(32) NOT NULL DEFAULT 'retired',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_share_code_reservations_code
ON share_code_reservations(code);
CREATE INDEX IF NOT EXISTS idx_share_code_reservations_file_code_id
ON share_code_reservations(file_code_id);
"""
)
+13
View File
@@ -46,6 +46,19 @@ class FileCodes(models.Model):
return f"{self.file_path}/{self.uuid_file_name}"
class ShareCodeReservations(models.Model):
"""已退役取件码的永久封存登记,防止恢复文件后旧码被重新分配。"""
id = fields.IntField(pk=True)
code = fields.CharField(max_length=255, unique=True, index=True)
file_code_id = fields.IntField(null=True, index=True)
reason = fields.CharField(max_length=32, default="retired")
created_at = fields.DatetimeField(auto_now_add=True)
class Meta:
table = "share_code_reservations"
class UploadChunk(models.Model):
id = fields.IntField(pk=True)
upload_id = fields.CharField(max_length=36, index=True)
+4 -2
View File
@@ -8,7 +8,7 @@ from fastapi import UploadFile, HTTPException
from typing import Optional, Tuple
from apps.base.dependencies import IPRateLimit
from apps.base.models import FileCodes
from apps.base.models import FileCodes, ShareCodeReservations
from core.settings import settings
from core.utils import (
get_random_num,
@@ -122,7 +122,9 @@ async def get_random_code(style: str | None = None) -> str:
if code_style == "number"
else await get_random_string(CODE_LENGTH)
)
if not await FileCodes.filter(code=code).exists():
if not await FileCodes.filter(code=code).exists() and not await ShareCodeReservations.filter(
code=code
).exists():
return str(code)
raise HTTPException(status_code=503, detail="取件码生成暂时繁忙,请重试")