✨ feat(config): 统一分片大小配置与续传校验
This commit is contained in:
1 parent
1023292e71
commit
c2d743ba51
8 files changed
+116
-14
No files matched your search
@@ -1709,6 +1709,7 @@ class ConfigService:
|
||||
"adminSessionExpire",
|
||||
"trashRetentionDays",
|
||||
"enableChunk",
|
||||
"chunkSizeMB",
|
||||
"errorCount",
|
||||
"errorMinute",
|
||||
"loginCount",
|
||||
@@ -1747,6 +1748,16 @@ class ConfigService:
|
||||
if key in settings.default_config and key not in INTERNAL_CONFIG_KEYS
|
||||
}
|
||||
|
||||
if "chunkSizeMB" in update_data:
|
||||
raw_size = update_data["chunkSizeMB"]
|
||||
if isinstance(raw_size, bool) or not (
|
||||
isinstance(raw_size, int) or
|
||||
isinstance(raw_size, str) and raw_size.strip().isdigit()
|
||||
):
|
||||
raise HTTPException(400, "分片大小必须是 1–100 MB 的整数")
|
||||
if not 1 <= int(raw_size) <= 100:
|
||||
raise HTTPException(400, "分片大小必须是 1–100 MB 的整数")
|
||||
|
||||
admin_token = update_data.get("admin_token")
|
||||
admin_password_changed = False
|
||||
if admin_token is None or admin_token == "":
|
||||
|
||||
+11
-9
@@ -442,7 +442,11 @@ async def parse_complete_upload(request: Request) -> CompleteUploadModel:
|
||||
async def init_chunk_upload(data: InitChunkUploadModel = Depends(parse_init_chunk_upload)):
|
||||
safe_file_name = await sanitize_filename(unquote(data.file_name or ""))
|
||||
validate_file_type(safe_file_name)
|
||||
# 服务端校验:根据 total_chunks * chunk_size 计算理论最大上传量
|
||||
# New sessions negotiate the smaller client/server size. Existing sessions
|
||||
# below retain their original offsets when an administrator changes this.
|
||||
configured_mb = max(1, min(100, int(settings.chunkSizeMB)))
|
||||
data.chunk_size = min(data.chunk_size, configured_mb * 1024 * 1024, data.file_size)
|
||||
# File limits concern actual bytes, not a rounded-up final chunk.
|
||||
total_chunks = (data.file_size + data.chunk_size - 1) // data.chunk_size
|
||||
max_possible_size = data.file_size
|
||||
if max_possible_size > settings.uploadSize:
|
||||
@@ -530,8 +534,6 @@ async def init_chunk_upload(data: InitChunkUploadModel = Depends(parse_init_chun
|
||||
)
|
||||
try:
|
||||
storage_mode = preferred_mode
|
||||
if storage_mode == "legacy" and total_chunks * data.chunk_size > settings.uploadSize:
|
||||
raise HTTPException(403, "文件分片大小超过上传限制")
|
||||
await UploadChunk.create(
|
||||
upload_id=upload_id,
|
||||
storage_mode=storage_mode,
|
||||
@@ -608,7 +610,7 @@ async def upload_chunk(
|
||||
upload_id=upload_id, completed=True
|
||||
).count()
|
||||
# 已上传分片的最大可能大小 + 当前分片
|
||||
max_uploaded_size = chunk_info.file_size if chunk_info.storage_mode in {"webdav-offset", "webdav-stream"} else uploaded_count * chunk_info.chunk_size + chunk_size
|
||||
max_uploaded_size = chunk_info.file_size
|
||||
if max_uploaded_size > settings.uploadSize:
|
||||
max_size_mb = settings.uploadSize / (1024 * 1024)
|
||||
raise HTTPException(
|
||||
@@ -616,10 +618,10 @@ async def upload_chunk(
|
||||
)
|
||||
|
||||
chunk_hash = hashlib.sha256(chunk_data).hexdigest()
|
||||
expected_size = min(chunk_info.chunk_size, chunk_info.file_size-chunk_index*chunk_info.chunk_size)
|
||||
if chunk_size != expected_size:
|
||||
raise HTTPException(400, "分片大小与声明值不一致")
|
||||
if chunk_info.storage_mode in {"webdav-offset", "webdav-stream"}:
|
||||
expected_size = min(chunk_info.chunk_size, chunk_info.file_size-chunk_index*chunk_info.chunk_size)
|
||||
if chunk_size != expected_size:
|
||||
raise HTTPException(400, "分片大小与声明值不一致")
|
||||
if existing_chunk:
|
||||
if existing_chunk.chunk_hash != chunk_hash:
|
||||
raise HTTPException(409, "重传分片内容不一致")
|
||||
@@ -809,8 +811,8 @@ async def _complete_upload(upload_id, data, ip):
|
||||
if len(completed_chunks_list) != chunk_info.total_chunks:
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="分片不完整")
|
||||
|
||||
# 用分片数 * chunk_size 校验最大可能大小
|
||||
max_total_size = chunk_info.file_size if chunk_info.storage_mode in {"webdav-offset", "webdav-stream"} else len(completed_chunks_list) * chunk_info.chunk_size
|
||||
# Each newly uploaded chunk has an exact-length check, including the last.
|
||||
max_total_size = chunk_info.file_size
|
||||
if max_total_size > settings.uploadSize:
|
||||
save_path = chunk_info.save_path
|
||||
if save_path:
|
||||
|
||||
Reference in new issue
Block a user