Compare commits

...

5 Commits

Author SHA1 Message Date
huangzd1997 7459177bb4 test(phase-5.1): 分片/教程接线的测试缝与棘轮台账随实现对齐
- upload.ts 保持 axios 实例 http.post(与打包产物一致),两处用例改对撞同一把缝
- 教程确认用例接受可选 file_key,并新增该参数的存在断言
- 静默 catch 台账登记 localStorage 两处,blocks 抬到 307 并逐支说明
2026-10-31 04:47:31 +08:00
huangzd1997 2c2cb23943 fix(phase-5.1): 回收/确认幂等与两前端分片重试按 CR 消项加固
- 分片回收与 savePart/complete 共用会话锁并锁内复查 mtime
- 版本/教程 confirm 按 version 或 fileName+version 幂等返回既有行
- 证据登记失败补偿删除已上传对象;标签截断到列宽 32
- 用户端分片持久化 upload_id、校验包体失败、失败不再 DELETE
- 后管逐片重试 2 次、放弃时删除会话;catch 台账抬到 1057/190
2026-10-31 04:26:58 +08:00
huangzd1997 b13d795731 fix(phase-4): 证据件与分片落点按既有尺子收口
- TaskEvidenceMapper 去掉注解 SQL,保留期回收改用 MyBatis-Plus 分页
- 超管出口只保留一处静态判定,AdminTaskEvidenceController 记 uid 对撞日志
- SHA-256 走 Hutool DigestUtil、临时件用 File.delete 收掉两枚新 catch
- 分页台账补 AdminTaskEvidenceController 两点,catch 台账抬到 1055/189
2026-10-31 04:03:14 +08:00
huangzd1997 6fe5c8c8d0 feat(phase-3,4): 证据件服务端落点与两前端大档分片接线
- 后端新增 evidence 模块(V182 证据表/独立桶/超管读取面/内部登记)与
  application.yml evidence 块;管理员四端点逐个 requireSuperAdmin。
- 分片通道补 @Scheduled 回收与 LocalTempCleanupService 的 chunk-uploads 覆盖;
  版本/教程 confirm 接受 file_key,把拼装件搬进 client 桶后落库。
- 用户端 9MB 视频改走 uploadTempFileToJava 分片;后管版本/教程 >8MB 走分片,
  小文件仍走原 presign 直传。
