fix(phase-5.1): 回收/确认幂等与两前端分片重试按 CR 消项加固

- 分片回收与 savePart/complete 共用会话锁并锁内复查 mtime
- 版本/教程 confirm 按 version 或 fileName+version 幂等返回既有行
- 证据登记失败补偿删除已上传对象;标签截断到列宽 32
- 用户端分片持久化 upload_id、校验包体失败、失败不再 DELETE
- 后管逐片重试 2 次、放弃时删除会话;catch 台账抬到 1057/190
This commit is contained in:
2026-10-31 04:26:58 +08:00
parent b13d795731
commit 2c2cb23943
7 changed files with 187 additions and 44 deletions
+30 -12
View File
@@ -4,6 +4,9 @@ import { http, unwrap } from '@/api/http'
export const CHUNK_UPLOAD_THRESHOLD = 8 * 1024 * 1024
export const CHUNK_UPLOAD_PART_SIZE = 8 * 1024 * 1024
/** 每片重试次数(不含首次):单片 502/超时不该让 512MB 从 0 再来。 */
const PART_RETRY_LIMIT = 2
function partUrl(uploadId: string, index: number): string {
return `/api/files/chunk/${encodeURIComponent(uploadId)}/part/${index}`
}
@@ -37,18 +40,33 @@ export async function uploadFileInChunks(
}
const blob = file.slice(index * CHUNK_UPLOAD_PART_SIZE,
Math.min(file.size, (index + 1) * CHUNK_UPLOAD_PART_SIZE))
const form = new FormData()
form.append('file', blob, `part-${index}`)
await http.post(partUrl(uploadId, index), form, {
headers: { 'Content-Type': 'multipart/form-data' },
timeout: 120_000 + Math.ceil(file.size / (1024 * 1024)) * 15_000,
onUploadProgress: (event) => {
if (!onProgress) return
const perPart = Math.round(((index + (event.loaded || 0) / Math.max(blob.size, 1)) / totalParts) * 100)
onProgress(Math.min(perPart, 99))
},
})
arrived.add(index)
let lastError: unknown = null
for (let attempt = 0; attempt <= PART_RETRY_LIMIT; attempt += 1) {
const form = new FormData()
form.append('file', blob, `part-${index}`)
try {
const partResponse = await http.post(partUrl(uploadId, index), form, {
headers: { 'Content-Type': 'multipart/form-data' },
timeout: 120_000 + Math.ceil(file.size / (1024 * 1024)) * 15_000,
onUploadProgress: (event) => {
if (!onProgress) return
const perPart = Math.round(((index + (event.loaded || 0) / Math.max(blob.size, 1)) / totalParts) * 100)
onProgress(Math.min(perPart, 99))
},
})
unwrap(partResponse.data)
arrived.add(index)
lastError = null
break
} catch (error) {
lastError = error
}
}
if (lastError) {
// 后管上传是一次性的:放弃时把会话连同临时片一起清掉,不留 24h 残盘。
await http.delete(`/api/files/chunk/${encodeURIComponent(uploadId)}`).catch(() => undefined)
throw lastError instanceof Error ? lastError : new Error('分片上传失败')
}
}
const completeResponse = await http.post(