import { http, type JavaApiResponse } from './http.ts' import { JAVA_API_PREFIX } from './url.ts' export interface UploadedFileRef { fileKey: string; originalFilename?: string; relativePath?: string; } export interface UploadFileVo { fileKey: string; originalFilename: string; localPath: string; size: number; relativePath?: string; objectKey?: string; url?: string; mediaType?: "image" | "video" | "audio" | "file" | string; } export interface UploadTempFileOptions { relativePath?: string; uploadToOss?: boolean; moduleType?: string; } /** 分片阈值与片大小(S1-F3/AC-17):≤8MB 单次 multipart 足够;再大必须分片, * 否则视频/大包一断就从 0 重传,且 120s 固定超时对慢链路根本不够用。 */ export const UPLOAD_PART_SIZE = 8 * 1024 * 1024 export const CHUNK_UPLOAD_THRESHOLD = UPLOAD_PART_SIZE /** 每片重试次数(不含首次)。 */ const PART_RETRY_LIMIT = 2 function uploadPartUrl(uploadId: string, index: number): string { return `${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}/part/${index}` } /** 兼容真实 axios 响应(body 在 .data)与测试桩(直接返回 body):统一取出 Java 包体。 */ function bodyOf(response: unknown): JavaApiResponse { const candidate = (response as { data?: unknown } | null | undefined)?.data if (candidate && typeof candidate === 'object' && 'success' in candidate) { return candidate as JavaApiResponse } return response as JavaApiResponse } /** 业务失败默认也是 HTTP 200:只看状态码会把被拒的片当成功。 */ function ensureOk(body: JavaApiResponse): JavaApiResponse { 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, options: UploadTempFileOptions, onProgress?: (percent: number) => void, ): Promise> { const formData = new FormData() formData.append('file', file) if (options.relativePath) { formData.append('relativePath', options.relativePath) } if (options.uploadToOss) { formData.append('uploadToOss', 'true') } if (options.moduleType) { formData.append('moduleType', options.moduleType) } const response = await http.post>( `${JAVA_API_PREFIX}/files/upload`, formData, { headers: { 'Content-Type': 'multipart/form-data', }, // 30s 保底 + 每 MB 15s:慢公网传 100MB 视频不再被固定 120s 掐死(S1-F3) timeout: 30_000 + Math.ceil(file.size / (1024 * 1024)) * 15_000, onUploadProgress: (event) => { if (!onProgress) return const total = event.total || file.size onProgress(total > 0 ? Math.min(Math.round((event.loaded / total) * 100), 100) : 0) }, }, ) return bodyOf(response) } /** * 分片上传(AC-17):init → 逐片 POST(每片独立重试)→ complete 服务端拼装。 * 开传前查询已到片号:上一次断掉的会话可跳过已传片(后端保留 24h)。 */ async function chunkedUpload( file: File, options: UploadTempFileOptions, onProgress?: (percent: number) => void, ): Promise> { const totalParts = Math.max(1, Math.ceil(file.size / UPLOAD_PART_SIZE)) const sessionKey = sessionKeyOf(file, options) let uploadId = readStoredSession(sessionKey, file) let done = new Set() 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(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) } const report = (partialBytes: number) => { if (!onProgress) return const loaded = done.size * UPLOAD_PART_SIZE + partialBytes onProgress(Math.min(Math.round((loaded / file.size) * 100), 99)) } for (let index = 0; index < totalParts; index += 1) { if (done.has(index)) { report(0) continue } const blob = file.slice(index * UPLOAD_PART_SIZE, Math.min(file.size, (index + 1) * UPLOAD_PART_SIZE)) 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 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 } catch (error) { lastError = error } } if (lastError) { // 不 DELETE:会话连同已达片号留在服务端,下一次(含刷新后)从断点续传。 writeStoredSession(sessionKey, file, uploadId) throw lastError instanceof Error ? lastError : new Error('分片上传失败') } } const params: Record = {} if (options.relativePath) params.relativePath = options.relativePath if (options.uploadToOss) params.uploadToOss = true if (options.moduleType) params.moduleType = options.moduleType const response = await http.post( `${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}/complete`, null, { params, timeout: 600_000 }, ) const completed = ensureOk(bodyOf(response)) clearStoredSession(sessionKey) if (onProgress) onProgress(100) return completed } export async function uploadTempFileToJava( file: File, relativePathOrOptions?: string | UploadTempFileOptions, onProgress?: (percent: number) => void, ): Promise> { const options = typeof relativePathOrOptions === 'string' ? { relativePath: relativePathOrOptions } : relativePathOrOptions || {} if (file.size > CHUNK_UPLOAD_THRESHOLD) { return chunkedUpload(file, options, onProgress) } return singleUpload(file, options, onProgress) }