fix(phase-5.2): 消项 CR 第二轮 P1/P2/P3(通道/证据链/交付时序/后管证据页)
This commit is contained in:
@@ -1,38 +1,172 @@
|
||||
import { http, unwrap } from '@/api/http'
|
||||
|
||||
/** 大档分片阈值(AC-11):8MB 以上不再一次 PUT 直传,断点续传由服务端 chunk 通道承担。 */
|
||||
export const CHUNK_UPLOAD_THRESHOLD = 8 * 1024 * 1024
|
||||
/**
|
||||
* 大档分片阈值与片大小(AC-11):后管这一侧只有这一个出口,阈值由片大小派生。
|
||||
*
|
||||
* ⚠ 同族的 8MB 数还另有两处:用户端上传出口一处、桌面客户端分片入口一处。两边是各自
|
||||
* 独立的构建根(各自的 `@` 别名与 tsconfig include),客户端是 Python 打包物 ⇒
|
||||
* 没有共享包就合不成同一个字面量(P3-5 的边界,已登记)。
|
||||
*/
|
||||
export const CHUNK_UPLOAD_PART_SIZE = 8 * 1024 * 1024
|
||||
export const CHUNK_UPLOAD_THRESHOLD = CHUNK_UPLOAD_PART_SIZE
|
||||
|
||||
/** 每片重试次数(不含首次):单片 502/超时不该让 512MB 从 0 再来。 */
|
||||
const PART_RETRY_LIMIT = 2
|
||||
|
||||
/**
|
||||
* 逐片超时(P2-8):预算只看**这一片**有多大——60s 保底 + 每 MB 30s(8MB 片=300s)。
|
||||
* 过去按整包体积算(120s + MB×15s),512MB 包里的一个 8MB 片能拿到 128 分钟,
|
||||
* 慢链路真卡死时没人知道,进度条还会一直爬。
|
||||
*/
|
||||
export function partTimeoutOf(partBytes: number): number {
|
||||
return 60_000 + Math.ceil(partBytes / (1024 * 1024)) * 30_000
|
||||
}
|
||||
|
||||
/** 会话登记/查询只发元数据,固定预算即可;失败要快快回来,别陪整包一起放大。 */
|
||||
const REQUEST_TIMEOUT_MS = 60_000
|
||||
/** 只有拼装这一步真随整包体积走:服务端要读全部分片、复算 sha256。 */
|
||||
const COMPLETE_TIMEOUT_MS = 600_000
|
||||
|
||||
function partUrl(uploadId: string, index: number): string {
|
||||
return `/api/files/chunk/${encodeURIComponent(uploadId)}/part/${index}`
|
||||
}
|
||||
|
||||
/** 后管大档上传(版本包/教程包共用):init → GET 已达片号 → 逐片 POST → complete。 */
|
||||
/** 分片会话跨尝试/刷新续用:同一文件身份复用 upload_id,服务端没回收就从已达片号继续(P2-7)。 */
|
||||
interface StoredChunkSession {
|
||||
uploadId: string;
|
||||
size: number;
|
||||
lastModified: number;
|
||||
}
|
||||
|
||||
const CHUNK_SESSION_PREFIX = 'shufu:admin:chunk:'
|
||||
|
||||
function lastModifiedOf(file: Blob): number {
|
||||
const value = (file as File).lastModified
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : 0
|
||||
}
|
||||
|
||||
/** 会话键:文件名+体积+修改时间,三者同一条上传才算同一个件。 */
|
||||
export function chunkSessionKeyOf(fileName: string, file: Blob): string {
|
||||
return `${CHUNK_SESSION_PREFIX}${fileName}:${file.size}:${lastModifiedOf(file)}`
|
||||
}
|
||||
|
||||
function readStoredSession(key: string, file: Blob): 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 === lastModifiedOf(file) ? parsed.uploadId : ''
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
function writeStoredSession(key: string, file: Blob, uploadId: string): void {
|
||||
if (typeof localStorage === 'undefined') return
|
||||
try {
|
||||
localStorage.setItem(key, JSON.stringify({
|
||||
uploadId, size: file.size, lastModified: lastModifiedOf(file),
|
||||
} satisfies StoredChunkSession))
|
||||
} catch {
|
||||
// 存储不可用(隐私模式/配额满)只影响跨刷新续传,不影响本次上传
|
||||
}
|
||||
}
|
||||
|
||||
function clearStoredSession(key: string): void {
|
||||
if (typeof localStorage === 'undefined') return
|
||||
try {
|
||||
localStorage.removeItem(key)
|
||||
} catch {
|
||||
// 同上:清不掉只会让下次多问一次 status
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务端对「这个会话还在不在」的三种答复:还在(附带已到片号)/确实没了/问不出来。
|
||||
* 第三态是 P2-7 与 P1-11 的共同判据——过去它被当成"没会话",瞬态抖动=整包作废。
|
||||
*/
|
||||
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))
|
||||
}
|
||||
|
||||
function classifyProbe(response: unknown): ProbeOutcome {
|
||||
const status = Number((response as { status?: unknown } | null | undefined)?.status)
|
||||
if (status === 404) return { state: 'gone' }
|
||||
if (status >= 500 || status === 408 || status === 429) return { state: 'transient' }
|
||||
const body = (response as { data?: unknown } | null | undefined)?.data
|
||||
if (body && typeof body === 'object' && (body as { success?: unknown }).success === false) {
|
||||
return messageSaysSessionGone((body as { message?: unknown }).message)
|
||||
? { state: 'gone' }
|
||||
: { state: 'transient' }
|
||||
}
|
||||
const core = (unwrap<Record<string, unknown>>(body) ?? {}) as Record<string, unknown>
|
||||
const arrived = core.uploaded_parts
|
||||
return { state: 'ok', parts: Array.isArray(arrived) ? arrived.map((value) => Number(value)) : [] }
|
||||
}
|
||||
|
||||
/**
|
||||
* 探测已达片号(P2-7/与用户端 upload.ts 同一口径)。
|
||||
* validateStatus 恒真:状态码要自己判——真 404/5xx 一旦被拦截器折成 Error 就分不出原因了;
|
||||
* 能走到 catch 的就只剩网络层失败与超时,铁定瞬态。
|
||||
*/
|
||||
async function probeUploadedParts(uploadId: string): Promise<ProbeOutcome> {
|
||||
try {
|
||||
const response = await http.get(
|
||||
`/api/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' }
|
||||
}
|
||||
}
|
||||
|
||||
/** 后管大档上传(版本包/教程包共用):会话续用 → GET 已达片号 → 逐片 POST → complete。 */
|
||||
export async function uploadFileInChunks(
|
||||
file: Blob,
|
||||
fileName: string,
|
||||
onProgress?: (percent: number) => void,
|
||||
): Promise<Record<string, unknown>> {
|
||||
const totalParts = Math.max(1, Math.ceil(file.size / CHUNK_UPLOAD_PART_SIZE))
|
||||
const initResponse = await http.post('/api/files/chunk/init', null, {
|
||||
params: { fileName, totalParts, totalBytes: file.size },
|
||||
timeout: 120_000 + Math.ceil(file.size / (1024 * 1024)) * 15_000,
|
||||
})
|
||||
const init = (unwrap(initResponse.data) ?? {}) as Record<string, unknown>
|
||||
const uploadId = String(init.upload_id ?? '')
|
||||
if (!uploadId) {
|
||||
throw new Error('后端未返回分片会话,请重试')
|
||||
const sessionKey = chunkSessionKeyOf(fileName, file)
|
||||
let uploadId = readStoredSession(sessionKey, file)
|
||||
const arrived = new Set<number>()
|
||||
if (uploadId) {
|
||||
const probed = await probeUploadedParts(uploadId)
|
||||
if (probed.state === 'gone') {
|
||||
// 服务端确实说会话没了(404/「不存在或已过期」):清掉重开,否则这个件每次都在同一个死会话上失败。
|
||||
clearStoredSession(sessionKey)
|
||||
uploadId = ''
|
||||
} else if (probed.state === 'ok') {
|
||||
probed.parts.forEach((index) => arrived.add(index))
|
||||
}
|
||||
// transient:片号未知但会话还在 ⇒ 照原会话逐片重传(后端同片覆盖,幂等),不重开 init。
|
||||
}
|
||||
if (!uploadId) {
|
||||
const initResponse = await http.post('/api/files/chunk/init', null, {
|
||||
params: { fileName, totalParts, totalBytes: file.size },
|
||||
timeout: REQUEST_TIMEOUT_MS,
|
||||
})
|
||||
const init = (unwrap(initResponse.data) ?? {}) as Record<string, unknown>
|
||||
uploadId = String(init.upload_id ?? '')
|
||||
if (!uploadId) {
|
||||
throw new Error('后端未返回分片会话,请重试')
|
||||
}
|
||||
writeStoredSession(sessionKey, file, uploadId)
|
||||
}
|
||||
|
||||
const statusResponse = await http.get(`/api/files/chunk/${encodeURIComponent(uploadId)}`)
|
||||
const status = (unwrap(statusResponse.data) ?? {}) as Record<string, unknown>
|
||||
const arrived = new Set<number>(
|
||||
Array.isArray(status.uploaded_parts) ? status.uploaded_parts.map((value) => Number(value)) : [],
|
||||
)
|
||||
|
||||
for (let index = 0; index < totalParts; index += 1) {
|
||||
if (arrived.has(index)) {
|
||||
@@ -47,7 +181,7 @@ export async function uploadFileInChunks(
|
||||
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,
|
||||
timeout: partTimeoutOf(blob.size),
|
||||
onUploadProgress: (event) => {
|
||||
if (!onProgress) return
|
||||
const perPart = Math.round(((index + (event.loaded || 0) / Math.max(blob.size, 1)) / totalParts) * 100)
|
||||
@@ -63,14 +197,15 @@ export async function uploadFileInChunks(
|
||||
}
|
||||
}
|
||||
if (lastError) {
|
||||
// 后管上传是一次性的:放弃时把会话连同临时片一起清掉,不留 24h 残盘。
|
||||
await http.delete(`/api/files/chunk/${encodeURIComponent(uploadId)}`).catch(() => undefined)
|
||||
// 不 DELETE(P2-7):会话连同已达片号留在服务端(后端 24h 过期回收兜底),
|
||||
// 下一次点开同一个文件就从断点接着传——这才是"续传"两个字的本义。
|
||||
throw lastError instanceof Error ? lastError : new Error('分片上传失败')
|
||||
}
|
||||
}
|
||||
|
||||
const completeResponse = await http.post(
|
||||
`/api/files/chunk/${encodeURIComponent(uploadId)}/complete`, null, { timeout: 600_000 })
|
||||
`/api/files/chunk/${encodeURIComponent(uploadId)}/complete`, null, { timeout: COMPLETE_TIMEOUT_MS })
|
||||
clearStoredSession(sessionKey)
|
||||
if (onProgress) onProgress(100)
|
||||
return (unwrap(completeResponse.data) ?? {}) as Record<string, unknown>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { http } from './http'
|
||||
import { unwrap } from './envelope'
|
||||
|
||||
/** 任务证据件列表行(AC-13:后管只读面,仅超级管理员)。 */
|
||||
export interface TaskEvidenceRow {
|
||||
id: number
|
||||
taskId: string
|
||||
uid: number | null
|
||||
evidenceType: string
|
||||
source: string
|
||||
objectKey: string
|
||||
byteSize: number
|
||||
contentType: string
|
||||
sha256: string
|
||||
createdAt: string | null
|
||||
}
|
||||
|
||||
export interface TaskEvidencePage {
|
||||
items: TaskEvidenceRow[]
|
||||
total: number
|
||||
page: number
|
||||
pageSize: number
|
||||
}
|
||||
|
||||
export interface TaskEvidenceQuery {
|
||||
taskId?: string
|
||||
uid?: number
|
||||
page: number
|
||||
pageSize: number
|
||||
}
|
||||
|
||||
export interface TaskEvidenceContent {
|
||||
id: number
|
||||
taskId: string
|
||||
contentType: string
|
||||
binary: boolean
|
||||
content: string
|
||||
}
|
||||
|
||||
export async function fetchTaskEvidenceFiles(params: TaskEvidenceQuery): Promise<TaskEvidencePage> {
|
||||
const query: Record<string, string | number> = { page: params.page, pageSize: params.pageSize }
|
||||
if (params.taskId) query.taskId = params.taskId
|
||||
if (params.uid) query.uid = params.uid
|
||||
const { data } = await http.get('/api/admin/task-evidence/files', { params: query })
|
||||
return unwrap<TaskEvidencePage>(data)
|
||||
}
|
||||
|
||||
export async function fetchTaskEvidenceContent(fileId: number): Promise<TaskEvidenceContent> {
|
||||
const { data } = await http.get('/api/admin/task-evidence/content', { params: { fileId } })
|
||||
return unwrap<TaskEvidenceContent>(data)
|
||||
}
|
||||
|
||||
/** 下载走服务端流,不把对象直链/预签名 URL 交给浏览器(AC-13 的不入日志口径)。 */
|
||||
export function taskEvidenceDownloadUrl(fileId: number): string {
|
||||
return `/api/admin/task-evidence/download?fileId=${fileId}`
|
||||
}
|
||||
|
||||
export async function deleteTaskEvidence(ids: number[]): Promise<number> {
|
||||
const { data } = await http.post('/api/admin/task-evidence/delete', ids)
|
||||
return unwrap<{ deleted: number }>(data).deleted
|
||||
}
|
||||
Reference in New Issue
Block a user