fix(phase-5.2): 消项 CR 第二轮 P1/P2/P3(通道/证据链/交付时序/后管证据页)
This commit is contained in:
@@ -31,6 +31,14 @@ export const CHUNK_UPLOAD_THRESHOLD = UPLOAD_PART_SIZE
|
||||
/** 每片重试次数(不含首次)。 */
|
||||
const PART_RETRY_LIMIT = 2
|
||||
|
||||
/**
|
||||
* 单次直传的超时预算(P3-5:这条尺子在本仓只有一个出口):30s 保底 + 每 MB 15s,
|
||||
* 慢公网传 100MB 视频不再被固定 120s 掐死。桌面桥的浏览器降级直传也走这里。
|
||||
*/
|
||||
export function uploadTimeoutForBytes(bytes: number): number {
|
||||
return 30_000 + Math.ceil(bytes / (1024 * 1024)) * 15_000
|
||||
}
|
||||
|
||||
function uploadPartUrl(uploadId: string, index: number): string {
|
||||
return `${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}/part/${index}`
|
||||
}
|
||||
@@ -98,6 +106,61 @@ function clearStoredSession(key: string): void {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务端对「这个会话还在不在」的三种答复:还在(顺带已到片号)/确实没了/问不出来。
|
||||
* 第三态是 P1-11 的关键——过去它和前两态被揉进一个 catch,断网=整包作废。
|
||||
*/
|
||||
type ProbeOutcome
|
||||
= { state: 'ok'; parts: number[] }
|
||||
| { state: 'gone' }
|
||||
| { state: 'transient' }
|
||||
|
||||
/** 后端回收会话的原文案(ChunkedUploadService.readMeta);BusinessException 不带 code ⇒ HTTP 200 出。 */
|
||||
function messageSaysSessionGone(text: unknown): boolean {
|
||||
const message = typeof text === 'string' ? text : ''
|
||||
return ['不存在', '过期'].some((marker) => message.includes(marker))
|
||||
}
|
||||
|
||||
/**
|
||||
* 判据顺序=可信度顺序:404 与后端点名句才清状态;5xx/429/别的业务失败一律瞬态。
|
||||
* 拿"问不到"当"没了",就是用一次网关抖动换整包重传。
|
||||
*/
|
||||
function classifyProbe(response: unknown): ProbeOutcome {
|
||||
const status = (response as { status?: unknown } | null | undefined)?.status
|
||||
const numeric = typeof status === 'number' ? status : -1
|
||||
if (numeric === 404) return { state: 'gone' }
|
||||
if (numeric >= 500 || numeric === 408 || numeric === 429) return { state: 'transient' }
|
||||
const body = bodyOf<{ uploaded_parts?: unknown }>(response)
|
||||
if (body && typeof body === 'object' && body.success === false) {
|
||||
return messageSaysSessionGone(body.message) ? { state: 'gone' } : { state: 'transient' }
|
||||
}
|
||||
if (numeric !== -1 && (numeric < 200 || numeric > 299)) return { state: 'transient' }
|
||||
const arrived = body?.data?.uploaded_parts
|
||||
return { state: 'ok', parts: Array.isArray(arrived) ? arrived.map((index) => Number(index)) : [] }
|
||||
}
|
||||
|
||||
/**
|
||||
* 探测已达片号(P1-11)。
|
||||
* validateStatus 恒真:本仓响应拦截器会把失败折叠成 `new Error(中文文案)`,状态码到不了这里,
|
||||
* 404/5xx 全靠这一步自己按码分因;能走到 catch 的就只剩网络层失败与超时=铁定瞬态。
|
||||
*/
|
||||
async function probeUploadedParts(uploadId: string): Promise<ProbeOutcome> {
|
||||
try {
|
||||
const response = await http.get<JavaApiResponse<{ uploaded_parts?: unknown[] }>>(
|
||||
`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`,
|
||||
{ validateStatus: () => true },
|
||||
)
|
||||
return classifyProbe(response)
|
||||
} catch (error) {
|
||||
const status = (error as { response?: { status?: unknown } } | null | undefined)?.response?.status
|
||||
if (status === 404) return { state: 'gone' }
|
||||
if (messageSaysSessionGone((error as { message?: unknown } | null | undefined)?.message)) {
|
||||
return { state: 'gone' }
|
||||
}
|
||||
return { state: 'transient' }
|
||||
}
|
||||
}
|
||||
|
||||
/** 单次 multipart 上传(小文件短链路)。超时按体积给,不再固定 120s。 */
|
||||
async function singleUpload(
|
||||
file: File,
|
||||
@@ -122,8 +185,8 @@ async function singleUpload(
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
// 30s 保底 + 每 MB 15s:慢公网传 100MB 视频不再被固定 120s 掐死(S1-F3)
|
||||
timeout: 30_000 + Math.ceil(file.size / (1024 * 1024)) * 15_000,
|
||||
// 超时按体积给(见 uploadTimeoutForBytes),不再固定 120s
|
||||
timeout: uploadTimeoutForBytes(file.size),
|
||||
onUploadProgress: (event) => {
|
||||
if (!onProgress) return
|
||||
const total = event.total || file.size
|
||||
@@ -148,17 +211,16 @@ async function chunkedUpload(
|
||||
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 {
|
||||
// 会话已被服务端回收/过期:只能清掉重开,否则这个件每次都在同一个死会话上失败。
|
||||
const probed = await probeUploadedParts(uploadId)
|
||||
if (probed.state === 'gone') {
|
||||
// 服务端确实说会话没了(404/「不存在或已过期」):只能清掉重开,否则这个件每次都在同一个死会话上失败。
|
||||
clearStoredSession(sessionKey)
|
||||
uploadId = ''
|
||||
} else if (probed.state === 'ok') {
|
||||
done = new Set<number>(probed.parts)
|
||||
}
|
||||
// transient(断网/超时/5xx/别的业务失败):会话还在服务端,片号未知也要接着往里写——
|
||||
// 同一片重复上传按覆盖处理(后端 savePart 幂等),重开 init 等于把已传上去的整包作废。
|
||||
}
|
||||
if (!uploadId) {
|
||||
const init = ensureOk(bodyOf<{ upload_id?: string }>(await http.post(
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
import type { DesktopConfig, DesktopConfigUpdate, PywebviewApi } from './pywebview'
|
||||
import { extractErrorMessage, http } from '@/shared/api/http'
|
||||
import { uploadTimeoutForBytes } from '@/shared/api/upload.ts'
|
||||
|
||||
/**
|
||||
* 虚拟路径 → 浏览器 File 对象(虚拟路径仅存活于当前页面会话,刷新即失效)
|
||||
@@ -240,7 +241,10 @@ export const webFallbackApi: PywebviewApi = {
|
||||
}
|
||||
try {
|
||||
// http 实例对 /newApi 前缀自动附带 Bearer;FormData 的 Content-Type 交由浏览器自动生成(含 boundary)
|
||||
const response = await http.post('/newApi/api/files/upload', form, { timeout: 120000 })
|
||||
// 超时按体积给(P3-5:与 upload.ts 同一把尺),几 MB 的 xlsx/zip 不再被固定 120s 掐死
|
||||
const response = await http.post('/newApi/api/files/upload', form, {
|
||||
timeout: uploadTimeoutForBytes(file.size),
|
||||
})
|
||||
// 消费完即释放:文件已落到服务端,页面后续只用返回的 fileKey/url,不再需要这份本地 File。
|
||||
// 失败分支保留条目,让用户能在不重新选文件的情况下重试(超量时由 rememberWebFile 的 FIFO 淘汰兜底)。
|
||||
if (response.data?.success) {
|
||||
|
||||
Reference in New Issue
Block a user