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:
@@ -44,6 +44,60 @@ function bodyOf<T>(response: unknown): JavaApiResponse<T> {
|
||||
return response as JavaApiResponse<T>
|
||||
}
|
||||
|
||||
/** 业务失败默认也是 HTTP 200:只看状态码会把被拒的片当成功。 */
|
||||
function ensureOk<T>(body: JavaApiResponse<T>): JavaApiResponse<T> {
|
||||
if (body && body.success === false) {
|
||||
const message = (body as { message?: string }).message
|
||||
throw new Error(message || '上传失败,请重试')
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
/** 分片会话跨重试/刷新续用:同一文件身份复用 upload_id,服务端没回收就从已达片号继续。 */
|
||||
interface StoredChunkSession {
|
||||
uploadId: string;
|
||||
size: number;
|
||||
lastModified: number;
|
||||
}
|
||||
|
||||
const CHUNK_SESSION_PREFIX = 'shufu:chunk:'
|
||||
|
||||
function sessionKeyOf(file: File, options: UploadTempFileOptions): string {
|
||||
return `${CHUNK_SESSION_PREFIX}${file.name}:${file.size}:${file.lastModified}:${options.relativePath || ''}`
|
||||
}
|
||||
|
||||
function readStoredSession(key: string, file: File): string {
|
||||
if (typeof localStorage === 'undefined') return ''
|
||||
try {
|
||||
const raw = localStorage.getItem(key)
|
||||
if (!raw) return ''
|
||||
const parsed = JSON.parse(raw) as StoredChunkSession
|
||||
return parsed.size === file.size && parsed.lastModified === file.lastModified ? parsed.uploadId : ''
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
function writeStoredSession(key: string, file: File, uploadId: string): void {
|
||||
if (typeof localStorage === 'undefined') return
|
||||
try {
|
||||
localStorage.setItem(key, JSON.stringify({
|
||||
uploadId, size: file.size, lastModified: file.lastModified,
|
||||
} satisfies StoredChunkSession))
|
||||
} catch {
|
||||
// 存储不可用(隐私模式/配额满)只影响跨刷新续传,不影响本次上传
|
||||
}
|
||||
}
|
||||
|
||||
function clearStoredSession(key: string): void {
|
||||
if (typeof localStorage === 'undefined') return
|
||||
try {
|
||||
localStorage.removeItem(key)
|
||||
} catch {
|
||||
// 同上:清不掉只会让下次多问一次 status
|
||||
}
|
||||
}
|
||||
|
||||
/** 单次 multipart 上传(小文件短链路)。超时按体积给,不再固定 120s。 */
|
||||
async function singleUpload(
|
||||
file: File,
|
||||
@@ -90,23 +144,34 @@ async function chunkedUpload(
|
||||
onProgress?: (percent: number) => void,
|
||||
): Promise<JavaApiResponse<UploadFileVo>> {
|
||||
const totalParts = Math.max(1, Math.ceil(file.size / UPLOAD_PART_SIZE))
|
||||
const init = bodyOf<{ upload_id?: string }>(await http.post(
|
||||
`${JAVA_API_PREFIX}/files/chunk/init`,
|
||||
null,
|
||||
{ params: { fileName: file.name, totalParts, totalBytes: file.size } },
|
||||
))
|
||||
const uploadId = String(init.data?.upload_id ?? '')
|
||||
if (!uploadId) {
|
||||
throw new Error('后端未返回分片会话,请重试')
|
||||
const sessionKey = sessionKeyOf(file, options)
|
||||
let uploadId = readStoredSession(sessionKey, file)
|
||||
let done = new Set<number>()
|
||||
if (uploadId) {
|
||||
try {
|
||||
const status = ensureOk(bodyOf<{ uploaded_parts?: unknown[] }>(await http.get(
|
||||
`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`,
|
||||
)))
|
||||
const arrived = status.data?.uploaded_parts
|
||||
done = new Set<number>(Array.isArray(arrived) ? arrived.map((index) => Number(index)) : [])
|
||||
} catch {
|
||||
// 会话已被服务端回收/过期:只能清掉重开,否则这个件每次都在同一个死会话上失败。
|
||||
clearStoredSession(sessionKey)
|
||||
uploadId = ''
|
||||
}
|
||||
}
|
||||
if (!uploadId) {
|
||||
const init = ensureOk(bodyOf<{ upload_id?: string }>(await http.post(
|
||||
`${JAVA_API_PREFIX}/files/chunk/init`,
|
||||
null,
|
||||
{ params: { fileName: file.name, totalParts, totalBytes: file.size } },
|
||||
)))
|
||||
uploadId = String(init.data?.upload_id ?? '')
|
||||
if (!uploadId) {
|
||||
throw new Error('后端未返回分片会话,请重试')
|
||||
}
|
||||
writeStoredSession(sessionKey, file, uploadId)
|
||||
}
|
||||
|
||||
// 开传前先问到已达片号:上一次断掉的会话可以跳过已传片(后端保留 24h)。
|
||||
// 这一问失败不再静默按"全缺"重传:断网/5xx 当场说清楚,别闷头把整包从 0 再传一遍。
|
||||
const status = bodyOf<{ uploaded_parts?: unknown[] }>(await http.get(
|
||||
`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`,
|
||||
))
|
||||
const arrived = status.data?.uploaded_parts
|
||||
const done = new Set<number>(Array.isArray(arrived) ? arrived.map((index) => Number(index)) : [])
|
||||
|
||||
const report = (partialBytes: number) => {
|
||||
if (!onProgress) return
|
||||
@@ -125,11 +190,12 @@ async function chunkedUpload(
|
||||
const form = new FormData()
|
||||
form.append('file', blob, `part-${index}`)
|
||||
try {
|
||||
await http.post(uploadPartUrl(uploadId, index), form, {
|
||||
const partBody = bodyOf(await http.post(uploadPartUrl(uploadId, index), form, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
timeout: 300_000,
|
||||
onUploadProgress: (event) => report(Math.min(event.loaded || 0, blob.size)),
|
||||
})
|
||||
}))
|
||||
ensureOk(partBody)
|
||||
done.add(index)
|
||||
lastError = null
|
||||
break
|
||||
@@ -138,7 +204,8 @@ async function chunkedUpload(
|
||||
}
|
||||
}
|
||||
if (lastError) {
|
||||
await http.delete(`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`).catch(() => undefined)
|
||||
// 不 DELETE:会话连同已达片号留在服务端,下一次(含刷新后)从断点续传。
|
||||
writeStoredSession(sessionKey, file, uploadId)
|
||||
throw lastError instanceof Error ? lastError : new Error('分片上传失败')
|
||||
}
|
||||
}
|
||||
@@ -152,8 +219,10 @@ async function chunkedUpload(
|
||||
null,
|
||||
{ params, timeout: 600_000 },
|
||||
)
|
||||
const completed = ensureOk(bodyOf<UploadFileVo>(response))
|
||||
clearStoredSession(sessionKey)
|
||||
if (onProgress) onProgress(100)
|
||||
return bodyOf<UploadFileVo>(response)
|
||||
return completed
|
||||
}
|
||||
|
||||
export async function uploadTempFileToJava(
|
||||
|
||||
Reference in New Issue
Block a user