项目整机拼装与资源文件上传

This commit is contained in:
lxp
2026-10-28 17:49:54 +08:00
parent 4e7d11d586
commit 0dd39eb23d
32 changed files with 964 additions and 29 deletions
+2
View File
@@ -27,6 +27,8 @@ OSS_LEGACY_PUBLIC_BASE_URLS=''
URDF_MAX_FILE_BYTES=52428800
URDF_MAX_TOTAL_BYTES=209715200
URDF_MAX_FILES=500
# 管理端四类项目资源直传签名有效期;不设置应用层文件大小、数量及总量配额。
PROJECT_RESOURCE_SIGNATURE_VALIDITY=1h
# AI 默认关闭;启用后密钥仅在后端使用。两个环境均支持这些变量。
AI_ENABLED=false
ARK_API_KEY=''
+91
View File
@@ -338,6 +338,12 @@ interface ProjectView {
coverUrl: string | null;
urdfMainUrl: string | null;
urdfBaseUrl: string | null;
modelGlbUrl: string | null;
modelPartsBaseUrl: string | null;
modelPartsFiles?: ResourceFileView[]; // 详情返回;列表省略
assemblyJsonUrl: string | null;
walkingResourcesBaseUrl: string | null;
walkingResourcesFiles?: ResourceFileView[]; // 详情返回;列表省略
projectType: string;
owner: { userCode: string | null; nickname: string };
repoStatus: RepoStatus;
@@ -362,6 +368,12 @@ interface ManagedProjectView {
deleted: boolean;
}
interface ResourceFileView {
relativePath: string;
sizeBytes: number;
url: string;
}
interface TypeView {
code: string;
name: string;
@@ -433,6 +445,12 @@ interface TypeView {
"coverUrl": null,
"urdfMainUrl": null,
"urdfBaseUrl": null,
"modelGlbUrl": null,
"modelPartsBaseUrl": null,
"modelPartsFiles": [],
"assemblyJsonUrl": null,
"walkingResourcesBaseUrl": null,
"walkingResourcesFiles": [],
"projectType": "WHEELED",
"owner": { "userCode": "c3d4e5f6a7b8", "nickname": "社区官方" },
"repoStatus": "READY",
@@ -804,6 +822,78 @@ async function uploadUrdf(projectCode: string, selected: SelectedFile[]): Promis
任一文件失败时不要确认绑定;保留失败状态并检查上传结果。签名过期可重新申请完整批次;同批次已成功文件禁止覆盖,不要无条件重传。移除 URDF 只清除绑定,不物理删除已上传对象。
### 8.3 整机、零件、拼装步骤及行走资源
四类资源均为选填,只允许管理员上传。先创建项目取得 `projectCode`,项目 READY 后分别上传;每类只保存一个当前绑定,替换后立即反映到社区详情,不需要重新发布。
| resourceType | 页面入口 | 规则 | 详情字段 |
| --- | --- | --- | --- |
| `model-glb` | 选择整机 GLB | 恰好一个 `.glb`,提交文件名 | `modelGlbUrl` |
| `model-parts` | 选择零件文件夹 | 只上传 `.glb`,保留子目录 | `modelPartsBaseUrl`、`modelPartsFiles` |
| `assembly-json` | 高级导入 JSON | 恰好一个 `.json`,提交文件名,不解析业务内容 | `assemblyJsonUrl` |
| `walking-resources` | 选择行走资源文件夹 | 保留全部文件与子目录,不要求 `robot.json` | `walkingResourcesBaseUrl`、`walkingResourcesFiles` |
扩展名忽略大小写,保留原文件名大小写。文件夹相对路径须去掉所选文件夹最外层名称,不能包含绝对路径、空段、`.`、`..`、反斜杠、控制字符、冒号或 `.git` 目录。不同目录可以有同名文件,同一相对路径不能重复。空文件夹不提交;未绑定或清除时地址为 null,详情清单为 `[]`。目录基础地址以 `/` 结尾,文件 URL 已按路径段编码,不要整体二次编码。
应用不设置文件数量、目录总量和单文件业务配额,不套用 URDF 的 50 MiB / 200 MiB / 500 文件限制;OSS 表单直传自身单文件上限为 5 GB(5368709120 字节),超出返回 413 `RESOURCE_FILE_TOO_LARGE`。新资源的签名和确认接口不套用普通 JSON 正文配额;反向代理需允许实际清单大小。零字节文件可以上传,本次仅核验扩展名、路径及对象大小。
| 方法和路径 | 请求 | 返回 data |
| --- | --- | --- |
| `POST /admin-api/projects/{projectCode}/resources/{resourceType}/signature` | `{ files: ResourceFile[] }` | `ResourceUploadSignature` |
| `PUT /admin-api/projects/{projectCode}/resources/{resourceType}` | `{ batchCode, files: ResourceFile[] }` | 更新后的平铺 `ProjectView` |
| `DELETE /admin-api/projects/{projectCode}/resources/{resourceType}` | 无 body | `null`,幂等清除绑定 |
```ts
type ResourceType = 'model-glb' | 'model-parts' | 'assembly-json' | 'walking-resources';
interface ResourceFile { relativePath: string; sizeBytes: number }
interface ResourceUploadSignature {
batchCode: string;
uploadUrl: string;
expiresAt: UtcDateTime;
files: { relativePath: string; objectKey: string; formFields: Record<string, string> }[];
}
function selectResourceFiles(type: ResourceType, chosen: FileList): SelectedFile[] {
return Array.from(chosen).map(file => {
const path = file.webkitRelativePath;
const relativePath = path ? path.slice(path.indexOf('/') + 1) : file.name;
return { file, relativePath };
}).filter(item => type !== 'model-parts' || /\.glb$/i.test(item.relativePath));
}
async function uploadProjectResource(
projectCode: string, type: ResourceType, selected: SelectedFile[],
): Promise<ProjectView> {
if (!selected.length) throw new Error('请选择资源文件');
const files: ResourceFile[] = selected.map(item => ({
relativePath: item.relativePath, sizeBytes: item.file.size,
}));
const endpoint = `/projects/${projectCode}/resources/${type}`;
const signature = await requestAdmin<ResourceUploadSignature>(`${endpoint}/signature`, {
method: 'POST', body: { files },
});
const byPath = new Map(selected.map(item => [item.relativePath, item.file]));
for (const item of signature.files) {
const file = byPath.get(item.relativePath);
if (!file) throw new Error(`缺少本地文件:${item.relativePath}`);
await uploadOssFile(signature.uploadUrl, item.formFields, file);
}
return requestAdmin<ProjectView>(endpoint, {
method: 'PUT', body: { batchCode: signature.batchCode, files },
});
}
```
前端把各文件的 `formFields` 原样加入 FormData,最后追加 `file`,直接 POST OSS,不携带平台 Authorization、不手动设置 multipart Content-Type。OSS 成功为 HTTP 200、空响应体。示例复用第 7 节的 `uploadOssFile`;实际大批量上传可使用有并发上限的队列。
每次签名生成新批次,有效期默认 1 小时,可通过 `PROJECT_RESOURCE_SIGNATURE_VALIDITY` 配置,例如 `2h`;签名响应禁止缓存。对象键为 `{环境根目录}/project-resources/{projectCode}/{resourceType}/{batchCode}/{relativePath}`,test 根目录为 `vibemake_dev`,prod 为 `vibemake`。Policy 精确绑定对象键、声明大小、MIME、成功状态,并禁止覆盖。全部上传成功后,提交原批次及原文件清单;后端逐文件 HEAD 核验存在和大小,再一次性更新当前绑定。
上传失败、取消或确认失败均保留原绑定;签名过期重新申请批次。确认缺文件返回 400 `RESOURCE_FILE_MISSING`,大小不符返回 400 `RESOURCE_FILE_SIZE_MISMATCH`;路径、扩展名、类型或清单错误返回 400,项目不存在或已删除返回 404,仓库未 READY 返回 409,OSS 未开启返回 503。清除绑定在 OSS 关闭时仍可执行。
Fork 只复制这四类资源的八个数据库字段,复用原 OSS 对象,不对这些资源执行 HEAD、复制或下载,OSS 暂不可用也不影响该继承过程。副本与来源后续各自替换绑定,替换、清除和删除项目均保留旧对象。已有 URDF 仍使用原独立复制规则。
部署前执行 [项目资源增量 SQL](admin-database.md#已有数据库增加四类项目资源字段)。RAM 用户对当前环境 `project-resources/*` 增加 `oss:PutObject` 和 `oss:GetObject` 权限;配置资源公开读取或公开 CDN,真实前端 Origin 的 OSS CORS 允许 POST、GET、HEAD,允许 Content-Type。无需为新增资源开通复制或删除权限,应用不修改 Bucket 配置。
## 9. GitHub 项目迁移
页面流程:**输入地址 → 预检 → 补充信息、可选上传封面和 URDF → 创建任务并绑定资源 → 查看阶段 → 检查文件 → 手动发布**。封面和 URDF 均可在迁移表单内完成,无需等迁移后再去项目列表寻找项目。
@@ -1096,6 +1186,7 @@ requestId 同时可能出现在响应头 `X-Request-Id`。错误提示可附“
| 文件编辑/上传 | 取得当前 SHA → 提交或上传 → 保存新 SHA → 刷新目录和历史 |
| 发布确认 | 读取并检查当前 main 文件 → PUT publication → 刷新详情 |
| URDF 设置 | 选目录 → signature → 逐文件 OSS 上传 → PUT urdf → 刷新详情 |
| 整机、零件、拼装步骤、行走资源 | 创建项目并等待 READY → resources/{resourceType}/signature → 逐文件 OSS 上传 → PUT resources/{resourceType} |
| 迁移创建 | preview → 补充表单 → 可选封面及迁移 URDF 签名/OSS 上传 → POST project-imports(含 urdf)→ 进入任务页 |
| 迁移任务 | 详情轮询 → SUCCEEDED 后检查项目 → 明确发布 |
+33 -1
View File
@@ -6,7 +6,39 @@
## 项目字段收敛
project 只新增 moderation_reason(管理员下架原因)和 repo_write_status(跨进程写入状态)两个字段。下架直接使用现有 publish_status=BLOCKED,不再增加独立治理状态。GitHub来源标识、地址、默认分支和许可证属于迁移任务,统一放在 project_import_task;第一版只支持GitHub,不存平台类型,也不额外区分用户创建与后台创建。
治理相关字段为 moderation_reason(管理员下架原因)和 repo_write_status(跨进程写入状态)。下架直接使用现有 publish_status=BLOCKED,不再增加独立治理状态。GitHub来源标识、地址、默认分支和许可证属于迁移任务,统一放在 project_import_task;第一版只支持GitHub,不存平台类型,也不额外区分用户创建与后台创建。
## 已有数据库增加四类项目资源字段
在已完成原有部署的数据库中手工执行以下 SQL,再升级社区端、管理端和导入工具。新建数据库的 `database.md` 建表语句已包含这些字段,不要重复执行。
```sql
ALTER TABLE project
ADD COLUMN model_glb_object_key VARCHAR(512)
CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT ''
COMMENT '整机GLB环境相对路径,不含域名和环境根目录',
ADD COLUMN model_glb_size_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0
COMMENT '整机GLB文件大小,单位字节',
ADD COLUMN model_parts_prefix VARCHAR(512)
CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT ''
COMMENT '零件模型环境相对目录,以斜杠开头和结尾',
ADD COLUMN model_parts_files JSON NOT NULL DEFAULT (JSON_ARRAY())
COMMENT '零件GLB文件清单,包含relativePath和sizeBytes',
ADD COLUMN assembly_json_object_key VARCHAR(512)
CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT ''
COMMENT '拼装步骤JSON环境相对路径,不含域名和环境根目录',
ADD COLUMN assembly_json_size_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0
COMMENT '拼装步骤JSON文件大小,单位字节',
ADD COLUMN walking_resources_prefix VARCHAR(512)
CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT ''
COMMENT '行走资源环境相对目录,以斜杠开头和结尾',
ADD COLUMN walking_resources_files JSON NOT NULL DEFAULT (JSON_ARRAY())
COMMENT '行走资源文件清单,包含relativePath和sizeBytes';
```
路径为未编码的 `/project-resources/{projectCode}/{resourceType}/{batchCode}/{relativePath}`,不含域名和环境根目录。目录字段以 `/` 结尾,清单内路径相对于对应目录。未绑定或清除后路径为 `''`、大小为 `0`、清单为 `[]`。
Fork 直接复制这八个字段并复用原 OSS 对象,因此副本保存的路径可以包含源项目编码。读取不校验路径属于当前项目;新上传和确认只能使用当前项目的目录。替换、清除和删除项目均保留 OSS 对象,避免破坏其他副本的引用。后续如增加资源清理任务,必须检查所有项目的共享引用。
## 已有数据库增加字段
+4 -1
View File
@@ -1,6 +1,6 @@
# Vibemake 管理端部署与运维
管理端为独立 Spring Boot 应用 `vibemake-adminapi`,默认端口 8994,接口前缀 `/admin-api`。项目文件复用现有 Gitea 私有组织仓库,封面与 URDF 复用 OSS;不需要新建 Gitea 账号或组织。前端对接见 [管理后台接口文档](admin-api.md)。
管理端为独立 Spring Boot 应用 `vibemake-adminapi`,默认端口 8994,接口前缀 `/admin-api`。项目文件复用现有 Gitea 私有组织仓库,封面、URDF 及四类模型/行走资源复用 OSS;不需要新建 Gitea 账号或组织。前端对接见 [管理后台接口文档](admin-api.md)。
## 部署与初始化
@@ -25,6 +25,7 @@ SPRING_PROFILES_ACTIVE=prod java -jar vibemake-adminapi/target/vibemake-adminapi
| --- | --- |
| `ADMIN_SERVER_PORT` | 8994;独立于社区端 `SERVER_PORT` |
| `ADMIN_ALLOWED_ORIGINS` | 空;完整前端来源用英文逗号分隔,不使用通配符 |
| `PROJECT_RESOURCE_SIGNATURE_VALIDITY` | 1h;整机、零件、拼装 JSON、行走资源直传签名有效期 |
| `ADMIN_IMPORT_WORKER_ENABLED` | true;维护期间可关闭消费 |
| `ADMIN_IMPORT_CONCURRENCY` | 2,范围 1~4 |
| `ADMIN_MIGRATION_TIMEOUT` | 30m,仅迁移请求使用,普通 Gitea 超时不变 |
@@ -59,6 +60,8 @@ project 表只新增 moderation_reason 和 repo_write_status,来源标识、
迁移表单支持预上传封面及 URDF。URDF 使用现有 OSS 配置与限额,创建任务前检查对象,并使用 project 既有 URDF 字段保存,不需要新增数据库变更。上传后未提交表单的 OSS 对象不会自动清理,沿用现有资源生命周期。
整机 GLB、零件 GLB 文件夹、拼装 JSON 和行走资源在项目 READY 后通过项目资源接口上传。升级前先手工执行 [八字段增量 SQL](admin-database.md#已有数据库增加四类项目资源字段)。RAM 用户需拥有当前环境 `project-resources/*` 的 `oss:PutObject`、`oss:GetObject` 权限,配置公开读取/CDN 及真实前端 Origin 的 POST、GET、HEAD CORS。新资源签名和确认清单不设置应用正文配额,反向代理须允许实际清单大小;文件内容由浏览器直接上传 OSS。Fork 对这四类资源只继承数据库引用,不调用 OSS;旧对象保留,后续任何清理机制均需检查共享引用。
## 异常核对
- 超时、进程在 MIGRATING 阶段中断或远端结果不明确时进入 REVIEW。禁止自动重发,也不能仅凭仓库存在判定成功。
+90
View File
File diff suppressed because one or more lines are too long
+8
View File
@@ -32,6 +32,14 @@ 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资源环境相对目录,包含上传批次,以斜杠开头和结尾',
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 '零件模型环境相对目录,以斜杠开头和结尾',
model_parts_files JSON NOT NULL DEFAULT (JSON_ARRAY()) COMMENT '零件GLB文件清单,包含relativePath和sizeBytes',
assembly_json_object_key VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '拼装步骤JSON环境相对路径,不含域名和环境根目录',
assembly_json_size_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '拼装步骤JSON文件大小,单位字节',
walking_resources_prefix VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '行走资源环境相对目录,以斜杠开头和结尾',
walking_resources_files JSON NOT NULL DEFAULT (JSON_ARRAY()) COMMENT '行走资源文件清单,包含relativePath和sizeBytes',
project_type VARCHAR(64) NOT NULL COMMENT '配置中的项目类型编码',
gitea_repo_id BIGINT NULL COMMENT 'Gitea 仓库标识',
gitea_owner VARCHAR(100) NOT NULL COMMENT 'Gitea 仓库所属组织',
+28
View File
@@ -195,6 +195,12 @@ export interface ProjectType {
name: string;
}
export interface ResourceFileView {
relativePath: string;
sizeBytes: number;
url: string; // 已编码的完整公开地址
}
export interface Project {
projectCode: string;
name: string;
@@ -202,6 +208,12 @@ export interface Project {
coverUrl: string | null; // 固定公开 HTTPS 图片地址,支持 OSS/CDN 和外部图片
urdfMainUrl: string | null; // 当前 URDF 主文件公开地址,无资源时为 null
urdfBaseUrl: string | null; // 当前资源包公开目录地址,以 / 结尾,无资源时为 null
modelGlbUrl: string | null;
modelPartsBaseUrl: string | null; // 目录地址以 / 结尾
modelPartsFiles?: ResourceFileView[]; // 详情始终返回,列表省略
assemblyJsonUrl: string | null;
walkingResourcesBaseUrl: string | null;
walkingResourcesFiles?: ResourceFileView[]; // 详情始终返回,列表省略
projectType: string;
owner: PublicUser;
repoStatus: 'CREATING' | 'READY' | 'FAILED';
@@ -300,6 +312,12 @@ export interface Comment {
"coverUrl": "https://images.example.com/vibemake/project-covers/11111111111111111111111111111111/20261015/33333333333333333333333333333333.png",
"urdfMainUrl": null,
"urdfBaseUrl": null,
"modelGlbUrl": null,
"modelPartsBaseUrl": null,
"modelPartsFiles": [],
"assemblyJsonUrl": null,
"walkingResourcesBaseUrl": null,
"walkingResourcesFiles": [],
"projectType": "GENERAL",
"owner": {
"userCode": "111111111111",
@@ -698,6 +716,16 @@ async function clearProjectUrdf(projectCode: string, token: string) {
}
```
## 管理员配置的模型、拼装及行走资源
`GET /api/workspace/projects/{projectCode}` 和 `GET /api/community/projects/{projectCode}` 返回当前绑定的整机 GLB、零件目录及清单、拼装步骤 JSON、行走资源目录及清单,字段见 Project 定义。未绑定时地址为 null,文件清单为 `[]`。列表保留四类资源地址,但省略两个文件清单;需要加载零件或行走资源时请求详情。
直接使用 `modelGlbUrl` 加载整机、`assemblyJsonUrl` 读取步骤 JSON,文件夹逐项使用清单中的 `url`,不需要列举 OSS 目录。`relativePath` 保留子目录及文件名大小写,目录地址以 `/` 结尾,URL 已编码,不要再次整体编码。行走资源不保证含 `robot.json`,需要由使用方自行识别;后端不校验拼装 JSON 的业务结构。
这四类资源只由管理端上传、替换和清除,用户端不提供上传接口,项目创建或 PATCH 也不接收对应资源字段。管理端成功确认后立即更新社区详情,不依赖仓库发布版本。
Fork 直接继承这四类资源的数据库字段和原资源地址,不访问 OSS,不复制或下载这些文件,因此大资源不会增加 Fork 的文件传输耗时。来源和副本独立保存绑定,任一方替换、清除或删除项目都不会改变另一方的绑定;旧 OSS 对象保留。URDF 继续按照上一节规则独立复制。
## 目录和文件读取
### 工作区目录
@@ -0,0 +1,42 @@
package com.linkerbot.vibemake.adminapi.controller;
import com.linkerbot.vibemake.bean.Contracts.*;
import com.linkerbot.vibemake.common.ApiResponse;
import com.linkerbot.vibemake.service.admin.AdminProjectResourceService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin-api/projects/{projectCode}/resources/{resourceType}")
public class AdminProjectResourceController {
private final AdminProjectResourceService resources;
@PostMapping("/signature")
@Operation(summary = "生成项目资源 OSS POST V4 直传签名")
public ResponseEntity<ApiResponse<ResourceUploadSignature>> signature(
@PathVariable String projectCode, @PathVariable String resourceType,
@Valid @RequestBody ResourceUploadInput input) {
return ResponseEntity.ok().cacheControl(CacheControl.noStore())
.body(ApiResponse.success(resources.signature(projectCode, resourceType, input)));
}
@PutMapping
@Operation(summary = "核验并绑定项目资源")
public ApiResponse<ProjectView> confirm(
@PathVariable String projectCode, @PathVariable String resourceType,
@Valid @RequestBody ResourceConfirmInput input) {
return ApiResponse.success(resources.confirm(projectCode, resourceType, input));
}
@DeleteMapping
@Operation(summary = "清除项目资源绑定,保留 OSS 对象")
public ApiResponse<Void> clear(@PathVariable String projectCode, @PathVariable String resourceType) {
resources.clear(projectCode, resourceType);
return ApiResponse.success(null);
}
}
@@ -75,6 +75,8 @@ vibemake:
access-key-secret: ${OSS_ACCESS_KEY_SECRET:}
public-base-url: ${OSS_PUBLIC_BASE_URL:https://linkerbot.oss-cn-beijing.aliyuncs.com}
legacy-public-base-urls: ${OSS_LEGACY_PUBLIC_BASE_URLS:}
project-resources:
signature-validity: ${PROJECT_RESOURCE_SIGNATURE_VALIDITY:1h}
urdf:
max-file-bytes: ${URDF_MAX_FILE_BYTES:52428800}
max-total-bytes: ${URDF_MAX_TOTAL_BYTES:209715200}
@@ -371,6 +371,124 @@ class AdminWorkflowTest {
oss.setEnabled(true);
}
private JsonNode resourceSignature(String code, String type, List<Map<String, Object>> files) throws Exception {
MvcResult result = mvc.perform(signed(body(post("/admin-api/projects/" + code + "/resources/" + type + "/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");
}
private void storeResourceFiles(JsonNode signature, List<Map<String, Object>> files) {
for (int index = 0; index < files.size(); index++) {
long size = ((Number) files.get(index).get("sizeBytes")).longValue();
ossObjects.objects.put(signature.path("files").get(index).path("objectKey").asText(),
new OssObjectStore.Metadata(size, "resource-etag-" + index));
}
}
@Test
void fourResourceTypesUploadReplaceAndClearIndependentlyWhileKeepingOssObjects() throws Exception {
String code = createProject();
String endpoint = "/admin-api/projects/" + code;
JsonNode empty = request(signed(get(endpoint)), 200).path("data").path("project");
assertThat(empty.path("modelGlbUrl").isNull()).isTrue();
assertThat(empty.path("modelPartsFiles").isEmpty()).isTrue();
assertThat(empty.path("walkingResourcesFiles").isEmpty()).isTrue();
enableOss();
Map<String, List<Map<String, Object>>> resources = new LinkedHashMap<>();
resources.put("model-glb", List.of(Map.of("relativePath", "整机.GLb", "sizeBytes", 52428801L)));
resources.put("model-parts", List.of(Map.of("relativePath", "左/a.glb", "sizeBytes", 1L),
Map.of("relativePath", "右/a.glb", "sizeBytes", 2L)));
resources.put("assembly-json", List.of(Map.of("relativePath", "steps.JSON", "sizeBytes", 8L)));
resources.put("walking-resources", List.of(Map.of("relativePath", "策略/a.bin", "sizeBytes", 0L)));
for (Map.Entry<String, List<Map<String, Object>>> resource : resources.entrySet()) {
JsonNode signature = resourceSignature(code, resource.getKey(), resource.getValue());
storeResourceFiles(signature, resource.getValue());
request(signed(body(put(endpoint + "/resources/" + resource.getKey()),
Map.of("batchCode", signature.path("batchCode").asText(), "files", resource.getValue()))), 200);
}
JsonNode bound = request(signed(get(endpoint)), 200).path("data").path("project");
assertThat(bound.path("modelGlbUrl").asText()).endsWith("/%E6%95%B4%E6%9C%BA.GLb");
assertThat(bound.path("modelPartsFiles").size()).isEqualTo(2);
assertThat(bound.path("modelPartsFiles").get(0).path("url").asText()).endsWith("/%E5%B7%A6/a.glb");
assertThat(bound.path("walkingResourcesFiles").get(0).path("relativePath").asText()).isEqualTo("策略/a.bin");
assertThat(bound.path("assemblyJsonUrl").asText()).endsWith("/steps.JSON");
JsonNode listing = request(signed(get("/admin-api/projects")), 200).path("data").path("items").get(0).path("project");
assertThat(listing.has("modelPartsFiles")).isFalse();
assertThat(listing.has("walkingResourcesFiles")).isFalse();
assertThat(jdbc.queryForObject("SELECT model_glb_object_key FROM project WHERE project_code=?", String.class, code))
.startsWith("/project-resources/" + code + "/model-glb/");
JsonNode replacement = resourceSignature(code, "model-glb", resources.get("model-glb"));
storeResourceFiles(replacement, resources.get("model-glb"));
JsonNode updated = request(signed(body(put(endpoint + "/resources/model-glb"),
Map.of("batchCode", replacement.path("batchCode").asText(), "files", resources.get("model-glb")))), 200).path("data");
assertThat(updated.path("modelGlbUrl")).isNotEqualTo(bound.path("modelGlbUrl"));
assertThat(updated.path("modelPartsFiles")).isEqualTo(bound.path("modelPartsFiles"));
int objectCount = ossObjects.objects.size();
oss.setEnabled(false);
for (String type : resources.keySet()) {
request(signed(delete(endpoint + "/resources/" + type)), 200);
request(signed(delete(endpoint + "/resources/" + type)), 200);
}
JsonNode cleared = request(signed(get(endpoint)), 200).path("data").path("project");
assertThat(cleared.path("modelGlbUrl").isNull()).isTrue();
assertThat(cleared.path("assemblyJsonUrl").isNull()).isTrue();
assertThat(cleared.path("modelPartsFiles").isEmpty()).isTrue();
assertThat(cleared.path("walkingResourcesFiles").isEmpty()).isTrue();
assertThat(ossObjects.objects).hasSize(objectCount);
}
@Test
void resourceConfirmationFailuresPreserveBindingAndCannotUseAnotherProjectsObjects() throws Exception {
String code = createProject();
String endpoint = "/admin-api/projects/" + code + "/resources/model-glb";
List<Map<String, Object>> files = List.of(Map.of("relativePath", "a.glb", "sizeBytes", 10L));
request(body(post(endpoint + "/signature"), Map.of("files", files)), 401);
String ordinaryToken = tokens.issue(ordinary.getId(), ordinary.getSessionVersion());
request(body(post(endpoint + "/signature"), Map.of("files", files))
.header("Authorization", "Bearer " + ordinaryToken), 403);
request(signed(body(post(endpoint + "/signature"), Map.of("files", files))), 503);
enableOss();
JsonNode signature = resourceSignature(code, "model-glb", files);
Map<String, Object> confirmation = Map.of("batchCode", signature.path("batchCode").asText(), "files", files);
assertThat(request(signed(body(put(endpoint), confirmation)), 400).path("code").asText()).isEqualTo("RESOURCE_FILE_MISSING");
storeResourceFiles(signature, files);
JsonNode bound = request(signed(body(put(endpoint), confirmation)), 200).path("data");
JsonNode next = resourceSignature(code, "model-glb", files);
String nextKey = next.path("files").get(0).path("objectKey").asText();
ossObjects.objects.put(nextKey, new OssObjectStore.Metadata(11L, "wrong-size"));
assertThat(request(signed(body(put(endpoint), Map.of("batchCode", next.path("batchCode").asText(), "files", files))), 400)
.path("code").asText()).isEqualTo("RESOURCE_FILE_SIZE_MISMATCH");
assertThat(request(signed(get("/admin-api/projects/" + code)), 200).path("data").path("project").path("modelGlbUrl"))
.isEqualTo(bound.path("modelGlbUrl"));
String another = createProject();
request(signed(body(put("/admin-api/projects/" + another + "/resources/model-glb"), confirmation)), 400);
request(signed(body(post(endpoint + "/signature"), Map.of("files", List.of(Map.of("relativePath", "a.txt", "sizeBytes", 1))))), 400);
request(signed(body(post(endpoint + "/signature"), Map.of("files", List.of()))), 400);
request(signed(body(post(endpoint + "/signature"), Map.of("files", List.of(Map.of("relativePath", "../a.glb", "sizeBytes", 1))))), 400);
request(signed(body(post(endpoint + "/signature"), Map.of("files", List.of(Map.of("relativePath", "a.glb", "sizeBytes", 5368709121L))))), 413);
request(signed(body(post("/admin-api/projects/" + code + "/resources/unknown/signature"), Map.of("files", files))), 400);
request(signed(delete("/admin-api/projects/" + code)), 200);
request(signed(body(put(endpoint), confirmation)), 404);
}
@Test
void resourceManifestsLargerThanOrdinaryJsonBodyLimitAreAccepted() throws Exception {
String code = createProject();
enableOss();
List<Map<String, Object>> files = new ArrayList<>();
for (int index = 0; index < 1200; index++)
files.add(Map.of("relativePath", "x".repeat(220) + index + ".glb", "sizeBytes", 300000000L));
assertThat(json.writeValueAsBytes(Map.of("files", files)).length).isGreaterThan(262144);
JsonNode signature = resourceSignature(code, "model-parts", files);
assertThat(signature.path("files").size()).isEqualTo(1200);
storeResourceFiles(signature, files);
JsonNode bound = request(signed(body(put("/admin-api/projects/" + code + "/resources/model-parts"),
Map.of("batchCode", signature.path("batchCode").asText(), "files", files))), 200).path("data");
assertThat(bound.path("modelPartsFiles").size()).isEqualTo(1200);
}
private List<Map<String, Object>> urdfFiles() {
return List.of(
Map.of("relativePath", "urdf/机械臂.URDF", "sizeBytes", 12),
@@ -10,15 +10,23 @@ final class FakeOssObjectStore implements OssObjectStore {
final Map<String, byte[]> objects = new ConcurrentHashMap<>();
boolean failNextCopy;
Runnable afterHead;
int headCalls;
int copyCalls;
boolean unavailable;
void reset() {
objects.clear();
failNextCopy = false;
afterHead = null;
headCalls = 0;
copyCalls = 0;
unavailable = false;
}
@Override
public Metadata head(String objectKey) {
headCalls++;
if (unavailable) throw new BusinessException(502, "OSS_ERROR", "OSS 暂不可用");
byte[] bytes = objects.get(objectKey);
if (bytes == null)
throw new BusinessException(400, "URDF_FILE_MISSING", "URDF 文件未上传完整,请重新上传");
@@ -33,6 +41,7 @@ final class FakeOssObjectStore implements OssObjectStore {
@Override
public void copy(String sourceKey, String targetKey, String sourceEtag) {
copyCalls++;
if (failNextCopy) {
failNextCopy = false;
throw new BusinessException(502, "OSS_ERROR", "OSS 资源访问失败,请稍后重试");
@@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.security.CaptchaService;
import com.linkerbot.vibemake.service.admin.AdminProjectResourceService;
import com.linkerbot.vibemake.service.support.ProjectActors;
import com.linkerbot.vibemake.service.config.VibemakeProperties;
import com.linkerbot.vibemake.service.config.OssProperties;
import com.linkerbot.vibemake.service.config.UrdfProperties;
@@ -90,6 +92,7 @@ class WorkflowTest {
@Autowired FakeOssObjectStore ossObjects;
@Autowired UrdfProperties urdfLimits;
@Autowired FakeGitSnapshotCopier snapshots;
@Autowired AdminProjectResourceService projectResources;
@BeforeEach
void reset() throws Exception {
@@ -977,6 +980,77 @@ class WorkflowTest {
.isEqualTo(source.path("urdfMainUrl"));
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void newProjectResourcesAreReturnedInDetailsAndForkedWithoutAnyOssAccess(boolean ossEnabled) throws Exception {
String owner = login("resource-source@example.com");
String other = login("resource-fork@example.com");
String code = create(owner, "资源来源");
enableUrdfOss();
oss.setEnabled(ossEnabled);
ossObjects.unavailable = true;
String base = "/project-resources/" + code + "/";
String batch = "a".repeat(32);
String parts = base + "model-parts/" + batch + "/";
String walking = base + "walking-resources/" + batch + "/";
String glb = base + "model-glb/" + batch + "/整机.glb";
String assembly = base + "assembly-json/" + batch + "/steps.json";
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);
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();
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"));
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.has("modelPartsFiles")).isFalse();
assertThat(listing.has("walkingResourcesFiles")).isFalse();
}
JsonNode fork = perform(body(auth(post("/api/community/projects/" + code + "/forks"), other),
Map.of("name", "资源副本", "publishedCommitSha", sha)), 200).path("data");
String target = fork.path("projectCode").asText();
assertThat(fork.path("repoStatus").asText()).isEqualTo("READY");
for (String field : List.of("modelGlbUrl", "modelPartsBaseUrl", "modelPartsFiles", "assemblyJsonUrl",
"walkingResourcesBaseUrl", "walkingResourcesFiles"))
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")) {
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));
}
// 模拟来源资源被管理员替换,副本仍持有原批次。
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"));
try (ProjectActors.Scope scope = ProjectActors.enter(new ProjectActors.AdminActor("a".repeat(32), "测试管理员"))) {
projectResources.clear(code, "model-parts");
projectResources.clear(target, "assembly-json");
}
JsonNode independentSource = perform(auth(get(ownerEndpoint), owner), 200).path("data");
JsonNode independentTarget = perform(auth(get("/api/workspace/projects/" + target), other), 200).path("data");
assertThat(independentSource.path("modelPartsFiles").isEmpty()).isTrue();
assertThat(independentSource.path("assemblyJsonUrl")).isEqualTo(source.path("assemblyJsonUrl"));
assertThat(independentTarget.path("modelPartsFiles")).isEqualTo(fork.path("modelPartsFiles"));
assertThat(independentTarget.path("assemblyJsonUrl").isNull()).isTrue();
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"));
assertThat(ossObjects.headCalls).isZero();
assertThat(ossObjects.copyCalls).isZero();
}
@Test
void coverSignaturesAuthenticateValidateAndReturnDirectUploadForms() throws Exception {
String endpoint = "/api/uploads/project-covers/signature";
+4
View File
@@ -8,6 +8,10 @@
</parent>
<artifactId>vibemake-bean</artifactId>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
@@ -1,5 +1,6 @@
package com.linkerbot.vibemake.bean;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
@@ -67,6 +68,26 @@ public final class Contracts {
public record TypeView(String code, String name) {}
public record ResourceFile(
@NotBlank String relativePath, @NotNull @PositiveOrZero Long sizeBytes) {}
public record ResourceUploadInput(@NotEmpty List<@NotNull @Valid ResourceFile> files) {}
public record ResourceConfirmInput(
@NotBlank @Pattern(regexp = "[a-f0-9]{32}") String batchCode,
@NotEmpty List<@NotNull @Valid ResourceFile> files) {}
public record ResourceFileSignature(
String relativePath, String objectKey, Map<String, String> formFields) {}
public record ResourceUploadSignature(
String batchCode,
String uploadUrl,
LocalDateTime expiresAt,
List<ResourceFileSignature> files) {}
public record ResourceFileView(String relativePath, long sizeBytes, String url) {}
public record ProjectView(
String projectCode,
String name,
@@ -74,6 +95,12 @@ public final class Contracts {
String coverUrl,
String urdfMainUrl,
String urdfBaseUrl,
String modelGlbUrl,
String modelPartsBaseUrl,
@JsonInclude(JsonInclude.Include.NON_NULL) List<ResourceFileView> modelPartsFiles,
String assemblyJsonUrl,
String walkingResourcesBaseUrl,
@JsonInclude(JsonInclude.Include.NON_NULL) List<ResourceFileView> walkingResourcesFiles,
String projectType,
PublicUser owner,
String repoStatus,
@@ -19,6 +19,14 @@ public class Project extends BaseEntity {
private String urdfMainObjectKey = "";
private String urdfFiles = "[]";
private String urdfPrefix = "";
private String modelGlbObjectKey = "";
private Long modelGlbSizeBytes = 0L;
private String modelPartsPrefix = "";
private String modelPartsFiles = "[]";
private String assemblyJsonObjectKey = "";
private Long assemblyJsonSizeBytes = 0L;
private String walkingResourcesPrefix = "";
private String walkingResourcesFiles = "[]";
private String projectType;
private Long giteaRepoId;
private String giteaOwner;
@@ -4,6 +4,7 @@ import com.linkerbot.vibemake.service.config.VibemakeProperties;
import com.linkerbot.vibemake.service.config.MybatisConfiguration;
import com.linkerbot.vibemake.service.config.OssProperties;
import com.linkerbot.vibemake.service.oss.OssResourcePaths;
import com.linkerbot.vibemake.service.oss.ProjectResourceFiles;
import com.linkerbot.vibemake.service.gitea.RestGiteaClient;
import com.linkerbot.vibemake.service.impl.ProjectServiceImpl;
import com.linkerbot.vibemake.service.impl.UserServiceImpl;
@@ -26,6 +27,7 @@ import org.springframework.context.annotation.Import;
UserServiceImpl.class,
ProjectLocks.class,
OssProperties.class,
OssResourcePaths.class
OssResourcePaths.class,
ProjectResourceFiles.class
})
public class DatabaseConfiguration {}
@@ -195,7 +195,8 @@ public class AdminManagementService {
checkKeyword(keyword);
LambdaQueryWrapper<Project> query =
new LambdaQueryWrapper<Project>().eq(Project::getDeleted, false);
query.select(Project.class, field -> !"urdfFiles".equals(field.getProperty()));
query.select(Project.class, field -> !Set.of("urdfFiles", "modelPartsFiles", "walkingResourcesFiles")
.contains(field.getProperty()));
if (ownerCode != null) query.eq(Project::getOwnerUserId, user(ownerCode).getId());
query.eq(type != null, Project::getProjectType, type)
.eq(publishStatus != null, Project::getPublishStatus, publishStatus)
@@ -0,0 +1,87 @@
package com.linkerbot.vibemake.service.admin;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.linkerbot.vibemake.bean.Contracts.*;
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.ProjectResources;
import com.linkerbot.vibemake.service.oss.ProjectResourceType;
import com.linkerbot.vibemake.service.support.ProjectActors;
import com.linkerbot.vibemake.service.support.ProjectLocks;
import com.linkerbot.vibemake.service.support.ProjectSupport;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class AdminProjectResourceService {
private static final Logger log = LoggerFactory.getLogger(AdminProjectResourceService.class);
private final ProjectService projects;
private final ProjectMapper mapper;
private final ProjectResources resources;
private final ProjectLocks locks;
private void requireAdmin() {
if (!ProjectActors.isAdmin()) throw new BusinessException(403, "FORBIDDEN", "仅管理员可管理项目资源");
}
public ResourceUploadSignature signature(String projectCode, String resourceType, ResourceUploadInput input) {
requireAdmin();
ProjectResourceType type = ProjectResourceType.from(resourceType);
projects.readyOwner(0, projectCode);
return resources.sign(projectCode, type, input.files());
}
public ProjectView confirm(String projectCode, String resourceType, ResourceConfirmInput input) {
requireAdmin();
ProjectResourceType type = ProjectResourceType.from(resourceType);
projects.readyOwner(0, projectCode);
resources.verify(projectCode, type, input.batchCode(), input.files());
String prefix = resources.prefix(projectCode, type, input.batchCode());
String manifest = resources.manifest(input.files());
return locks.serial(projectCode, () -> {
Project project = projects.readyOwner(0, projectCode);
persist(project, type, prefix, input.files(), manifest);
log.info("绑定项目资源 projectCode={} resourceType={} batchCode={} fileCount={}",
projectCode, type.code(), input.batchCode(), input.files().size());
return projects.view(projects.owner(0, projectCode), null);
});
}
public void clear(String projectCode, String resourceType) {
requireAdmin();
ProjectResourceType type = ProjectResourceType.from(resourceType);
locks.serial(projectCode, () -> {
persist(projects.owner(0, projectCode), type, "", List.of(), "[]");
log.info("清除项目资源绑定 projectCode={} resourceType={}", projectCode, type.code());
return null;
});
}
private void persist(Project project, ProjectResourceType type, String prefix,
List<ResourceFile> files, String manifest) {
LambdaUpdateWrapper<Project> update = new LambdaUpdateWrapper<Project>()
.eq(Project::getProjectCode, project.getProjectCode())
.eq(Project::getOwnerUserId, project.getOwnerUserId())
.eq(Project::getDeleted, false)
.set(Project::getUpdatedAt, ProjectSupport.now());
String key = prefix.isEmpty() ? "" : prefix + files.getFirst().relativePath();
long size = files.isEmpty() ? 0L : files.getFirst().sizeBytes();
switch (type) {
case MODEL_GLB -> update.set(Project::getModelGlbObjectKey, key)
.set(Project::getModelGlbSizeBytes, size);
case MODEL_PARTS -> update.set(Project::getModelPartsPrefix, prefix)
.set(Project::getModelPartsFiles, manifest);
case ASSEMBLY_JSON -> update.set(Project::getAssemblyJsonObjectKey, key)
.set(Project::getAssemblyJsonSizeBytes, size);
case WALKING_RESOURCES -> update.set(Project::getWalkingResourcesPrefix, prefix)
.set(Project::getWalkingResourcesFiles, manifest);
}
if (mapper.update(null, update) != 1) throw BusinessException.notFound();
}
}
@@ -174,7 +174,8 @@ public class AiProjectSearch {
if (codes.isEmpty()) return List.of();
return projects.selectList(
new LambdaQueryWrapper<Project>()
.select(Project.class, field -> !"urdfFiles".equals(field.getProperty()))
.select(Project.class, field -> !Set.of("urdfFiles", "modelPartsFiles", "walkingResourcesFiles")
.contains(field.getProperty()))
.in(Project::getProjectCode, codes)
.eq(Project::getDeleted, false)
.eq(Project::getRepoStatus, "READY")
@@ -0,0 +1,26 @@
package com.linkerbot.vibemake.service.config;
import jakarta.validation.constraints.AssertTrue;
import java.time.Duration;
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.project-resources")
public class ProjectResourceProperties {
private Duration signatureValidity = Duration.ofHours(1);
@AssertTrue(message = "项目资源签名有效期必须大于零且不超过七天")
public boolean isSignatureValidityValid() {
return signatureValidity != null
&& !signatureValidity.isNegative()
&& !signatureValidity.isZero()
&& signatureValidity.compareTo(Duration.ofDays(7)) <= 0;
}
}
@@ -19,6 +19,8 @@ import com.linkerbot.vibemake.service.fork.GitSnapshotCopier;
import com.linkerbot.vibemake.service.gitea.*;
import com.linkerbot.vibemake.service.mapper.*;
import com.linkerbot.vibemake.service.oss.OssResourcePaths;
import com.linkerbot.vibemake.service.oss.ProjectResourceFiles;
import com.linkerbot.vibemake.service.oss.ProjectResourceType;
import com.linkerbot.vibemake.service.oss.UrdfResources;
import com.linkerbot.vibemake.service.state.RateLimits;
import com.linkerbot.vibemake.service.support.ProjectActors;
@@ -52,6 +54,7 @@ public class ProjectServiceImpl implements ProjectService {
private final OssResourcePaths resourcePaths;
// 离线导入只需地址转换配置,没有 URDF 时无需初始化 OSS 客户端。
private final ObjectProvider<UrdfResources> urdf;
private final ObjectProvider<ProjectResourceFiles> resources;
private final ObjectProvider<ForkExecutor> forks;
private final ObjectProvider<GitSnapshotCopier> snapshots;
@@ -116,11 +119,15 @@ public class ProjectServiceImpl implements ProjectService {
@Override
public ProjectView view(Project project, Long currentUser) {
return views(List.of(project), currentUser).getFirst();
return views(List.of(project), currentUser, true).getFirst();
}
@Override
public List<ProjectView> views(List<Project> records, Long currentUser) {
return views(records, currentUser, false);
}
private List<ProjectView> views(List<Project> records, Long currentUser, boolean includeFiles) {
if (records.isEmpty()) return List.of();
Set<Long> ids = new HashSet<>();
Set<Long> ownerIds = new HashSet<>();
@@ -177,7 +184,8 @@ public class ProjectServiceImpl implements ProjectService {
likeCounts.getOrDefault(project.getId(), 0L),
commentCounts.getOrDefault(project.getId(), 0L),
likedIds.contains(project.getId()),
imported.get(project.getProjectCode())));
imported.get(project.getProjectCode()),
includeFiles));
return List.copyOf(result);
}
@@ -188,25 +196,24 @@ public class ProjectServiceImpl implements ProjectService {
long likeCount,
long commentCount,
boolean liked,
ProjectImportTask imported) {
String coverUrl;
String urdfMainUrl;
String urdfBaseUrl;
ProjectImportTask imported,
boolean includeFiles) {
try {
coverUrl = resourcePaths.coverUrl(project.getCoverUrl());
urdfMainUrl = urdfUrl(project.getUrdfMainObjectKey());
urdfBaseUrl = urdfUrl(project.getUrdfPrefix());
} catch (BusinessException error) {
log.warn("项目资源地址生成失败 projectCode={} code={}", project.getProjectCode(), error.code());
throw error;
}
return new ProjectView(
return new ProjectView(
project.getProjectCode(),
project.getName(),
project.getDescription(),
coverUrl,
urdfMainUrl,
urdfBaseUrl,
resourcePaths.coverUrl(project.getCoverUrl()),
urdfUrl(project.getUrdfMainObjectKey()),
urdfUrl(project.getUrdfPrefix()),
resourcePaths.publicUrl(project.getModelGlbObjectKey()),
resourcePaths.publicUrl(project.getModelPartsPrefix()),
includeFiles ? resourceFiles(project.getModelPartsPrefix(), project.getModelPartsFiles(),
ProjectResourceType.MODEL_PARTS) : null,
resourcePaths.publicUrl(project.getAssemblyJsonObjectKey()),
resourcePaths.publicUrl(project.getWalkingResourcesPrefix()),
includeFiles ? resourceFiles(project.getWalkingResourcesPrefix(), project.getWalkingResourcesFiles(),
ProjectResourceType.WALKING_RESOURCES) : null,
project.getProjectType(),
owner,
project.getRepoStatus(),
@@ -222,12 +229,21 @@ public class ProjectServiceImpl implements ProjectService {
imported == null ? null : imported.getSourceLicense(),
project.getCreatedAt(),
project.getUpdatedAt());
} catch (BusinessException error) {
log.warn("项目资源地址生成失败 projectCode={} code={}", project.getProjectCode(), error.code());
throw error;
}
}
private String urdfUrl(String objectKey) {
return objectKey == null || objectKey.isEmpty() ? null : urdf.getObject().url(objectKey);
}
private List<ResourceFileView> resourceFiles(String prefix, String manifest, ProjectResourceType type) {
if (prefix == null || prefix.isEmpty()) return List.of();
return resources.getObject().fileViews(prefix, manifest, type);
}
@Override
public PageResponse<ProjectView> list(
Long ownerId, String type, String keyword, long page, long size, Long currentUser) {
@@ -241,7 +257,8 @@ public class ProjectServiceImpl implements ProjectService {
.lambda()
.eq(Project::getDeleted, false);
// 列表不需要资源包清单,避免为每个项目读取可能很大的 JSON 字段。
query.select(Project.class, field -> !"urdfFiles".equals(field.getProperty()));
query.select(Project.class, field -> !Set.of("urdfFiles", "modelPartsFiles", "walkingResourcesFiles")
.contains(field.getProperty()));
if (ownerId == null)
query.eq(Project::getRepoStatus, "READY")
.eq(Project::getPublishStatus, "PUBLISHED")
@@ -290,6 +307,15 @@ public class ProjectServiceImpl implements ProjectService {
if (source != null) {
project.setSourceProjectId(source.getId());
project.setSourceCommitSha(source.getPublishedCommitSha());
// 新增资源只继承数据库引用;不查询或复制 OSS,源项目替换也不会改变副本绑定。
project.setModelGlbObjectKey(source.getModelGlbObjectKey());
project.setModelGlbSizeBytes(source.getModelGlbSizeBytes());
project.setModelPartsPrefix(source.getModelPartsPrefix());
project.setModelPartsFiles(source.getModelPartsFiles());
project.setAssemblyJsonObjectKey(source.getAssemblyJsonObjectKey());
project.setAssemblyJsonSizeBytes(source.getAssemblyJsonSizeBytes());
project.setWalkingResourcesPrefix(source.getWalkingResourcesPrefix());
project.setWalkingResourcesFiles(source.getWalkingResourcesFiles());
}
for (int attempt = 1; attempt <= 5; attempt++) {
try {
@@ -113,7 +113,7 @@ public class AliyunOssObjectStore implements OssObjectStore {
}
private BusinessException failure(String operation, String key, OSSException error) {
log.warn("URDF OSS 请求失败 operation={} objectKey={} ossCode={} requestId={}",
log.warn("OSS 请求失败 operation={} objectKey={} ossCode={} requestId={}",
operation, key, error.getErrorCode(), error.getRequestId());
if ("NoSuchKey".equals(error.getErrorCode()))
return new BusinessException(400, "URDF_FILE_MISSING", "URDF 文件未上传完整,请重新上传");
@@ -123,7 +123,7 @@ public class AliyunOssObjectStore implements OssObjectStore {
}
private BusinessException transportFailure(String operation, String key, ClientException error) {
log.warn("URDF OSS 连接失败 operation={} objectKey={} errorType={}",
log.warn("OSS 连接失败 operation={} objectKey={} errorType={}",
operation, key, error.getClass().getSimpleName());
return new BusinessException(502, "OSS_ERROR", "OSS 资源访问失败,请稍后重试");
}
@@ -1,6 +1,6 @@
package com.linkerbot.vibemake.service.oss;
/** URDF 所需的 OSS 操作边界,测试使用内存实现。 */
/** 项目资源所需的 OSS 操作边界,测试使用内存实现。 */
public interface OssObjectStore {
record Metadata(long sizeBytes, String etag) {}
@@ -58,7 +58,11 @@ public class OssPostSigner {
public record SignedPost(LocalDateTime expiresAt, Map<String, String> formFields) {}
public SignedPost signObject(String key, String contentType, long sizeBytes) {
return post(key, contentType, sizeBytes, sizeBytes, Duration.ofMinutes(15), true);
return signObject(key, contentType, sizeBytes, Duration.ofMinutes(15));
}
public SignedPost signObject(String key, String contentType, long sizeBytes, Duration validity) {
return post(key, contentType, sizeBytes, sizeBytes, validity, true);
}
private SignedPost post(String key, String contentType, long minBytes, long maxBytes,
@@ -65,7 +65,8 @@ public class OssResourcePaths {
String root = oss.getRootDirectory();
if (root != null && !root.isBlank() && path.startsWith(root + "/"))
path = path.substring(root.length());
if (!path.startsWith("/project-covers/") && !path.startsWith("/urdf/")) throw invalidPath();
if (!path.startsWith("/project-covers/") && !path.startsWith("/urdf/")
&& !path.startsWith("/project-resources/")) throw invalidPath();
String relative = path.substring(1);
if (relative.endsWith("/")) relative = relative.substring(0, relative.length() - 1);
try {
@@ -0,0 +1,31 @@
package com.linkerbot.vibemake.service.oss;
import com.linkerbot.vibemake.common.BusinessException;
public enum ProjectResourceType {
MODEL_GLB("model-glb", ".glb", true),
MODEL_PARTS("model-parts", ".glb", false),
ASSEMBLY_JSON("assembly-json", ".json", true),
WALKING_RESOURCES("walking-resources", "", false);
private final String code;
private final String extension;
private final boolean singleFile;
ProjectResourceType(String code, String extension, boolean singleFile) {
this.code = code;
this.extension = extension;
this.singleFile = singleFile;
}
public String code() { return code; }
public String extension() { return extension; }
public boolean singleFile() { return singleFile; }
public static ProjectResourceType from(String code) {
for (ProjectResourceType type : values()) {
if (type.code.equals(code)) return type;
}
throw BusinessException.badRequest("项目资源类型不合法");
}
}
@@ -0,0 +1,98 @@
package com.linkerbot.vibemake.service.oss;
import com.linkerbot.vibemake.bean.Contracts.ResourceFile;
import com.linkerbot.vibemake.bean.Contracts.ResourceFileSignature;
import com.linkerbot.vibemake.bean.Contracts.ResourceUploadSignature;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.common.Codes;
import com.linkerbot.vibemake.service.config.OssProperties;
import com.linkerbot.vibemake.service.config.ProjectResourceProperties;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaTypeFactory;
import org.springframework.stereotype.Component;
/** 管理端资源签名及上传结果核验。 */
@Component
@RequiredArgsConstructor
public class ProjectResources {
public static final long OSS_MAX_FILE_BYTES = ProjectResourceFiles.OSS_MAX_FILE_BYTES;
private static final Logger log = LoggerFactory.getLogger(ProjectResources.class);
private final OssProperties oss;
private final ProjectResourceProperties properties;
private final OssPostSigner signer;
private final OssObjectStore store;
private final ProjectResourceFiles manifests;
private final OssResourcePaths paths;
private void requireEnabled() {
if (!oss.isEnabled())
throw new BusinessException(503, "OSS_UNAVAILABLE", "OSS 项目资源上传暂未开启");
}
public String prefix(String projectCode, ProjectResourceType type, String batchCode) {
if (projectCode == null || !projectCode.matches("[a-z0-9]{12}|[a-z0-9]{32}")
|| batchCode == null || !batchCode.matches("[a-f0-9]{32}"))
throw BusinessException.badRequest("项目或上传批次编码不合法");
return "/project-resources/" + projectCode + "/" + type.code() + "/" + batchCode + "/";
}
public void validate(String prefix, ProjectResourceType type, List<ResourceFile> files) {
manifests.validate(prefix, type, files);
}
public ResourceUploadSignature sign(String projectCode, ProjectResourceType type, List<ResourceFile> files) {
requireEnabled();
String batchCode = Codes.next();
String prefix = prefix(projectCode, type, batchCode);
validate(prefix, type, files);
List<ResourceFileSignature> signatures = new ArrayList<>();
LocalDateTime expiration = null;
for (ResourceFile file : files) {
String key = paths.objectKey(prefix + file.relativePath());
OssPostSigner.SignedPost post = signer.signObject(
key, contentType(file.relativePath()), file.sizeBytes(), properties.getSignatureValidity());
signatures.add(new ResourceFileSignature(file.relativePath(), key, post.formFields()));
if (expiration == null || post.expiresAt().isBefore(expiration)) expiration = post.expiresAt();
}
log.info("生成项目资源上传签名 projectCode={} resourceType={} batchCode={} fileCount={}",
projectCode, type.code(), batchCode, files.size());
return new ResourceUploadSignature(batchCode, oss.uploadUrl(), expiration, List.copyOf(signatures));
}
private String contentType(String path) {
String lower = path.toLowerCase(Locale.ROOT);
if (lower.endsWith(".glb")) return "model/gltf-binary";
if (lower.endsWith(".json")) return "application/json";
return MediaTypeFactory.getMediaType(lower).map(Object::toString).orElse("application/octet-stream");
}
public void verify(String projectCode, ProjectResourceType type, String batchCode, List<ResourceFile> files) {
requireEnabled();
String prefix = prefix(projectCode, type, batchCode);
validate(prefix, type, files);
for (ResourceFile file : files) {
try {
OssObjectStore.Metadata metadata = store.head(paths.objectKey(prefix + file.relativePath()));
if (metadata.sizeBytes() != file.sizeBytes())
throw new BusinessException(400, "RESOURCE_FILE_SIZE_MISMATCH", "资源文件大小不符,请重新上传");
} catch (BusinessException error) {
log.warn("项目资源核验失败 projectCode={} resourceType={} batchCode={} relativePath={} code={}",
projectCode, type.code(), batchCode, file.relativePath(), error.code());
if ("URDF_FILE_MISSING".equals(error.code()))
throw new BusinessException(400, "RESOURCE_FILE_MISSING", "资源文件未上传完整,请重新上传");
throw error;
}
}
}
public String manifest(List<ResourceFile> files) {
return manifests.manifest(files);
}
}
@@ -86,7 +86,12 @@ public class HttpSafetyFilter extends OncePerRequestFilter {
if (!acquired) throw new RateLimitedException("FILE_BUSY", "文件服务繁忙,请稍后重试", 1);
}
String contentType = request.getContentType();
if (contentType != null && contentType.toLowerCase(Locale.ROOT).contains("json")) {
// 仅管理端的新资源清单不设正文配额;安全链鉴权通过后,控制器才会解析正文。
boolean resourceManifest = ("POST".equals(method) || "PUT".equals(method))
&& path.matches("/admin-api/projects/[^/]+/resources/"
+ "(model-glb|model-parts|assembly-json|walking-resources)(/signature)?");
if (contentType != null && contentType.toLowerCase(Locale.ROOT).contains("json")
&& !resourceManifest) {
long maximum =
path.endsWith("/commits") && "POST".equals(method)
? properties.getCommitJsonBytes()
@@ -109,6 +109,7 @@ class ProjectCodeAllocationTest {
new ProjectLocks(),
new OssResourcePaths(new OssProperties()),
new StaticListableBeanFactory().getBeanProvider(UrdfResources.class),
new StaticListableBeanFactory().getBeanProvider(com.linkerbot.vibemake.service.oss.ProjectResourceFiles.class),
new StaticListableBeanFactory().getBeanProvider(ForkExecutor.class),
new StaticListableBeanFactory().getBeanProvider(GitSnapshotCopier.class));
}
@@ -100,6 +100,7 @@ class ProjectForkStateTest {
locks,
new OssResourcePaths(new OssProperties()),
factory.getBeanProvider(UrdfResources.class),
factory.getBeanProvider(com.linkerbot.vibemake.service.oss.ProjectResourceFiles.class),
factory.getBeanProvider(ForkExecutor.class),
factory.getBeanProvider(GitSnapshotCopier.class));
try {
@@ -98,4 +98,16 @@ class OssPostSignerTest {
assertThat(result.objectKey()).startsWith("vibemake/project-covers/").endsWith(".webp");
assertThat(result.coverUrl()).isEqualTo("https://images.example.com/assets/" + result.objectKey());
}
@Test
void resourcePolicySupportsFiveGiBAndConfiguredOneHourExpiry() throws Exception {
Clock clock = Clock.fixed(Instant.parse("2026-10-14T23:58:30Z"), ZoneOffset.UTC);
OssPostSigner signer = new OssPostSigner(properties(), json, clock);
String key = "vibemake_dev/project-resources/7m4k9x2p8q6r/model-glb/" + "a".repeat(32) + "/a.glb";
long bytes = ProjectResources.OSS_MAX_FILE_BYTES;
OssPostSigner.SignedPost post = signer.signObject(key, "model/gltf-binary", bytes, Duration.ofHours(1));
assertThat(post.expiresAt()).isEqualTo(LocalDateTime.parse("2026-10-15T00:58:30"));
JsonNode policy = json.readTree(Base64.getDecoder().decode(post.formFields().get("policy")));
assertThat(policy.path("conditions").toString()).contains("[\"content-length-range\",5368709120,5368709120]");
}
}
@@ -0,0 +1,101 @@
package com.linkerbot.vibemake.service.oss;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkerbot.vibemake.bean.Contracts.*;
import com.linkerbot.vibemake.common.BusinessException;
import com.linkerbot.vibemake.service.config.OssProperties;
import com.linkerbot.vibemake.service.config.ProjectResourceProperties;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
class ProjectResourcesTest {
private final OssProperties oss = properties();
private final OssObjectStore store = mock(OssObjectStore.class);
private final ObjectMapper json = new ObjectMapper();
private final ProjectResourceFiles manifests = new ProjectResourceFiles(json, new OssResourcePaths(oss));
private final ProjectResources resources = new ProjectResources(oss, new ProjectResourceProperties(),
new OssPostSigner(oss, json), store, manifests, new OssResourcePaths(oss));
private static final String PROJECT = "7m4k9x2p8q6r";
private static final String BATCH = "a".repeat(32);
private static OssProperties properties() {
OssProperties properties = new OssProperties();
properties.setEnabled(true);
properties.setRootDirectory("vibemake_dev");
properties.setRegion("cn-hangzhou");
properties.setBucket("examplebucket");
properties.setAccessKeyId("TestAccessKey");
properties.setAccessKeySecret("test-secret");
properties.setPublicBaseUrl("https://cdn.example.com/assets");
return properties;
}
@Test
void acceptsLargeFilesAndMoreThanFiveHundredFilesWithoutUrdfQuotas() {
List<ResourceFile> files = new ArrayList<>();
for (int index = 0; index < 501; index++)
files.add(new ResourceFile("零件/" + index + ".GLB", ProjectResources.OSS_MAX_FILE_BYTES));
ResourceUploadSignature signed = resources.sign(PROJECT, ProjectResourceType.MODEL_PARTS, files);
assertThat(signed.files()).hasSize(501);
assertThat(signed.files().getFirst().formFields()).containsEntry("content-type", "model/gltf-binary");
assertThat(signed.files().getFirst().objectKey()).startsWith("vibemake_dev/project-resources/" + PROJECT);
verifyNoInteractions(store);
assertThatThrownBy(() -> resources.validate(resources.prefix(PROJECT, ProjectResourceType.MODEL_GLB, BATCH),
ProjectResourceType.MODEL_GLB, List.of(new ResourceFile("a.glb", ProjectResources.OSS_MAX_FILE_BYTES + 1))))
.isInstanceOfSatisfying(BusinessException.class,
error -> assertThat(error.code()).isEqualTo("RESOURCE_FILE_TOO_LARGE"));
}
@Test
void rejectsInvalidPathsDuplicateNamesAndWrongTypesBeforeOssAccess() {
String prefix = resources.prefix(PROJECT, ProjectResourceType.MODEL_PARTS, BATCH);
for (String path : List.of("../a.glb", "/a.glb", "a//b.glb", "a/./b.glb", "a\\b.glb", "a/.git/b.glb",
"a\u0000.glb", "a.txt", "a".repeat(510) + ".glb", "中".repeat(320) + ".glb")) {
assertThatThrownBy(() -> resources.validate(prefix, ProjectResourceType.MODEL_PARTS,
List.of(new ResourceFile(path, 1L)))).isInstanceOf(BusinessException.class);
}
assertThatThrownBy(() -> resources.validate(prefix, ProjectResourceType.MODEL_PARTS,
List.of(new ResourceFile("a.glb", 1L), new ResourceFile("a.glb", 2L))))
.isInstanceOf(BusinessException.class);
assertThatThrownBy(() -> resources.validate(prefix, ProjectResourceType.MODEL_GLB,
List.of(new ResourceFile("a.glb", 1L), new ResourceFile("b.glb", 1L))))
.isInstanceOf(BusinessException.class);
assertThatThrownBy(() -> resources.validate(prefix, ProjectResourceType.ASSEMBLY_JSON,
List.of(new ResourceFile("nested/a.json", 1L)))).isInstanceOf(BusinessException.class);
verifyNoInteractions(store);
}
@Test
void readsInheritedFilesWhileOssDisabledAndPreservesEncodedNames() {
String prefix = resources.prefix(PROJECT, ProjectResourceType.WALKING_RESOURCES, BATCH);
List<ResourceFile> files = List.of(new ResourceFile("目录/a #?%+.bin", 0L));
oss.setEnabled(false);
List<ResourceFileView> views = manifests.fileViews(prefix, resources.manifest(files), ProjectResourceType.WALKING_RESOURCES);
assertThat(views.getFirst().relativePath()).isEqualTo("目录/a #?%+.bin");
assertThat(views.getFirst().url()).isEqualTo("https://cdn.example.com/assets/vibemake_dev"
+ prefix + "%E7%9B%AE%E5%BD%95/a%20%23%3F%25+.bin");
verifyNoInteractions(store);
assertThatThrownBy(() -> resources.sign(PROJECT, ProjectResourceType.WALKING_RESOURCES, files))
.isInstanceOfSatisfying(BusinessException.class, error -> assertThat(error.code()).isEqualTo("OSS_UNAVAILABLE"));
}
@Test
void verifiesExactSizeAndReportsResourceSpecificMissingFileErrors() {
String prefix = resources.prefix(PROJECT, ProjectResourceType.MODEL_GLB, BATCH);
String key = "vibemake_dev" + prefix + "a.glb";
when(store.head(key)).thenReturn(new OssObjectStore.Metadata(2L, "etag"));
assertThatThrownBy(() -> resources.verify(PROJECT, ProjectResourceType.MODEL_GLB, BATCH,
List.of(new ResourceFile("a.glb", 1L))))
.isInstanceOfSatisfying(BusinessException.class,
error -> assertThat(error.code()).isEqualTo("RESOURCE_FILE_SIZE_MISMATCH"));
when(store.head(key)).thenThrow(new BusinessException(400, "URDF_FILE_MISSING", "missing"));
assertThatThrownBy(() -> resources.verify(PROJECT, ProjectResourceType.MODEL_GLB, BATCH,
List.of(new ResourceFile("a.glb", 1L))))
.isInstanceOfSatisfying(BusinessException.class,
error -> assertThat(error.code()).isEqualTo("RESOURCE_FILE_MISSING"));
}
}