- 激活 Phase 3/4 后端与两前端预置契约。
2026-10-31 03:45:50 +08:00
huangzd1997 5c283a79fc test(phase-0): 确认基线全绿并预置迭代验收测试契约 2026-10-31 01:14:39 +08:00
38 changed files with 1977 additions and 82 deletions
@@ -0,0 +1,76 @@
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
/** 每片重试次数(不含首次):单片 502/超时不该让 512MB 从 0 再来。 */
const PART_RETRY_LIMIT = 2
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<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 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)) {
continue
}
const blob = file.slice(index * CHUNK_UPLOAD_PART_SIZE,
Math.min(file.size, (index + 1) * CHUNK_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 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(
`/api/files/chunk/${encodeURIComponent(uploadId)}/complete`, null, { timeout: 600_000 })
if (onProgress) onProgress(100)
return (unwrap(completeResponse.data) ?? {}) as Record<string, unknown>
}
@@ -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<TutorialPackageItem | null> {
const { data } = await http.post<unknown>(TUTORIAL_CONFIRM_ENDPOINT, null, {
params: { object_key: objectKey, file_name: fileName, version },
})
export async function confirmTutorialPackage(
objectKey: string,
fileName: string,
version = '',
fileKey = '',
): Promise<TutorialPackageItem | null> {
const params: Record<string, string> = { object_key: objectKey, file_name: fileName, version }
if (fileKey) params.file_key = fileKey
const { data } = await http.post<unknown>(TUTORIAL_CONFIRM_ENDPOINT, null, { params })
return parseTutorialPackageUpload(data)
}
@@ -68,6 +76,11 @@ export async function uploadTutorialPackage(
version = '',
onProgress?: (percent: number) => void,
): Promise<TutorialPackageItem | null> {
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' },
@@ -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<So
}
}
/** 第三步:直传完成后通知后端校验对象并写入版本记录(data.item 为新版本行)。 */
export async function confirmSoftwareVersion(version: string): Promise<SoftwareVersionItem | null> {
const { data } = await http.post<unknown>(SOFTWARE_VERSION_CONFIRM_ENDPOINT, null, { params: { version } })
/**
* 第三步:上传完成后通知后端校验并写入版本记录(data.item 为新版本行)。
* 分片通道把拼装件留在 Java 临时目录,服务端按 file_key 搬进 client 桶后再落库。
*/
export async function confirmSoftwareVersion(version: string, fileKey = ''): Promise<SoftwareVersionItem | null> {
const params: Record<string, string> = { version }
if (fileKey) params.file_key = fileKey
const { data } = await http.post<unknown>(SOFTWARE_VERSION_CONFIRM_ENDPOINT, null, { params })
return parseSoftwareVersionUpload(data)
}
@@ -69,6 +77,12 @@ export async function uploadSoftwareVersion(
_filename = '',
onProgress?: (percent: number) => void,
): Promise<SoftwareVersionUploadResult> {
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' },
@@ -45,7 +45,8 @@ test('align_tutorial_api_contract', () => {
assert.match(api, /\$\{TUTORIAL_UPLOAD_ENDPOINT\}\/confirm/, '确认端点')
assert.match(api, /\$\{TUTORIAL_UPLOAD_ENDPOINT\}\/delete/, '删除端点')
assert.match(api, /params: \{ file_name: fileName, version \}/, '预签名回传文件名与版本号')
assert.match(api, /params: \{ object_key: objectKey, file_name: fileName, version \}/, '确认回传对象 key、文件名与版本号')
assert.match(api, /params: Record<string, string> = \{ object_key: objectKey, file_name: fileName, version \}/, '确认回传对象 key、文件名与版本号')
assert.match(api, /if \(fileKey\) params\.file_key = fileKey/, '分片拼装件的 file_key 必须随确认一起回传')
assert.match(api, /withCredentials: false/, '直传实例不挂会话拦截器')
})
@@ -1,10 +1,10 @@
{
"note": "readSource 钉子逐文件基线。两把尺子:pins=落在用例体内的读盘,reads=全文读点总数(含模块级夹具与 helper)。都只许减;pins 下降而 reads 没同步下降=把读盘挪出体内,不算改进。由 tests/source-pin-ratchet.test.ts 生成。",
"totals": {
"files": 250,
"cases": 1748,
"pins": 451,
"reads": 644
"files": 251,
"cases": 1750,
"pins": 453,
"reads": 646
},
"files": {
"admin-scaffold.test.ts": {
@@ -337,6 +337,11 @@
"pins": 0,
"reads": 1
},
"pending-phase4-大档上传接线.test.ts": {
"cases": 2,
"pins": 2,
"reads": 2
},
"pending-phase4-错误兜底与大文件上传.test.ts": {
"cases": 1,
"pins": 0,
@@ -0,0 +1,42 @@
// AC-11(R4):后管版本/教程大档上传接线的「源码尺子」那一半。
//
// Pending Phase 4 激活。今天 `src/pages/records/version-api.ts` 与 `tutorial-api.ts`
// 都是 presign → 一次 PUT 直传(512MB 级也一样,蓝图 §1.2 R4 实读);本文件要求两条
// 路径接上既有 `/api/files/chunk/**` 分片通道(断点续传/按片重试)。
//
// 跑法:npm test(node --test tests/*.test.ts)。
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const here = dirname(fileURLToPath(import.meta.url))
const root = resolve(here, '..')
const LARGE_UPLOAD_FILES = [
'src/pages/records/version-api.ts',
'src/pages/records/tutorial-api.ts',
]
test('后管版本/教程大档必须接分片通道', () => {
for (const file of LARGE_UPLOAD_FILES) {
const source = readFileSync(resolve(root, file), 'utf-8')
assert.match(
source,
/files\/chunk/,
`AC-11:${file} 仍是 presign 直传(512MB 断网从 0 重来,没有分片续传)`,
)
}
})
test('分片接线不得只落注释', () => {
for (const file of LARGE_UPLOAD_FILES) {
const source = readFileSync(resolve(root, file), 'utf-8')
const codeLines = source
.split('\n')
.filter((line) => !line.trim().startsWith('//') && !line.trim().startsWith('*'))
.join('\n')
assert.match(codeLines, /files\/chunk/, `AC-11:${file} 的分片引用只出现在注释里`)
}
})
@@ -0,0 +1,72 @@
// AC-11(R4):后管大档上传接线的「行为用例」那一半。
//
// Pending Phase 4 激活。9MB 安装包(跨分片阈值)调 uploadSoftwareVersion,
// 必须出现 POST /files/chunk/init;今天它走 presign → 一次 PUT 直传,
// 512MB 断网只能从 0 重来。本文件不出网,mock 掉 http 与 axios 直传实例。
import { describe, it, expect, vi } from 'vitest'
const mocks = vi.hoisted(() => ({
post: vi.fn(),
get: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
directPut: vi.fn(),
}))
vi.mock('axios', () => ({
default: {
create: () => ({ put: mocks.directPut }),
},
}))
vi.mock('@/api/http', () => ({
http: { post: mocks.post, get: mocks.get, put: mocks.put, delete: mocks.delete },
unwrap: (value: unknown) => {
const envelope = value as { success?: boolean; data?: unknown; message?: string }
if (envelope && typeof envelope === 'object' && 'success' in envelope) {
if (!envelope.success) {
throw new Error(envelope.message || '请求失败')
}
return envelope.data
}
return value
},
}))
import { uploadSoftwareVersion } from '@/pages/records/version-api.ts'
describe('AC-11 后管大档上传', () => {
it('9MB 安装包必须走分片 init', async () => {
// 预签名与分片两套响应揉成一个超集:哪条实现路径先走都能拿到自己要的字段。
mocks.post.mockResolvedValue({
data: {
success: true,
data: {
upload_url: 'https://oss.example/put',
file_url: 'https://oss.example/pkg.zip',
version: '9.9.9',
object_key: 'client/versions/pkg.zip',
upload_id: 'a'.repeat(32),
uploaded_parts: [],
item: null,
},
},
})
mocks.get.mockResolvedValue({
data: { success: true, data: { upload_id: 'a'.repeat(32), uploaded_parts: [] } },
})
mocks.directPut.mockResolvedValue({})
const file = new Blob([new Uint8Array(9 * 1024 * 1024)], {
type: 'application/octet-stream',
})
await uploadSoftwareVersion('9.9.9', file)
const urls = mocks.post.mock.calls.map(([url]) => String(url))
expect(
urls.some((url) => url.includes('/files/chunk/init')),
`AC-11:9MB 安装包仍走 presign 直传,实际出口:${urls.join(', ')}`,
).toBe(true)
})
})
@@ -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)。
*
* <p>证据件落在**独立桶**:默认桶 nanri-ai-images 带 7 天整桶 ILM,
* 与"证据单独保留、单独回收"的口径互相绑死;桶名必须显式写在 application.yml,
* 不许只在代码里留默认值(部署漏配就变成隐形行为)。</p>
*/
@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;
}
}
@@ -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 {
}
@@ -0,0 +1,126 @@
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):列表 / 内容 / 下载 / 删除,仅超级管理员。
*
* <p>证据件含账号可见的现场信息与窗口截图,访问面比常规后台更严;这里每个方法都走
* 私有 {@link #requireSuperAdmin(HttpServletRequest)},与 AdminDeviceLogController 同口径。
* 对象直链与预签名 URL 不入日志、不回前端——下载统一走服务端流。</p>
*/
@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<Map<String, Object>> 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())) {
// requireSuperAdmin 已保证当前是超管;uid 过滤仍与操作者归属对撞,防伪造 uid 越权读。
log.info("[task-evidence] 超管按 uid 过滤证据 userId={} targetUid={}",
operator.getId(), uid);
}
return ApiResponse.success(taskEvidenceService.page(taskId, uid, page, pageSize));
}
@GetMapping("/content")
@Operation(summary = "查看证据件内容", description = "文本证据按 UTF-8 返回;图片证据返回 base64。")
public ApiResponse<Map<String, Object>> 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<Map<String, Object>> delete(
HttpServletRequest request,
@RequestBody(required = false) List<Long> ids,
@RequestParam(required = false) Long fileId) {
requireSuperAdmin(request);
List<Long> 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;
}
}
@@ -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。
*
* <p>/api/internal 虽在 AdminApiGuardFilter 兜底名单内、可信令牌会放行,controller
* 仍自校验令牌——防止配置漂移时匿名可达。</p>
*/
@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<Map<String, Object>> 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<String, Object> 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);
}
}
@@ -0,0 +1,9 @@
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;
@Mapper
public interface TaskEvidenceMapper extends BaseMapper<TaskEvidenceEntity> {
}
@@ -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;
}
@@ -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)。
*
* <p>只登记归属与对象元数据;对象本体在独立证据桶,直链/预签名 URL 不入库、不入日志。</p>
*/
@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;
}
@@ -0,0 +1,219 @@
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)。
*
* <p>登记走「客户端先分片上云到 Java 临时目录,再由服务端搬进独立证据桶」:客户端不持有
* 任何对象存储凭据,object_key/sha256/size 全部由服务端生成,登记接口不接受直链。</p>
*/
@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<String, Object> 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());
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<String, Object> result = new LinkedHashMap<>();
result.put("id", entity.getId());
result.put("objectKey", entity.getObjectKey());
result.put("byteSize", entity.getByteSize());
return result;
}
public Map<String, Object> 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<TaskEvidenceEntity> 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<TaskEvidenceEntity> result = mapper.selectPage(new Page<>(safePage, safeSize), query);
Map<String, Object> 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<String, Object> content(Long fileId) {
TaskEvidenceEntity entity = requireEvidence(fileId);
byte[] bytes = storage.read(entity.getObjectKey(), MAX_CONTENT_BYTES);
Map<String, Object> 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<Long> 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) {
Page<TaskEvidenceEntity> expiredPage = mapper.selectPage(
new Page<>(1, RETENTION_BATCH),
new LambdaQueryWrapper<TaskEvidenceEntity>()
.lt(TaskEvidenceEntity::getCreatedAt, expireBefore)
.orderByAsc(TaskEvidenceEntity::getId));
List<TaskEvidenceEntity> batch = expiredPage.getRecords();
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;
}
// 列宽是 VARCHAR(32),超长标签在这里截断而不是让 insert 抛数据截断错。
return text.length() > 32 ? text.substring(0, 32) : text;
}
private void deleteTempQuietly(File file) {
if (file != null && file.exists() && !file.delete()) {
log.warn("[task-evidence] temp evidence cleanup failed name={}", file.getName());
}
}
}
@@ -0,0 +1,117 @@
package com.nanri.aiimage.modules.evidence.storage;
import cn.hutool.crypto.digest.DigestUtil;
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.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) {
return DigestUtil.sha256Hex(source);
}
public record StoredEvidence(String objectKey, long byteSize, String sha256, String contentType) {
}
}
@@ -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 分片会话的周期回收。
*
* <p>此前唯一调用点在 {@link #init},断掉的上传不 init 就永久占盘;清扫间隔与
* {@code stale-hours} 同一口径,清理阈值仍由 {@code stale-hours} 决定。</p>
*/
@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)) {
@@ -283,13 +298,25 @@ public class ChunkedUploadService {
long cutoff = System.currentTimeMillis() - Math.max(1, staleHours) * 3600_000L;
try (Stream<Path> 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) {
@@ -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<String> 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();
}
@@ -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);
@@ -70,10 +70,14 @@ public class SoftwareVersionAdminController {
@Operation(summary = "确认软件版本直传完成", description = "服务端核对 client 桶对象存在与大小并写 web_config,返回新版本行")
public ApiResponse<Map<String, Object>> 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")
@@ -1,14 +1,17 @@
package com.nanri.aiimage.modules.softwareversion.service;
import cn.hutool.crypto.digest.DigestUtil;
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 +60,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 +246,62 @@ public class SoftwareVersionService {
return result;
}
/**
* 分片通道确认(AC-11):fileKey 指向 Java 临时目录里的拼装件,服务端搬进 client 桶后落库。
* 与预签名直传共用同一个对象 key 规则,客户端不接触任何对象存储凭据。
*/
public Map<String, Object> 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("文件超过允许的大小限制");
}
// 同一 version 已登记(超时重试/双击):直接返回既有行,临时件删掉即可。
SoftwareVersionEntity existing = softwareVersionMapper.selectOne(
new LambdaQueryWrapper<SoftwareVersionEntity>()
.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();
entity.setVersion(normalizedVersion);
entity.setFileUrl(fileUrl);
entity.setSha256(DigestUtil.sha256Hex(source));
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<String, Object> confirmSoftwareVersion(String version) {
String normalizedVersion = requireVersion(version);
@@ -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")
@@ -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<Map<String, Object>> listTutorialPackages() {
List<TutorialPackageEntity> entities = tutorialPackageMapper.selectList(
@@ -117,6 +123,61 @@ public class TutorialPackageService {
return result;
}
/**
* 分片通道确认(AC-11):fileKey 指向 Java 临时目录里的拼装件,服务端生成对象 key、
* 搬进 client 桶 tutorial/ 前缀后落库。客户端不接触任何对象存储凭据。
*/
public Map<String, Object> 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("文件超过允许的大小限制");
}
// 同一 fileName+version 已登记(超时重试/双击):返回既有行,不重复搬桶。
TutorialPackageEntity existing = tutorialPackageMapper.selectOne(
new LambdaQueryWrapper<TutorialPackageEntity>()
.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);
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<String, Object> confirmTutorialPackage(String objectKey, String fileName, String version) {
if (objectKey == null || !objectKey.startsWith(OBJECT_KEY_PREFIX)) {
@@ -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
@@ -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 = <prefix><taskId>/<UUID><扩展名>(铁律 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:
@@ -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='任务异常现场证据件登记';
@@ -18,7 +18,7 @@ import org.junit.jupiter.api.Test;
* 分页/条数默认值的<b>逐点台账 + 棘轮</b>(2026-10-09 评审「可读 5.0 / 交互 5.5」项 5)。
*
* <p>评审量到的是"15/20/50/1000 四种并存、31 处 {@code defaultValue}"。本轮实测更细:
* 数字型 {@code defaultValue} 共 <b>53</b> 处,其中 52 处是分页/条数口径(23 处页码 + 29 处容量与上限),
* 数字型 {@code defaultValue} 共 <b>55</b> 处,其中 54 处是分页/条数口径(24 处页码 + 30 处容量与上限),
* 另有 1 处是 {@code timeout_minutes=30},不是分页。
*
* <p>这里<b>不规定"全局统一默认页容量"</b>——把 15/20/50/1000 拍成一个数是改行为。判据只做两件事:
@@ -39,9 +39,11 @@ class PageDefaultsLedgerTest {
/**
* 出站点台账:{@code 文件#参数 -> 该页当前默认值的具名出处}。
* 52 条,逐条对应一次真实翻页行为;加页要登记,改值要表态。
* 54 条,逐条对应一次真实翻页行为;加页要登记,改值要表态。
*/
private static final List<String> LEDGER = List.of(
"AdminTaskEvidenceController.java#page=FIRST_PAGE",
"AdminTaskEvidenceController.java#pageSize=ADMIN_LIST_PAGE_SIZE",
"AdminUserController.java#page=FIRST_PAGE",
"AdminUserController.java#page_size=USER_LIST_PAGE_SIZE",
"AdminUserApiSecretController.java#page=FIRST_PAGE",
@@ -107,10 +109,10 @@ class PageDefaultsLedgerTest {
List<String> constants = actual.stream().filter(s -> !s.contains("LITERAL")).sorted().toList();
assertThat(constants)
.as("52 处分页/条数出站点的具名默认值必须与台账一一对应")
.as("54 处分页/条数出站点的具名默认值必须与台账一一对应")
.isEqualTo(LEDGER.stream().sorted().toList());
// 同口径非零对照:台账不是空的,扫描器也确实数得出 48 处
assertThat(constants).hasSize(52);
// 同口径非零对照:台账不是空的,扫描器也确实数得出 54 处
assertThat(constants).hasSize(54);
}
@Test
@@ -126,7 +128,7 @@ class PageDefaultsLedgerTest {
List<String> actual = sites();
assertThat(kindsUsing(actual, "page", "FIRST_PAGE"))
.as("页码口径只有一套:全部从 1 起")
.isEqualTo(23);
.isEqualTo(24);
assertThat(distinctConstants(actual, "page_size"))
.as("page_size 的默认容量集合:15/20/50/1000 四种各自登记,新来第五种要红")
.containsExactlyInAnyOrder("USER_LIST_PAGE_SIZE", "ADMIN_LIST_PAGE_SIZE",
@@ -102,7 +102,7 @@ class SuperAdminPredicateSingleOutletTest {
}
@Test
void superAdminOutletIsOneStaticPredicateAndEighteenCallSites() {
void superAdminOutletIsOneStaticPredicateAndNineteenCallSites() {
int definitions = 0;
int callSites = 0;
int wrappers = 0;
@@ -122,8 +122,9 @@ class SuperAdminPredicateSingleOutletTest {
assertThat(callSites)
// 17 -> 18:v1.1 Phase 1(AC-7 数据读取按归属收口)在
// AdminShopDataCrawlTasksController:151 新增一处走同一出口的调用,Phase 3 补记。
// 18 -> 19:v1.2 Phase 3 证据读取面 AdminTaskEvidenceController#requireSuperAdmin。
.as("走出口的调用点数:17 个业务调用方 + DataReadScope 包装体(新增调用要一起改这个数字,别改回字面量)")
.isEqualTo(18);
.isEqualTo(19);
// 自检:这三把尺子的锚点必须真落在预期的文件里,否则"恰好也是 N"可能是扫错地方凑出来的
assertThat(read(mainRoot().resolve("common/security/DeviceSessionPolicy.java")))
.contains("public static boolean isSuper(String resolvedRole)");
@@ -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;
@@ -260,8 +261,26 @@ class CatchSilenceRatchetTest {
* ⚠ 本批提交时整树已是 1053/notRethrow 599:多出的 1 格不属本批——已提交的 79b04afd 在
* {@code AdminApiGuardFilter.decodePath} 增加了「畸形百分号编码保持原样、交给容器 400」的 catch
* (NOT_RETHROW 形状),该笔未抬本基线。这里按整树实测一并包住并在这一行记名。
*
* <p>10-31 v1.2 Phase 3 证据件落点抬 total 1053 -&gt; 1055,两枚逐处记名(都在
* {@code TaskEvidenceService}):
* <ul>
* <li>{@code register} 一枚:{@code IOException | RuntimeException} 记 WARN 后转
* {@code BusinessException}(RETHROW 形状,同时进 rethrow 400);证据件写不进独立桶
* 必须让客户端拿到可读失败,不能登记一条指向空对象的行。</li>
* <li>{@code purgeExpired} 一枚:保留期回收逐行删除,单行删对象/删登记失败只记
* {@code [task-evidence]} WARN 并继续扫下一行(NOT_RETHROW+LOG_ONLY);一行坏数据
* 不该让整轮保留期回收停摆,下一轮还会重试。</li>
* </ul>
* 同批实测 total 1055、rethrow 400、notRethrow 599(未涨,register 是重抛形状)、
* logOnly 189、empty 56。
*
* <p>10-31 Phase 5.1 CR 消项再抬 1055 -&gt; 1057,两枚逐处记名(都在
* {@code TaskEvidenceService.register} 的登记行写入失败兜底):外层 catch 回收已上传对象
* 后转业务错误(RETHROW+rethrow 401);内层 catch 是对象回收本身失败只 WARN
* (LOG_ONLY+logOnly 190)。两处误差方向都是「不让孤儿对象永久占桶」。
*/
static final int STRICT_TOTAL = 1053;
static final int STRICT_TOTAL = 1057;
/**
* 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。
* 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。
@@ -384,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」一句,写心跳证据失败时既不重试也不抛出——
@@ -413,8 +433,12 @@ class CatchSilenceRatchetTest {
* (这四枚原本是 {@code catch (IOException ignored)} 空体,按 10-26 那批先例改成带 path 的 WARN,
* 是「空体换 LOG_ONLY」的迁移而不是新增沉默),加上
* {@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 = 188; // 严格口径:块内每条顶层语句都是 log 调用才算
static final int STRICT_LOG_ONLY = 190; // 严格口径:块内每条顶层语句都是 log 调用才算
static final int STRICT_EMPTY = 56;
/**
@@ -0,0 +1,419 @@
package com.nanri.aiimage.modules.evidence;
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 jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* AC-7/AC-13/AC-14(R3):证据件上云后的服务端落点与访问面契约。
*
* <p>Pending Phase 3 激活。Phase 0 预置的判据(接缝名是判据的一部分,改名必须同步改本文件
* 并在蓝图 §5 登记):</p>
*
* <ul>
* <li>Flyway {@code V182+} 新增 {@code biz_task_evidence}:task_id/uid/evidence_type/source/
* object_key/byte_size/content_type/sha256/created_at,按 task_id 关联但**不建外键**;</li>
* <li>{@code application.yml} 的 {@code evidence:} 块显式落 bucket(独立桶,不得为
* nanri-ai-images)、retention-days(必须有数字默认值)、key-prefix;</li>
* <li>证据读取端点落在 {@code /api/admin/task-evidence/**}(files/content/download/delete),
* 每个方法都走私有 {@code requireSuperAdmin(HttpServletRequest)}(对齐
* {@code AdminDeviceLogController:167-175});</li>
* <li>守卫行为三样本:无 token/bad token → 401、非超管 → 403、超管 → 放行;伪造 uid 由
* files 端点的 uid 归属校验兜住(源码判据);</li>
* <li>证据模块日志零命中 presign / 直链(AC-13);对象 key 带 UUID(铁律 10)。</li>
* </ul>
*
* <p>四样本中的「合法 JWT 200」在 Phase 3 由真机/集成用例补齐(本文件钉守卫放行分支,
* 蓝图 Phase 3 准出另见 {@code 鉴权四样本真机留痕})。</p>
*/
class EvidencePhase3ContractTest {
private static final Path MODULE = Path.of("").toAbsolutePath();
private static final Path MAIN_JAVA = MODULE.resolve("src/main/java");
private static final Path DB_DIR = MODULE.resolve("src/main/resources/db");
private static final Path APP_YML = MODULE.resolve("src/main/resources/application.yml");
private static final String CONTROLLER_FQCN =
"com.nanri.aiimage.modules.evidence.controller.AdminTaskEvidenceController";
private static final String EVIDENCE_PATH = "/api/admin/task-evidence";
@Test
void 迁移必须新增证据表且字段与索引齐全() throws IOException {
List<Path> migrations = new ArrayList<>();
try (Stream<Path> stream = Files.list(DB_DIR)) {
stream.filter(path -> path.getFileName().toString().matches("V\\d+__.*\\.sql"))
.filter(path -> versionOf(path.getFileName().toString()) >= 182)
.forEach(migrations::add);
}
assertFalse(migrations.isEmpty(), "AC-7:本迭代必须新增 V182+ 迁移(当前最高 V181)");
StringBuilder joined = new StringBuilder();
for (Path migration : migrations) {
joined.append(read(migration)).append('\n');
}
String all = joined.toString();
Matcher table = Pattern.compile("(?is)create\\s+table[^;]*biz_task_evidence[^;]*;")
.matcher(all);
assertTrue(table.find(), "AC-7:V182+ 必须建 biz_task_evidence 表(蓝图 §3.3)");
String ddl = table.group();
for (String column : List.of("task_id", "uid", "evidence_type", "source", "object_key",
"byte_size", "content_type", "sha256", "created_at")) {
assertTrue(Pattern.compile("(?i)\\b" + column + "\\b").matcher(ddl).find(),
"AC-7:证据表缺列 " + column);
}
assertFalse(ddl.toUpperCase().contains("FOREIGN KEY"),
"AC-7:证据表按 task_id 关联但不建外键(全库唯一 FK 是 V80:47)");
assertTrue(Pattern.compile("(?i)(index|key)\\s+\\w+\\s*\\(\\s*task_id").matcher(ddl).find(),
"AC-7:必须有 task_id 索引(按任务关联查询)");
assertTrue(Pattern.compile("(?i)(index|key)\\s+\\w+\\s*\\(\\s*uid").matcher(ddl).find(),
"AC-7:必须有 uid 索引(数据范围/清理)");
}
@Test
void 证据桶与保留期必须显式落在applicationYml() throws IOException {
String yaml = read(APP_YML);
String block = yamlBlock(yaml, "evidence");
assertNotNull(block, "AC-13:application.yml 必须有 evidence: 配置块(不许只落代码默认值)");
String bucket = scalar(block, "bucket");
assertNotNull(bucket, "AC-13:必须配置证据桶");
String bucketDefault = defaultValue(bucket);
assertNotNull(bucketDefault, "AC-13:证据桶不得只有变量无默认值(部署漏配即隐形)");
assertFalse(bucketDefault.contains("nanri-ai-images"),
"AC-13:证据必须落独立桶,不得复用业务桶 nanri-ai-images");
String retention = scalar(block, "retention-days");
assertNotNull(retention, "AC-13:必须配置保留天数");
String retentionDefault = defaultValue(retention);
assertNotNull(retentionDefault, "AC-13:保留天数必须有确定的数字默认值(天数要进判据)");
assertTrue(Integer.parseInt(retentionDefault) >= 1, "AC-13:保留天数必须为正整数");
assertNotNull(scalar(block, "key-prefix"), "AC-13:必须配置对象 key 前缀");
}
@Test
void 证据读取端点必须全部走super访问面() throws IOException {
Path controller = evidenceController();
String source = read(controller);
assertTrue(source.contains(EVIDENCE_PATH),
"AC-13:证据读取控制器必须落在 " + EVIDENCE_PATH);
List<String> bodies = mappedMethodBodies(source);
assertTrue(bodies.size() >= 4,
"AC-13:证据读取至少要有 files/content/download/delete 四个端点,实扫 " + bodies.size());
for (String body : bodies) {
assertTrue(body.contains("requireSuperAdmin"),
"AC-13:证据读取端点必须逐个走 requireSuperAdmin(非 super 一律 403)\n" + body);
}
assertTrue(source.contains("isSuper"),
"AC-13:超管判定必须复用 DeviceSessionPolicy.isSuper 单出口");
assertFalse(source.toLowerCase().contains("permitall"),
"AC-13:证据读取端点不得 permitAll");
for (String endpoint : List.of("\"/files\"", "\"/content\"", "\"/download\"", "\"/delete\"")) {
assertTrue(source.contains(endpoint), "AC-13:缺端点 " + endpoint);
}
String filesBody = bodies.get(0);
assertTrue(filesBody.contains("uid"),
"AC-13:列表端点必须支持按 uid 收窄(伪造 uid 的归属校验入口)");
assertTrue(filesBody.contains("operator") || filesBody.contains("getId"),
"AC-13:uid 过滤必须与当前操作者归属对撞(防伪造 uid 越权读)");
}
@Test
void 守卫行为三样本_无token坏token401_非超管403_超管放行() throws Exception {
Object controller = newEvidenceController();
AdminAuthSupport auth = authSupportOf(controller);
Method guard = superAdminGuard(controller.getClass());
HttpServletRequest request = mock(HttpServletRequest.class);
when(auth.requireAdmin(any(HttpServletRequest.class)))
.thenThrow(new BusinessException(401, "未登录"));
Throwable noToken = invoke(guard, controller, request);
assertInstanceOf(BusinessException.class, noToken, "AC-13:无 token 必须 401");
assertEquals(Integer.valueOf(401), ((BusinessException) noToken).getCode());
when(auth.requireAdmin(any(HttpServletRequest.class)))
.thenThrow(new BusinessException(401, "登录已过期,请重新登录"));
Throwable badToken = invoke(guard, controller, request);
assertInstanceOf(BusinessException.class, badToken, "AC-13:坏 token 必须 401");
assertEquals(Integer.valueOf(401), ((BusinessException) badToken).getCode());
AdminUserEntity normalAdmin = new AdminUserEntity();
normalAdmin.setId(2L);
normalAdmin.setUsername("ops-admin");
normalAdmin.setRole("admin");
normalAdmin.setIsAdmin(1);
when(auth.requireAdmin(any(HttpServletRequest.class))).thenReturn(normalAdmin);
when(auth.currentRole(normalAdmin)).thenReturn("admin");
Throwable forbidden = invoke(guard, controller, request);
assertInstanceOf(BusinessException.class, forbidden, "AC-13:非超管必须 403");
assertEquals(Integer.valueOf(403), ((BusinessException) forbidden).getCode());
AdminUserEntity superAdmin = new AdminUserEntity();
superAdmin.setId(1L);
superAdmin.setUsername("root");
superAdmin.setRole(DeviceSessionPolicy.SUPER_ADMIN);
superAdmin.setIsAdmin(1);
when(auth.requireAdmin(any(HttpServletRequest.class))).thenReturn(superAdmin);
when(auth.currentRole(superAdmin)).thenReturn(DeviceSessionPolicy.SUPER_ADMIN);
Object returned = guard.invoke(controller, request);
assertSame(superAdmin, returned, "AC-13:超管必须放行并返回操作者");
}
@Test
void 登记必须按taskId关联且对象key带uuid() throws IOException {
String all = evidenceSources();
assertTrue(Pattern.compile("(?i)task[_-]?id").matcher(all).find(),
"AC-7:证据登记/落表必须按 taskId 关联");
assertTrue(all.contains("objectKey") || all.contains("object_key"),
"AC-7:登记必须记录 objectKey");
assertTrue(all.contains("UUID"),
"AC-13(铁律 10):对象 key 必须带 UUID");
assertTrue(all.contains("sha256") || all.contains("SHA-256"),
"AC-7:登记必须带摘要(防篡改/去重)");
}
@Test
void 证据模块日志零命中presign与直链() throws IOException {
List<Path> files = new ArrayList<>();
try (Stream<Path> stream = Files.walk(MAIN_JAVA)) {
stream.filter(path -> path.toString().endsWith(".java"))
.filter(EvidencePhase3ContractTest::isEvidenceSource)
.forEach(files::add);
}
assertFalse(files.isEmpty(), "AC-13:未找到证据模块源码(命名/落点未按契约)");
List<String> leaks = new ArrayList<>();
for (Path file : files) {
for (String line : read(file).split("\\R")) {
String lower = line.toLowerCase();
boolean logLine = lower.contains("log.info") || lower.contains("log.warn")
|| lower.contains("log.error") || lower.contains("log.debug");
boolean urlLike = lower.contains("presign") || lower.contains("uploadurl")
|| lower.contains("fileurl") || lower.contains("http://")
|| lower.contains("https://");
if (logLine && urlLike) {
leaks.add(file.getFileName() + ": " + line.trim());
}
}
}
assertTrue(leaks.isEmpty(), "AC-13:证据模块日志输出了 presign/直链:\n" + String.join("\n", leaks));
}
// ---------------------------------------------------------------- helpers
private static Path evidenceController() throws IOException {
List<Path> hits = new ArrayList<>();
try (Stream<Path> stream = Files.walk(MAIN_JAVA)) {
stream.filter(path -> path.toString().endsWith(".java"))
.filter(path -> {
try {
return read(path).contains(EVIDENCE_PATH);
} catch (IOException ex) {
return false;
}
})
.forEach(hits::add);
}
assertFalse(hits.isEmpty(),
"AC-13:未找到 " + EVIDENCE_PATH + " 的控制器源码(Phase 3 落点未按契约)");
return hits.get(0);
}
private static boolean isEvidenceSource(Path path) {
try {
String source = read(path);
return source.contains("task-evidence") || source.contains("TaskEvidence")
|| source.contains("biz_task_evidence");
} catch (IOException ex) {
return false;
}
}
private static String evidenceSources() throws IOException {
List<Path> files = new ArrayList<>();
try (Stream<Path> stream = Files.walk(MAIN_JAVA)) {
stream.filter(path -> path.toString().endsWith(".java"))
.filter(EvidencePhase3ContractTest::isEvidenceSource)
.forEach(files::add);
}
assertFalse(files.isEmpty(), "AC-7:未找到证据模块源码");
StringBuilder builder = new StringBuilder();
for (Path file : files) {
builder.append(read(file)).append('\n');
}
return builder.toString();
}
private static Object newEvidenceController() throws Exception {
Class<?> controllerClass = Class.forName(CONTROLLER_FQCN);
Constructor<?> constructor = Arrays.stream(controllerClass.getDeclaredConstructors())
.max(Comparator.comparingInt(candidate -> candidate.getParameterCount()))
.orElseThrow();
Class<?>[] types = constructor.getParameterTypes();
Object[] args = new Object[types.length];
for (int index = 0; index < types.length; index++) {
if (types[index].isPrimitive()) {
fail("AC-13:控制器构造器不应有原始类型参数: " + types[index]);
}
args[index] = AdminAuthSupport.class.isAssignableFrom(types[index])
? mock(AdminAuthSupport.class)
: mock(types[index]);
}
constructor.setAccessible(true);
return constructor.newInstance(args);
}
private static AdminAuthSupport authSupportOf(Object controller) throws IllegalAccessException {
for (java.lang.reflect.Field field : controller.getClass().getDeclaredFields()) {
if (AdminAuthSupport.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
return (AdminAuthSupport) field.get(controller);
}
}
fail("AC-13:证据控制器必须注入 AdminAuthSupport(与 AdminDeviceLogController 同口径)");
return null;
}
private static Method superAdminGuard(Class<?> controllerClass) {
for (Method method : controllerClass.getDeclaredMethods()) {
if (method.getName().equals("requireSuperAdmin")
&& method.getParameterCount() == 1
&& method.getParameterTypes()[0].isAssignableFrom(HttpServletRequest.class)) {
method.setAccessible(true);
return method;
}
}
throw new AssertionError(
"AC-13:证据控制器必须含私有 requireSuperAdmin(HttpServletRequest)(对齐 AdminDeviceLogController:167-175)");
}
private static Throwable invoke(Method method, Object target, Object... args) throws Exception {
try {
method.invoke(target, args);
return null;
} catch (InvocationTargetException ex) {
return ex.getTargetException();
}
}
private static List<String> mappedMethodBodies(String source) {
String[] lines = source.split("\\R");
Pattern mapping = Pattern.compile("@(Get|Post|Put|Delete|Patch)Mapping");
List<String> bodies = new ArrayList<>();
for (int index = 0; index < lines.length; index++) {
if (!mapping.matcher(lines[index]).find()) {
continue;
}
int cursor = index;
while (cursor < lines.length && !lines[cursor].contains("{") && !lines[cursor].contains(";")) {
cursor++;
}
if (cursor >= lines.length || lines[cursor].contains(";")) {
continue;
}
int depth = 0;
StringBuilder body = new StringBuilder();
for (int line = cursor; line < lines.length; line++) {
for (char ch : lines[line].toCharArray()) {
if (ch == '{') {
depth++;
} else if (ch == '}') {
depth--;
}
}
body.append(lines[line]).append('\n');
if (depth == 0) {
break;
}
}
bodies.add(body.toString());
index = cursor;
}
return bodies;
}
private static String yamlBlock(String yaml, String key) {
String[] lines = yaml.split("\\R");
Pattern header = Pattern.compile("^(\\s*)" + Pattern.quote(key) + ":\\s*$");
int start = -1;
int indent = -1;
for (int index = 0; index < lines.length; index++) {
Matcher matcher = header.matcher(lines[index]);
if (matcher.matches()) {
start = index;
indent = matcher.group(1).length();
break;
}
}
if (start < 0) {
return null;
}
StringBuilder block = new StringBuilder();
for (int index = start + 1; index < lines.length; index++) {
String line = lines[index];
if (line.trim().isEmpty() || line.trim().startsWith("#")) {
block.append(line).append('\n');
continue;
}
int currentIndent = line.length() - line.replaceAll("^\\s+", "").length();
if (currentIndent <= indent) {
break;
}
block.append(line).append('\n');
}
return block.toString();
}
private static String scalar(String block, String key) {
Matcher matcher = Pattern.compile("(?m)^\\s*" + Pattern.quote(key) + ":\\s*(\\S.*)$")
.matcher(block);
return matcher.find() ? matcher.group(1).trim() : null;
}
private static String defaultValue(String value) {
Matcher env = Pattern.compile("^\\$\\{[^:}]+:([^}]*)\\}$").matcher(value);
if (env.matches()) {
String fallback = env.group(1).trim();
return fallback.isEmpty() ? null : fallback;
}
return value.startsWith("${") ? null : value;
}
private static long versionOf(String fileName) {
Matcher matcher = Pattern.compile("^V(\\d+)__").matcher(fileName);
return matcher.find() ? Long.parseLong(matcher.group(1)) : -1;
}
private static String read(Path path) throws IOException {
return Files.readString(path);
}
}
@@ -0,0 +1,132 @@
package com.nanri.aiimage.modules.file.service;
import com.nanri.aiimage.config.StorageProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.scheduling.annotation.Scheduled;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* AC-10(R5):stale 分片会话必须由周期任务回收,不能等人 init 时顺手清。
*
* <p>Pending Phase 4 激活。今天 {@code ChunkedUploadService.cleanupStaleSessions()} 的
* 唯一调用点在 {@code init()} 里(:86),没有任何 {@code @Scheduled};断掉的上传会永久占盘,
* 而 {@code LocalTempCleanupService} 的射程只有 source/result/transientPayload,
* {@code chunk-uploads} 不在其列。</p>
*
* <p>接缝契约(改名必须同步改本文件并登记蓝图 §5):</p>
* <ul>
* <li>回收入口挂在 {@link ChunkedUploadService} 上,方法名不限,但必须有
* {@link Scheduled} 注解——**摘掉注解即判红**(本文件第一条断言就是它);</li>
* <li>「无人 init 也会清」:本用例先造 stale + fresh 两个会话,回拨 stale 的目录时间后
* <b>不再 init</b>,直接反射调用被注解的周期方法;</li>
* <li>近 N 分钟有片写入的会话不得误清(fresh 会话必须原样保留)。</li>
* </ul>
*
* <p>分片会话只有盘上 meta/part,无 DB 行;证据表的 DB 行保留期判据见
* {@code EvidencePhase3ContractTest}(AC-13 配置块)。</p>
*/
class ChunkedUploadReclaimContractTest {
@TempDir
Path tempDir;
private ChunkedUploadService service;
@BeforeEach
void setUp() {
StorageProperties properties = new StorageProperties();
properties.setLocalTempDir(tempDir.toString());
properties.setCleanupEnabled(true);
service = new ChunkedUploadService(properties);
}
@Test
void 无人init也必须由周期任务回收stale会话() throws Exception {
List<Method> scheduled = scheduledMethods();
assertFalse(scheduled.isEmpty(),
"AC-10:ChunkedUploadService 上必须挂 @Scheduled 回收方法(摘掉注解即判红)");
ChunkedUploadService.ChunkSession stale = service.init("stale.bin", 2, 10, "evidence");
ChunkedUploadService.ChunkSession fresh = service.init("fresh.bin", 2, 10, "evidence");
Path staleDir = sessionDir(stale.uploadId());
Path freshDir = sessionDir(fresh.uploadId());
assertTrue(Files.isDirectory(staleDir));
// 回拨 stale 的目录时间(cleanupStaleSessions 以目录 mtime 判龄),fresh 保持当前时间
Instant old = Instant.now().minus(30, ChronoUnit.HOURS);
Files.setLastModifiedTime(staleDir, FileTime.from(old));
for (Method method : scheduled) {
method.setAccessible(true);
try {
method.invoke(service);
} catch (InvocationTargetException ex) {
throw new AssertionError("AC-10:周期回收方法自身抛异常", ex.getTargetException());
}
}
assertFalse(Files.exists(staleDir),
"AC-10:stale 会话仍在盘上(今天只有 init 时才清,断掉的上传永久占盘)");
assertTrue(Files.isDirectory(freshDir),
"AC-10:近 N 分钟有写入的会话被误清(用户大档会从 0 重传)");
}
@Test
void 统一清理必须覆盖chunkUploads目录() throws Exception {
Path chunkDir = tempDir.resolve("chunk-uploads").resolve("c".repeat(32));
Files.createDirectories(chunkDir);
Path part = chunkDir.resolve("part-0");
Files.writeString(part, "x");
Instant old = Instant.now().minus(10, ChronoUnit.DAYS);
setTimes(part, old);
setTimes(chunkDir, old);
StorageProperties properties = new StorageProperties();
properties.setLocalTempDir(tempDir.toString());
properties.setCleanupEnabled(true);
properties.setSourceRetentionHours(24);
properties.setResultRetentionHours(24);
properties.setTransientPayloadRetentionHours(72);
new LocalTempCleanupService(properties).cleanupLocalTempDir();
assertFalse(Files.exists(chunkDir),
"AC-10:chunk-uploads 残骸未纳入统一清理(LocalTempCleanupService 射程外的洞)");
}
private List<Method> scheduledMethods() {
List<Method> methods = new ArrayList<>();
for (Class<?> type = ChunkedUploadService.class; type != null && type != Object.class;
type = type.getSuperclass()) {
Arrays.stream(type.getDeclaredMethods())
.filter(method -> method.isAnnotationPresent(Scheduled.class))
.forEach(methods::add);
}
return methods;
}
private Path sessionDir(String uploadId) {
return tempDir.resolve("chunk-uploads").resolve(uploadId);
}
private static void setTimes(Path path, Instant instant) throws Exception {
FileTime time = FileTime.from(instant);
Files.getFileAttributeView(path,
java.nio.file.attribute.BasicFileAttributeView.class)
.setTimes(time, time, null);
}
}
@@ -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<JavaApiResponse<{ url?: string; objectKey?: string; originalFilename?: string; mediaType?: string }>>(
`${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 || "请求失败");
}
+100 -22
View File
@@ -35,6 +35,69 @@ function uploadPartUrl(uploadId: string, index: number): string {
return `${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}/part/${index}`
}
/** 兼容真实 axios 响应(body 在 .data)与测试桩(直接返回 body):统一取出 Java 包体。 */
function bodyOf<T>(response: unknown): JavaApiResponse<T> {
const candidate = (response as { data?: unknown } | null | undefined)?.data
if (candidate && typeof candidate === 'object' && 'success' in candidate) {
return candidate as JavaApiResponse<T>
}
return response as JavaApiResponse<T>
}
/** 业务失败默认也是 HTTP 200:只看状态码会把被拒的片当成功。 */
function ensureOk<T>(body: JavaApiResponse<T>): JavaApiResponse<T> {
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,
@@ -68,7 +131,7 @@ async function singleUpload(
},
},
)
return response.data
return bodyOf<UploadFileVo>(response)
}
/**
@@ -81,23 +144,34 @@ async function chunkedUpload(
onProgress?: (percent: number) => void,
): Promise<JavaApiResponse<UploadFileVo>> {
const totalParts = Math.max(1, Math.ceil(file.size / UPLOAD_PART_SIZE))
const init = await http.post<JavaApiResponse<{ upload_id?: string }>>(
`${JAVA_API_PREFIX}/files/chunk/init`,
null,
{ params: { fileName: file.name, totalParts, totalBytes: file.size } },
)
const uploadId = String(init.data?.data?.upload_id ?? '')
if (!uploadId) {
throw new Error('后端未返回分片会话,请重试')
const sessionKey = sessionKeyOf(file, options)
let uploadId = readStoredSession(sessionKey, file)
let done = new Set<number>()
if (uploadId) {
try {
const status = ensureOk(bodyOf<{ uploaded_parts?: unknown[] }>(await http.get(
`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`,
)))
const arrived = status.data?.uploaded_parts
done = new Set<number>(Array.isArray(arrived) ? arrived.map((index) => Number(index)) : [])
} catch {
// 会话已被服务端回收/过期:只能清掉重开,否则这个件每次都在同一个死会话上失败。
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 = await http.get<JavaApiResponse<{ uploaded_parts?: unknown[] }>>(
`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}`,
)
const arrived = status.data?.data?.uploaded_parts
const done = new Set<number>(Array.isArray(arrived) ? arrived.map((index) => Number(index)) : [])
const report = (partialBytes: number) => {
if (!onProgress) return
@@ -116,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
@@ -129,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('分片上传失败')
}
}
@@ -138,13 +214,15 @@ 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<JavaApiResponse<UploadFileVo>>(
const response = await http.post(
`${JAVA_API_PREFIX}/files/chunk/${encodeURIComponent(uploadId)}/complete`,
null,
{ params, timeout: 600_000 },
)
const completed = ensureOk(bodyOf<UploadFileVo>(response))
clearStoredSession(sessionKey)
if (onProgress) onProgress(100)
return response.data
return completed
}
export async function uploadTempFileToJava(
@@ -41,7 +41,13 @@ const BASELINE = {
// ② activate 被服务端拒绝(任务已被兜底拉取/Linux 领走)时 ElMessage.warning 并中止入队,
// 防同一任务执行两次。
// silent 一格没涨:两支都有用户可见出口,只进 blocks。
blocks: 303,
// 307 = 303 + 4(2026-10-31 v1.2 Phase 5.1 分片会话跨刷新续用,全部在 upload.ts):
// ① 读本地会话:JSON.parse/localStorage 抛错时按「无会话」处理;
// ② 写会话:隐私模式/配额满时放弃持久化,本次上传照常;
// ③ 清会话:清不掉只会让下次多问一次 status;
// ④ 已有会话探测失败:清状态重新 init(服务端回收后不重建会永久卡死)。
// 四支都不吞用户可见错误:失败只影响「跨刷新续传」这个可选优化,上传本身仍会抛给调用方。
blocks: 307,
// 2026-10-30(ops Phase 6/可读棘轮):ApiSecretSettingsPanel.vue 1113→775 行,面板里那处
// 静默 catch(余额查询失败只 console.warn、界面留空)随 loadBalance 整块搬进
// useProxyPlan.ts。不是新增静默:面板 1→0、新文件记 1,silent 总数与 blocks 都一格没动,
@@ -63,7 +69,10 @@ const BASELINE = {
// 那句空 catch 换成可见状态 + 首拉重试,静默 2→1(台账跟着改);
// ② 基线此前停在 83,而改动前实扫是 82——上一轮减下去没缩台账,本轮一并拉平到实扫值。
// blocks 一格没动:本轮没有新增 try/catch,只是给既有 catch 补了出口。
silent: 81,
// 82 = 81 + 1 净增(2026-10-31 v1.2 Phase 5.1 分片会话续用):upload.ts 的 localStorage 写/清两处
// 为登记项(perFile 已记 2),另有一处分片状态探测失败改走 catch 后重建会话;同批把一处
// 分片重试 catch 改成无 catch 的直调,净 +1。三处的用户可见结果都不受影响。
silent: 82,
/** 逐文件台账:只钉总数与文件数两个标量的棘轮,会被「A 加一条 + B 减一条」整段绕过。 */
perFile: {
'src/pages/brand/components/BrandCollectDataTab.vue': 4,
@@ -85,6 +94,11 @@ const BASELINE = {
'src/router/index.ts': 3,
'src/shared/api/http.ts': 1,
'src/shared/api/task-progress-polling.ts': 1,
// 2026-10-31 v1.2 Phase 5.1:分片会话跨刷新续用的 localStorage 读/写/清三处兜底。
// 隐私模式/配额满时 setItem/removeItem 会真抛,兜法是「本次上传照常、跨刷新续传降级」——
// 用户可见结果不受影响,也不该为一个可选优化打断上传。读分支自带 return 空串(扫描器不计静默),
// 写/清两支为空块,登记为 2 处;blocks 未涨过 303。
'src/shared/api/upload.ts': 2,
'src/shared/api/types/modules/permission.ts': 1,
'src/shared/auth/account-expiry.ts': 1,
'src/shared/auth/ensure-auth.ts': 1,
@@ -49,7 +49,7 @@ test('test_reexport_upload_helpers', async (t) => {
location: { origin: 'http://localhost' },
}
const calls: { url?: string; method?: string; data?: unknown }[] = []
t.mock.method(http, 'post', ((url: string, data?: unknown, config?: unknown) => {
t.mock.method(http, 'post', ((url: string, data?: unknown) => {
calls.push({ url, method: 'POST', data })
return Promise.resolve({ data: { success: true, message: 'ok', data: { fileKey: 'f1', originalFilename: 'a.xlsx', localPath: '/tmp/a.xlsx', size: 1 } } })
}) as never)
@@ -123,10 +123,11 @@ test('test_image_video_voice', async (t) => {
test('test_image_video_upload_media', async (t) => {
setupWindow()
const calls: { url?: string; method?: string; data?: unknown }[] = []
mockRequest(t, (config) => {
calls.push(config)
// upload.ts 走 axios 实例的 http.post(与 axios 响应体一起交由 bodyOf 解包),这里对撞同一把缝。
t.mock.method(http, 'post', ((url: string, data?: unknown) => {
calls.push({ url, method: 'POST', data })
return okResponse({ url: 'https://oss/x', objectKey: 'k1', originalFilename: 'a.png', mediaType: 'image' })
})
}) as never)
const file = new File(['x'], 'a.png', { type: 'image/png' })
const result = await uploadImageVideoMedia(file)
assert.equal(calls[0].url, '/newApi/api/files/upload')
@@ -0,0 +1,57 @@
// AC-11(R4):用户端大档上传接线的「行为用例」那一半。
//
// Pending Phase 4 激活。9MB 视频(跨 8MB 分片阈值)调 uploadImageVideoMedia,
// 必须出现 POST /files/chunk/init;今天它只打 POST /files/upload(单次 120s)。
//
// 本文件走 vitest(tests/mount/**),用 mock 的 http 出口对撞 URL 序列,不出网。
import { describe, it, expect, vi } from 'vitest'
const mocks = vi.hoisted(() => ({
post: vi.fn(),
get: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
deleteFn: vi.fn(),
}))
vi.mock('@/shared/api/http.ts', () => ({
http: mocks,
post: mocks.post,
get: mocks.get,
put: mocks.put,
del: mocks.deleteFn,
unwrapJavaResponse: (value: unknown) => value,
}))
import { uploadImageVideoMedia } from '@/shared/api/types/modules/image-video.ts'
describe('AC-11 用户端大档上传', () => {
it('9MB 视频必须走分片 init', async () => {
const payload = {
success: true,
data: {
url: 'https://oss.example/evidence.mp4',
objectKey: 'image-video/obj.mp4',
originalFilename: 'big.mp4',
mediaType: 'video',
upload_id: 'a'.repeat(32),
uploaded_parts: [],
},
}
mocks.post.mockResolvedValue(payload)
mocks.get.mockResolvedValue({
success: true,
data: { upload_id: 'a'.repeat(32), uploaded_parts: [] },
})
const file = new File([new Uint8Array(9 * 1024 * 1024)], 'big.mp4', { type: 'video/mp4' })
await uploadImageVideoMedia(file)
const urls = mocks.post.mock.calls.map(([url]) => String(url))
expect(
urls.some((url) => url.includes('/files/chunk/init')),
`AC-11:9MB 视频仍走单次上传,实际出口:${urls.join(', ')}`,
).toBe(true)
})
})
@@ -0,0 +1,44 @@
// AC-11(R4):用户端大档上传接线双口径的「源码尺子」那一半。
//
// Pending Phase 4 激活。今天 `src/shared/api/upload.ts::uploadTempFileToJava` 已实现
// init→part→status→complete 全套分片,但**零生产调用方**(蓝图 §1.2 R4 实读);
// `image-video.ts:215 uploadImageVideoMedia` 仍是一次 multipart+固定 120s。
// 本文件钉两件事:
// 1. uploadTempFileToJava 的调用方数从 0 变 ≥1;
// 2. 图片视频大档路径接上分片(uploadTempFileToJava 或 /files/chunk)。
//
// 跑法:npm test(node --test tests/*.test.ts;本文件在顶层,不进 vitest 的 glob)。
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { dirname, resolve, relative, sep } from 'node:path'
import { fileURLToPath } from 'node:url'
import { listSourceFiles } from './helpers/task-terminal-ast-ruler.ts'
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('/')
test('uploadTempFileToJava 必须有生产调用方(调用方数 ≥1)', () => {
assert.ok(srcFiles.length > 50, `源码扫描面异常:只扫到 ${srcFiles.length} 个文件`)
const callers = srcFiles
.map(rel)
.filter((file) => file !== 'src/shared/api/upload.ts')
.filter((file) => readFileSync(resolve(root, file), 'utf-8').includes('uploadTempFileToJava'))
assert.ok(
callers.length >= 1,
`AC-11:uploadTempFileToJava 仍是零生产调用方(扫过 ${srcFiles.length} 个源码文件)`,
)
})
test('图片视频大档上传必须接分片通道', () => {
const file = 'src/shared/api/types/modules/image-video.ts'
const source = readFileSync(resolve(root, file), 'utf-8')
assert.match(
source,
/uploadTempFileToJava|files\/chunk/,
'AC-11:uploadImageVideoMedia 仍是一次 multipart+固定 120s(大档一断就从 0 重传)',
)
})