step上传

This commit is contained in:
lxp
2026-11-02 14:32:15 +08:00
parent c5df2162d0
commit d5624c5f0d
21 changed files with 797 additions and 6 deletions
+44
View File
@@ -30,6 +30,7 @@
- [目录文件与提交](#6-目录文件与提交)
- [封面上传](#7-封面上传)
- [URDF 目录上传](#8-urdf-目录上传)
- [STEP 单文件上传](#84-step-单文件上传)
- [GitHub 项目迁移](#9-github-项目迁移)
- [错误处理](#10-错误处理)
- [页面调用顺序与联调检查](#11-页面调用顺序与联调检查)
@@ -112,6 +113,8 @@ function parseServerTime(value: string): Date {
}
```
STEP 上传签名的 `expiresAt` 已包含 UTC 时区后缀 `Z`,使用上述兼容解析函数即可,不额外补充时区。
### 2.3 可复用请求示例
以下示例使用原生 `fetch`。`ADMIN_API_BASE` 已包含 `/admin-api`,函数调用时传 `/users` 等后续路径,避免重复拼接。实际项目中把 `adminToken` 接到自己的登录状态管理即可。
@@ -338,6 +341,9 @@ interface ProjectView {
coverUrl: string | null;
urdfMainUrl: string | null;
urdfBaseUrl: string | null;
stepMainUrl: string | null;
stepFileName: string | null;
stepSizeBytes: number | null;
modelGlbUrl: string | null;
modelPartsBaseUrl: string | null;
modelPartsFiles?: ResourceFileView[]; // 详情返回;列表省略
@@ -391,6 +397,7 @@ interface TypeView {
| sourceProjectCode / sourceCommitSha | 从社区内部 Fork 的来源;没有则为 null |
| sourceRepositoryUrl / sourceLicense | 在线迁移项目的外部来源和许可证;没有或未取得则为 null |
| urdfMainUrl / urdfBaseUrl | 已绑定的 URDF 主文件和资源基地址;未绑定则为 null |
| stepMainUrl / stepFileName / stepSizeBytes | 当前 STEP 公开下载地址、原始文件名和实际字节数;未绑定三个字段均为 null,Fork 继承同一对象引用 |
| liked | 沿用项目公共字段,后台管理页面通常不需要使用 |
**响应层次有差别:** 列表、详情、用户名下项目返回 `ManagedProjectView`,项目名称是 `data.project.name` 或 `data.items[i].project.name`;创建、编辑、发布、URDF 确认返回 `ProjectView`,名称是 `data.name`。
@@ -445,6 +452,9 @@ interface TypeView {
"coverUrl": null,
"urdfMainUrl": null,
"urdfBaseUrl": null,
"stepMainUrl": null,
"stepFileName": null,
"stepSizeBytes": null,
"modelGlbUrl": null,
"modelPartsBaseUrl": null,
"modelPartsFiles": [],
@@ -894,6 +904,40 @@ Fork 只复制这四类资源的八个数据库字段,复用原 OSS 对象,
部署前执行 [项目资源增量 SQL](admin-database.md#已有数据库增加四类项目资源字段)。RAM 用户对当前环境 `project-resources/*` 增加 `oss:PutObject` 和 `oss:GetObject` 权限;配置资源公开读取或公开 CDN,真实前端 Origin 的 OSS CORS 允许 POST、GET、HEAD,允许 Content-Type。无需为新增资源开通复制或删除权限,应用不修改 Bucket 配置。
### 8.4 STEP 单文件上传
STEP 独立于 URDF 和 Git,只允许管理员操作已 READY 且未删除的项目;上传、替换、移除立即更新当前资源,不创建提交、不改变发布状态,浏览 Git 历史不会切换 STEP。
| 方法和路径 | JSON 请求 | 返回的 data |
| --- | --- | --- |
| `POST /admin-api/projects/{projectCode}/step/signature` | `{ files: StepFile[] }` | `StepUploadSignature` |
| `PUT /admin-api/projects/{projectCode}/step` | `{ batchCode: string, files: StepFile[] }` | 完整 `ProjectView`,不包裹 project |
| `DELETE /admin-api/projects/{projectCode}/step` | 无 body | `null` |
```ts
interface StepFile {
relativePath: string;
sizeBytes: number;
}
interface StepUploadSignature {
batchCode: string;
mainRelativePath: string;
uploadUrl: string;
expiresAt: string; // 带时区,例如 2026-11-02T08:15:00Z
files: [{ relativePath: string; objectKey: string; formFields: Record<string, string> }];
}
```
只接受一个 `.step` / `.stp` 文件,忽略后缀大小写,默认范围为 **1–524288000 字节(500 MiB)**。文件名最多 1024 字符,保留中文、空格和大小写,不允许目录、反斜杠、冒号、控制字符等。前端常量和提示文案需同步为 500 MiB。
签名有效期 15 分钟,响应禁止缓存。上传时原样附加所有 `formFields`,最后追加 `file`,向 `uploadUrl` 发起 multipart POST;不要携带管理端 Bearer Token。Policy 精确限制对象及大小,并禁止覆盖。对象以 `model{原始后缀}` 存储,原始名称通过 `stepFileName` 和签名中的 Content-Disposition 保留,不从 URL 猜文件名。
上传完成后使用原 `batchCode` 和文件清单 PUT 确认;成功返回完整项目。详情 GET 的层级仍为 `data.project`。前端直接访问 `stepMainUrl` 下载,无额外下载接口。替换、移除及删除项目不物理删除 OSS 对象,旧链接仍可访问;Fork 继承资源引用,后续可分别修改。
本期与 URDF 一样不保存批次:确认只核验对应项目 STEP 目录的对象存在及大小,不比对历史签名清单,不提供严格幂等、批次过期或绑定冲突检测。确认失败可以重试,但每次确认都重新核验和写入;已上传对象可在签名过期后确认;并发以最后一次成功写入为准;旧请求可能在替换或移除后重新绑定旧对象。不要依据旧需求假设服务端会返回 STEP_BATCH_EXPIRED、STEP_BATCH_MISMATCH 或 STEP_BINDING_CHANGED。
参数错误为 `400 STEP_INVALID_FILE`,超过上限为 `413 STEP_FILE_TOO_LARGE`;对象缺失、大小不符分别为 `409 STEP_FILE_MISSING`、`409 STEP_FILE_SIZE_MISMATCH`。OSS 访问故障沿用 `OSS_ERROR` / `OSS_UNAVAILABLE`,前端保留已上传请求以便重试确认。
## 9. GitHub 项目迁移
页面流程:**输入地址 → 预检 → 补充信息、可选上传封面和 URDF → 创建任务并绑定资源 → 查看阶段 → 检查文件 → 手动发布**。封面和 URDF 均可在迁移表单内完成,无需等迁移后再去项目列表寻找项目。
+22
View File
@@ -8,6 +8,28 @@
治理相关字段为 moderation_reason(管理员下架原因)和 repo_write_status(跨进程写入状态)。下架直接使用现有 publish_status=BLOCKED,不再增加独立治理状态。GitHub来源标识、地址、默认分支和许可证属于迁移任务,统一放在 project_import_task;第一版只支持GitHub,不存平台类型,也不额外区分用户创建与后台创建。
## 已有数据库增加 STEP 单文件字段
已有数据库手工执行以下 SQL,再升级社区端、管理端和导入工具;新建数据库的 `database.md` 已包含这三个字段,不要重复执行。应用不自动迁库,不新增上传批次表。
```sql
ALTER TABLE project
ADD COLUMN step_object_key VARCHAR(512)
CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT ''
COMMENT 'STEP文件环境相对路径,不含域名和环境根目录',
ADD COLUMN step_file_name VARCHAR(1024)
CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT ''
COMMENT 'STEP原始文件名,保留大小写中文和空格',
ADD COLUMN step_size_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0
COMMENT 'STEP文件大小,单位字节,未绑定时为零';
```
STEP 默认最大 500 MiB(524288000 字节),可通过管理端 `STEP_MAX_FILE_BYTES` 配置;调整时须同步前端限制和文案。签名有效期为 15 分钟,文件由浏览器直传 OSS,无需调整 API multipart 限制。对象路径为 `/project-resources/{projectCode}/step/{batchCode}/model{原始后缀}`,不包含环境根目录;原始文件名单独保存。未绑定时路径和文件名为 `''`,大小为 `0`,接口的三个 STEP 字段均为 `null`。
上传签名限定对象、精确大小、禁止覆盖和附件下载响应头;公开 URL 可直接下载,不提供额外下载接口。OSS 需允许管理端来源的 multipart POST,CDN 需透传 Content-Disposition。Fork 复制这三个字段并共享 OSS 对象。替换、移除或删除项目不物理删除对象,不影响其他副本。
STEP 与现有 URDF 使用相同的无状态确认方式:批次不落库,确认核验 OSS 对象及大小,项目锁内局部更新。重复确认会再次写入;签名过期后已上传对象仍可确认;并发以最后一次成功写入为准;替换或移除后重放旧请求可以重新绑定旧对象。不提供批次历史比对、严格幂等或旧批次防复活保证。旧版前端需求文档中的相关保证不适用于本期。
## 已有数据库增加四类项目资源字段
在已完成原有部署的数据库中手工执行以下 SQL,再升级社区端、管理端和导入工具。新建数据库的 `database.md` 建表语句已包含这些字段,不要重复执行。
+4
View File
@@ -62,6 +62,10 @@ project 表只新增 moderation_reason 和 repo_write_status,来源标识、
整机 GLB、零件 GLB 文件夹、拼装 JSON 和行走资源在项目 READY 后通过项目资源接口上传。升级前先手工执行 [八字段增量 SQL](admin-database.md#已有数据库增加四类项目资源字段)。RAM 用户需拥有当前环境 `project-resources/*` 的 `oss:PutObject`、`oss:GetObject` 权限,配置公开读取/CDN 及真实前端 Origin 的 POST、GET、HEAD CORS。新资源签名和确认清单不设置应用正文配额,反向代理须允许实际清单大小;文件内容由浏览器直接上传 OSS。Fork 对这四类资源只继承数据库引用,不调用 OSS;旧对象保留,后续任何清理机制均需检查共享引用。
STEP 上线前还需手工执行 [STEP 三字段增量 SQL](admin-database.md#已有数据库增加-step-单文件字段),然后更新社区端、管理端及导入进程。无需新增批次表。管理端 `STEP_MAX_FILE_BYTES` 默认 `524288000`(500 MiB),前端大小限制和文案须一致;URDF 限制和 API multipart 配额不变。沿用当前环境 `project-resources/*` 的 OSS 读写权限和公开读取配置,浏览器 POST 需允许管理端实际 Origin,CDN 需透传 Content-Disposition 附件下载响应头。
测试环境验收:签名 → 真实 OSS 上传 → 确认绑定 → 普通链接下载,检查中文文件名、500 MiB 边界、替换及重复移除;确认 Git SHA、发布状态和 URDF 不变。回滚应用时保留 STEP 列和对象。无状态确认不提供旧批次防复活保证。
## 异常核对
- 超时、进程在 MIGRATING 阶段中断或远端结果不明确时进入 REVIEW。禁止自动重发,也不能仅凭仓库存在判定成功。
+3
View File
@@ -32,6 +32,9 @@ CREATE TABLE project (
urdf_main_object_key VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT 'URDF主文件环境相对路径,以斜杠开头,不含域名和环境根目录',
urdf_files JSON NOT NULL DEFAULT (JSON_ARRAY()) COMMENT 'URDF文件清单数组,元素包含relativePath和sizeBytes,相对路径以urdf_prefix为基准',
urdf_prefix VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT 'URDF资源环境相对目录,包含上传批次,以斜杠开头和结尾',
step_object_key VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT 'STEP文件环境相对路径,不含域名和环境根目录',
step_file_name VARCHAR(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT 'STEP原始文件名,保留大小写中文和空格',
step_size_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'STEP文件大小,单位字节,未绑定时为零',
model_glb_object_key VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '整机GLB环境相对路径,不含域名和环境根目录',
model_glb_size_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '整机GLB文件大小,单位字节',
model_parts_prefix VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '零件模型环境相对目录,以斜杠开头和结尾',
+7
View File
@@ -208,6 +208,9 @@ export interface Project {
coverUrl: string | null; // 固定公开 HTTPS 图片地址,支持 OSS/CDN 和外部图片
urdfMainUrl: string | null; // 当前 URDF 主文件公开地址,无资源时为 null
urdfBaseUrl: string | null; // 当前资源包公开目录地址,以 / 结尾,无资源时为 null
stepMainUrl: string | null; // 当前 STEP 文件公开下载地址
stepFileName: string | null; // 原始文件名
stepSizeBytes: number | null; // 实际字节数,默认最大 500 MiB
modelGlbUrl: string | null;
modelPartsBaseUrl: string | null; // 目录地址以 / 结尾
modelPartsFiles?: ResourceFileView[]; // 详情始终返回,列表省略
@@ -312,6 +315,9 @@ export interface Comment {
"coverUrl": "https://images.example.com/vibemake/project-covers/11111111111111111111111111111111/20261015/33333333333333333333333333333333.png",
"urdfMainUrl": null,
"urdfBaseUrl": null,
"stepMainUrl": null,
"stepFileName": null,
"stepSizeBytes": null,
"modelGlbUrl": null,
"modelPartsBaseUrl": null,
"modelPartsFiles": [],
@@ -350,6 +356,7 @@ export interface Comment {
| liked | 当前请求用户是否点赞,匿名访问为 false |
| coverUrl | 固定公开封面地址,null 时展示前端默认图;工作区、社区、Fork 和 AI 卡片共用此字段 |
| urdfMainUrl / urdfBaseUrl | 当前 URDF 主文件与资源目录的公开地址,无资源时均为 null;工作区、社区、AI 卡片均返回,Fork 使用独立副本 |
| stepMainUrl / stepFileName / stepSizeBytes | 当前 STEP 公开下载地址、原始文件名和字节数;无绑定三个字段均为 null;由管理端上传,Fork 复用同一对象,替换或移除互不影响 |
工作区归属由 `owner.userCode` 判断,不存在 `workspaceCode`。`updatedAt` 不代表仓库最新提交时间。来源项目撤回或删除后,sourceProjectCode 仍可能存在,访问该来源的社区详情会返回 404。
@@ -0,0 +1,58 @@
package com.linkerbot.vibemake.adminapi.controller;
import com.linkerbot.vibemake.bean.Contracts.ProjectView;
import com.linkerbot.vibemake.bean.StepContracts.*;
import com.linkerbot.vibemake.common.ApiResponse;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.common.RequestTooLargeException;
import com.linkerbot.vibemake.service.admin.AdminStepService;
import com.linkerbot.vibemake.service.oss.StepResources;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.*;
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin-api/projects/{projectCode}/step")
public class AdminStepController {
private final AdminStepService steps;
@PostMapping("/signature")
@Operation(summary = "生成 STEP 单文件 OSS 直传签名,默认最大 500 MiB")
public ResponseEntity<ApiResponse<StepUploadSignature>> signature(
@PathVariable String projectCode, @RequestBody(required = false) StepUploadInput input) {
return ResponseEntity.ok().cacheControl(CacheControl.noStore())
.body(ApiResponse.success(steps.signature(projectCode, input)));
}
@PutMapping
@Operation(summary = "核验并绑定 STEP 文件")
public ApiResponse<ProjectView> confirm(
@PathVariable String projectCode, @RequestBody(required = false) StepConfirmInput input) {
return ApiResponse.success(steps.confirm(projectCode, input));
}
@DeleteMapping
@Operation(summary = "移除 STEP 绑定,保留 OSS 对象")
public ApiResponse<Void> clear(@PathVariable String projectCode) {
steps.clear(projectCode);
return ApiResponse.success(null);
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ApiResponse<Object>> invalidJson(HttpMessageNotReadableException error) {
BusinessException failure = StepResources.invalidFile();
// 保留通用 JSON 正文配额的 413,不能将流式读取超限误报为文件参数错误。
for (Throwable cause = error; cause != null; cause = cause.getCause()) {
if (cause instanceof RequestTooLargeException large) {
failure = large;
break;
}
}
return ResponseEntity.status(failure.status()).cacheControl(CacheControl.noStore())
.body(ApiResponse.failure(failure));
}
}
@@ -77,6 +77,8 @@ vibemake:
legacy-public-base-urls: ${OSS_LEGACY_PUBLIC_BASE_URLS:}
project-resources:
signature-validity: ${PROJECT_RESOURCE_SIGNATURE_VALIDITY:1h}
step:
max-file-bytes: ${STEP_MAX_FILE_BYTES:524288000}
urdf:
max-file-bytes: ${URDF_MAX_FILE_BYTES:52428800}
max-total-bytes: ${URDF_MAX_TOTAL_BYTES:209715200}
@@ -132,6 +132,7 @@ class AdminWorkflowTest {
gitea.reset();
oss.setEnabled(false);
ossObjects.objects.clear();
ossObjects.unavailable = false;
admin = userService.login(UUID.randomUUID() + "@example.invalid");
ordinary = userService.login(UUID.randomUUID() + "@example.invalid");
auth.bootstrap(admin.getUserCode(), PASSWORD);
@@ -489,6 +490,168 @@ class AdminWorkflowTest {
assertThat(bound.path("modelPartsFiles").size()).isEqualTo(1200);
}
private JsonNode stepSignature(String code, List<Map<String, Object>> files) throws Exception {
MvcResult result = mvc.perform(signed(body(post("/admin-api/projects/" + code + "/step/signature"),
Map.of("files", files)))).andReturn();
assertThat(result.getResponse().getStatus()).isEqualTo(200);
assertThat(result.getResponse().getHeader("Cache-Control")).isEqualTo("no-store");
return json.readTree(result.getResponse().getContentAsString()).path("data");
}
@Test
void stepUploadReplaceRetryAndClearPreserveOtherResourcesAndGitState() throws Exception {
String code = createProject();
String endpoint = "/admin-api/projects/" + code;
JsonNode empty = request(signed(get(endpoint)), 200).path("data").path("project");
for (String field : List.of("stepMainUrl", "stepFileName", "stepSizeBytes"))
assertThat(empty.has(field) && empty.path(field).isNull()).as(field).isTrue();
enableOss();
String sha = initialSha(code);
request(signed(body(put(endpoint + "/publication"), Map.of("commitSha", sha))), 200);
String urdfPrefix = "/urdf/" + code + "/" + "b".repeat(32) + "/";
jdbc.update("UPDATE project SET urdf_main_object_key=?, urdf_prefix=?, cover_url=? WHERE project_code=?",
urdfPrefix + "robot.urdf", urdfPrefix, "https://images.example.com/cover.png", code);
JsonNode before = request(signed(get(endpoint)), 200).path("data").path("project");
List<Map<String, Object>> files = List.of(Map.of("relativePath", "机械 臂.STEP", "sizeBytes", 524288000L));
JsonNode signature = stepSignature(code, files);
assertThat(signature.path("mainRelativePath").asText()).isEqualTo("机械 臂.STEP");
assertThat(java.time.OffsetDateTime.parse(signature.path("expiresAt").asText()).getOffset())
.isEqualTo(java.time.ZoneOffset.UTC);
JsonNode policy = json.readTree(Base64.getDecoder().decode(signature.path("files").get(0)
.path("formFields").path("policy").asText()));
assertThat(java.time.Instant.parse(signature.path("expiresAt").asText()))
.isEqualTo(java.time.Instant.parse(policy.path("expiration").asText()));
assertThat(request(signed(get(endpoint)), 200).path("data").path("project").path("stepMainUrl").isNull()).isTrue();
storeResourceFiles(signature, files);
assertThat(request(signed(get(endpoint)), 200).path("data").path("project").path("stepMainUrl").isNull()).isTrue();
Map<String, Object> confirmation = Map.of("batchCode", signature.path("batchCode").asText(), "files", files);
JsonNode bound = request(signed(body(put(endpoint + "/step"), confirmation)), 200).path("data");
assertThat(bound.has("project")).isFalse();
assertThat(bound.path("projectCode").asText()).isEqualTo(code);
assertThat(bound.path("stepMainUrl").asText()).contains("/project-resources/" + code + "/step/").endsWith("/model.STEP");
assertThat(bound.path("stepFileName").asText()).isEqualTo("机械 臂.STEP");
assertThat(bound.path("stepSizeBytes").asLong()).isEqualTo(524288000L);
assertThat(bound.has("id")).isFalse();
assertThat(jdbc.queryForObject("SELECT step_object_key FROM project WHERE project_code=?", String.class, code))
.startsWith("/project-resources/" + code + "/step/");
assertThat(request(signed(body(put(endpoint + "/step"), confirmation)), 200).path("data").path("stepMainUrl"))
.isEqualTo(bound.path("stepMainUrl"));
Map<String, Object> attemptedMetadata = new LinkedHashMap<>(metadata());
attemptedMetadata.put("stepMainUrl", "https://example.invalid/arbitrary.step");
attemptedMetadata.put("stepFileName", "override.step");
attemptedMetadata.put("stepSizeBytes", 1L);
JsonNode metadataUpdated = request(signed(body(patch(endpoint), attemptedMetadata)), 200).path("data");
assertThat(metadataUpdated.path("stepFileName")).isEqualTo(bound.path("stepFileName"));
assertThat(metadataUpdated.path("stepMainUrl")).isEqualTo(bound.path("stepMainUrl"));
assertThat(metadataUpdated.path("stepSizeBytes")).isEqualTo(bound.path("stepSizeBytes"));
List<Map<String, Object>> replacementFiles = List.of(Map.of("relativePath", "new.stp", "sizeBytes", 1L));
JsonNode replacement = stepSignature(code, replacementFiles);
storeResourceFiles(replacement, replacementFiles);
assertThat(request(signed(get(endpoint)), 200).path("data").path("project").path("stepMainUrl"))
.isEqualTo(bound.path("stepMainUrl"));
JsonNode replaced = request(signed(body(put(endpoint + "/step"),
Map.of("batchCode", replacement.path("batchCode").asText(), "files", replacementFiles))), 200).path("data");
assertThat(replaced.path("stepMainUrl")).isNotEqualTo(bound.path("stepMainUrl"));
JsonNode listed = request(signed(get("/admin-api/projects")), 200).path("data").path("items").get(0).path("project");
assertThat(listed.path("stepMainUrl")).isEqualTo(replaced.path("stepMainUrl"));
int objects = ossObjects.objects.size();
oss.setEnabled(false);
assertThat(request(signed(delete(endpoint + "/step")), 200).path("data").isNull()).isTrue();
request(signed(delete(endpoint + "/step")), 200);
JsonNode cleared = request(signed(get(endpoint)), 200).path("data").path("project");
for (String field : List.of("stepMainUrl", "stepFileName", "stepSizeBytes"))
assertThat(cleared.path(field).isNull()).as(field).isTrue();
for (String field : List.of("urdfMainUrl", "urdfBaseUrl", "coverUrl", "repoStatus", "publishStatus", "publishedCommitSha", "publishedAt"))
assertThat(cleared.path(field)).as(field).isEqualTo(before.path(field));
assertThat(initialSha(code)).isEqualTo(sha);
assertThat(ossObjects.objects).hasSize(objects);
// 与 URDF 相同:不保存批次历史,旧请求可以重新确认已上传对象。
enableOss();
assertThat(request(signed(body(put(endpoint + "/step"), confirmation)), 200).path("data").path("stepMainUrl"))
.isEqualTo(bound.path("stepMainUrl"));
}
@Test
void stepConfirmationFailuresPreserveBindingAndEnforceProjectNamespaces() throws Exception {
String code = createProject();
String endpoint = "/admin-api/projects/" + code;
enableOss();
List<Map<String, Object>> files = List.of(Map.of("relativePath", "a.step", "sizeBytes", 10L));
JsonNode signature = stepSignature(code, files);
Map<String, Object> confirmation = Map.of("batchCode", signature.path("batchCode").asText(), "files", files);
assertThat(request(signed(body(put(endpoint + "/step"), confirmation)), 409).path("code").asText())
.isEqualTo("STEP_FILE_MISSING");
storeResourceFiles(signature, files);
JsonNode bound = request(signed(body(put(endpoint + "/step"), confirmation)), 200).path("data");
JsonNode replacement = stepSignature(code, files);
Map<String, Object> nextConfirmation = Map.of("batchCode", replacement.path("batchCode").asText(), "files", files);
assertThat(request(signed(body(put(endpoint + "/step"), nextConfirmation)), 409).path("code").asText())
.isEqualTo("STEP_FILE_MISSING");
ossObjects.objects.put(replacement.path("files").get(0).path("objectKey").asText(), new OssObjectStore.Metadata(11, "bad"));
assertThat(request(signed(body(put(endpoint + "/step"), nextConfirmation)), 409).path("code").asText())
.isEqualTo("STEP_FILE_SIZE_MISMATCH");
ossObjects.unavailable = true;
assertThat(request(signed(body(put(endpoint + "/step"), confirmation)), 502).path("code").asText())
.isEqualTo("OSS_ERROR");
ossObjects.unavailable = false;
String another = createProject();
assertThat(request(signed(body(put("/admin-api/projects/" + another + "/step"), confirmation)), 409).path("code").asText())
.isEqualTo("STEP_FILE_MISSING");
JsonNode urdf = request(signed(body(post(endpoint + "/urdf/signature"), Map.of("files", urdfFiles()))), 200).path("data");
storeResourceFiles(urdf, urdfFiles());
assertThat(request(signed(body(put(endpoint + "/step"),
Map.of("batchCode", urdf.path("batchCode").asText(), "files", files))), 409).path("code").asText())
.isEqualTo("STEP_FILE_MISSING");
assertThat(request(signed(get(endpoint)), 200).path("data").path("project").path("stepMainUrl"))
.isEqualTo(bound.path("stepMainUrl"));
}
@ParameterizedTest
@ValueSource(strings = {"{}", "null", "{\"files\":null}", "{\"files\":[]}", "{\"files\":[null]}",
"{\"files\":[{\"relativePath\":\"a.step\"}]}",
"{\"files\":[{\"relativePath\":\"a.step\",\"sizeBytes\":1.5}]}",
"{\"files\":[{\"relativePath\":\"a.step\",\"sizeBytes\":\"1\"}]}",
"{\"files\":[{\"relativePath\":\"a.step\",\"sizeBytes\":9223372036854775808}]}",
"{\"files\":[{\"relativePath\":\"a.step\",\"sizeBytes\":0}]}",
"{\"files\":[{\"relativePath\":\"../a.step\",\"sizeBytes\":1}]}",
"{\"files\":[{\"relativePath\":\"a.step.exe\",\"sizeBytes\":1}]}",
"{\"files\":[{\"relativePath\":\"a.step\",\"sizeBytes\":1},{\"relativePath\":\"b.stp\",\"sizeBytes\":1}]}"})
void stepInvalidRequestsUseDedicatedErrorCode(String input) throws Exception {
String code = createProject();
enableOss();
assertThat(request(signed(post("/admin-api/projects/" + code + "/step/signature")
.contentType(MediaType.APPLICATION_JSON).content(input)), 400).path("code").asText())
.isEqualTo("STEP_INVALID_FILE");
}
@Test
void stepEndpointsRequireAdminAndReadyNonDeletedProjectAndRejectOversizeFiles() throws Exception {
String code = createProject();
String endpoint = "/admin-api/projects/" + code + "/step";
List<Map<String, Object>> files = List.of(Map.of("relativePath", "a.step", "sizeBytes", 1L));
Map<String, Object> signatureInput = Map.of("files", files);
Map<String, Object> confirmation = Map.of("batchCode", "a".repeat(32), "files", files);
String ordinaryToken = tokens.issue(ordinary.getId(), ordinary.getSessionVersion());
for (MockHttpServletRequestBuilder operation : List.of(body(post(endpoint + "/signature"), signatureInput),
body(put(endpoint), confirmation), delete(endpoint))) request(operation, 401);
for (MockHttpServletRequestBuilder operation : List.of(body(post(endpoint + "/signature"), signatureInput),
body(put(endpoint), confirmation), delete(endpoint)))
request(operation.header("Authorization", "Bearer " + ordinaryToken), 403);
request(signed(body(post(endpoint + "/signature"), signatureInput)), 503);
enableOss();
assertThat(request(signed(body(post(endpoint + "/signature"),
Map.of("files", List.of(Map.of("relativePath", "a.step", "sizeBytes", 524288001L))))), 413).path("code").asText())
.isEqualTo("STEP_FILE_TOO_LARGE");
assertThat(request(signed(body(put(endpoint), Map.of("batchCode", "../other", "files", files))), 400).path("code").asText())
.isEqualTo("STEP_INVALID_FILE");
jdbc.update("UPDATE project SET repo_status='CREATING' WHERE project_code=?", code);
for (MockHttpServletRequestBuilder operation : List.of(body(post(endpoint + "/signature"), signatureInput),
body(put(endpoint), confirmation), delete(endpoint))) request(signed(operation), 409);
jdbc.update("UPDATE project SET repo_status='READY', deleted=1 WHERE project_code=?", code);
for (MockHttpServletRequestBuilder operation : List.of(body(post(endpoint + "/signature"), signatureInput),
body(put(endpoint), confirmation), delete(endpoint))) request(signed(operation), 404);
}
private List<Map<String, Object>> urdfFiles() {
return List.of(
Map.of("relativePath", "urdf/机械臂.URDF", "sizeBytes", 12),
@@ -8,9 +8,11 @@ import java.util.concurrent.ConcurrentHashMap;
final class FakeOssObjectStore implements OssObjectStore {
final Map<String, Metadata> objects = new ConcurrentHashMap<>();
boolean unavailable;
@Override
public Metadata head(String objectKey) {
if (unavailable) throw new BusinessException(502, "OSS_ERROR", "OSS 暂时不可用");
Metadata metadata = objects.get(objectKey);
if (metadata == null)
throw new BusinessException(400, "URDF_FILE_MISSING", "URDF 文件未上传完整,请重新上传");
@@ -93,6 +93,7 @@ class WorkflowTest {
@Autowired UrdfProperties urdfLimits;
@Autowired FakeGitSnapshotCopier snapshots;
@Autowired AdminProjectResourceService projectResources;
@Autowired com.linkerbot.vibemake.service.admin.AdminStepService steps;
@BeforeEach
void reset() throws Exception {
@@ -995,26 +996,33 @@ class WorkflowTest {
String walking = base + "walking-resources/" + batch + "/";
String glb = base + "model-glb/" + batch + "/整机.glb";
String assembly = base + "assembly-json/" + batch + "/steps.json";
String step = base + "step/" + batch + "/model.STEP";
String manifest = "[{\"relativePath\":\"目录/a #?.glb\",\"sizeBytes\":5368709120}]";
jdbc.update("UPDATE project SET model_glb_object_key=?, model_glb_size_bytes=?, model_parts_prefix=?,"
+ " model_parts_files=?, assembly_json_object_key=?, assembly_json_size_bytes=?,"
+ " walking_resources_prefix=?, walking_resources_files=? WHERE project_code=?",
glb, 5368709120L, parts, manifest, assembly, 123L, walking, manifest, code);
jdbc.update("UPDATE project SET step_object_key=?, step_file_name=?, step_size_bytes=? WHERE project_code=?",
step, "机械 臂.STEP", 524288000L, code);
String ownerEndpoint = "/api/workspace/projects/" + code;
JsonNode source = perform(auth(get(ownerEndpoint), owner), 200).path("data");
assertThat(source.path("modelGlbUrl").asText()).endsWith("/%E6%95%B4%E6%9C%BA.glb");
assertThat(source.path("modelPartsFiles").get(0).path("url").asText()).endsWith("/%E7%9B%AE%E5%BD%95/a%20%23%3F.glb");
assertThat(source.path("walkingResourcesFiles").get(0).path("sizeBytes").asLong()).isEqualTo(5368709120L);
assertThat(source.has("id")).isFalse();
assertThat(source.path("stepFileName").asText()).isEqualTo("机械 臂.STEP");
assertThat(source.path("stepSizeBytes").asLong()).isEqualTo(524288000L);
String sha = head(owner, code);
publish(owner, code, sha);
JsonNode publicDetail = perform(get("/api/community/projects/" + code), 200).path("data");
assertThat(publicDetail.path("modelPartsFiles")).isEqualTo(source.path("modelPartsFiles"));
assertThat(publicDetail.path("assemblyJsonUrl")).isEqualTo(source.path("assemblyJsonUrl"));
assertThat(publicDetail.path("stepMainUrl")).isEqualTo(source.path("stepMainUrl"));
for (JsonNode listing : List.of(
perform(auth(get("/api/workspace/projects"), owner), 200).path("data").path("items").get(0),
perform(get("/api/community/projects"), 200).path("data").path("items").get(0))) {
assertThat(listing.path("modelGlbUrl")).isEqualTo(source.path("modelGlbUrl"));
assertThat(listing.path("stepMainUrl")).isEqualTo(source.path("stepMainUrl"));
assertThat(listing.has("modelPartsFiles")).isFalse();
assertThat(listing.has("walkingResourcesFiles")).isFalse();
}
@@ -1023,10 +1031,11 @@ class WorkflowTest {
String target = fork.path("projectCode").asText();
assertThat(fork.path("repoStatus").asText()).isEqualTo("READY");
for (String field : List.of("modelGlbUrl", "modelPartsBaseUrl", "modelPartsFiles", "assemblyJsonUrl",
"walkingResourcesBaseUrl", "walkingResourcesFiles"))
"walkingResourcesBaseUrl", "walkingResourcesFiles", "stepMainUrl", "stepFileName", "stepSizeBytes"))
assertThat(fork.path(field)).isEqualTo(source.path(field));
for (String column : List.of("model_glb_object_key", "model_glb_size_bytes", "model_parts_prefix", "model_parts_files",
"assembly_json_object_key", "assembly_json_size_bytes", "walking_resources_prefix", "walking_resources_files")) {
"assembly_json_object_key", "assembly_json_size_bytes", "walking_resources_prefix", "walking_resources_files",
"step_object_key", "step_file_name", "step_size_bytes")) {
assertThat(jdbc.queryForObject("SELECT " + column + " FROM project WHERE project_code=?", String.class, target))
.isEqualTo(jdbc.queryForObject("SELECT " + column + " FROM project WHERE project_code=?", String.class, code));
}
@@ -1034,9 +1043,13 @@ class WorkflowTest {
jdbc.update("UPDATE project SET model_glb_object_key=? WHERE project_code=?", glb.replace(batch, "b".repeat(32)), code);
assertThat(perform(auth(get("/api/workspace/projects/" + target), other), 200).path("data").path("modelGlbUrl"))
.isEqualTo(fork.path("modelGlbUrl"));
jdbc.update("UPDATE project SET step_object_key=? WHERE project_code=?", step.replace(batch, "b".repeat(32)), code);
assertThat(perform(auth(get("/api/workspace/projects/" + target), other), 200).path("data").path("stepMainUrl"))
.isEqualTo(fork.path("stepMainUrl"));
try (ProjectActors.Scope scope = ProjectActors.enter(new ProjectActors.AdminActor("a".repeat(32), "测试管理员"))) {
projectResources.clear(code, "model-parts");
projectResources.clear(target, "assembly-json");
steps.clear(code);
}
JsonNode independentSource = perform(auth(get(ownerEndpoint), owner), 200).path("data");
JsonNode independentTarget = perform(auth(get("/api/workspace/projects/" + target), other), 200).path("data");
@@ -1044,6 +1057,8 @@ class WorkflowTest {
assertThat(independentSource.path("assemblyJsonUrl")).isEqualTo(source.path("assemblyJsonUrl"));
assertThat(independentTarget.path("modelPartsFiles")).isEqualTo(fork.path("modelPartsFiles"));
assertThat(independentTarget.path("assemblyJsonUrl").isNull()).isTrue();
assertThat(independentSource.path("stepMainUrl").isNull()).isTrue();
assertThat(independentTarget.path("stepMainUrl")).isEqualTo(fork.path("stepMainUrl"));
perform(auth(delete(ownerEndpoint), owner), 200);
JsonNode remaining = perform(auth(get("/api/workspace/projects/" + target), other), 200).path("data");
assertThat(remaining.path("walkingResourcesFiles")).isEqualTo(fork.path("walkingResourcesFiles"));
+4
View File
@@ -12,6 +12,10 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
@@ -95,6 +95,9 @@ public final class Contracts {
String coverUrl,
String urdfMainUrl,
String urdfBaseUrl,
String stepMainUrl,
String stepFileName,
Long stepSizeBytes,
String modelGlbUrl,
String modelPartsBaseUrl,
@JsonInclude(JsonInclude.Include.NON_NULL) List<ResourceFileView> modelPartsFiles,
@@ -0,0 +1,41 @@
package com.linkerbot.vibemake.bean;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.Map;
public final class StepContracts {
private StepContracts() {}
// STEP 参数由专用服务校验,统一返回 STEP_INVALID_FILE。
public record StepFile(String relativePath,
@JsonDeserialize(using = StepSizeDeserializer.class) Long sizeBytes) {}
public record StepUploadInput(List<StepFile> files) {}
public record StepConfirmInput(String batchCode, List<StepFile> files) {}
public record StepFileSignature(
String relativePath, String objectKey, Map<String, String> formFields) {}
public record StepUploadSignature(
String batchCode, String mainRelativePath, String uploadUrl,
Instant expiresAt, List<StepFileSignature> files) {}
/** 拒绝小数和数字字符串,避免 Jackson 默认截断或转换文件大小。 */
public static final class StepSizeDeserializer extends JsonDeserializer<Long> {
@Override
public Long deserialize(JsonParser parser, DeserializationContext context) throws IOException {
if (!parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
return (Long) context.handleUnexpectedToken(Long.class, parser);
}
return parser.getLongValue();
}
}
}
@@ -19,6 +19,9 @@ public class Project extends BaseEntity {
private String urdfMainObjectKey = "";
private String urdfFiles = "[]";
private String urdfPrefix = "";
private String stepObjectKey = "";
private String stepFileName = "";
private Long stepSizeBytes = 0L;
private String modelGlbObjectKey = "";
private Long modelGlbSizeBytes = 0L;
private String modelPartsPrefix = "";
@@ -0,0 +1,85 @@
package com.linkerbot.vibemake.service.admin;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.linkerbot.vibemake.bean.Contracts.ProjectView;
import com.linkerbot.vibemake.bean.StepContracts.*;
import com.linkerbot.vibemake.bean.entity.Project;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.service.ProjectService;
import com.linkerbot.vibemake.service.mapper.ProjectMapper;
import com.linkerbot.vibemake.service.oss.StepResources;
import com.linkerbot.vibemake.service.support.ProjectActors;
import com.linkerbot.vibemake.service.support.ProjectLocks;
import com.linkerbot.vibemake.service.support.ProjectSupport;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class AdminStepService {
private static final Logger log = LoggerFactory.getLogger(AdminStepService.class);
private final ProjectService projects;
private final ProjectMapper mapper;
private final StepResources resources;
private final ProjectLocks locks;
private void requireAdmin() {
if (!ProjectActors.isAdmin())
throw new BusinessException(403, "FORBIDDEN", "仅管理员可管理 STEP 文件");
}
public StepUploadSignature signature(String projectCode, StepUploadInput input) {
requireAdmin();
projects.readyOwner(0, projectCode);
if (input == null) throw StepResources.invalidFile();
return resources.sign(projectCode, input.files());
}
public ProjectView confirm(String projectCode, StepConfirmInput input) {
requireAdmin();
projects.readyOwner(0, projectCode);
if (input == null) throw StepResources.invalidFile();
// OSS 网络调用不占用项目锁;落库前重新读取项目并核验状态。
String path = resources.verify(projectCode, input.batchCode(), input.files());
StepFile file = input.files().getFirst();
return locks.serial(projectCode, () -> {
Project project = projects.readyOwner(0, projectCode);
project.setStepObjectKey(path);
project.setStepFileName(file.relativePath());
project.setStepSizeBytes(file.sizeBytes());
persist(project);
log.info("绑定 STEP 文件 projectCode={} batchCode={} operatorCode={} sizeBytes={}",
projectCode, input.batchCode(), ProjectActors.admin().userCode(), file.sizeBytes());
return projects.view(project, null);
});
}
public void clear(String projectCode) {
requireAdmin();
locks.serial(projectCode, () -> {
Project project = projects.readyOwner(0, projectCode);
project.setStepObjectKey("");
project.setStepFileName("");
project.setStepSizeBytes(0L);
persist(project);
log.info("移除 STEP 绑定 projectCode={} operatorCode={}",
projectCode, ProjectActors.admin().userCode());
return null;
});
}
private void persist(Project project) {
project.setUpdatedAt(ProjectSupport.now());
int updated = mapper.update(null, new LambdaUpdateWrapper<Project>()
.eq(Project::getProjectCode, project.getProjectCode())
.eq(Project::getDeleted, false)
.eq(Project::getRepoStatus, "READY")
.set(Project::getStepObjectKey, project.getStepObjectKey())
.set(Project::getStepFileName, project.getStepFileName())
.set(Project::getStepSizeBytes, project.getStepSizeBytes())
.set(Project::getUpdatedAt, project.getUpdatedAt()));
if (updated != 1) throw BusinessException.conflict("项目状态已变化,请刷新后重试");
}
}
@@ -0,0 +1,20 @@
package com.linkerbot.vibemake.service.config;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Positive;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
@Getter
@Setter
@Validated
@Component
@ConfigurationProperties(prefix = "vibemake.oss.step")
public class StepProperties {
@Positive
@Max(value = 5368709120L, message = "STEP 文件大小不能超过 OSS 表单直传的 5 GiB 上限")
private long maxFileBytes = 500L * 1024 * 1024;
}
@@ -199,6 +199,7 @@ public class ProjectServiceImpl implements ProjectService {
ProjectImportTask imported,
boolean includeFiles) {
try {
boolean hasStep = project.getStepObjectKey() != null && !project.getStepObjectKey().isEmpty();
return new ProjectView(
project.getProjectCode(),
project.getName(),
@@ -206,6 +207,9 @@ public class ProjectServiceImpl implements ProjectService {
resourcePaths.coverUrl(project.getCoverUrl()),
urdfUrl(project.getUrdfMainObjectKey()),
urdfUrl(project.getUrdfPrefix()),
resourcePaths.publicUrl(project.getStepObjectKey()),
hasStep ? project.getStepFileName() : null,
hasStep ? project.getStepSizeBytes() : null,
resourcePaths.publicUrl(project.getModelGlbObjectKey()),
resourcePaths.publicUrl(project.getModelPartsPrefix()),
includeFiles ? resourceFiles(project.getModelPartsPrefix(), project.getModelPartsFiles(),
@@ -307,7 +311,10 @@ public class ProjectServiceImpl implements ProjectService {
if (source != null) {
project.setSourceProjectId(source.getId());
project.setSourceCommitSha(source.getPublishedCommitSha());
// 新增资源只继承数据库引用;不查询或复制 OSS,源项目替换也不会改变副本绑定。
// STEP 等项目资源只继承数据库引用;不访问 OSS,源项目替换不会改变副本绑定。
project.setStepObjectKey(source.getStepObjectKey());
project.setStepFileName(source.getStepFileName());
project.setStepSizeBytes(source.getStepSizeBytes());
project.setModelGlbObjectKey(source.getModelGlbObjectKey());
project.setModelGlbSizeBytes(source.getModelGlbSizeBytes());
project.setModelPartsPrefix(source.getModelPartsPrefix());
@@ -54,7 +54,7 @@ public class OssPostSigner {
public CoverUploadSignature sign(String userCode, String contentType, String extension) {
String key = properties.getRootDirectory() + "/project-covers/" + userCode + "/"
+ BUSINESS_DATE.format(clock.instant()) + "/" + Codes.next() + "." + extension;
SignedPost post = post(key, contentType, 1, MAX_FILE_BYTES, VALIDITY, false);
SignedPost post = post(key, contentType, 1, MAX_FILE_BYTES, VALIDITY, false, null);
return new CoverUploadSignature(properties.uploadUrl(), key,
resourcePaths.publicUrl(key), post.expiresAt(), post.formFields());
}
@@ -66,11 +66,16 @@ public class OssPostSigner {
}
public SignedPost signObject(String key, String contentType, long sizeBytes, Duration validity) {
return post(key, contentType, sizeBytes, sizeBytes, validity, true);
return signObject(key, contentType, sizeBytes, validity, null);
}
public SignedPost signObject(String key, String contentType, long sizeBytes,
Duration validity, String contentDisposition) {
return post(key, contentType, sizeBytes, sizeBytes, validity, true, contentDisposition);
}
private SignedPost post(String key, String contentType, long minBytes, long maxBytes,
Duration validity, boolean forbidOverwrite) {
Duration validity, boolean forbidOverwrite, String contentDisposition) {
Instant now = clock.instant().truncatedTo(ChronoUnit.MILLIS);
Instant expiration = now.plus(validity);
String date = DATE.format(now);
@@ -92,6 +97,8 @@ public class OssPostSigner {
conditions.add(List.of("eq", "$x-oss-forbid-overwrite", "true"));
conditions.add(List.of("eq", "$x-oss-content-type", contentType));
}
if (contentDisposition != null)
conditions.add(List.of("eq", "$Content-Disposition", contentDisposition));
policy.put("conditions", conditions);
try {
String encoded = Base64.getEncoder().encodeToString(json.writeValueAsBytes(policy));
@@ -105,6 +112,7 @@ public class OssPostSigner {
properties.getRegion(), encoded));
fields.put("content-type", contentType);
fields.put("success_action_status", "200");
if (contentDisposition != null) fields.put("Content-Disposition", contentDisposition);
if (forbidOverwrite) {
fields.put("x-oss-forbid-overwrite", "true");
fields.put("x-oss-content-type", contentType);
@@ -0,0 +1,105 @@
package com.linkerbot.vibemake.service.oss;
import com.linkerbot.vibemake.bean.StepContracts.*;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.common.Codes;
import com.linkerbot.vibemake.common.FilePaths;
import com.linkerbot.vibemake.service.config.OssProperties;
import com.linkerbot.vibemake.service.config.StepProperties;
import com.linkerbot.vibemake.service.support.ProjectSupport;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.Locale;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ContentDisposition;
import org.springframework.stereotype.Component;
/** 与 URDF 一致的无状态签名和核验;批次编码仅用于隔离对象目录。 */
@Component
@RequiredArgsConstructor
public class StepResources {
private static final Logger log = LoggerFactory.getLogger(StepResources.class);
private final OssProperties oss;
private final StepProperties limits;
private final OssPostSigner signer;
private final OssObjectStore store;
private final OssResourcePaths paths;
public static BusinessException invalidFile() {
return new BusinessException(400, "STEP_INVALID_FILE", "请提供一个合法的非空 STEP 或 STP 文件");
}
public StepFile validate(List<StepFile> files) {
if (files == null || files.size() != 1 || files.getFirst() == null) throw invalidFile();
StepFile file = files.getFirst();
if (file.sizeBytes() == null || file.sizeBytes() <= 0) throw invalidFile();
if (file.sizeBytes() > limits.getMaxFileBytes())
throw new BusinessException(413, "STEP_FILE_TOO_LARGE",
"STEP 文件大小不能超过 " + limits.getMaxFileBytes() + " 字节");
String name;
try {
name = FilePaths.file(file.relativePath());
} catch (BusinessException error) {
throw invalidFile();
}
int dot = name.lastIndexOf('.');
if (name.contains("/") || name.chars().anyMatch(Character::isISOControl)
|| dot <= 0 || name.substring(0, dot).isBlank()) throw invalidFile();
String extension = name.substring(dot).toLowerCase(Locale.ROOT);
if (!extension.equals(".step") && !extension.equals(".stp")) throw invalidFile();
return file;
}
public String storagePath(String projectCode, String batchCode, StepFile file) {
if (projectCode == null || !projectCode.matches("[a-z0-9]{12}|[a-z0-9]{32}")
|| batchCode == null || !batchCode.matches("[a-f0-9]{32}"))
throw new BusinessException(400, "STEP_INVALID_FILE", "项目或 STEP 上传批次编码不合法");
String extension = file.relativePath().substring(file.relativePath().lastIndexOf('.'));
return "/project-resources/" + projectCode + "/step/" + batchCode + "/model" + extension;
}
private void requireEnabled() {
if (!oss.isEnabled())
throw new BusinessException(503, "OSS_UNAVAILABLE", "OSS STEP 上传暂未开启");
}
public StepUploadSignature sign(String projectCode, List<StepFile> files) {
StepFile file = validate(files);
requireEnabled();
String batchCode = Codes.next();
String key = paths.objectKey(storagePath(projectCode, batchCode, file));
String disposition = ContentDisposition.attachment()
.filename(file.relativePath(), StandardCharsets.UTF_8).build().toString();
OssPostSigner.SignedPost post = signer.signObject(key, "application/octet-stream",
file.sizeBytes(), Duration.ofMinutes(15), disposition);
log.info("生成 STEP 上传签名 projectCode={} batchCode={} sizeBytes={}",
projectCode, batchCode, file.sizeBytes());
return new StepUploadSignature(batchCode, file.relativePath(), oss.uploadUrl(),
post.expiresAt().atZone(ProjectSupport.TIME_ZONE).toInstant(),
List.of(new StepFileSignature(file.relativePath(), key, post.formFields())));
}
public String verify(String projectCode, String batchCode, List<StepFile> files) {
StepFile file = validate(files);
String path = storagePath(projectCode, batchCode, file);
requireEnabled();
try {
OssObjectStore.Metadata metadata = store.head(paths.objectKey(path));
if (metadata.sizeBytes() != file.sizeBytes()) {
log.warn("STEP 文件大小不符 projectCode={} batchCode={} expectedBytes={} actualBytes={}",
projectCode, batchCode, file.sizeBytes(), metadata.sizeBytes());
throw new BusinessException(409, "STEP_FILE_SIZE_MISMATCH", "STEP 文件大小不符,请重新上传");
}
return path;
} catch (BusinessException error) {
log.warn("STEP 文件核验失败 projectCode={} batchCode={} code={}",
projectCode, batchCode, error.code());
if ("URDF_FILE_MISSING".equals(error.code()))
throw new BusinessException(409, "STEP_FILE_MISSING", "STEP 文件尚未上传,请重新上传");
throw error;
}
}
}
@@ -0,0 +1,48 @@
package com.linkerbot.vibemake.service.admin;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import com.linkerbot.vibemake.bean.StepContracts.*;
import com.linkerbot.vibemake.bean.entity.Project;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.service.ProjectService;
import com.linkerbot.vibemake.service.mapper.ProjectMapper;
import com.linkerbot.vibemake.service.oss.StepResources;
import com.linkerbot.vibemake.service.support.ProjectActors;
import com.linkerbot.vibemake.service.support.ProjectLocks;
import java.util.List;
import org.junit.jupiter.api.Test;
class AdminStepServiceTest {
private static final String PROJECT = "7m4k9x2p8q6r";
private final ProjectService projects = mock(ProjectService.class);
private final ProjectMapper mapper = mock(ProjectMapper.class);
private final StepResources resources = mock(StepResources.class);
private final AdminStepService service = new AdminStepService(projects, mapper, resources, new ProjectLocks());
@Test
void directServiceCallsWithoutAdminCannotAccessProjectOrStorage() {
assertThatThrownBy(() -> service.signature(PROJECT, null)).isInstanceOfSatisfying(BusinessException.class,
error -> assertThat(error.status()).isEqualTo(403));
assertThatThrownBy(() -> service.confirm(PROJECT, null)).isInstanceOf(BusinessException.class);
assertThatThrownBy(() -> service.clear(PROJECT)).isInstanceOf(BusinessException.class);
verifyNoInteractions(projects, mapper, resources);
}
@Test
void changedProjectStateAfterOssVerificationPreventsBinding() {
String batch = "a".repeat(32);
List<StepFile> files = List.of(new StepFile("a.step", 1L));
when(projects.readyOwner(0, PROJECT)).thenReturn(new Project())
.thenThrow(BusinessException.conflict("项目状态已变化"));
when(resources.verify(PROJECT, batch, files)).thenReturn("/project-resources/" + PROJECT + "/step/" + batch + "/model.step");
try (ProjectActors.Scope scope = ProjectActors.enter(new ProjectActors.AdminActor("b".repeat(32), "管理员"))) {
assertThatThrownBy(() -> service.confirm(PROJECT, new StepConfirmInput(batch, files)))
.isInstanceOfSatisfying(BusinessException.class, error -> assertThat(error.status()).isEqualTo(409));
}
verify(projects, times(2)).readyOwner(0, PROJECT);
verify(resources).verify(PROJECT, batch, files);
verifyNoInteractions(mapper);
}
}
@@ -0,0 +1,147 @@
package com.linkerbot.vibemake.service.oss;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkerbot.vibemake.bean.StepContracts.StepFile;
import com.linkerbot.vibemake.bean.StepContracts.StepUploadSignature;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.service.config.OssProperties;
import com.linkerbot.vibemake.service.config.StepProperties;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.http.ContentDisposition;
class StepResourcesTest {
private static final String PROJECT = "7m4k9x2p8q6r";
private static final String BATCH = "a".repeat(32);
private final ObjectMapper json = new ObjectMapper();
private final OssProperties oss = new OssProperties();
private final StepProperties limits = new StepProperties();
private final OssObjectStore store = mock(OssObjectStore.class);
private final StepResources resources;
StepResourcesTest() {
oss.setEnabled(true);
oss.setRegion("cn-hangzhou");
oss.setBucket("examplebucket");
oss.setAccessKeyId("UnitTestAccessKey");
oss.setAccessKeySecret("unit-test-secret");
oss.setRootDirectory("vibemake_dev");
Clock clock = Clock.fixed(Instant.parse("2026-11-02T00:00:00Z"), ZoneOffset.UTC);
resources = new StepResources(oss, limits, new OssPostSigner(oss, json, clock),
store, new OssResourcePaths(oss));
}
@ParameterizedTest
@ValueSource(strings = {"机械 臂.step", "机械 臂.stp", "机械 臂.STEP", "机械 臂.STP"})
void signsExactFiveHundredMiBWithOriginalDownloadNameAndUtcExpiry(String name) throws Exception {
long size = 524288000L;
StepUploadSignature signed = resources.sign(PROJECT, List.of(new StepFile(name, size)));
assertThat(signed.mainRelativePath()).isEqualTo(name);
assertThat(signed.files()).hasSize(1);
assertThat(signed.files().getFirst().relativePath()).isEqualTo(name);
String key = signed.files().getFirst().objectKey();
assertThat(key).isEqualTo("vibemake_dev/project-resources/" + PROJECT + "/step/"
+ signed.batchCode() + "/model" + name.substring(name.lastIndexOf('.')));
assertThat(signed.expiresAt().toString()).isEqualTo("2026-11-02T00:15:00Z");
String disposition = signed.files().getFirst().formFields().get("Content-Disposition");
assertThat(ContentDisposition.parse(disposition).getType()).isEqualTo("attachment");
assertThat(ContentDisposition.parse(disposition).getFilename()).isEqualTo(name);
JsonNode policy = json.readTree(Base64.getDecoder().decode(signed.files().getFirst().formFields().get("policy")));
assertThat(policy.path("expiration").asText()).isEqualTo("2026-11-02T00:15:00.000Z");
assertThat(policy.path("conditions").toString())
.contains("[\"content-length-range\",524288000,524288000]")
.contains(json.writeValueAsString(List.of("eq", "$key", key)))
.contains(json.writeValueAsString(List.of("eq", "$Content-Disposition", disposition)))
.contains("[\"eq\",\"$x-oss-forbid-overwrite\",\"true\"]");
verifyNoInteractions(store);
}
@ParameterizedTest
@ValueSource(strings = {"", " ", ".", "..", ".step", ".stp", " .step", "a.step.exe", "a.stl",
"a/b.step", "../b.step", "a\\b.step", "a:b.step", "a\nb.step", "a\u007fb.step", "a\u0085b.step"})
void rejectsUnsafeNames(String name) {
assertInvalid(List.of(new StepFile(name, 1L)));
}
@Test
void validatesCountNullsSizeBoundariesAndConfiguredLimit() {
assertInvalid(null);
assertInvalid(List.of());
assertInvalid(Arrays.asList((StepFile) null));
assertInvalid(List.of(new StepFile("a.step", 1L), new StepFile("b.stp", 1L)));
assertInvalid(List.of(new StepFile(null, 1L)));
assertInvalid(List.of(new StepFile("a.step", null)));
assertInvalid(List.of(new StepFile("a.step", 0L)));
assertInvalid(List.of(new StepFile("a.step", -1L)));
assertInvalid(List.of(new StepFile("x".repeat(1020) + ".step", 1L)));
assertThat(resources.validate(List.of(new StepFile("a.step", 1L))).sizeBytes()).isEqualTo(1);
assertThatThrownBy(() -> resources.validate(List.of(new StepFile("a.step", 524288001L))))
.isInstanceOfSatisfying(BusinessException.class, error -> {
assertThat(error.status()).isEqualTo(413);
assertThat(error.code()).isEqualTo("STEP_FILE_TOO_LARGE");
});
limits.setMaxFileBytes(1);
assertThatThrownBy(() -> resources.validate(List.of(new StepFile("a.step", 2L))))
.isInstanceOfSatisfying(BusinessException.class,
error -> assertThat(error.code()).isEqualTo("STEP_FILE_TOO_LARGE"));
}
@Test
void storesLongOriginalNamesWithoutCreatingLongObjectKeys() {
String name = "机".repeat(1019) + ".STEP";
StepUploadSignature signed = resources.sign(PROJECT, List.of(new StepFile(name, 1L)));
assertThat(signed.mainRelativePath()).hasSize(1024).isEqualTo(name);
assertThat(signed.files().getFirst().objectKey()).hasSizeLessThan(150);
}
@Test
void verifiesMetadataWithoutTreatingOssFailuresAsMissingFiles() {
List<StepFile> files = List.of(new StepFile("a.STEP", 10L));
String key = "vibemake_dev" + resources.storagePath(PROJECT, BATCH, files.getFirst());
when(store.head(key)).thenThrow(new BusinessException(502, "OSS_ERROR", "存储暂不可用"))
.thenThrow(new BusinessException(400, "URDF_FILE_MISSING", "缺失"))
.thenReturn(new OssObjectStore.Metadata(11, "etag"))
.thenReturn(new OssObjectStore.Metadata(10, "etag"));
assertThatThrownBy(() -> resources.verify(PROJECT, BATCH, files))
.isInstanceOfSatisfying(BusinessException.class, error -> assertThat(error.code()).isEqualTo("OSS_ERROR"));
assertThatThrownBy(() -> resources.verify(PROJECT, BATCH, files))
.isInstanceOfSatisfying(BusinessException.class, error -> {
assertThat(error.status()).isEqualTo(409);
assertThat(error.code()).isEqualTo("STEP_FILE_MISSING");
});
assertThatThrownBy(() -> resources.verify(PROJECT, BATCH, files))
.isInstanceOfSatisfying(BusinessException.class,
error -> assertThat(error.code()).isEqualTo("STEP_FILE_SIZE_MISMATCH"));
assertThat(resources.verify(PROJECT, BATCH, files)).isEqualTo(key.substring("vibemake_dev".length()));
}
@Test
void rejectsInvalidBatchAndProjectCodesAndDisabledStorage() {
StepFile file = new StepFile("a.step", 1L);
assertThatThrownBy(() -> resources.storagePath(PROJECT, "../other", file)).isInstanceOf(BusinessException.class);
assertThatThrownBy(() -> resources.storagePath("../other", BATCH, file)).isInstanceOf(BusinessException.class);
oss.setEnabled(false);
assertThatThrownBy(() -> resources.sign(PROJECT, List.of(file)))
.isInstanceOfSatisfying(BusinessException.class,
error -> assertThat(error.code()).isEqualTo("OSS_UNAVAILABLE"));
verifyNoInteractions(store);
}
private void assertInvalid(List<StepFile> files) {
assertThatThrownBy(() -> resources.validate(files)).isInstanceOfSatisfying(BusinessException.class, error -> {
assertThat(error.status()).isEqualTo(400);
assertThat(error.code()).isEqualTo("STEP_INVALID_FILE");
});
}
}