From 6fe5c8c8d0bb27b6c3ec8f8df0da9c97402dadeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Sat, 31 Oct 2026 03:45:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(phase-3,4):=20=E8=AF=81=E6=8D=AE=E4=BB=B6?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=E8=90=BD=E7=82=B9=E4=B8=8E=E4=B8=A4?= =?UTF-8?q?=E5=89=8D=E7=AB=AF=E5=A4=A7=E6=A1=A3=E5=88=86=E7=89=87=E6=8E=A5?= =?UTF-8?q?=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 后端新增 evidence 模块(V182 证据表/独立桶/超管读取面/内部登记)与 application.yml evidence 块;管理员四端点逐个 requireSuperAdmin。 - 分片通道补 @Scheduled 回收与 LocalTempCleanupService 的 chunk-uploads 覆盖; 版本/教程 confirm 接受 file_key,把拼装件搬进 client 桶后落库。 - 用户端 9MB 视频改走 uploadTempFileToJava 分片;后管版本/教程 >8MB 走分片, 小文件仍走原 presign 直传。 - 激活 Phase 3/4 后端与两前端预置契约。 --- admin-frontend-vue/src/api/chunk-upload.ts | 58 +++++ .../src/pages/records/tutorial-api.ts | 21 +- .../src/pages/records/version-api.ts | 20 +- .../tests/pending-phase4-大档上传接线.test.ts | 5 +- .../pure/pending-phase4-大档上传接线.test.ts | 3 +- .../aiimage/config/EvidenceProperties.java | 51 +++++ .../aiimage/config/PropertiesConfig.java | 2 +- .../AdminTaskEvidenceController.java | 125 +++++++++++ .../InternalTaskEvidenceController.java | 55 +++++ .../evidence/mapper/TaskEvidenceMapper.java | 19 ++ .../dto/TaskEvidenceRegisterRequest.java | 24 +++ .../model/entity/TaskEvidenceEntity.java | 45 ++++ .../evidence/service/TaskEvidenceService.java | 202 ++++++++++++++++++ .../storage/TaskEvidenceStorageService.java | 131 ++++++++++++ .../file/service/ChunkedUploadService.java | 15 ++ .../file/service/LocalTempCleanupService.java | 26 +++ .../file/service/oss/OssStorageService.java | 31 +++ .../SoftwareVersionAdminController.java | 10 +- .../service/SoftwareVersionService.java | 57 +++++ .../controller/TutorialAdminController.java | 11 +- .../service/TutorialPackageService.java | 49 +++++ .../resources/application-local.example.yml | 5 + .../src/main/resources/application.yml | 16 +- .../main/resources/db/V182__task_evidence.sql | 29 +++ .../evidence/EvidencePhase3ContractTest.java | 2 - .../ChunkedUploadReclaimContractTest.java | 2 - .../shared/api/types/modules/image-video.ts | 21 +- frontend-vue/src/shared/api/upload.ts | 27 ++- .../pending-phase4-大档上传接线.mount.test.ts | 3 +- .../tests/pending-phase4-大档上传接线.test.ts | 5 +- 30 files changed, 1016 insertions(+), 54 deletions(-) create mode 100644 admin-frontend-vue/src/api/chunk-upload.ts create mode 100644 backend-java/src/main/java/com/nanri/aiimage/config/EvidenceProperties.java create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/AdminTaskEvidenceController.java create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/InternalTaskEvidenceController.java create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/mapper/TaskEvidenceMapper.java create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/dto/TaskEvidenceRegisterRequest.java create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/entity/TaskEvidenceEntity.java create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/storage/TaskEvidenceStorageService.java create mode 100644 backend-java/src/main/resources/db/V182__task_evidence.sql diff --git a/admin-frontend-vue/src/api/chunk-upload.ts b/admin-frontend-vue/src/api/chunk-upload.ts new file mode 100644 index 000000000..247a9cf0f --- /dev/null +++ b/admin-frontend-vue/src/api/chunk-upload.ts @@ -0,0 +1,58 @@ +import { http, unwrap } from '@/api/http' + +/** 大档分片阈值(AC-11):8MB 以上不再一次 PUT 直传,断点续传由服务端 chunk 通道承担。 */ +export const CHUNK_UPLOAD_THRESHOLD = 8 * 1024 * 1024 +export const CHUNK_UPLOAD_PART_SIZE = 8 * 1024 * 1024 + +function partUrl(uploadId: string, index: number): string { + return `/api/files/chunk/${encodeURIComponent(uploadId)}/part/${index}` +} + +/** 后管大档上传(版本包/教程包共用):init → GET 已达片号 → 逐片 POST → complete。 */ +export async function uploadFileInChunks( + file: Blob, + fileName: string, + onProgress?: (percent: number) => void, +): Promise> { + 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 + const uploadId = String(init.upload_id ?? '') + if (!uploadId) { + throw new Error('后端未返回分片会话,请重试') + } + + const statusResponse = await http.get(`/api/files/chunk/${encodeURIComponent(uploadId)}`) + const status = (unwrap(statusResponse.data) ?? {}) as Record + const arrived = new Set( + Array.isArray(status.uploaded_parts) ? status.uploaded_parts.map((value) => Number(value)) : [], + ) + + for (let index = 0; index < totalParts; index += 1) { + if (arrived.has(index)) { + continue + } + 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) + } + + const completeResponse = await http.post( + `/api/files/chunk/${encodeURIComponent(uploadId)}/complete`, null, { timeout: 600_000 }) + if (onProgress) onProgress(100) + return (unwrap(completeResponse.data) ?? {}) as Record +} diff --git a/admin-frontend-vue/src/pages/records/tutorial-api.ts b/admin-frontend-vue/src/pages/records/tutorial-api.ts index 4fb0155a1..9af15aefd 100644 --- a/admin-frontend-vue/src/pages/records/tutorial-api.ts +++ b/admin-frontend-vue/src/pages/records/tutorial-api.ts @@ -1,6 +1,7 @@ /** 教程包接口适配(教程管理页):列表 / 直传预签名 / 确认 / 删除。 */ import axios from 'axios' import { http, unwrap } from '@/api/http' +import { CHUNK_UPLOAD_THRESHOLD, uploadFileInChunks } from '@/api/chunk-upload' import { parseTutorialPackageList, parseTutorialPackageUpload } from './tutorial-model.ts' import type { TutorialPackageItem, TutorialPackageList } from './tutorial-dto.ts' @@ -9,6 +10,8 @@ export const TUTORIAL_UPLOAD_ENDPOINT = '/api/admin/tutorial' export const TUTORIAL_PRESIGN_ENDPOINT = `${TUTORIAL_UPLOAD_ENDPOINT}/presign` export const TUTORIAL_CONFIRM_ENDPOINT = `${TUTORIAL_UPLOAD_ENDPOINT}/confirm` export const TUTORIAL_DELETE_ENDPOINT = `${TUTORIAL_UPLOAD_ENDPOINT}/delete` +/** 大档分片通道(AC-11):>8MB 的教程包走这里,不再一次性 presign PUT。 */ +export const TUTORIAL_CHUNK_ENDPOINT = '/api/files/chunk' /** MinIO 直传专用实例:不带 cookie、不挂 401 跳登录拦截器(预签名过期/签名失败不能误判会话过期);大 zip 给足超时。 */ const directPut = axios.create({ timeout: 600_000, withCredentials: false }) @@ -43,10 +46,15 @@ export async function requestTutorialPresign(fileName: string, version = ''): Pr } /** 第三步:直传完成后通知后端校验对象并写入记录(data.item 为新记录行)。 */ -export async function confirmTutorialPackage(objectKey: string, fileName: string, version = ''): Promise { - const { data } = await http.post(TUTORIAL_CONFIRM_ENDPOINT, null, { - params: { object_key: objectKey, file_name: fileName, version }, - }) +export async function confirmTutorialPackage( + objectKey: string, + fileName: string, + version = '', + fileKey = '', +): Promise { + const params: Record = { object_key: objectKey, file_name: fileName, version } + if (fileKey) params.file_key = fileKey + const { data } = await http.post(TUTORIAL_CONFIRM_ENDPOINT, null, { params }) return parseTutorialPackageUpload(data) } @@ -68,6 +76,11 @@ export async function uploadTutorialPackage( version = '', onProgress?: (percent: number) => void, ): Promise { + if (file.size > CHUNK_UPLOAD_THRESHOLD) { + const uploaded = await uploadFileInChunks(file, fileName, onProgress) + const fileKey = String(uploaded.fileKey ?? uploaded.file_key ?? '') + return confirmTutorialPackage(String(uploaded.objectKey ?? ''), fileName, version, fileKey) + } const target = await requestTutorialPresign(fileName, version) await directPut.put(target.uploadUrl, file, { headers: { 'Content-Type': 'application/octet-stream' }, diff --git a/admin-frontend-vue/src/pages/records/version-api.ts b/admin-frontend-vue/src/pages/records/version-api.ts index e7a09fe1e..7b1913a43 100644 --- a/admin-frontend-vue/src/pages/records/version-api.ts +++ b/admin-frontend-vue/src/pages/records/version-api.ts @@ -1,6 +1,7 @@ /** 软件版本列表加载适配(任务 127):GET /api/admin/versions。 */ import axios from 'axios' import { http, unwrap } from '@/api/http' +import { CHUNK_UPLOAD_THRESHOLD, uploadFileInChunks } from '@/api/chunk-upload' import { parseSoftwareVersionList, parseSoftwareVersionUpload } from './version-model.ts' import type { SoftwareVersionItem, SoftwareVersionList } from './version-dto.ts' @@ -9,6 +10,8 @@ export const SOFTWARE_VERSION_UPLOAD_ENDPOINT = '/api/admin/version' export const SOFTWARE_VERSION_PRESIGN_ENDPOINT = `${SOFTWARE_VERSION_UPLOAD_ENDPOINT}/presign` export const SOFTWARE_VERSION_CONFIRM_ENDPOINT = `${SOFTWARE_VERSION_UPLOAD_ENDPOINT}/confirm` export const SOFTWARE_VERSION_DELETE_ENDPOINT = `${SOFTWARE_VERSION_UPLOAD_ENDPOINT}/delete` +/** 大档分片通道(AC-11):>8MB 的安装包走这里,不再一次性 presign PUT。 */ +export const SOFTWARE_VERSION_CHUNK_ENDPOINT = '/api/files/chunk' /** MinIO 直传专用实例:不带 cookie、不挂 401 跳登录拦截器(预签名过期/签名失败不能误判会话过期);大 zip 给足超时。 */ const directPut = axios.create({ timeout: 600_000, withCredentials: false }) @@ -45,9 +48,14 @@ export async function requestSoftwareVersionPresign(version: string): Promise { - const { data } = await http.post(SOFTWARE_VERSION_CONFIRM_ENDPOINT, null, { params: { version } }) +/** + * 第三步:上传完成后通知后端校验并写入版本记录(data.item 为新版本行)。 + * 分片通道把拼装件留在 Java 临时目录,服务端按 file_key 搬进 client 桶后再落库。 + */ +export async function confirmSoftwareVersion(version: string, fileKey = ''): Promise { + const params: Record = { version } + if (fileKey) params.file_key = fileKey + const { data } = await http.post(SOFTWARE_VERSION_CONFIRM_ENDPOINT, null, { params }) return parseSoftwareVersionUpload(data) } @@ -69,6 +77,12 @@ export async function uploadSoftwareVersion( _filename = '', onProgress?: (percent: number) => void, ): Promise { + if (file.size > CHUNK_UPLOAD_THRESHOLD) { + const uploaded = await uploadFileInChunks(file, `${version}.zip`, onProgress) + const fileKey = String(uploaded.fileKey ?? uploaded.file_key ?? '') + const item = await confirmSoftwareVersion(version, fileKey) + return { version, fileUrl: item?.fileUrl || String(uploaded.url ?? ''), item } + } const target = await requestSoftwareVersionPresign(version) await directPut.put(target.uploadUrl, file, { headers: { 'Content-Type': 'application/octet-stream' }, diff --git a/admin-frontend-vue/tests/pending-phase4-大档上传接线.test.ts b/admin-frontend-vue/tests/pending-phase4-大档上传接线.test.ts index dda45158c..54a9ef23e 100644 --- a/admin-frontend-vue/tests/pending-phase4-大档上传接线.test.ts +++ b/admin-frontend-vue/tests/pending-phase4-大档上传接线.test.ts @@ -13,14 +13,13 @@ import { fileURLToPath } from 'node:url' const here = dirname(fileURLToPath(import.meta.url)) const root = resolve(here, '..') -const SKIP = 'Pending Phase 4: 断点续传大档接线(AC-11)' const LARGE_UPLOAD_FILES = [ 'src/pages/records/version-api.ts', 'src/pages/records/tutorial-api.ts', ] -test('后管版本/教程大档必须接分片通道', { skip: SKIP }, () => { +test('后管版本/教程大档必须接分片通道', () => { for (const file of LARGE_UPLOAD_FILES) { const source = readFileSync(resolve(root, file), 'utf-8') assert.match( @@ -31,7 +30,7 @@ test('后管版本/教程大档必须接分片通道', { skip: SKIP }, () => { } }) -test('分片接线不得只落注释', { skip: SKIP }, () => { +test('分片接线不得只落注释', () => { for (const file of LARGE_UPLOAD_FILES) { const source = readFileSync(resolve(root, file), 'utf-8') const codeLines = source diff --git a/admin-frontend-vue/tests/pure/pending-phase4-大档上传接线.test.ts b/admin-frontend-vue/tests/pure/pending-phase4-大档上传接线.test.ts index b030cea4f..ed77504b1 100644 --- a/admin-frontend-vue/tests/pure/pending-phase4-大档上传接线.test.ts +++ b/admin-frontend-vue/tests/pure/pending-phase4-大档上传接线.test.ts @@ -35,9 +35,8 @@ vi.mock('@/api/http', () => ({ import { uploadSoftwareVersion } from '@/pages/records/version-api.ts' -const SKIP = 'Pending Phase 4: 断点续传大档接线(AC-11)' -describe.skip('AC-11 后管大档上传', () => { +describe('AC-11 后管大档上传', () => { it('9MB 安装包必须走分片 init', async () => { // 预签名与分片两套响应揉成一个超集:哪条实现路径先走都能拿到自己要的字段。 mocks.post.mockResolvedValue({ diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/EvidenceProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/EvidenceProperties.java new file mode 100644 index 000000000..3be049523 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/config/EvidenceProperties.java @@ -0,0 +1,51 @@ +package com.nanri.aiimage.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * 任务异常证据件配置(v1.2 Phase 3/AC-13)。 + * + *

证据件落在**独立桶**:默认桶 nanri-ai-images 带 7 天整桶 ILM, + * 与"证据单独保留、单独回收"的口径互相绑死;桶名必须显式写在 application.yml, + * 不许只在代码里留默认值(部署漏配就变成隐形行为)。

+ */ +@Data +@ConfigurationProperties(prefix = "aiimage.evidence") +public class EvidenceProperties { + + /** 证据桶;默认 task-evidence,独立于业务桶。 */ + private String bucket = "task-evidence"; + + /** 证据保留天数(本地与云端同口径,按天回收)。 */ + private Integer retentionDays = 7; + + /** 对象 key 前缀;实际 key = 前缀 + taskId + "/" + UUID + 扩展名。 */ + private String keyPrefix = "task-evidence/"; + + /** 单件证据上限(字节),超过直接拒绝,避免截图/大 JSON 打满桶。 */ + private Long maxFileBytes = 64L * 1024 * 1024; + + /** 保留期回收 cron;默认每天 03:30。 */ + private String retentionCron = "0 30 3 * * *"; + + public String bucketOrDefault() { + return bucket == null || bucket.isBlank() ? "task-evidence" : bucket.trim(); + } + + public int retentionDaysOrDefault() { + return retentionDays == null || retentionDays < 1 ? 7 : retentionDays; + } + + public String keyPrefixOrDefault() { + String prefix = keyPrefix == null ? "" : keyPrefix.trim(); + if (prefix.isEmpty()) { + return "task-evidence/"; + } + return prefix.endsWith("/") ? prefix : prefix + "/"; + } + + public long maxFileBytesOrDefault() { + return maxFileBytes == null || maxFileBytes < 1 ? 64L * 1024 * 1024 : maxFileBytes; + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java b/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java index 192ec9153..c583d96e3 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java @@ -4,6 +4,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Configuration; @Configuration -@EnableConfigurationProperties({OssProperties.class, TransientStorageProperties.class, StorageProperties.class, BrandProgressProperties.class, DeleteBrandProgressProperties.class, BrandCheckProperties.class, ZiniaoProperties.class, ModuleCleanupProperties.class, TaskPressureProperties.class, TaskImageCacheCleanupProperties.class, AppearancePatentProperties.class, SimilarAsinProperties.class, ImageVideoProperties.class, InstanceRoutingProperties.class, CapacityPlanProperties.class, UserSecretProperties.class, NotificationProperties.class, DeviceLogOssProperties.class, CollectDataBrandRecheckProperties.class, ClientCredentialProperties.class, CostGuardProperties.class, KeywordShareSyncProperties.class, CollectRoutingProperties.class}) +@EnableConfigurationProperties({OssProperties.class, TransientStorageProperties.class, StorageProperties.class, BrandProgressProperties.class, DeleteBrandProgressProperties.class, BrandCheckProperties.class, ZiniaoProperties.class, ModuleCleanupProperties.class, TaskPressureProperties.class, TaskImageCacheCleanupProperties.class, AppearancePatentProperties.class, SimilarAsinProperties.class, ImageVideoProperties.class, InstanceRoutingProperties.class, CapacityPlanProperties.class, UserSecretProperties.class, NotificationProperties.class, DeviceLogOssProperties.class, CollectDataBrandRecheckProperties.class, ClientCredentialProperties.class, CostGuardProperties.class, KeywordShareSyncProperties.class, CollectRoutingProperties.class, EvidenceProperties.class}) public class PropertiesConfig { } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/AdminTaskEvidenceController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/AdminTaskEvidenceController.java new file mode 100644 index 000000000..21229f71f --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/AdminTaskEvidenceController.java @@ -0,0 +1,125 @@ +package com.nanri.aiimage.modules.evidence.controller; + +import com.nanri.aiimage.common.api.ApiResponse; +import com.nanri.aiimage.common.api.PageDefaults; +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.common.model.entity.AdminUserEntity; +import com.nanri.aiimage.common.security.AdminAuthSupport; +import com.nanri.aiimage.common.security.DeviceSessionPolicy; +import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity; +import com.nanri.aiimage.modules.evidence.service.TaskEvidenceService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * 后台「任务证据」读取面(AC-13):列表 / 内容 / 下载 / 删除,仅超级管理员。 + * + *

证据件含账号可见的现场信息与窗口截图,访问面比常规后台更严;这里每个方法都走 + * 私有 {@link #requireSuperAdmin(HttpServletRequest)},与 AdminDeviceLogController 同口径。 + * 对象直链与预签名 URL 不入日志、不回前端——下载统一走服务端流。

+ */ +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/admin/task-evidence") +@Tag(name = "任务证据(后台)", description = "任务异常现场证据件的列表、内容、下载与删除(仅超管)。") +public class AdminTaskEvidenceController { + + private final TaskEvidenceService taskEvidenceService; + private final AdminAuthSupport adminAuthSupport; + + @GetMapping("/files") + @Operation(summary = "证据件分页列表", description = "按 taskId 关联查询;uid 只作为精确过滤条件。") + public ApiResponse> files( + HttpServletRequest request, + @Parameter(description = "业务任务号") @RequestParam(required = false) String taskId, + @Parameter(description = "归属用户 uid") @RequestParam(required = false) Long uid, + @RequestParam(defaultValue = PageDefaults.FIRST_PAGE) Long page, + @RequestParam(defaultValue = PageDefaults.ADMIN_LIST_PAGE_SIZE) Long pageSize) { + AdminUserEntity operator = requireSuperAdmin(request); + if (uid != null && !uid.equals(operator.getId()) + && !DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator))) { + throw new BusinessException(403, "无权读取其他用户的证据件"); + } + return ApiResponse.success(taskEvidenceService.page(taskId, uid, page, pageSize)); + } + + @GetMapping("/content") + @Operation(summary = "查看证据件内容", description = "文本证据按 UTF-8 返回;图片证据返回 base64。") + public ApiResponse> content( + HttpServletRequest request, + @Parameter(description = "证据主键") @RequestParam Long fileId) { + requireSuperAdmin(request); + return ApiResponse.success(taskEvidenceService.content(fileId)); + } + + @GetMapping("/download") + @Operation(summary = "下载证据件", description = "服务端从独立证据桶流式读取,不返回直链。") + public void download(HttpServletRequest request, HttpServletResponse response, + @Parameter(description = "证据主键") @RequestParam Long fileId) throws IOException { + requireSuperAdmin(request); + TaskEvidenceEntity entity = taskEvidenceService.requireEvidence(fileId); + byte[] bytes = taskEvidenceService.download(fileId); + String name = "evidence-" + entity.getId() + + (entity.getObjectKey() != null && entity.getObjectKey().contains(".") + ? entity.getObjectKey().substring(entity.getObjectKey().lastIndexOf('.')) : ".bin"); + response.setContentType(entity.getContentType() == null + ? "application/octet-stream" : entity.getContentType()); + response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + + URLEncoder.encode(name, StandardCharsets.UTF_8).replace("+", "%20")); + response.getOutputStream().write(bytes); + } + + @PostMapping("/delete") + @Operation(summary = "删除证据件", description = "先删对象再删登记行;不可恢复。") + public ApiResponse> delete( + HttpServletRequest request, + @RequestBody(required = false) List ids, + @RequestParam(required = false) Long fileId) { + requireSuperAdmin(request); + List targets = new ArrayList<>(); + if (ids != null) { + targets.addAll(ids); + } + if (fileId != null) { + targets.add(fileId); + } + if (targets.isEmpty()) { + throw new BusinessException("未指定要删除的证据"); + } + int deleted = taskEvidenceService.delete(targets); + return ApiResponse.success("已删除", Map.of("deleted", deleted)); + } + + /** + * 证据件仅超管可读:requireAdmin 不含超管判定,这里再判一层。 + * 返回操作者供列表端点做 uid 归属对撞。 + */ + private AdminUserEntity requireSuperAdmin(HttpServletRequest request) { + AdminUserEntity user = adminAuthSupport.requireAdmin(request); + if (!DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(user))) { + log.warn("[task-evidence] 非超管访问被拒 userId={} username={}", + user.getId(), user.getUsername()); + throw new BusinessException(403, "仅超级管理员可访问任务证据"); + } + return user; + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/InternalTaskEvidenceController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/InternalTaskEvidenceController.java new file mode 100644 index 000000000..d89931c3b --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/InternalTaskEvidenceController.java @@ -0,0 +1,55 @@ +package com.nanri.aiimage.modules.evidence.controller; + +import com.nanri.aiimage.common.api.ApiResponse; +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.common.security.AdminAuthSupport; +import com.nanri.aiimage.modules.evidence.model.dto.TaskEvidenceRegisterRequest; +import com.nanri.aiimage.modules.evidence.service.TaskEvidenceService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +/** + * 内部接口:客户端证据件登记(AC-7)。客户端先把现场件分片上传到 Java 临时目录, + * 上传完成后携带 X-Internal-Token 与 task_id/file_key 登记;服务端再把临时件搬进 + * 独立证据桶并落 biz_task_evidence。 + * + *

/api/internal 虽在 AdminApiGuardFilter 兜底名单内、可信令牌会放行,controller + * 仍自校验令牌——防止配置漂移时匿名可达。

+ */ +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/internal/task-evidence") +@Tag(name = "内部接口", description = "证据件登记(X-Internal-Token 鉴权)。") +public class InternalTaskEvidenceController { + + private final TaskEvidenceService taskEvidenceService; + private final AdminAuthSupport adminAuthSupport; + + @PostMapping("/register") + @Operation(summary = "登记任务证据件", description = "按 task_id 关联;file_key 指向已上传到临时目录的证据件。") + public ApiResponse> register(HttpServletRequest request, + @RequestBody(required = false) TaskEvidenceRegisterRequest body) { + if (!adminAuthSupport.isTrustedInternalToken(request)) { + log.warn("[task-evidence] 拒绝未携带可信内部令牌的登记请求 remoteAddr={}", + request.getRemoteAddr()); + throw new BusinessException(401, "未授权"); + } + if (body == null) { + throw new BusinessException("登记载荷为空"); + } + Map result = taskEvidenceService.register( + body.getTaskId(), body.getUid(), body.getEvidenceType(), body.getSource(), body.getFileKey()); + log.info("[task-evidence] registered id={} taskId={}", result.get("id"), body.getTaskId()); + return ApiResponse.success(result); + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/mapper/TaskEvidenceMapper.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/mapper/TaskEvidenceMapper.java new file mode 100644 index 000000000..05c1a1811 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/mapper/TaskEvidenceMapper.java @@ -0,0 +1,19 @@ +package com.nanri.aiimage.modules.evidence.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.time.LocalDateTime; +import java.util.List; + +@Mapper +public interface TaskEvidenceMapper extends BaseMapper { + + /** 回收批次的 key 清单;先删对象再删行,避免对象清不掉而登记行先消失。 */ + @Select("SELECT * FROM biz_task_evidence WHERE created_at < #{expireBefore} ORDER BY id LIMIT #{limit}") + List selectExpired(@Param("expireBefore") LocalDateTime expireBefore, + @Param("limit") int limit); +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/dto/TaskEvidenceRegisterRequest.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/dto/TaskEvidenceRegisterRequest.java new file mode 100644 index 000000000..feb22fb3d --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/dto/TaskEvidenceRegisterRequest.java @@ -0,0 +1,24 @@ +package com.nanri.aiimage.modules.evidence.model.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +/** + * 客户端证据登记载荷(snake_case 与客户端现场件字段同口径)。 + */ +@Data +public class TaskEvidenceRegisterRequest { + + @JsonProperty("task_id") + private String taskId; + + @JsonProperty("file_key") + private String fileKey; + + @JsonProperty("evidence_type") + private String evidenceType; + + private String source; + + private Long uid; +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/entity/TaskEvidenceEntity.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/entity/TaskEvidenceEntity.java new file mode 100644 index 000000000..9de691ee2 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/entity/TaskEvidenceEntity.java @@ -0,0 +1,45 @@ +package com.nanri.aiimage.modules.evidence.model.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.time.LocalDateTime; + +/** + * 任务异常现场证据件登记(表 biz_task_evidence,V182)。 + * + *

只登记归属与对象元数据;对象本体在独立证据桶,直链/预签名 URL 不入库、不入日志。

+ */ +@Data +@TableName("biz_task_evidence") +public class TaskEvidenceEntity { + + @TableId(type = IdType.AUTO) + private Long id; + + /** 业务任务号(客户端字符串口径,允许非数字)。 */ + private String taskId; + + /** 任务归属用户;未知时为空。 */ + private Long uid; + + /** 证据类型:client/crash/brand 等。 */ + private String evidenceType; + + /** 来源入口(崩溃记录器/商品采集等)。 */ + private String source; + + /** 证据桶内对象 key(含 UUID)。 */ + private String objectKey; + + private Long byteSize; + + private String contentType; + + /** 对象 sha256(防篡改/去重)。 */ + private String sha256; + + private LocalDateTime createdAt; +} 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 new file mode 100644 index 000000000..3080ac0ca --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java @@ -0,0 +1,202 @@ +package com.nanri.aiimage.modules.evidence.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.config.EvidenceProperties; +import com.nanri.aiimage.modules.evidence.mapper.TaskEvidenceMapper; +import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity; +import com.nanri.aiimage.modules.evidence.storage.TaskEvidenceStorageService; +import com.nanri.aiimage.modules.file.service.LocalFileStorageService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.time.LocalDateTime; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * 证据登记与读取(AC-7/AC-13)。 + * + *

登记走「客户端先分片上云到 Java 临时目录,再由服务端搬进独立证据桶」:客户端不持有 + * 任何对象存储凭据,object_key/sha256/size 全部由服务端生成,登记接口不接受直链。

+ */ +@Slf4j +@Service +@RequiredArgsConstructor +public class TaskEvidenceService { + + private static final int MAX_TASK_ID_LENGTH = 64; + private static final int MAX_CONTENT_BYTES = 8 * 1024 * 1024; + private static final int RETENTION_BATCH = 200; + + private final TaskEvidenceMapper mapper; + private final TaskEvidenceStorageService storage; + private final LocalFileStorageService localFileStorageService; + private final EvidenceProperties properties; + + /** 客户端上传完成后登记:fileKey 指向 Java 临时目录里的分片拼装件。 */ + public Map register(String taskId, Long uid, String evidenceType, + String source, String fileKey) { + String normalizedTaskId = normalizeTaskId(taskId); + if (fileKey == null || fileKey.isBlank()) { + throw new BusinessException("证据文件标识为空"); + } + File file = localFileStorageService.findLocalSourceFile(fileKey.trim()); + if (file == null || !file.isFile()) { + throw new BusinessException("证据文件不存在或已过期"); + } + TaskEvidenceStorageService.StoredEvidence stored; + try { + stored = storage.store(file, normalizedTaskId, file.getName()); + } catch (IOException | RuntimeException ex) { + log.warn("[task-evidence] register failed taskId={} reason={}", + normalizedTaskId, ex.getMessage()); + throw new BusinessException("证据件入库失败,请稍后重试"); + } + TaskEvidenceEntity entity = new TaskEvidenceEntity(); + entity.setTaskId(normalizedTaskId); + entity.setUid(uid); + entity.setEvidenceType(normalizeLabel(evidenceType, "client")); + entity.setSource(normalizeLabel(source, "client")); + entity.setObjectKey(stored.objectKey()); + entity.setByteSize(stored.byteSize()); + entity.setContentType(stored.contentType()); + entity.setSha256(stored.sha256()); + entity.setCreatedAt(LocalDateTime.now()); + mapper.insert(entity); + deleteTempQuietly(file); + Map result = new LinkedHashMap<>(); + result.put("id", entity.getId()); + result.put("objectKey", entity.getObjectKey()); + result.put("byteSize", entity.getByteSize()); + return result; + } + + public Map page(String taskId, Long uid, long page, long pageSize) { + long safePage = Math.max(1, page); + long safeSize = Math.min(Math.max(1, pageSize), 200); + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + if (taskId != null && !taskId.isBlank()) { + query.eq(TaskEvidenceEntity::getTaskId, taskId.trim()); + } + if (uid != null) { + query.eq(TaskEvidenceEntity::getUid, uid); + } + query.orderByDesc(TaskEvidenceEntity::getId); + Page result = mapper.selectPage(new Page<>(safePage, safeSize), query); + Map data = new LinkedHashMap<>(); + data.put("items", result.getRecords()); + data.put("total", result.getTotal()); + data.put("page", safePage); + data.put("pageSize", safeSize); + return data; + } + + public TaskEvidenceEntity requireEvidence(Long fileId) { + if (fileId == null) { + throw new BusinessException("证据主键为空"); + } + TaskEvidenceEntity entity = mapper.selectById(fileId); + if (entity == null) { + throw new BusinessException("证据不存在或已过期"); + } + return entity; + } + + public Map content(Long fileId) { + TaskEvidenceEntity entity = requireEvidence(fileId); + byte[] bytes = storage.read(entity.getObjectKey(), MAX_CONTENT_BYTES); + Map data = new LinkedHashMap<>(); + data.put("id", entity.getId()); + data.put("taskId", entity.getTaskId()); + data.put("contentType", entity.getContentType()); + boolean binary = entity.getContentType() != null && entity.getContentType().startsWith("image/"); + data.put("binary", binary); + data.put("content", binary + ? java.util.Base64.getEncoder().encodeToString(bytes) + : new String(bytes, java.nio.charset.StandardCharsets.UTF_8)); + return data; + } + + public byte[] download(Long fileId) { + TaskEvidenceEntity entity = requireEvidence(fileId); + return storage.read(entity.getObjectKey(), properties.maxFileBytesOrDefault()); + } + + /** 删除证据:先删对象再删登记行;对象删除失败不吞(留在库里下一轮可重试)。 */ + public int delete(List ids) { + if (ids == null || ids.isEmpty()) { + return 0; + } + int deleted = 0; + for (Long id : ids) { + TaskEvidenceEntity entity = id == null ? null : mapper.selectById(id); + if (entity == null) { + continue; + } + storage.delete(entity.getObjectKey()); + deleted += mapper.deleteById(id); + } + return deleted; + } + + /** 保留期回收:按天清扫到期行与对象(AC-13 的保留期必须有执行者,不能只写在配置里)。 */ + @Scheduled(cron = "${aiimage.evidence.retention-cron:0 30 3 * * *}") + public void purgeExpired() { + LocalDateTime expireBefore = LocalDateTime.now().minusDays(properties.retentionDaysOrDefault()); + while (true) { + List batch = mapper.selectExpired(expireBefore, RETENTION_BATCH); + if (batch.isEmpty()) { + return; + } + int removed = 0; + for (TaskEvidenceEntity entity : batch) { + try { + storage.delete(entity.getObjectKey()); + removed += mapper.deleteById(entity.getId()); + } catch (RuntimeException ex) { + log.warn("[task-evidence] retention delete failed id={} reason={}", + entity.getId(), ex.getMessage()); + } + } + if (removed == 0) { + return; + } + log.info("[task-evidence] retention purge removed={} before={}", removed, expireBefore); + } + } + + private String normalizeTaskId(String taskId) { + String value = taskId == null ? "" : taskId.trim(); + if (value.isEmpty()) { + throw new BusinessException("任务号为空"); + } + if (value.length() > MAX_TASK_ID_LENGTH) { + value = value.substring(0, MAX_TASK_ID_LENGTH); + } + return value; + } + + private String normalizeLabel(String value, String fallback) { + String text = value == null ? "" : value.trim(); + if (text.isEmpty()) { + return fallback; + } + return text.length() > 64 ? text.substring(0, 64) : text; + } + + private void deleteTempQuietly(File file) { + try { + Files.deleteIfExists(file.toPath()); + } catch (IOException ex) { + log.warn("[task-evidence] temp evidence cleanup failed name={}", file.getName()); + } + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/storage/TaskEvidenceStorageService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/storage/TaskEvidenceStorageService.java new file mode 100644 index 000000000..1db360d06 --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/storage/TaskEvidenceStorageService.java @@ -0,0 +1,131 @@ +package com.nanri.aiimage.modules.evidence.storage; + +import com.nanri.aiimage.config.EvidenceProperties; +import com.nanri.aiimage.modules.file.service.oss.OssStorageService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.HexFormat; +import java.util.Locale; +import java.util.UUID; +import java.util.regex.Pattern; + +/** + * 证据对象的独立落点(AC-13):桶与 key 前缀来自 {@link EvidenceProperties}, + * 对象本体不经过业务桶,也不把直链/预签名 URL 写进任何日志或数据库。 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class TaskEvidenceStorageService { + + private static final Pattern TASK_ID_SAFE = Pattern.compile("[^0-9A-Za-z_.-]+"); + private static final Pattern EXT_SAFE = Pattern.compile("\\.[0-9A-Za-z]{1,10}"); + + private final EvidenceProperties properties; + private final OssStorageService ossStorageService; + + private volatile boolean bucketReady; + + /** 落一份证据对象,返回对象 key 与对象元数据。 */ + public StoredEvidence store(File source, String taskId, String originalFilename) throws IOException { + if (source == null || !source.isFile()) { + throw new IllegalArgumentException("evidence file must exist"); + } + long size = source.length(); + if (size > properties.maxFileBytesOrDefault()) { + throw new IllegalArgumentException("evidence file too large"); + } + String bucket = properties.bucketOrDefault(); + ensureBucket(bucket); + String key = objectKey(taskId, originalFilename); + ossStorageService.uploadFileToBucket(source, bucket, key); + return new StoredEvidence(key, size, sha256(source), contentType(originalFilename)); + } + + public byte[] read(String objectKey, long maxBytes) { + return ossStorageService.readObjectBytes( + properties.bucketOrDefault(), objectKey, maxBytes); + } + + public void delete(String objectKey) { + if (objectKey == null || objectKey.isBlank()) { + return; + } + ossStorageService.removeObject(properties.bucketOrDefault(), objectKey); + } + + public String bucket() { + return properties.bucketOrDefault(); + } + + private void ensureBucket(String bucket) { + if (bucketReady) { + return; + } + synchronized (this) { + if (bucketReady) { + return; + } + ossStorageService.ensureBucketExists(bucket); + bucketReady = true; + log.info("[task-evidence] evidence bucket ready bucket={}", bucket); + } + } + + private String objectKey(String taskId, String originalFilename) { + String safeTask = TASK_ID_SAFE.matcher(taskId == null ? "" : taskId.trim()) + .replaceAll("_"); + if (safeTask.isEmpty()) { + safeTask = "unknown"; + } + return properties.keyPrefixOrDefault() + safeTask + "/" + UUID.randomUUID() + extension(originalFilename); + } + + private String extension(String originalFilename) { + String name = originalFilename == null ? "" : originalFilename.trim(); + int slash = Math.max(name.lastIndexOf('/'), name.lastIndexOf('\\')); + if (slash >= 0) { + name = name.substring(slash + 1); + } + int dot = name.lastIndexOf('.'); + String ext = dot < 0 ? "" : name.substring(dot).toLowerCase(Locale.ROOT); + return EXT_SAFE.matcher(ext).matches() ? ext : ".bin"; + } + + private String contentType(String originalFilename) { + String ext = extension(originalFilename); + return switch (ext) { + case ".json" -> "application/json"; + case ".png" -> "image/png"; + case ".jpg", ".jpeg" -> "image/jpeg"; + case ".webp" -> "image/webp"; + case ".txt", ".log" -> "text/plain; charset=utf-8"; + default -> "application/octet-stream"; + }; + } + + private String sha256(File source) throws IOException { + try (InputStream in = Files.newInputStream(source.toPath())) { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] buffer = new byte[1 << 16]; + int read; + while ((read = in.read(buffer)) > 0) { + digest.update(buffer, 0, read); + } + return HexFormat.of().formatHex(digest.digest()); + } catch (NoSuchAlgorithmException ex) { + throw new IllegalStateException("SHA-256 not available", ex); + } + } + + public record StoredEvidence(String objectKey, long byteSize, String sha256, String contentType) { + } +} 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 cebebd568..5526ecff7 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 @@ -9,6 +9,7 @@ import com.nanri.aiimage.config.StorageProperties; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -275,6 +276,20 @@ public class ChunkedUploadService { return sessionLocks.computeIfAbsent(uploadId, key -> new Object()); } + /** + * AC-10(v1.2 Phase 4):stale 分片会话的周期回收。 + * + *

此前唯一调用点在 {@link #init},断掉的上传不 init 就永久占盘;清扫间隔与 + * {@code stale-hours} 同一口径,清理阈值仍由 {@code stale-hours} 决定。

+ */ + @Scheduled(cron = "${aiimage.file.chunk-upload.cleanup-cron:0 15 * * * *}") + public void scheduledCleanupStaleSessions() { + if (!storageProperties.isCleanupEnabled()) { + return; + } + cleanupStaleSessions(); + } + private void cleanupStaleSessions() { Path root = chunkRoot(); if (!Files.isDirectory(root)) { diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/LocalTempCleanupService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/LocalTempCleanupService.java index c55070700..d603ae50c 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/LocalTempCleanupService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/LocalTempCleanupService.java @@ -22,6 +22,8 @@ public class LocalTempCleanupService { private static final Pattern ROOT_TEMP_FILE_PATTERN = Pattern.compile("^[a-fA-F0-9]{32}(\\.[^.]+)?$"); private static final String TRANSIENT_PAYLOAD_DIR_NAME = "transient-payload"; + /** 分片上传会话目录(AC-10):断掉的上传没有业务结果目录条目,统一清理必须覆盖它。 */ + private static final String CHUNK_UPLOAD_DIR_NAME = "chunk-uploads"; private static final String CLEANUP_FAILED_METRIC = "aiimage.temp-cleanup.failed"; private static final List RESULT_DIR_NAMES = List.of( "dedupe-result", @@ -83,6 +85,12 @@ public class LocalTempCleanupService { if (child.isDirectory() && TRANSIENT_PAYLOAD_DIR_NAME.equals(child.getName())) { deletedTransientPayloadCount += deleteExpiredChildrenRecursively(child, transientPayloadExpireBefore); deleteEmptyDirectories(child, tempDir); + continue; + } + if (child.isDirectory() && CHUNK_UPLOAD_DIR_NAME.equals(child.getName())) { + deletedSourceCount += deleteExpiredChildrenRecursively(child, sourceExpireBefore); + // 删片会刷新会话目录 mtime,按"过期"再判目录会留下空壳;这里按空目录自底向上收口。 + deleteEmptyDirectoriesDeep(child, tempDir); } } catch (Exception ex) { handleCleanupFailure(child, ex); @@ -128,6 +136,24 @@ public class LocalTempCleanupService { } } + /** 自底向上删空目录(不看 mtime):分片会话目录删片后会刷新父目录时间,按龄判断会留空壳。 */ + private void deleteEmptyDirectoriesDeep(File directory, File stopAt) { + if (directory == null || directory.equals(stopAt) || !directory.isDirectory()) { + return; + } + File[] children = directory.listFiles(); + if (children != null) { + for (File child : children) { + if (child.isDirectory() && PathSafetyGuard.isInside(directory, child)) { + deleteEmptyDirectoriesDeep(child, stopAt); + } + } + } + if (isDirectoryEmpty(directory)) { + FileUtil.del(directory); + } + } + private boolean isManagedRootTempFile(File file) { return ROOT_TEMP_FILE_PATTERN.matcher(file.getName()).matches(); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/oss/OssStorageService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/oss/OssStorageService.java index ea476c24e..c266f8c46 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/oss/OssStorageService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/file/service/oss/OssStorageService.java @@ -286,6 +286,13 @@ public class OssStorageService { removePublicClientObject(objectKey, SOFTWARE_VERSION_PREFIX, "software version"); } + /** 服务端把教程包搬进公开 client 桶(分片通道确认用;不经浏览器直传)。 */ + public String uploadTutorialPackage(File file, String objectKey) { + requireTutorialKey(objectKey); + uploadFile(file, publicClientBucket(), objectKey); + return getTutorialDownloadUrl(objectKey); + } + /** * 为教程安装包签发浏览器直传用的预签名 PUT URL(公开 client 桶 tutorial/ 前缀)。 * 签名 host 同 {@link #presignSoftwareVersionUpload}:必须是公网入口,不能用内网 client。 @@ -478,6 +485,30 @@ public class OssStorageService { } } + /** + * 把本地文件流式上传到指定桶/对象键(证据件等独立桶使用)。 + * 与 {@link #uploadBytes} 同一归属:桶名与 key 由服务端生成,不接受用户文件名拼路径。 + */ + public void uploadFileToBucket(File file, String bucket, String objectKey) { + String normalizedBucket = requireStorageName(bucket, "bucket"); + String normalizedObjectKey = requireStorageName(objectKey, "objectKey"); + uploadFile(file, normalizedBucket, normalizedObjectKey); + } + + /** 删除指定桶内的对象(证据件回收用);对象不存在不抛。 */ + public void removeObject(String bucket, String objectKey) { + String normalizedBucket = requireStorageName(bucket, "bucket"); + String normalizedObjectKey = requireStorageName(objectKey, "objectKey"); + try { + buildClient().removeObject(RemoveObjectArgs.builder() + .bucket(normalizedBucket) + .object(normalizedObjectKey) + .build()); + } catch (Exception ex) { + throw storageFailure("delete", normalizedBucket + "/" + normalizedObjectKey, ex); + } + } + public byte[] readObjectBytes(String bucket, String objectKey) { // 防御性上限:防止被误用到大对象时一次性读入内存导致 OOM(当前仅模板下载场景,模板为 xlsx KB 级) return readObjectBytes(bucket, objectKey, DEFAULT_READ_MAX_BYTES); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/softwareversion/controller/SoftwareVersionAdminController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/softwareversion/controller/SoftwareVersionAdminController.java index 2ea5fdb46..4e1f85483 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/softwareversion/controller/SoftwareVersionAdminController.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/softwareversion/controller/SoftwareVersionAdminController.java @@ -70,10 +70,14 @@ public class SoftwareVersionAdminController { @Operation(summary = "确认软件版本直传完成", description = "服务端核对 client 桶对象存在与大小并写 web_config,返回新版本行") public ApiResponse> confirmUploadVersion( HttpServletRequest request, - @Parameter(description = "版本号,如 3.0.67") @RequestParam(value = "version", required = false) String version) { + @Parameter(description = "版本号,如 3.0.67") @RequestParam(value = "version", required = false) String version, + @Parameter(description = "分片通道拼装件 fileKey(>8MB 大档用;直传路径不传)") + @RequestParam(value = "file_key", required = false) String fileKey) { AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request); - log.info("[software-version] 管理端确认版本直传完成 operator={} version={}", operator.getUsername(), version); - return ApiResponse.success("上传成功", Map.of("item", softwareVersionService.confirmSoftwareVersion(version))); + log.info("[software-version] 管理端确认版本上传完成 operator={} version={} chunked={}", + operator.getUsername(), version, fileKey != null && !fileKey.isBlank()); + return ApiResponse.success("上传成功", + Map.of("item", softwareVersionService.confirmSoftwareVersion(version, fileKey))); } @PostMapping(value = "/version/delete") 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 01a1d063c..a867e42ec 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 @@ -3,12 +3,14 @@ package com.nanri.aiimage.modules.softwareversion.service; import com.nanri.aiimage.common.text.UserMessages; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.modules.file.service.LocalFileStorageService; import com.nanri.aiimage.modules.file.service.oss.OssStorageService; import com.nanri.aiimage.modules.softwareversion.mapper.SoftwareVersionMapper; import com.nanri.aiimage.modules.softwareversion.model.entity.SoftwareVersionEntity; import com.nanri.aiimage.modules.softwareversion.service.support.PublicVersionLookup; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -57,6 +59,10 @@ public class SoftwareVersionService { private final SoftwareVersionMapper softwareVersionMapper; private final OssStorageService ossStorageService; + /** 分片通道确认用;纯单元测试可缺省(只走预签名直传分支时不需要它)。 */ + @Autowired(required = false) + private LocalFileStorageService localFileStorageService; + /** * 版本列表:web_config 全量按创建时间倒序,与 Flask 返回结构一致 * (items 内为 id / version / file_url / created_at)。 @@ -239,6 +245,57 @@ public class SoftwareVersionService { return result; } + /** + * 分片通道确认(AC-11):fileKey 指向 Java 临时目录里的拼装件,服务端搬进 client 桶后落库。 + * 与预签名直传共用同一个对象 key 规则,客户端不接触任何对象存储凭据。 + */ + public Map confirmSoftwareVersion(String version, String fileKey) { + if (fileKey == null || fileKey.isBlank()) { + return confirmSoftwareVersion(version); + } + if (localFileStorageService == null) { + throw new BusinessException("分片上传通道未就绪,请稍后重试"); + } + String normalizedVersion = requireVersion(version); + File source = localFileStorageService.findLocalSourceFile(fileKey.trim()); + if (source == null || !source.isFile()) { + throw new BusinessException("上传的版本包不存在或已过期,请重新上传"); + } + if (!source.getName().toLowerCase(Locale.ROOT).endsWith(".zip")) { + throw new BusinessException("仅支持 .zip 格式"); + } + long size = source.length(); + if (size <= 0) { + throw new BusinessException("文件为空"); + } + if (size > MAX_UPLOAD_BYTES) { + throw new BusinessException("文件超过允许的大小限制"); + } + String objectKey = softwareVersionObjectKey(normalizedVersion); + String fileUrl = ossStorageService.uploadSoftwareVersionPackage(source, objectKey); + SoftwareVersionEntity entity = new SoftwareVersionEntity(); + entity.setVersion(normalizedVersion); + entity.setFileUrl(fileUrl); + try { + entity.setSha256(sha256Hex(source)); + } catch (IOException ex) { + throw new BusinessException("版本包校验失败,请重新上传"); + } + softwareVersionMapper.insert(entity); + softwareVersionMapper.markLatestById(entity.getId()); + log.info("[software-version] 分片拼装件登记成功 id={} version={} bytes={}", + entity.getId(), normalizedVersion, size); + deleteTempQuietly(source); + SoftwareVersionEntity saved = softwareVersionMapper.selectById(entity.getId()); + return toItemMap(saved == null ? entity : saved); + } + + private void deleteTempQuietly(File file) { + if (file != null && file.exists() && !file.delete()) { + log.warn("[software-version] 清理分片临时件失败 path={}", file.getAbsolutePath()); + } + } + /** 直传完成后确认落库:服务端重算 key、校验对象存在且不超限,写 web_config 并返回新行。 */ public Map confirmSoftwareVersion(String version) { String normalizedVersion = requireVersion(version); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/tutorial/controller/TutorialAdminController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/tutorial/controller/TutorialAdminController.java index 04ddd82e6..3f9417345 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/tutorial/controller/TutorialAdminController.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/tutorial/controller/TutorialAdminController.java @@ -60,11 +60,14 @@ public class TutorialAdminController { HttpServletRequest request, @Parameter(description = "预签名返回的对象 key") @RequestParam(value = "object_key", required = false) String objectKey, @Parameter(description = "上传的 zip 文件名") @RequestParam(value = "file_name", required = false) String fileName, - @Parameter(description = "教程包版本号(展示与排序用,可空)") @RequestParam(value = "version", required = false) String version) { + @Parameter(description = "教程包版本号(展示与排序用,可空)") @RequestParam(value = "version", required = false) String version, + @Parameter(description = "分片通道拼装件 fileKey(>8MB 大档用;直传路径不传)") + @RequestParam(value = "file_key", required = false) String fileKey) { AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request); - log.info("[tutorial] 管理端确认教程包直传完成 operator={} objectKey={} fileName={} version={}", - operator.getUsername(), objectKey, fileName, version); - return ApiResponse.success("上传成功", Map.of("item", tutorialPackageService.confirmTutorialPackage(objectKey, fileName, version))); + log.info("[tutorial] 管理端确认教程包上传完成 operator={} fileName={} version={} chunked={}", + operator.getUsername(), fileName, version, fileKey != null && !fileKey.isBlank()); + return ApiResponse.success("上传成功", + Map.of("item", tutorialPackageService.confirmTutorialPackage(objectKey, fileName, version, fileKey))); } @PostMapping("/tutorial/delete") 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 e5dd05fce..73840dfd1 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 @@ -2,11 +2,13 @@ package com.nanri.aiimage.modules.tutorial.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.modules.file.service.LocalFileStorageService; import com.nanri.aiimage.modules.file.service.oss.OssStorageService; import com.nanri.aiimage.modules.tutorial.mapper.TutorialPackageMapper; import com.nanri.aiimage.modules.tutorial.model.entity.TutorialPackageEntity; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.LocalDateTime; @@ -54,6 +56,10 @@ public class TutorialPackageService { private final TutorialPackageMapper tutorialPackageMapper; private final OssStorageService ossStorageService; + /** 分片通道确认用;纯单元测试可缺省(只走预签名直传分支时不需要它)。 */ + @Autowired(required = false) + private LocalFileStorageService localFileStorageService; + /** 教程包列表:按上传时间倒序(第一条即工具台下载的"最新")。 */ public List> listTutorialPackages() { List entities = tutorialPackageMapper.selectList( @@ -117,6 +123,49 @@ public class TutorialPackageService { return result; } + /** + * 分片通道确认(AC-11):fileKey 指向 Java 临时目录里的拼装件,服务端生成对象 key、 + * 搬进 client 桶 tutorial/ 前缀后落库。客户端不接触任何对象存储凭据。 + */ + public Map confirmTutorialPackage(String objectKey, String fileName, String version, + String fileKey) { + if (fileKey == null || fileKey.isBlank()) { + return confirmTutorialPackage(objectKey, fileName, version); + } + if (localFileStorageService == null) { + throw new BusinessException("分片上传通道未就绪,请稍后重试"); + } + var source = localFileStorageService.findLocalSourceFile(fileKey.trim()); + if (source == null || !source.isFile()) { + throw new BusinessException("上传的教程包不存在或已过期,请重新上传"); + } + String normalizedName = requireZipFileName(fileName); + String normalizedVersion = normalizeVersion(version); + long size = source.length(); + if (size <= 0) { + throw new BusinessException("文件为空"); + } + if (size > MAX_UPLOAD_BYTES) { + throw new BusinessException("文件超过允许的大小限制"); + } + String generatedKey = OBJECT_KEY_PREFIX + OBJECT_NAME_FORMATTER.format(LocalDateTime.now()) + + "-" + safeFileName(normalizedName); + ossStorageService.uploadTutorialPackage(source, generatedKey); + TutorialPackageEntity entity = new TutorialPackageEntity(); + entity.setFileName(normalizedName); + entity.setVersion(normalizedVersion); + entity.setObjectKey(generatedKey); + entity.setFileSize(size); + tutorialPackageMapper.insert(entity); + log.info("[tutorial] 分片拼装件登记成功 id={} fileName={} version={} bytes={}", + entity.getId(), normalizedName, normalizedVersion, size); + if (source.exists() && !source.delete()) { + log.warn("[tutorial] 清理分片临时件失败 path={}", source.getAbsolutePath()); + } + TutorialPackageEntity saved = tutorialPackageMapper.selectById(entity.getId()); + return toItemMap(saved == null ? entity : saved); + } + /** 直传完成后确认落库:校验对象存在且不超限,新 key 不与其它记录冲突,写入记录并返回新行。 */ public Map confirmTutorialPackage(String objectKey, String fileName, String version) { if (objectKey == null || !objectKey.startsWith(OBJECT_KEY_PREFIX)) { diff --git a/backend-java/src/main/resources/application-local.example.yml b/backend-java/src/main/resources/application-local.example.yml index b2400a332..d6d381814 100644 --- a/backend-java/src/main/resources/application-local.example.yml +++ b/backend-java/src/main/resources/application-local.example.yml @@ -36,6 +36,11 @@ AIIMAGE_DIGITAL_HUMAN_OSS_BUCKET=nanri-ai-digital-human AIIMAGE_OSS_ACCESS_KEY_ID=change-me AIIMAGE_OSS_ACCESS_KEY_SECRET=change-me +# 任务异常现场证据件(v1.2 Phase 3):独立桶与保留期必须显式配置;桶不得复用 nanri-ai-images。 +AIIMAGE_EVIDENCE_OSS_BUCKET=task-evidence +AIIMAGE_EVIDENCE_RETENTION_DAYS=7 +AIIMAGE_EVIDENCE_KEY_PREFIX=task-evidence/ + # 工作台客户端包直传预签名有效期(秒);90MB 级包在慢公网入口需要更长窗口。 AIIMAGE_WORKBENCH_CLIENT_PRESIGN_EXPIRY_SECONDS=3600 diff --git a/backend-java/src/main/resources/application.yml b/backend-java/src/main/resources/application.yml index 9c74921ff..1b4a1aed2 100644 --- a/backend-java/src/main/resources/application.yml +++ b/backend-java/src/main/resources/application.yml @@ -153,8 +153,9 @@ aiimage: digital-human-bucket: ${AIIMAGE_DIGITAL_HUMAN_OSS_BUCKET:nanri-ai-digital-human} template-bucket: ${AIIMAGE_TEMPLATE_OSS_BUCKET:aiimage-templates} software-version-bucket: ${AIIMAGE_OSS_SOFTWARE_VERSION_BUCKET:client} - # 店铺数据采集累计档独立桶:默认桶 nanri-ai-images 带 30 天全桶过期规则, - # 而店铺数据记录约定「无更新则一直保留、只准人工删」,故单独成桶且不设过期规则。 + # 店铺数据采集累计档独立桶:默认桶 nanri-ai-images 带 7 天整桶过期规则(铁律 17, + # 2026-10-26 产品确认),而店铺数据记录约定「无更新则一直保留、只准人工删」, + # 故单独成桶且不设过期规则。 shop-data-bucket: ${AIIMAGE_OSS_SHOP_DATA_BUCKET:shufu-shop-data} access-key-id: ${AIIMAGE_OSS_ACCESS_KEY_ID:} access-key-secret: ${AIIMAGE_OSS_ACCESS_KEY_SECRET:} @@ -167,6 +168,15 @@ aiimage: access-key-id: ${AIIMAGE_DEVICE_LOG_OSS_ACCESS_KEY_ID:} access-key-secret: ${AIIMAGE_DEVICE_LOG_OSS_ACCESS_KEY_SECRET:} retention-days: ${AIIMAGE_DEVICE_LOG_RETENTION_DAYS:7} + # 任务异常现场证据件(v1.2 Phase 3/AC-7/AC-13):独立桶 + 独立保留期。 + # 桶必须与业务桶 nanri-ai-images 隔离(那边是 7 天整桶 ILM,证据保留策略不得被它绑死); + # key-prefix 之后实际 key = /<扩展名>(铁律 10:key 必带 UUID)。 + evidence: + bucket: ${AIIMAGE_EVIDENCE_OSS_BUCKET:task-evidence} + retention-days: ${AIIMAGE_EVIDENCE_RETENTION_DAYS:7} + key-prefix: ${AIIMAGE_EVIDENCE_KEY_PREFIX:task-evidence/} + max-file-bytes: ${AIIMAGE_EVIDENCE_MAX_FILE_BYTES:67108864} + retention-cron: ${AIIMAGE_EVIDENCE_RETENTION_CRON:0 30 3 * * *} transient-storage: enabled: ${AIIMAGE_TRANSIENT_STORAGE_ENABLED:true} endpoint: ${AIIMAGE_TRANSIENT_STORAGE_ENDPOINT:http://121.196.149.225:9000} @@ -224,6 +234,8 @@ aiimage: max-parts: ${AIIMAGE_FILE_CHUNK_UPLOAD_MAX_PARTS:4096} # 未完成会话与残留片的驻留小时数:超时由清理动作回收,避免断掉的上传永久占盘 stale-hours: ${AIIMAGE_FILE_CHUNK_UPLOAD_STALE_HOURS:24} + # 周期回收 cron(AC-10):断掉的上传不会再 init,不能只靠 init 时顺手清 + cleanup-cron: ${AIIMAGE_FILE_CHUNK_UPLOAD_CLEANUP_CRON:0 15 * * * *} temp-dir: retention-hours: ${AIIMAGE_TEMP_DIR_RETENTION_HOURS:24} capacity-plan: diff --git a/backend-java/src/main/resources/db/V182__task_evidence.sql b/backend-java/src/main/resources/db/V182__task_evidence.sql new file mode 100644 index 000000000..979cc780f --- /dev/null +++ b/backend-java/src/main/resources/db/V182__task_evidence.sql @@ -0,0 +1,29 @@ +-- V182: 任务异常现场证据件(AC-7/AC-13,v1.2 Phase 3) +-- +-- 背景:客户端在任务异常时会把现场件(JSON 快照+窗口/页面截图)上传到独立证据桶; +-- 本表只登记「哪份证据属于哪个任务」,对象本体留在对象存储,按 retention-days 单独回收。 +-- +-- 设计口径(蓝图 §3.3): +-- * 按 task_id 关联但**不建外键**(全库唯一 FK 是 V80 的 columns.parent_id); +-- * 与 device_log_file(无 task_id/content_type)和 biz_file_result(业务交付物语义)分表, +-- 避免把文本日志通道或业务交付物的保留策略绑死; +-- * object_key 带 UUID(铁律 10),库内只存 key,不存任何直链/预签名 URL(AC-13)。 +-- +-- 回滚(手工): +-- DROP TABLE `biz_task_evidence`; + +CREATE TABLE IF NOT EXISTS `biz_task_evidence` ( + `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键', + `task_id` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '业务任务号(客户端字符串口径)', + `uid` BIGINT NULL COMMENT '任务归属用户(未知时为空)', + `evidence_type` VARCHAR(32) NOT NULL DEFAULT 'client' COMMENT '证据类型:client/crash/brand 等', + `source` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '来源入口(崩溃记录器/商品采集等)', + `object_key` VARCHAR(512) NOT NULL COMMENT '证据桶内对象 key(含 UUID)', + `byte_size` BIGINT NOT NULL DEFAULT 0 COMMENT '对象字节数', + `content_type` VARCHAR(128) NOT NULL DEFAULT 'application/octet-stream' COMMENT '对象媒体类型', + `sha256` CHAR(64) NOT NULL DEFAULT '' COMMENT '对象 sha256(防篡改/去重)', + `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '登记时间', + PRIMARY KEY (`id`), + KEY idx_task_evidence_task_id (task_id, created_at), + KEY idx_task_evidence_uid (uid) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='任务异常现场证据件登记'; diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/EvidencePhase3ContractTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/EvidencePhase3ContractTest.java index b6bb50a36..3c628edf1 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/EvidencePhase3ContractTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/EvidencePhase3ContractTest.java @@ -5,7 +5,6 @@ import com.nanri.aiimage.common.model.entity.AdminUserEntity; import com.nanri.aiimage.common.security.AdminAuthSupport; import com.nanri.aiimage.common.security.DeviceSessionPolicy; import jakarta.servlet.http.HttpServletRequest; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.io.IOException; @@ -55,7 +54,6 @@ import static org.mockito.Mockito.when; *

四样本中的「合法 JWT 200」在 Phase 3 由真机/集成用例补齐(本文件钉守卫放行分支, * 蓝图 Phase 3 准出另见 {@code 鉴权四样本真机留痕})。

*/ -@Disabled("Pending Phase 3: 证据件上云与服务端落点(AC-7/AC-13/AC-14)") class EvidencePhase3ContractTest { private static final Path MODULE = Path.of("").toAbsolutePath(); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/ChunkedUploadReclaimContractTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/ChunkedUploadReclaimContractTest.java index f379b0f30..052bfea42 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/ChunkedUploadReclaimContractTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/ChunkedUploadReclaimContractTest.java @@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.file.service; import com.nanri.aiimage.config.StorageProperties; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.springframework.scheduling.annotation.Scheduled; @@ -41,7 +40,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; *

分片会话只有盘上 meta/part,无 DB 行;证据表的 DB 行保留期判据见 * {@code EvidencePhase3ContractTest}(AC-13 配置块)。

*/ -@Disabled("Pending Phase 4: 服务端残骸回收补洞(AC-10)") class ChunkedUploadReclaimContractTest { @TempDir diff --git a/frontend-vue/src/shared/api/types/modules/image-video.ts b/frontend-vue/src/shared/api/types/modules/image-video.ts index f61ac61af..cebdd32e6 100644 --- a/frontend-vue/src/shared/api/types/modules/image-video.ts +++ b/frontend-vue/src/shared/api/types/modules/image-video.ts @@ -1,4 +1,5 @@ import { get, post, put, type JavaApiResponse, unwrapJavaResponse } from '../../http.ts' +import { uploadTempFileToJava } from '../../upload.ts' import { JAVA_API_PREFIX } from '../../url.ts' import { getCurrentUserId } from '../../user.ts' @@ -213,20 +214,12 @@ function resolveImageVideoMediaType(file: File) { } export async function uploadImageVideoMedia(file: File) { - const formData = new FormData(); - formData.append("file", file); - formData.append("uploadToOss", "true"); - formData.append("moduleType", "IMAGE_VIDEO"); - const response = await post>( - `${JAVA_API_PREFIX}/files/upload`, - formData, - { - headers: { - 'Content-Type': 'multipart/form-data', - }, - timeout: 120000, - }, - ); + // AC-11:>8MB 视频/图片走既有分片通道,断点续传与按体积超时由 uploadTempFileToJava 统一负责; + // 小文件仍回落单次 multipart(同一出口按体积选路)。 + const response = await uploadTempFileToJava(file, { + uploadToOss: true, + moduleType: "IMAGE_VIDEO", + }); if (!response.success) { throw new Error(response.message || "请求失败"); } diff --git a/frontend-vue/src/shared/api/upload.ts b/frontend-vue/src/shared/api/upload.ts index 64da3482c..6aaea959f 100644 --- a/frontend-vue/src/shared/api/upload.ts +++ b/frontend-vue/src/shared/api/upload.ts @@ -35,6 +35,15 @@ 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 +} + /** 单次 multipart 上传(小文件短链路)。超时按体积给,不再固定 120s。 */ async function singleUpload( file: File, @@ -68,7 +77,7 @@ async function singleUpload( }, }, ) - return response.data + return bodyOf(response) } /** @@ -81,22 +90,22 @@ async function chunkedUpload( onProgress?: (percent: number) => void, ): Promise> { const totalParts = Math.max(1, Math.ceil(file.size / UPLOAD_PART_SIZE)) - const init = await http.post>( + 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?.data?.upload_id ?? '') + )) + const uploadId = String(init.data?.upload_id ?? '') if (!uploadId) { throw new Error('后端未返回分片会话,请重试') } // 开传前先问到已达片号:上一次断掉的会话可以跳过已传片(后端保留 24h)。 // 这一问失败不再静默按"全缺"重传:断网/5xx 当场说清楚,别闷头把整包从 0 再传一遍。 - const status = await http.get>( + const status = bodyOf<{ uploaded_parts?: unknown[] }>(await http.get( `${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`, - ) - const arrived = status.data?.data?.uploaded_parts + )) + const arrived = status.data?.uploaded_parts const done = new Set(Array.isArray(arrived) ? arrived.map((index) => Number(index)) : []) const report = (partialBytes: number) => { @@ -138,13 +147,13 @@ async function chunkedUpload( 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>( + const response = await http.post( `${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}/complete`, null, { params, timeout: 600_000 }, ) if (onProgress) onProgress(100) - return response.data + return bodyOf(response) } export async function uploadTempFileToJava( diff --git a/frontend-vue/tests/mount/pending-phase4-大档上传接线.mount.test.ts b/frontend-vue/tests/mount/pending-phase4-大档上传接线.mount.test.ts index 49b29f443..5a9543c32 100644 --- a/frontend-vue/tests/mount/pending-phase4-大档上传接线.mount.test.ts +++ b/frontend-vue/tests/mount/pending-phase4-大档上传接线.mount.test.ts @@ -25,9 +25,8 @@ vi.mock('@/shared/api/http.ts', () => ({ import { uploadImageVideoMedia } from '@/shared/api/types/modules/image-video.ts' -const SKIP = 'Pending Phase 4: 断点续传大档接线(AC-11)' -describe.skip('AC-11 用户端大档上传', () => { +describe('AC-11 用户端大档上传', () => { it('9MB 视频必须走分片 init', async () => { const payload = { success: true, diff --git a/frontend-vue/tests/pending-phase4-大档上传接线.test.ts b/frontend-vue/tests/pending-phase4-大档上传接线.test.ts index c18b9ae9e..246c74191 100644 --- a/frontend-vue/tests/pending-phase4-大档上传接线.test.ts +++ b/frontend-vue/tests/pending-phase4-大档上传接线.test.ts @@ -20,9 +20,8 @@ const here = dirname(fileURLToPath(import.meta.url)) const root = resolve(here, '..') const srcFiles = listSourceFiles(resolve(root, 'src')) const rel = (path: string) => relative(root, path).split(sep).join('/') -const SKIP = 'Pending Phase 4: 断点续传大档接线(AC-11)' -test('uploadTempFileToJava 必须有生产调用方(调用方数 ≥1)', { skip: SKIP }, () => { +test('uploadTempFileToJava 必须有生产调用方(调用方数 ≥1)', () => { assert.ok(srcFiles.length > 50, `源码扫描面异常:只扫到 ${srcFiles.length} 个文件`) const callers = srcFiles .map(rel) @@ -34,7 +33,7 @@ test('uploadTempFileToJava 必须有生产调用方(调用方数 ≥1)', { s ) }) -test('图片视频大档上传必须接分片通道', { skip: SKIP }, () => { +test('图片视频大档上传必须接分片通道', () => { const file = 'src/shared/api/types/modules/image-video.ts' const source = readFileSync(resolve(root, file), 'utf-8') assert.match(