diff --git a/admin-frontend-vue/src/api/chunk-upload.ts b/admin-frontend-vue/src/api/chunk-upload.ts index 247a9cf0f..a0a42d3df 100644 --- a/admin-frontend-vue/src/api/chunk-upload.ts +++ b/admin-frontend-vue/src/api/chunk-upload.ts @@ -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( diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java index d50dbcf81..4881666f1 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java @@ -70,7 +70,20 @@ public class TaskEvidenceService { entity.setContentType(stored.contentType()); entity.setSha256(stored.sha256()); entity.setCreatedAt(LocalDateTime.now()); - mapper.insert(entity); + try { + mapper.insert(entity); + } catch (RuntimeException ex) { + // 对象已写进桶、登记行没落:不回收就是永久孤儿(保留期只扫登记行)。 + log.warn("[task-evidence] 登记行写入失败,回收对象 taskId={} key={} reason={}", + normalizedTaskId, stored.objectKey(), ex.getMessage()); + try { + storage.delete(stored.objectKey()); + } catch (RuntimeException cleanup) { + log.warn("[task-evidence] 回收孤儿对象失败 key={} reason={}", + stored.objectKey(), cleanup.getMessage()); + } + throw new BusinessException("证据件登记失败,请稍后重试"); + } deleteTempQuietly(file); Map result = new LinkedHashMap<>(); result.put("id", entity.getId()); @@ -194,7 +207,8 @@ public class TaskEvidenceService { if (text.isEmpty()) { return fallback; } - return text.length() > 64 ? text.substring(0, 64) : text; + // 列宽是 VARCHAR(32),超长标签在这里截断而不是让 insert 抛数据截断错。 + return text.length() > 32 ? text.substring(0, 32) : text; } private void deleteTempQuietly(File file) { diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/ChunkedUploadService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/ChunkedUploadService.java index 5526ecff7..bf4bdbe7f 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/ChunkedUploadService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/ChunkedUploadService.java @@ -298,13 +298,25 @@ public class ChunkedUploadService { long cutoff = System.currentTimeMillis() - Math.max(1, staleHours) * 3600_000L; try (Stream children = Files.list(root)) { children.forEach(child -> { - try { - if (Files.getLastModifiedTime(child).toMillis() < cutoff) { - deleteQuietly(child); - log.info("[chunk-upload] 清理过期分片会话/残留文件 path={}", child.getFileName()); + String name = child.getFileName().toString(); + Runnable sweep = () -> { + try { + // 复查一次 mtime:等锁期间刚被 savePart 刷新的会话不能删。 + if (Files.getLastModifiedTime(child).toMillis() < cutoff) { + deleteQuietly(child); + log.info("[chunk-upload] 清理过期分片会话/残留文件 path={}", child.getFileName()); + } + } catch (IOException ex) { + log.warn("[chunk-upload] 条目时间戳读不出,本轮跳过该条 path={}", child.getFileName()); } - } catch (IOException ex) { - log.warn("[chunk-upload] 条目时间戳读不出,本轮跳过该条 path={}", child.getFileName()); + }; + if (UPLOAD_ID_PATTERN.matcher(name).matches()) { + // 与 savePart/complete 共用会话锁:在飞上传不会被整点回收腰斩。 + synchronized (lockOf(name)) { + sweep.run(); + } + } else { + sweep.run(); } }); } catch (IOException ex) { diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/softwareversion/service/SoftwareVersionService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/softwareversion/service/SoftwareVersionService.java index 3bb93eb23..1d9362797 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/softwareversion/service/SoftwareVersionService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/softwareversion/service/SoftwareVersionService.java @@ -272,6 +272,15 @@ public class SoftwareVersionService { if (size > MAX_UPLOAD_BYTES) { throw new BusinessException("文件超过允许的大小限制"); } + // 同一 version 已登记(超时重试/双击):直接返回既有行,临时件删掉即可。 + SoftwareVersionEntity existing = softwareVersionMapper.selectOne( + new LambdaQueryWrapper() + .eq(SoftwareVersionEntity::getVersion, normalizedVersion) + .last("LIMIT 1")); + if (existing != null) { + deleteTempQuietly(source); + return toItemMap(existing); + } String objectKey = softwareVersionObjectKey(normalizedVersion); String fileUrl = ossStorageService.uploadSoftwareVersionPackage(source, objectKey); SoftwareVersionEntity entity = new SoftwareVersionEntity(); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/tutorial/service/TutorialPackageService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/tutorial/service/TutorialPackageService.java index 73840dfd1..2c91143e3 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/tutorial/service/TutorialPackageService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/tutorial/service/TutorialPackageService.java @@ -148,6 +148,18 @@ public class TutorialPackageService { if (size > MAX_UPLOAD_BYTES) { throw new BusinessException("文件超过允许的大小限制"); } + // 同一 fileName+version 已登记(超时重试/双击):返回既有行,不重复搬桶。 + TutorialPackageEntity existing = tutorialPackageMapper.selectOne( + new LambdaQueryWrapper() + .eq(TutorialPackageEntity::getFileName, normalizedName) + .eq(TutorialPackageEntity::getVersion, normalizedVersion) + .last("LIMIT 1")); + if (existing != null) { + if (source.exists() && !source.delete()) { + log.warn("[tutorial] 清理重复提交的临时件失败 path={}", source.getAbsolutePath()); + } + return toItemMap(existing); + } String generatedKey = OBJECT_KEY_PREFIX + OBJECT_NAME_FORMATTER.format(LocalDateTime.now()) + "-" + safeFileName(normalizedName); ossStorageService.uploadTutorialPackage(source, generatedKey); diff --git a/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java b/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java index 3b992519d..bcad2089d 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/config/CatchSilenceRatchetTest.java @@ -61,7 +61,8 @@ class CatchSilenceRatchetTest { // 逐处记名见 STRICT_TOTAL 上方注释;EMPTY 一格没涨)。 // 10-30 用户端代理白名单(极客/青果)抬 591 -> 598(7 处逐一记名见 STRICT_TOTAL 上方注释; // 实测量到 599,多出的 1 格来自已提交的 79b04afd(AdminApiGuardFilter 编码绕过 catch),见同处说明)。 - static final int GIVEN_NOT_RETHROW = 599; + // 10-31 Phase 5.1:证据登记失败回收对象一枚(内层回收失败只 WARN),599 -> 600。 + static final int GIVEN_NOT_RETHROW = 600; static final int GIVEN_LOG_ONLY = 249; static final int GIVEN_EMPTY = 56; @@ -273,8 +274,13 @@ class CatchSilenceRatchetTest { * * 同批实测 total 1055、rethrow 400、notRethrow 599(未涨,register 是重抛形状)、 * logOnly 189、empty 56。 + * + *

10-31 Phase 5.1 CR 消项再抬 1055 -> 1057,两枚逐处记名(都在 + * {@code TaskEvidenceService.register} 的登记行写入失败兜底):外层 catch 回收已上传对象 + * 后转业务错误(RETHROW+rethrow 401);内层 catch 是对象回收本身失败只 WARN + * (LOG_ONLY+logOnly 190)。两处误差方向都是「不让孤儿对象永久占桶」。 */ - static final int STRICT_TOTAL = 1055; + static final int STRICT_TOTAL = 1057; /** * 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。 * 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。 @@ -397,7 +403,8 @@ class CatchSilenceRatchetTest { * 复测整树 599:多出的 1 格来自已提交的 79b04afd {@code AdminApiGuardFilter.decodePath} * (畸形编码保持原样交给容器 400),非本批新增,说明见 STRICT_TOTAL 上方。 */ - static final int STRICT_NOT_RETHROW = 599; + // 10-31 Phase 5.1:同 GIVEN_NOT_RETHROW,证据登记补偿回收失败只 WARN,599 -> 600。 + static final int STRICT_NOT_RETHROW = 600; /** * 10-17 抬 172 -> 173,出处就是上面点名过的 {@code TaskHeartbeatLivenessService.stamp()}: * 块体只有「取到 key 名 + log.warn」一句,写心跳证据失败时既不重试也不抛出—— @@ -428,8 +435,10 @@ class CatchSilenceRatchetTest { * {@code WorkbenchClientPackageService.abortChunkedPackage} 的 INFO 与 WARN 两枚。 * 10-31 抬 188 -> 189:{@code TaskEvidenceService.purgeExpired} 的逐行保留期回收出口, * 块体只有一句带 {@code [task-evidence]} 锚点的 WARN(id+reason),坏行跳过、其余照删。 + * 同日 Phase 5.1 再抬 189 -> 190:登记行落库失败后的对象回收失败只 WARN(同上锚点), + * 主错误仍由外层转业务错误;不吞主错误。 */ - static final int STRICT_LOG_ONLY = 189; // 严格口径:块内每条顶层语句都是 log 调用才算 + static final int STRICT_LOG_ONLY = 190; // 严格口径:块内每条顶层语句都是 log 调用才算 static final int STRICT_EMPTY = 56; /** diff --git a/frontend-vue/src/shared/api/upload.ts b/frontend-vue/src/shared/api/upload.ts index 6aaea959f..5d16e9d9f 100644 --- a/frontend-vue/src/shared/api/upload.ts +++ b/frontend-vue/src/shared/api/upload.ts @@ -44,6 +44,60 @@ function bodyOf(response: unknown): 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, @@ -90,23 +144,34 @@ async function chunkedUpload( onProgress?: (percent: number) => void, ): Promise> { 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() + 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) } - - // 开传前先问到已达片号:上一次断掉的会话可以跳过已传片(后端保留 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(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(response)) + clearStoredSession(sessionKey) if (onProgress) onProgress(100) - return bodyOf(response) + return completed } export async function uploadTempFileToJava(