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: