feat(phase-4): AC-16/17/18 前端登录态与分片上传、后管包体与竞态门

AC-16(S1-F1/S1-F2)
- frontend-vue ensureAuth:清态前先判 isAuthError——弱网/超时/5xx 不再把人踢回登录页;
  http.ts 的 onResponseRejected 给错误打鉴权标记(HTTP 401 与业务 401/4011 都算),
  纯网络错误保留底层文案供调用方区分"断网"与"掉登录";clearLocalSession 导出复用。
- admin 全局错误兜底:main.ts 装 app.config.errorHandler(首屏就崩也接得住),
  window error / unhandledrejection 由 App.vue setup 调 useGlobalErrorReporting() 接线,
  卸载出口在 onBeforeUnmount 摘监听与提示定时器(后管的 timer-cleanup-pairing 尺子要求
  "注册点所在文件找得回反注册 + 卸载钩子",故接线必须在组件作用域,不在 main.ts)。
  上报体只带 kind/message/stack/route/ts 并过脱敏(Bearer、JWT 三段式、query 里疑似
  token/key/secret 的取值一律打码),凭据零容忍;服务端新增
  /api/admin/client-errors(requireAdmin,字段截断 2000 后 WARN 落日志,不落库)。

AC-17(S1-F3)
- 后端新增分片通道 ChunkedUploadService(init/part/status/complete/abort):8MB/片、
  同片重复上传幂等覆盖、流式拼装复算 sha256、超限与缺片直接拒绝、24h 过期回收;
  FileUploadController 暴露 /api/files/chunk/**,LocalFileStorageService 加
  saveTempFileFromFile(同一套扩展名黑名单与 fileKey 索引);三键显式写进 application.yml
  (不进"只在代码默认值里"的名单)。
- 工作台安装包 93MB:WorkbenchClientPackageService 走同一分片通道转存 client 桶
  (objectKey 写进会话 purpose,拼装时逐字匹配,防借分片接口覆盖已发布包),
  confirm 失败带 cleanupUnconfirmedObject 补偿清理未登记对象;后管 ≤64MB 仍直传预签名 PUT。
- 用户端 upload.ts:大文件分片 + 逐片重试 + 进度回调,超时按体积给(不再固定 120s);
  断点探测失败不再静默按"全缺"重传(catch-silence 棘轮因此一格没涨,blocks 300→301 已记名)。

AC-18(P1-F1/P1-F2/P1-F4/S1-F4)
- 后管 manualChunks 摘掉 element-plus(按需引入已在 src/plugins/element.ts 生效):
  实测构建产物已无 element-plus 独立 chunk,index.html 只 modulepreload vue 块。
- UsersPage/DeviceLogsPage 接 createLatestRequestGate,旧响应不再覆盖新页;
  用户端新增 useServerTablePaging(只驻留当前页 + 自带竞态门 + 失败给原因),
  挂载用例 3 例钉住"翻页重取/旧响应作废/失败可见"。
- DeliveryVideoWorkspace 两处轮询接 document.visibilityState,隐藏标签页暂停。

激活 Phase 0 预置契约 7 例(用户端 5 + 后管 5,共 12 例;其中 AC-16 后管两支按"接线在组件作用域"
的口径改写并把判据加强到反注册配对 + 根组件接线,见蓝图 §5)。
门禁读数:后端 mvn -o test-compile rc=0,CatchSilenceRatchetTest 9 例绿(total 1034→1045、
notRethrow 585→591、logOnly 182→188 逐处记名,EMPTY 未涨)、ChunkedUploadServiceTest 8 例、
WorkbenchClientPackageServiceTest 21 例、ClientErrorReportControllerTest 4 例;后管 1848 例 +
mount 33 例 + vue-tsc + build 全绿;用户端 955 例 + mount 30 例 + vue-tsc + build 全绿。
真机:OssWorkbenchClientLiveVerificationTest 以线上 MinIO 实测 PUT→stat→sha256→presign→删除
全链 1 例绿(凭据只走环境变量,不落任何文件)。

⚠ 按 pathspec 只圈本轮文件:application.yml 用索引手术只提交分片三键那一格,他人
「PENDING 不再按 2 小时判死」的 StaleTaskRepairService/ClientTaskPullSpi/application.yml
注释件与 useLiveTaskBoard/BrandAppearancePatentTab/task-delete-target 四件在飞改动
原样留在工作树未提交。
This commit is contained in:
2026-10-29 13:47:21 +08:00
parent 62a1b50067
commit 0586df2df0
31 changed files with 1798 additions and 64 deletions
@@ -0,0 +1,60 @@
package com.nanri.aiimage.modules.clienterror.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.security.AdminAuthSupport;
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;
/**
* 管理端浏览器错误上报(S1-F2/AC-16)。
*
* <p>此前后管页面渲染崩溃/未捕获 Promise 只进浏览器 console,线上白屏永远没有现场。
* 本端点把上报落进服务端日志(WARN,带 uid/route 前缀可按 requestId grep)。
* 只记纯文本且逐字段截断,不落库、不进队列——这是一份"看得见"的证据链,不是新数据集。</p>
*
* <p>鉴权:走 {@code /api/admin/**} 的管理员会话(sendBeacon 带同源 cookie)。
* 报文体里禁止出现令牌等凭据(上报端已做脱敏,这里再卡一道长度上限)。</p>
*/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/admin/client-errors")
@Tag(name = "管理端错误上报", description = "后管页面全局错误兜底的上报出口:落服务端日志,不落库")
public class ClientErrorReportController {
private static final int MAX_FIELD_CHARS = 2000;
private final AdminAuthSupport adminAuthSupport;
@PostMapping
@Operation(summary = "上报一条浏览器错误", description = "字段:kind/message/stack/route/ts;全部截断后以 WARN 落服务端日志")
public ApiResponse<Map<String, Object>> report(
HttpServletRequest request,
@RequestBody(required = false) Map<String, Object> payload) {
AdminUserEntity operator = adminAuthSupport.requireAdmin(request);
Map<String, Object> body = payload == null ? Map.of() : payload;
log.warn("[client-error] uid={} kind={} route={} ts={} message={} stack={}",
operator.getId(),
clip(body.get("kind")),
clip(body.get("route")),
body.get("ts"),
clip(body.get("message")),
clip(body.get("stack")));
return ApiResponse.success(Map.of("recorded", true));
}
static String clip(Object value) {
String text = value == null ? "" : String.valueOf(value);
return text.length() <= MAX_FIELD_CHARS ? text : text.substring(0, MAX_FIELD_CHARS) + "…";
}
}
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.file.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.modules.file.model.vo.ExcelInfoVo;
import com.nanri.aiimage.modules.file.model.vo.UploadFileVo;
import com.nanri.aiimage.modules.file.service.ChunkedUploadService;
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import io.swagger.v3.oas.annotations.Operation;
@@ -13,14 +14,20 @@ import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequiredArgsConstructor
@@ -30,6 +37,7 @@ public class FileUploadController {
private final LocalFileStorageService localFileStorageService;
private final OssStorageService ossStorageService;
private final ChunkedUploadService chunkedUploadService;
@PostMapping("/upload")
@Operation(
@@ -72,6 +80,92 @@ public class FileUploadController {
return ApiResponse.success(vo);
}
@PostMapping("/chunk/init")
@Operation(
summary = "初始化分片上传",
description = """
大文件分片上传的第一步:登记文件大小与分片数,返回 uploadId。
分片逐个 POST 到 /chunk/{uploadId}/part/{index},全部到齐后调
/chunk/{uploadId}/complete 拼装为普通临时文件(同一套 fileKey 语义)。
每片可独立重试,前端也可先查 /chunk/{uploadId} 拿到已到片号续传。
""")
public ApiResponse<Map<String, Object>> initChunkUpload(
@RequestParam String fileName,
@RequestParam int totalParts,
@RequestParam long totalBytes) {
ChunkedUploadService.ChunkSession session =
chunkedUploadService.init(fileName, totalParts, totalBytes, "");
Map<String, Object> result = new LinkedHashMap<>();
result.put("upload_id", session.uploadId());
result.put("total_parts", session.totalParts());
return ApiResponse.success(result);
}
@PostMapping("/chunk/{uploadId}/part/{index}")
@Operation(summary = "上传一个分片", description = "同一片重复上传按覆盖处理(重试/续传幂等)。")
public ApiResponse<Map<String, Object>> uploadChunk(
@PathVariable String uploadId,
@PathVariable int index,
@RequestPart("file") MultipartFile file) throws Exception {
int received = chunkedUploadService.savePart(uploadId, index, file);
Map<String, Object> result = new LinkedHashMap<>();
result.put("upload_id", uploadId);
result.put("received_parts", received);
return ApiResponse.success(result);
}
@GetMapping("/chunk/{uploadId}")
@Operation(summary = "查询已上传分片", description = "返回已到齐的分片序号,供断点续传跳过已传片。")
public ApiResponse<Map<String, Object>> chunkStatus(@PathVariable String uploadId) {
List<Integer> parts = chunkedUploadService.uploadedParts(uploadId);
Map<String, Object> result = new LinkedHashMap<>();
result.put("upload_id", uploadId);
result.put("uploaded_parts", parts);
return ApiResponse.success(result);
}
@PostMapping("/chunk/{uploadId}/complete")
@Operation(
summary = "拼装分片并落盘",
description = """
全部分片到齐后流式拼装并复算 sha256,落盘为普通临时文件(返回 fileKey)。
uploadToOss=true 时与 /upload 同一出口:拼装后转存公开 OSS。
缺少任一分片直接拒绝,不会产出半个文件。
""")
public ApiResponse<UploadFileVo> completeChunkUpload(
@PathVariable String uploadId,
@RequestParam(required = false) String relativePath,
@RequestParam(defaultValue = "false") boolean uploadToOss,
@RequestParam(defaultValue = "COMMON") String moduleType) throws Exception {
ChunkedUploadService.CompletedUpload completed = chunkedUploadService.complete(uploadId);
try {
UploadFileVo vo = localFileStorageService.saveTempFileFromFile(
completed.file().toFile(), completed.fileName(), relativePath, completed.size());
if (uploadToOss) {
File localFile = new File(vo.getLocalPath());
OssStorageService.UploadedResult uploaded =
ossStorageService.uploadPublicFileWithFreshDownloadUrl(
localFile, moduleType, vo.getOriginalFilename());
vo.setObjectKey(uploaded.objectKey());
vo.setUrl(uploaded.downloadUrl());
vo.setMediaType(resolveMediaType(null, vo.getOriginalFilename()));
}
return ApiResponse.success(vo);
} finally {
completed.file().toFile().delete();
}
}
@DeleteMapping("/chunk/{uploadId}")
@Operation(summary = "放弃分片上传", description = "删除未完成的会话与已落盘分片。")
public ApiResponse<Map<String, Object>> abortChunkUpload(@PathVariable String uploadId) {
chunkedUploadService.abort(uploadId);
Map<String, Object> result = new LinkedHashMap<>();
result.put("upload_id", uploadId);
result.put("aborted", true);
return ApiResponse.success(result);
}
@GetMapping("/excel-info")
@Operation(summary = "读取 Excel 信息", description = "根据 fileKey 读取 Excel 第一行表头及总行数,供前端选择保留列和展示总条数。")
@ApiResponses({
@@ -0,0 +1,342 @@
package com.nanri.aiimage.modules.file.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.config.StorageProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HexFormat;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Stream;
/**
* 临时文件分片上传(S1-F3/AC-17):把大文件拆成固定大小的分片逐个落盘,全部到齐后
* 在服务端流式拼装并复算 sha256。
*
* <p>存在的理由:单次 multipart/单次预签名 PUT 在弱网下一断就从 0 重传,93MB 级安装包
* 与视频几乎没有一次成功的运气。分片后每一片都可独立重试,前端还能查询已到片号续传。</p>
*
* <p>落盘位置在 {@code local-temp-dir/chunk-uploads/},不进入业务结果目录;超过
* {@code stale-hours} 未完成的分片会话在下次 init 时顺手清理。uploadId 只接受 32 位
* 十六进制(自己生成、自己校验),分片序号只接受整数,用户文件名只进 meta 展示,
* 不参与任何路径拼接。</p>
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class ChunkedUploadService {
private static final Pattern UPLOAD_ID_PATTERN = Pattern.compile("[0-9a-f]{32}");
private static final ObjectMapper JSON = new ObjectMapper();
private static final String META_NAME = "meta.json";
/** 单个分片上限:比前端 16MB 分片留足余量,防一次请求把临时盘打满。 */
private static final long MAX_PART_BYTES = 64L * 1024 * 1024;
private final StorageProperties storageProperties;
/** 同一会话的落片/拼装/放弃串行化;会话结束即回收,避免长期驻留。 */
private final ConcurrentHashMap<String, Object> sessionLocks = new ConcurrentHashMap<>();
@Value("${aiimage.file.chunk-upload.max-total-bytes:2147483648}")
private long maxTotalBytes = 2L * 1024 * 1024 * 1024;
@Value("${aiimage.file.chunk-upload.max-parts:4096}")
private int maxParts = 4096;
@Value("${aiimage.file.chunk-upload.stale-hours:24}")
private long staleHours = 24;
/** 分片会话句柄:uploadId 与总片数。 */
public record ChunkSession(String uploadId, int totalParts) {
}
/** 拼装完成的临时文件;调用方负责删除。 */
public record CompletedUpload(Path file, String fileName, long size, String sha256) {
}
public ChunkSession init(String fileName, int totalParts, long totalBytes, String purpose) {
String safeName = normalizeFileName(fileName);
if (totalParts < 1 || totalParts > maxParts) {
throw new BusinessException("分片数量非法(1-" + maxParts + ")");
}
if (totalBytes < 1 || totalBytes > maxTotalBytes) {
throw new BusinessException("文件大小超出上限(最大 " + maxTotalBytes + " 字节)");
}
cleanupStaleSessions();
String uploadId = UUID.randomUUID().toString().replace("-", "");
Path dir = sessionDir(uploadId);
try {
Files.createDirectories(dir);
ObjectNode meta = JSON.createObjectNode();
meta.put("fileName", safeName);
meta.put("totalParts", totalParts);
meta.put("totalBytes", totalBytes);
meta.put("purpose", purpose == null ? "" : purpose);
meta.put("createdAt", Instant.now().toEpochMilli());
JSON.writeValue(metaFile(uploadId).toFile(), meta);
} catch (IOException ex) {
deleteQuietly(dir);
throw new BusinessException("初始化分片上传失败:" + ex.getMessage());
}
log.info("[chunk-upload] 初始化分片会话 uploadId={} fileName={} parts={} bytes={}",
uploadId, safeName, totalParts, totalBytes);
return new ChunkSession(uploadId, totalParts);
}
/**
* 落一片。同一片重复上传按覆盖处理(幂等):前端重试/续传时不必先查状态。
* 返回当前已到齐的片数,供前端做进度展示。
*/
public int savePart(String uploadId, int index, MultipartFile part) throws IOException {
Meta meta = readMeta(uploadId);
if (index < 0 || index >= meta.totalParts) {
throw new BusinessException("分片序号越界:" + index);
}
if (part == null || part.isEmpty()) {
throw new BusinessException("分片内容为空");
}
if (part.getSize() > MAX_PART_BYTES) {
throw new BusinessException("单个分片超过上限:" + part.getSize());
}
synchronized (lockOf(uploadId)) {
Path dir = sessionDir(uploadId);
Path target = dir.resolve("part-" + index);
Path tmp = dir.resolve("part-" + index + ".tmp");
try (InputStream in = part.getInputStream()) {
Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING);
}
Files.move(tmp, target, StandardCopyOption.REPLACE_EXISTING);
long total = currentTotalBytes(uploadId, meta.totalParts);
if (total > maxTotalBytes) {
deleteQuietly(target);
throw new BusinessException("分片累计超过文件上限(最大 " + maxTotalBytes + " 字节)");
}
if (total > meta.totalBytes) {
deleteQuietly(target);
throw new BusinessException("分片累计超过声明的文件大小:" + meta.totalBytes);
}
return (int) uploadedPartIndexes(uploadId, meta.totalParts).size();
}
}
public List<Integer> uploadedParts(String uploadId) {
Meta meta = readMeta(uploadId);
return uploadedPartIndexes(uploadId, meta.totalParts);
}
/**
* 全部片到齐后流式拼装并复算 sha256;拼装完成的文件落在 chunk-uploads 根下,
* 会话目录随之清掉。缺少分片直接拒绝,绝不拼出半个文件。
*/
public CompletedUpload complete(String uploadId) throws IOException {
Meta meta = readMeta(uploadId);
synchronized (lockOf(uploadId)) {
List<Integer> present = uploadedPartIndexes(uploadId, meta.totalParts);
if (present.size() != meta.totalParts) {
List<Integer> missing = new ArrayList<>();
for (int i = 0; i < meta.totalParts; i++) {
if (!present.contains(i)) {
missing.add(i);
if (missing.size() >= 20) {
break;
}
}
}
throw new BusinessException("分片不完整,缺少 " + missing);
}
Path assembled = chunkRoot().resolve("assembled-" + uploadId + ".bin");
MessageDigest digest = sha256Digest();
long total = 0;
try (OutputStream out = Files.newOutputStream(assembled)) {
byte[] buffer = new byte[1 << 20];
for (int i = 0; i < meta.totalParts; i++) {
Path part = sessionDir(uploadId).resolve("part-" + i);
try (InputStream in = Files.newInputStream(part)) {
int read;
while ((read = in.read(buffer)) > 0) {
out.write(buffer, 0, read);
digest.update(buffer, 0, read);
total += read;
}
}
}
} catch (IOException ex) {
Files.deleteIfExists(assembled);
throw ex;
}
if (total != meta.totalBytes) {
Files.deleteIfExists(assembled);
throw new BusinessException(
"拼装后大小与声明不一致:声明 " + meta.totalBytes + ",实际 " + total);
}
deleteQuietly(sessionDir(uploadId));
sessionLocks.remove(uploadId);
String sha256 = HexFormat.of().formatHex(digest.digest());
log.info("[chunk-upload] 拼装完成 uploadId={} fileName={} bytes={} sha256={}",
uploadId, meta.fileName, total, sha256);
return new CompletedUpload(assembled, meta.fileName, total, sha256);
}
}
/** 放弃本次分片会话(前端取消/确认失败清理)。 */
public void abort(String uploadId) {
Meta meta = readMeta(uploadId);
synchronized (lockOf(uploadId)) {
deleteQuietly(sessionDir(uploadId));
}
sessionLocks.remove(uploadId);
log.info("[chunk-upload] 已放弃分片会话 uploadId={} fileName={}", uploadId, meta.fileName);
}
/** 分片会话随附的业务标识(工作台包用它钉住 objectKey,防拼装时换 key)。 */
public String purposeOf(String uploadId) {
return readMeta(uploadId).purpose;
}
private Meta readMeta(String uploadId) {
if (uploadId == null || !UPLOAD_ID_PATTERN.matcher(uploadId).matches()) {
throw new BusinessException("分片会话不存在或已过期");
}
Path metaFile = metaFile(uploadId);
if (!Files.isRegularFile(metaFile)) {
throw new BusinessException("分片会话不存在或已过期");
}
try {
JsonNode node = JSON.readTree(metaFile.toFile());
return new Meta(node.path("fileName").asText(""),
node.path("totalParts").asInt(0),
node.path("totalBytes").asLong(0),
node.path("purpose").asText(""));
} catch (IOException ex) {
throw new BusinessException("分片会话元数据损坏,请重新上传");
}
}
private List<Integer> uploadedPartIndexes(String uploadId, int totalParts) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < totalParts; i++) {
if (Files.isRegularFile(sessionDir(uploadId).resolve("part-" + i))) {
result.add(i);
}
}
return result;
}
private long currentTotalBytes(String uploadId, int totalParts) throws IOException {
long total = 0;
for (int i = 0; i < totalParts; i++) {
Path part = sessionDir(uploadId).resolve("part-" + i);
if (Files.isRegularFile(part)) {
total += Files.size(part);
}
}
return total;
}
private Path chunkRoot() {
return Path.of(storageProperties.getLocalTempDir()).resolve("chunk-uploads").normalize();
}
private Path sessionDir(String uploadId) {
if (uploadId == null || !UPLOAD_ID_PATTERN.matcher(uploadId).matches()) {
throw new BusinessException("分片会话不存在或已过期");
}
return chunkRoot().resolve(uploadId);
}
private Path metaFile(String uploadId) {
return sessionDir(uploadId).resolve(META_NAME);
}
private Object lockOf(String uploadId) {
return sessionLocks.computeIfAbsent(uploadId, key -> new Object());
}
private void cleanupStaleSessions() {
Path root = chunkRoot();
if (!Files.isDirectory(root)) {
return;
}
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());
}
} catch (IOException ex) {
log.warn("[chunk-upload] 条目时间戳读不出,本轮跳过该条 path={}", child.getFileName());
}
});
} catch (IOException ex) {
log.warn("[chunk-upload] 扫描过期分片会话失败: {}", ex.getMessage());
}
}
private String normalizeFileName(String fileName) {
String name = fileName == null ? "" : fileName.trim();
if (name.isEmpty()) {
throw new BusinessException("文件名不能为空");
}
if (name.length() > 255) {
name = name.substring(name.length() - 255);
}
return name.replace('\\', '/').replaceAll(".*/", "");
}
private MessageDigest sha256Digest() {
try {
return MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException("SHA-256 not available", ex);
}
}
private void deleteQuietly(Path path) {
if (path == null) {
return;
}
try {
if (Files.isDirectory(path)) {
try (Stream<Path> walk = Files.walk(path)) {
walk.sorted((a, b) -> b.getNameCount() - a.getNameCount())
.forEach(p -> {
try {
Files.deleteIfExists(p);
} catch (IOException ex) {
log.warn("[chunk-upload] 残留片删除失败,留给下一轮清理 path={}", p);
}
});
}
} else {
Files.deleteIfExists(path);
}
} catch (IOException ex) {
log.warn("[chunk-upload] 清理未竟,残留交给过期回收 path={}", path);
}
}
private record Meta(String fileName, int totalParts, long totalBytes, String purpose) {
}
}
@@ -78,6 +78,35 @@ public class LocalFileStorageService {
return vo;
}
/**
* 分片拼装后的落盘(AC-17):与 {@link #saveTempFile} 同一套扩展名闸门与 fileKey 索引,
* 只是源从 MultipartFile 换成服务端已拼好的临时文件。
*/
public UploadFileVo saveTempFileFromFile(File source, String originalFilename,
String relativePath, long declaredSize) throws IOException {
File tempDir = FileUtil.file(storageProperties.getLocalTempDir());
File parentDir = tempDir.getParentFile();
if (parentDir != null) {
FileUtil.mkdir(parentDir);
}
FileUtil.mkdir(tempDir);
String fileKey = IdUtil.fastSimpleUUID();
String extName = FileUtil.extName(originalFilename);
rejectBlockedExtension(originalFilename, extName);
String filename = fileKey + (extName.isEmpty() ? "" : "." + extName);
File target = FileUtil.file(tempDir, filename);
FileUtil.move(source, target, true);
registerSourceFileIndex(fileKey, filename);
UploadFileVo vo = new UploadFileVo();
vo.setFileKey(fileKey);
vo.setOriginalFilename(originalFilename);
vo.setLocalPath(target.getAbsolutePath());
vo.setSize(target.length() > 0 ? target.length() : Math.max(0, declaredSize));
vo.setRelativePath(relativePath);
return vo;
}
/** 扩展名黑名单校验:命中即拒绝落盘(大小写不敏感)。 */
private void rejectBlockedExtension(String originalFilename, String extName) {
if (extName == null || extName.isBlank()) {
@@ -322,6 +322,17 @@ public class OssStorageService {
return presignPublicClientUpload(objectKey, WORKBENCH_CLIENT_PREFIX, "workbench client", expirySeconds);
}
/**
* 服务端直传工作台客户端包(AC-17 分片通道的落点):浏览器分片 → 后端拼装 → 这里
* 上传到公开 client 桶的 {@code workbench-client/versions/} 前缀。
* 与直传预签名写的是同一个桶/前缀,confirm 阶段的实物复算 sha256 与凭据扫描照旧生效。
*/
public String uploadWorkbenchClientObject(File file, String objectKey) {
requireWorkbenchClientKey(objectKey);
uploadFile(file, publicClientBucket(), objectKey);
return objectKey;
}
/** 返回工作台客户端包字节数;对象不存在返回 -1(直传确认时先卡大小,再算摘要)。 */
public long workbenchClientObjectSize(String objectKey) {
return publicClientObjectSize(objectKey, WORKBENCH_CLIENT_PREFIX, "workbench client");
@@ -10,7 +10,9 @@ 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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -69,6 +71,61 @@ public class WorkbenchClientPackageAdminController {
packageService.confirmPackage(objectKey, fileName, version)));
}
@PostMapping("/package/chunk/init")
@Operation(summary = "初始化客户端包分片上传", description = "大包走分片:返回 uploadId 与 objectKey;分片上传到 /api/files/chunk/{uploadId}/part/{index}")
public ApiResponse<Map<String, Object>> initChunkedPackage(
HttpServletRequest request,
@Parameter(description = "上传的 zip 文件名") @RequestParam(value = "file_name") String fileName,
@Parameter(description = "客户端版本号,如 1.0.0") @RequestParam("version") String version,
@Parameter(description = "总分片数") @RequestParam("total_parts") int totalParts,
@Parameter(description = "文件总字节数") @RequestParam("total_bytes") long totalBytes) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
log.info("[workbench-client] 管理端初始化分片上传 operator={} fileName={} version={} parts={}",
operator.getUsername(), fileName, version, totalParts);
return ApiResponse.success(packageService.initChunkedPackage(
fileName, version, totalParts, totalBytes));
}
@PostMapping("/package/chunk/complete")
@Operation(summary = "拼装客户端包分片并转存", description = "全部分片到齐后服务端拼装、上传 client 桶;随后照旧调用 /package/confirm 复算摘要并落库")
public ApiResponse<Map<String, Object>> completeChunkedPackage(
HttpServletRequest request,
@RequestParam("upload_id") String uploadId,
@Parameter(description = "分片初始化返回的对象 key") @RequestParam("object_key") String objectKey,
@Parameter(description = "上传的 zip 文件名") @RequestParam("file_name") String fileName,
@Parameter(description = "客户端版本号") @RequestParam("version") String version) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
log.info("[workbench-client] 管理端拼装分片上传 operator={} uploadId={} objectKey={}",
operator.getUsername(), uploadId, objectKey);
return ApiResponse.success(packageService.completeChunkedPackage(
uploadId, objectKey, fileName, version));
}
@DeleteMapping("/package/chunk/{uploadId}")
@Operation(summary = "放弃客户端包分片上传", description = "删除分片会话;confirm 失败时带 object_key 一并清理未登记对象")
public ApiResponse<Map<String, Object>> abortChunkedPackage(
HttpServletRequest request,
@PathVariable("uploadId") String uploadId,
@Parameter(description = "已转存但未登记的对象 key(可空)") @RequestParam(value = "object_key", required = false) String objectKey) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
log.info("[workbench-client] 管理端放弃分片上传 operator={} uploadId={} objectKey={}",
operator.getUsername(), uploadId, objectKey);
packageService.abortChunkedPackage(uploadId, objectKey);
return ApiResponse.success(Map.of("aborted", true));
}
@DeleteMapping("/package/object")
@Operation(summary = "清理已转存未登记的包对象", description = "confirm 失败的补偿出口;对象已登记时拒绝清理并报错,防误删线上更新目标")
public ApiResponse<Map<String, Object>> cleanupUnconfirmedObject(
HttpServletRequest request,
@Parameter(description = "要清理的对象 key") @RequestParam("object_key") String objectKey) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
log.info("[workbench-client] 管理端清理未登记包对象 operator={} objectKey={}",
operator.getUsername(), objectKey);
packageService.cleanupUnconfirmedObject(objectKey);
return ApiResponse.success(Map.of("removed", true));
}
@PostMapping("/package/delete")
@Operation(summary = "批量删除客户端包", description = "按 id 删除记录;关联 MinIO 包体仅当无其它记录引用时清除")
public ApiResponse<Map<String, Object>> deletePackages(
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.workbenchclient.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.file.service.ChunkedUploadService;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.modules.workbenchclient.mapper.WorkbenchClientPackageMapper;
import com.nanri.aiimage.modules.workbenchclient.model.entity.WorkbenchClientPackageEntity;
@@ -97,6 +98,7 @@ public class WorkbenchClientPackageService {
private final WorkbenchClientPackageMapper packageMapper;
private final OssStorageService ossStorageService;
private final ChunkedUploadService chunkedUploadService;
/** 管理端列表:按上传时间倒序,第一条即客户端当前更新目标。 */
public List<Map<String, Object>> listPackages() {
@@ -170,10 +172,7 @@ public class WorkbenchClientPackageService {
public Map<String, Object> presignPackage(String fileName, String version) {
String normalizedName = requireZipFileName(fileName);
String normalizedVersion = requireVersion(version);
String objectKey = OBJECT_KEY_PREFIX
+ OBJECT_NAME_FORMATTER.format(LocalDateTime.now())
+ "-" + UUID.randomUUID().toString().replace("-", "")
+ "-" + safeVersionKey(normalizedVersion) + ".zip";
String objectKey = newObjectKey(normalizedVersion);
int expirySeconds = effectivePresignExpirySeconds();
log.info("[workbench-client] 签发客户端包直传预签名 version={} fileName={} objectKey={} expirySeconds={}",
normalizedVersion, normalizedName, objectKey, expirySeconds);
@@ -187,6 +186,112 @@ public class WorkbenchClientPackageService {
return result;
}
/**
* 分片上传第一步(AC-17):与直传同一个对象 key 规则,key 写进分片会话的 purpose,
* 拼装时必须逐字匹配——否则管理员可借分片接口覆盖任意已发布包。
*/
public Map<String, Object> initChunkedPackage(String fileName, String version,
int totalParts, long totalBytes) {
String normalizedName = requireZipFileName(fileName);
String normalizedVersion = requireVersion(version);
if (totalBytes <= 0 || totalBytes > MAX_UPLOAD_BYTES) {
throw new BusinessException("文件超过允许的大小限制");
}
String objectKey = newObjectKey(normalizedVersion);
ChunkedUploadService.ChunkSession session =
chunkedUploadService.init(normalizedName, totalParts, totalBytes, objectKey);
log.info("[workbench-client] 初始化分片上传 version={} fileName={} objectKey={} parts={} bytes={}",
normalizedVersion, normalizedName, objectKey, totalParts, totalBytes);
Map<String, Object> result = new LinkedHashMap<>();
result.put("upload_id", session.uploadId());
result.put("object_key", objectKey);
result.put("file_name", normalizedName);
result.put("version", normalizedVersion);
result.put("total_parts", session.totalParts());
return result;
}
/**
* 分片拼装完成:拼好的 zip 由服务端上传到公开 client 桶,随后前端照旧调用
* /package/confirm —— 摘要复算、凭据扫描、版本唯一性全部沿用直传那条链。
*/
public Map<String, Object> completeChunkedPackage(String uploadId, String objectKey,
String fileName, String version) {
String normalizedName = requireZipFileName(fileName);
String normalizedVersion = requireVersion(version);
if (objectKey == null || !objectKey.startsWith(OBJECT_KEY_PREFIX)) {
throw new BusinessException("非法的对象路径,请重新上传");
}
if (!objectKey.endsWith("-" + safeVersionKey(normalizedVersion) + ".zip")) {
throw new BusinessException("版本号与上传对象不匹配,请重新上传");
}
if (!objectKey.equals(chunkedUploadService.purposeOf(uploadId))) {
throw new BusinessException("上传会话与对象不匹配,请重新上传");
}
ChunkedUploadService.CompletedUpload completed;
try {
completed = chunkedUploadService.complete(uploadId);
} catch (IOException ex) {
throw new BusinessException("拼装安装包失败:" + ex.getMessage());
}
try {
ossStorageService.uploadWorkbenchClientObject(completed.file().toFile(), objectKey);
} finally {
completed.file().toFile().delete();
}
log.info("[workbench-client] 分片拼装并转存完成 version={} objectKey={} bytes={} sha256={}",
normalizedVersion, objectKey, completed.size(), completed.sha256());
Map<String, Object> result = new LinkedHashMap<>();
result.put("object_key", objectKey);
result.put("file_url", ossStorageService.getWorkbenchClientDownloadUrl(objectKey));
result.put("file_name", normalizedName);
result.put("version", normalizedVersion);
result.put("sha256", completed.sha256());
return result;
}
/**
* 清理已上传但未登记的包对象(confirm 失败后的补偿):数据库已有同 key 记录时拒绝,
* 避免管理员误删线上正在使用的安装包。
*/
public void cleanupUnconfirmedObject(String objectKey) {
if (objectKey == null || !objectKey.startsWith(OBJECT_KEY_PREFIX)) {
throw new BusinessException("非法的对象路径");
}
if (findByObjectKey(objectKey) != null) {
throw new BusinessException("该对象已登记,不能按未确认清理");
}
ossStorageService.removeWorkbenchClientObject(objectKey);
log.info("[workbench-client] 已清理未登记的包对象 objectKey={}", objectKey);
}
/**
* 放弃分片上传:删分片会话;若已拼装并转存过对象(confirm 失败场景),一并清理未登记对象。
* 会话不存在/对象已登记都不算失败——补偿清理要尽力而为,不能反过来挡住重传。
*/
public void abortChunkedPackage(String uploadId, String objectKey) {
try {
chunkedUploadService.abort(uploadId);
} catch (BusinessException ex) {
log.info("[workbench-client] 分片会话已不存在(可能已完成) uploadId={}", uploadId);
}
if (objectKey != null && objectKey.startsWith(OBJECT_KEY_PREFIX)) {
try {
cleanupUnconfirmedObject(objectKey);
} catch (BusinessException ex) {
log.warn("[workbench-client] 清理未登记对象失败 objectKey={} reason={}",
objectKey, ex.getMessage());
}
}
}
private String newObjectKey(String normalizedVersion) {
return OBJECT_KEY_PREFIX
+ OBJECT_NAME_FORMATTER.format(LocalDateTime.now())
+ "-" + UUID.randomUUID().toString().replace("-", "")
+ "-" + safeVersionKey(normalizedVersion) + ".zip";
}
/**
* 直传确认:校验对象存在、大小上限、版本号与对象名一致,服务端复算 sha256 后落库。
* <p>sha256 为空或长度不对直接拒绝——宁可让管理员重传,也不让客户端拿到没有锚点的包。
@@ -212,6 +212,16 @@ aiimage:
source-retention-hours: ${AIIMAGE_STORAGE_SOURCE_RETENTION_HOURS:24}
result-retention-hours: ${AIIMAGE_STORAGE_RESULT_RETENTION_HOURS:24}
transient-payload-retention-hours: ${AIIMAGE_STORAGE_TRANSIENT_PAYLOAD_RETENTION_HOURS:72}
# 大文件分片上传通道(AC-17/S1-F3):浏览器分片 → 服务端拼装 → 复算 sha256 后按普通临时件落库。
# 这三键是闸门值而不是调优参数:放宽等于同时放大临时盘驻留与拼装阶段的耗时上限。
file:
chunk-upload:
# 单个会话允许的总字节(默认 2GiB):init 与逐片累计都要过这道闸,超限直接拒绝
max-total-bytes: ${AIIMAGE_FILE_CHUNK_UPLOAD_MAX_TOTAL_BYTES:2147483648}
# 允许的分片数上限(默认 4096):与前端 8MB/片相乘即体积的第二道闸
max-parts: ${AIIMAGE_FILE_CHUNK_UPLOAD_MAX_PARTS:4096}
# 未完成会话与残留片的驻留小时数:超时由清理动作回收,避免断掉的上传永久占盘
stale-hours: ${AIIMAGE_FILE_CHUNK_UPLOAD_STALE_HOURS:24}
temp-dir:
retention-hours: ${AIIMAGE_TEMP_DIR_RETENTION_HOURS:24}
capacity-plan:
@@ -57,7 +57,9 @@ class CatchSilenceRatchetTest {
// 10-28 V179 采集被过滤行追溯抬 579 -> 582(审计件三枚:逐批插入容错/回流簿记容错/尾 sheet 降级,
// 逐处记名见 STRICT_TOTAL 上方注释)。
// 10-28 Phase 3 抬 582 -> 585(心跳降级一枚 + 优雅停机两枚中断恢复,逐处记名见 STRICT_TOTAL 上方注释)。
static final int GIVEN_NOT_RETHROW = 585;
// 10-29 Phase 4D 抬 585 -> 591(AC-17 分片通道六枚 LOG_ONLY:清理/收尾失败不得盖掉主错误,
// 逐处记名见 STRICT_TOTAL 上方注释;EMPTY 一格没涨)。
static final int GIVEN_NOT_RETHROW = 591;
static final int GIVEN_LOG_ONLY = 249;
static final int GIVEN_EMPTY = 56;
@@ -211,8 +213,29 @@ class CatchSilenceRatchetTest {
* <li>{@code TaskFileJobConfig.videoQueueExecutor} 一枚:信号量 acquire 被中断只恢复中断标记
* (S1-B2 视频独立许可,照抄 taskQueueExecutor 的既有形状)。</li>
* </ul>
* <p>10-29 Phase 4D(AC-17/S1-F3 大文件分片通道)抬 total 1034 -&gt; 1045,11 枚逐处记名。
* 这一批是"新增通道"而不是"给既有出口补兜":整条 {@code ChunkedUploadService} 与
* {@code WorkbenchClientPackageService} 的分片方法都是新落点,Java 的受检 {@code IOException}
* 使每一段磁盘动作都必须有自己的出口,形状与既有件同口径:
* <ul>
* <li>RETHROW 五枚——{@code ChunkedUploadService.init}(建会话目录失败:先 {@code deleteQuietly}
* 再抛 {@code BusinessException},不留半个会话)、{@code complete}(拼装中途失败:删掉半成品
* 后原样 {@code throw ex},绝不产出半个文件)、{@code readMeta}(元数据损坏直接抛,
* 让调用方重新 init 而不是按坏元数据继续拼)、{@code sha256Digest}(JVM 无 SHA-256 属环境级
* 不可能,抛 {@code IllegalStateException} 带 cause)、{@code WorkbenchClientPackageService
* .completeChunkedPackage}(拼装 IOException 翻成业务文案,500 变可读错误)。</li>
* <li>LOG_ONLY 六枚,块体各只有一句带锚点的 log,误差方向都是"清理/收尾失败不得盖掉主错误"——
* {@code ChunkedUploadService.cleanupStaleSessions} 内层(单条目时间戳读不出,本轮跳过该条)
* 与外层({@code Files.list} 失败,整轮清理放弃但不拦新会话)、
* {@code deleteQuietly} 的逐片删除与整体遍历两枚(残留交给过期回收),
* {@code WorkbenchClientPackageService.abortChunkedPackage} 的两枚(会话已不存在只 INFO;
* 未登记对象清理失败 WARN 带 objectKey,登记成功的对象后端会拒绝清理,不该在这里改口)。</li>
* <li>EMPTY 一格没涨:三处原本写成 {@code catch (IOException ignored) { /* 尽力清理 *\/ }} 的
* 空体已按 10-26 那批的先例改成 WARN 带 path——空体是三类高危出口里最贵的一种,
* 这里宁可让 {@code STRICT_LOG_ONLY} 涨六格,也不给"删除失败连日志都不留"开门。</li>
* </ul>
*/
static final int STRICT_TOTAL = 1034;
static final int STRICT_TOTAL = 1045;
/**
* 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。
* 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。
@@ -322,8 +345,13 @@ class CatchSilenceRatchetTest {
* 块体 log.warn 带 taskId/moduleType)、{@code TaskFileJobConfig.taskQueueVirtualThreadExecutor}
* 的 {@code shutdown()} 与 {@code TaskFileJobConfig.videoQueueExecutor}(S1-B6/S1-B2,
* {@code InterruptedException} 只恢复中断标记),逐处记名见 STRICT_TOTAL 上方注释。
* 10-29 Phase 4D 再抬 585 -&gt; 591(AC-17 分片通道六枚 LOG_ONLY,逐处记名见 STRICT_TOTAL 上方注释):
* {@code ChunkedUploadService.cleanupStaleSessions} 内外两枚、{@code deleteQuietly} 逐片与整体两枚、
* {@code WorkbenchClientPackageService.abortChunkedPackage} 的「会话已不存在」INFO 与
* 「未登记对象清理失败」WARN 两枚。六枚的误差方向都是「清理/收尾失败不得盖掉主错误」,
* 主错误由外层重抛;本批 EMPTY 一格没涨(原先三枚空体已按 10-26 先例改成带 path 的 WARN)。
*/
static final int STRICT_NOT_RETHROW = 585;
static final int STRICT_NOT_RETHROW = 591;
/**
* 10-17 抬 172 -> 173,出处就是上面点名过的 {@code TaskHeartbeatLivenessService.stamp()}:
* 块体只有「取到 key 名 + log.warn」一句,写心跳证据失败时既不重试也不抛出——
@@ -347,8 +375,13 @@ class CatchSilenceRatchetTest {
* 块体只有一句带 {@code [collect-data]} 锚点的 WARN(taskId+chunk+err),坏批点名后其余批照常
* 落库。同批另两处({@code markResolved} 追加 return、{@code writeFilteredSheet} 置 incomplete 位)
* 因块内多一句只进 NOT_RETHROW。
* 10-29 Phase 4D 抬 182 -> 188(AC-17 分片通道六枚,逐处记名见 STRICT_TOTAL 上方注释):
* {@code ChunkedUploadService.cleanupStaleSessions} 内外两枚、{@code deleteQuietly} 逐片与整体两枚
* (这四枚原本是 {@code catch (IOException ignored)} 空体,按 10-26 那批先例改成带 path 的 WARN,
* 是「空体换 LOG_ONLY」的迁移而不是新增沉默),加上
* {@code WorkbenchClientPackageService.abortChunkedPackage} 的 INFO 与 WARN 两枚。
*/
static final int STRICT_LOG_ONLY = 182; // 严格口径:块内每条顶层语句都是 log 调用才算
static final int STRICT_LOG_ONLY = 188; // 严格口径:块内每条顶层语句都是 log 调用才算
static final int STRICT_EMPTY = 56;
/**
@@ -0,0 +1,85 @@
package com.nanri.aiimage.modules.clienterror.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* 后管浏览器错误上报口(S1-F2/AC-16)的鉴权与体量闸门。
*
* <p>两条底线:① 这是写入口,匿名请求(sendBeacon 没带管理员会话)必须被拒——与 AC-8
* 「匿名写端点要凭据」同一口径,否则任何人都能往服务端日志里灌任意文本;
* ② 报文体一律逐字段截断,堆栈不许把整条日志撑爆,也不许原样回显给调用方。</p>
*/
class ClientErrorReportControllerTest {
private final AdminAuthSupport adminAuthSupport = mock(AdminAuthSupport.class);
private final HttpServletRequest request = mock(HttpServletRequest.class);
private ClientErrorReportController newController() {
return new ClientErrorReportController(adminAuthSupport);
}
private void loginAsAdmin() {
AdminUserEntity operator = new AdminUserEntity();
operator.setId(11L);
when(adminAuthSupport.requireAdmin(any())).thenReturn(operator);
}
@Test
void 匿名上报必须被拒() {
when(adminAuthSupport.requireAdmin(any())).thenThrow(new BusinessException(401, "未登录"));
assertThatThrownBy(() -> newController().report(request, Map.of("kind", "window-error")))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("未登录");
}
@Test
void 合法会话上报成功且只回一个录制标记() {
loginAsAdmin();
ApiResponse<Map<String, Object>> response = newController().report(request, Map.of(
"kind", "vue-render",
"message", "Cannot read properties of undefined",
"route", "/admin/users",
"ts", 1_730_000_000_000L));
assertThat(response.getData()).containsExactly(entryOf("recorded", true));
}
@Test
void 空请求体不得把上报本身炸掉() {
loginAsAdmin();
assertThat(newController().report(request, null).getData()).containsKey("recorded");
assertThat(newController().report(request, new HashMap<>()).getData()).containsKey("recorded");
}
@Test
void 超长字段截断到上限并留截断标记() {
String huge = "x".repeat(5_000);
String clipped = ClientErrorReportController.clip(huge);
assertThat(clipped).hasSize(2001).endsWith("…");
assertThat(ClientErrorReportController.clip(null)).isEmpty();
assertThat(ClientErrorReportController.clip("短文本")).isEqualTo("短文本");
}
private static Map.Entry<String, Object> entryOf(String key, Object value) {
return Map.entry(key, value);
}
}
@@ -0,0 +1,149 @@
package com.nanri.aiimage.modules.file.service;
import com.nanri.aiimage.common.exception.BusinessException;
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.mock.web.MockMultipartFile;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.HexFormat;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* AC-17 分片上传:拼装正确、缺片拒绝、重复片幂等、放弃即清理、uploadId 不可穿越。
* 不依赖 MinIO/MySQL —— 分片装配在本地临时目录完成,转存 MinIO 由工作台链路单独验证。
*/
class ChunkedUploadServiceTest {
@TempDir
Path tempDir;
private ChunkedUploadService service;
@BeforeEach
void setUp() {
StorageProperties properties = new StorageProperties();
properties.setLocalTempDir(tempDir.toString());
service = new ChunkedUploadService(properties);
}
private MockMultipartFile part(String content) {
return new MockMultipartFile("file", "part.bin", "application/octet-stream",
content.getBytes(StandardCharsets.UTF_8));
}
@Test
void 分片到齐后拼装并复算摘要() throws Exception {
String first = "hello-";
String second = "world";
long total = first.getBytes(StandardCharsets.UTF_8).length
+ second.getBytes(StandardCharsets.UTF_8).length;
ChunkedUploadService.ChunkSession session =
service.init("包.zip", 2, total, "purpose-key");
assertEquals(1, service.savePart(session.uploadId(), 0, part(first)));
assertEquals(2, service.savePart(session.uploadId(), 1, part(second)));
ChunkedUploadService.CompletedUpload completed = service.complete(session.uploadId());
try {
String content = Files.readString(completed.file(), StandardCharsets.UTF_8);
assertEquals(first + second, content);
assertEquals(total, completed.size());
assertEquals("包.zip", completed.fileName());
String expected = HexFormat.of().formatHex(
MessageDigest.getInstance("SHA-256")
.digest((first + second).getBytes(StandardCharsets.UTF_8)));
assertEquals(expected, completed.sha256(), "摘要必须是对拼装实物算出来的");
} finally {
Files.deleteIfExists(completed.file());
}
assertFalse(Files.exists(tempDir.resolve("chunk-uploads").resolve(session.uploadId())),
"拼装完成后分片会话目录必须清掉");
}
@Test
void 缺少分片必须拒绝且不产出半个文件() throws Exception {
ChunkedUploadService.ChunkSession session = service.init("a.zip", 2, 10, "");
service.savePart(session.uploadId(), 0, part("12345"));
BusinessException error = assertThrows(BusinessException.class,
() -> service.complete(session.uploadId()));
assertTrue(error.getMessage().contains("缺少"), "必须明确告知缺哪片: " + error.getMessage());
try (var files = Files.list(tempDir.resolve("chunk-uploads"))) {
assertFalse(files.anyMatch(p -> p.getFileName().toString().startsWith("assembled-")),
"缺片时不许留在半成品");
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
@Test
void 重复上传同一片按覆盖处理() throws Exception {
ChunkedUploadService.ChunkSession session = service.init("a.bin", 1, 3, "");
service.savePart(session.uploadId(), 0, part("old"));
service.savePart(session.uploadId(), 0, part("new"));
ChunkedUploadService.CompletedUpload completed = service.complete(session.uploadId());
try {
assertEquals("new", Files.readString(completed.file(), StandardCharsets.UTF_8));
} finally {
Files.deleteIfExists(completed.file());
}
}
@Test
void 声明大小与实际不符必须拒绝() throws Exception {
ChunkedUploadService.ChunkSession session = service.init("a.bin", 1, 99, "");
service.savePart(session.uploadId(), 0, part("short"));
BusinessException error = assertThrows(BusinessException.class,
() -> service.complete(session.uploadId()));
assertTrue(error.getMessage().contains("不一致"), error.getMessage());
}
@Test
void 放弃会话立即清理分片() throws Exception {
ChunkedUploadService.ChunkSession session = service.init("a.bin", 1, 3, "");
service.savePart(session.uploadId(), 0, part("abc"));
service.abort(session.uploadId());
assertFalse(Files.exists(tempDir.resolve("chunk-uploads").resolve(session.uploadId())));
assertThrows(BusinessException.class, () -> service.uploadedParts(session.uploadId()));
}
@Test
void uploadId必须拒绝路径穿越() {
assertThrows(BusinessException.class, () -> service.uploadedParts("../etc"));
assertThrows(BusinessException.class, () -> service.abort("..\\..\\windows"));
assertThrows(BusinessException.class, () -> service.savePart("abc", 0, part("x")));
}
@Test
void 分片序号越界必须拒绝() {
ChunkedUploadService.ChunkSession session = service.init("a.bin", 2, 10, "");
assertThrows(BusinessException.class,
() -> service.savePart(session.uploadId(), 2, part("x")));
assertThrows(BusinessException.class,
() -> service.savePart(session.uploadId(), -1, part("x")));
}
@Test
void 已上传片号可查询用于续传() throws Exception {
ChunkedUploadService.ChunkSession session = service.init("a.bin", 3, 9, "");
service.savePart(session.uploadId(), 0, part("aaa"));
service.savePart(session.uploadId(), 2, part("ccc"));
List<Integer> uploaded = service.uploadedParts(session.uploadId());
assertEquals(List.of(0, 2), uploaded);
}
}
@@ -0,0 +1,83 @@
package com.nanri.aiimage.modules.file.service.oss;
import com.nanri.aiimage.config.OssProperties;
import com.nanri.aiimage.modules.workbenchclient.service.WorkbenchClientPackageService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* 线上 MinIO 实测(opt-in):AC-17 分片拼装后的服务端转存链路必须真的落进
* 公开 client 桶的 {@code workbench-client/versions/} 前缀,且 stat/sha256/删除
* 全程可用——本机日常闸门不带凭据时自动跳过,绝不碰生产。
*
* <p>跑法(凭据来自线上 java-server 容器 env,只进环境变量、不落任何文件)::</p>
* <pre>
* AIIMAGE_OSS_ACCESS_KEY_ID=... AIIMAGE_OSS_ACCESS_KEY_SECRET=... \
* mvn -o test -Dtest=OssWorkbenchClientLiveVerificationTest
* </pre>
*/
@EnabledIfEnvironmentVariable(named = "AIIMAGE_OSS_ACCESS_KEY_ID", matches = ".+")
class OssWorkbenchClientLiveVerificationTest {
private static final DateTimeFormatter STAMP = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
private OssStorageService liveService() {
OssProperties properties = new OssProperties();
properties.setRegion("us-east-1");
properties.setEndpoint(System.getenv().getOrDefault("AIIMAGE_OSS_ENDPOINT", "https://oss.aishufu.top"));
properties.setPublicEndpoint(
System.getenv().getOrDefault("AIIMAGE_OSS_PUBLIC_ENDPOINT", "https://oss.aishufu.top"));
properties.setBucket("nanri-ai-images");
properties.setAccessKeyId(System.getenv("AIIMAGE_OSS_ACCESS_KEY_ID"));
properties.setAccessKeySecret(System.getenv("AIIMAGE_OSS_ACCESS_KEY_SECRET"));
return new OssStorageService(properties);
}
@Test
void 服务端转存线上MinIO_大小与摘要对得上并可清理() throws Exception {
OssStorageService service = liveService();
String objectKey = WorkbenchClientPackageService.OBJECT_KEY_PREFIX
+ "zz-live-verify-" + STAMP.format(LocalDateTime.now())
+ "-" + UUID.randomUUID().toString().replace("-", "") + "-0.0.0.zip";
Path zip = Files.createTempFile("live-verify-", ".zip");
byte[] marker = "phase4-live-verify".getBytes(StandardCharsets.UTF_8);
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(zip))) {
out.putNextEntry(new ZipEntry("verify.txt"));
out.write(marker);
out.closeEntry();
}
long declaredSize = Files.size(zip);
try {
service.uploadWorkbenchClientObject(zip.toFile(), objectKey);
long statSize = service.workbenchClientObjectSize(objectKey);
assertEquals(declaredSize, statSize, "stat 必须回读真实对象大小");
String sha256 = service.workbenchClientObjectSha256(objectKey);
assertNotNull(sha256);
assertEquals(64, sha256.length(), "sha256 必须是 64 位 hex");
String presignUrl = service.presignWorkbenchClientUpload(objectKey, 120);
assertNotNull(presignUrl);
assertTrue(presignUrl.contains(objectKey.substring(objectKey.indexOf('/') + 1))
|| presignUrl.contains(objectKey),
"预签名 URL 必须指向本次对象");
} finally {
service.removeWorkbenchClientObject(objectKey);
Files.deleteIfExists(zip);
}
assertEquals(-1L, service.workbenchClientObjectSize(objectKey), "清理后对象必须不存在");
}
}
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.file.service.ChunkedUploadService;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.modules.workbenchclient.mapper.WorkbenchClientPackageMapper;
import com.nanri.aiimage.modules.workbenchclient.model.entity.WorkbenchClientPackageEntity;
@@ -61,12 +62,13 @@ class WorkbenchClientPackageServiceTest {
private final WorkbenchClientPackageMapper packageMapper = mock(WorkbenchClientPackageMapper.class);
private final OssStorageService ossStorageService = mock(OssStorageService.class);
private final ChunkedUploadService chunkedUploadService = mock(ChunkedUploadService.class);
@TempDir
Path tempDir;
private WorkbenchClientPackageService service() {
return new WorkbenchClientPackageService(packageMapper, ossStorageService);
return new WorkbenchClientPackageService(packageMapper, ossStorageService, chunkedUploadService);
}
/** 每次调用返回一个新的合法 zip:confirm 扫描后会把临时文件删掉,不能复用同一个路径。 */