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:
@@ -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 -> 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 -> 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;
|
||||
|
||||
/**
|
||||
|
||||
+85
@@ -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);
|
||||
}
|
||||
}
|
||||
+149
@@ -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);
|
||||
}
|
||||
}
|
||||
+83
@@ -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), "清理后对象必须不存在");
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -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 扫描后会把临时文件删掉,不能复用同一个路径。 */
|
||||
|
||||
Reference in New Issue
Block a user