minIO改为阿里云对象存储

This commit is contained in:
lxp
2026-10-10 14:13:23 +08:00
parent 0d103c3285
commit da3d956429
38 changed files with 1107 additions and 651 deletions
+6 -5
View File
@@ -4,13 +4,14 @@ SERVER_PORT=8992
APP_DB_URL='jdbc:mysql://39.105.93.138:3306/cadworld?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai'
APP_DB_USERNAME=
APP_DB_PASSWORD=
APP_STORAGE_PROVIDER=minio
# Maximum bytes per upload, including raw PUT streams (default 100 MiB).
APP_STORAGE_MAX_UPLOAD_BYTES=104857600
APP_MINIO_ENDPOINT=
APP_MINIO_ACCESS_KEY=
APP_MINIO_SECRET_KEY=
APP_MINIO_BUCKET=
OSS_ACCESS_KEY_ID=
OSS_ACCESS_KEY_SECRET=
OSS_REGION=oss-cn-beijing
OSS_BUCKET=linkerbot
# 默认 test: cadworld_dev,prod: cadworld;需要覆盖时取消注释。
# OSS_ROOT_PREFIX=cadworld_dev
APP_STORAGE_PUBLIC_API_BASE_URL=http://127.0.0.1:8992
APP_STORAGE_PUBLIC_BASE_URL=http://127.0.0.1:8992/admin-api/component-assets
# At least 32 UTF-8 bytes. Empty uses a random key and invalidates sessions on restart.
+24 -12
View File
@@ -7,7 +7,7 @@ Java 21 / Spring Boot 3.4.4 / MyBatis-Plus 后台多模块项目。包名统一
| 模块 | 内容 |
| --- | --- |
| cadworld-adminapi | 后台启动应用、Controller、Web 配置、操作日志拦截 |
| cadworld-service | 业务服务、Mapper、MinIO/本地存储、几何与资源校验 |
| cadworld-service | 业务服务、Mapper、阿里云 OSS、几何与资源校验 |
| cadworld-bean | 实体、DTO、枚举 |
| cadworld-common | 响应、异常、JSON 类型处理、请求编号、进程执行工具 |
| cadworld-api | 预留前台 API 模块 |
@@ -33,7 +33,7 @@ mysql -h 39.105.93.138 -P 3306 -u YOUR_USER -p cadworld < db/init.sql
原有两个脚本保留不变。执行初始化或增量脚本后,还需执行下面的接口重构补充 SQL。当前业务使用五张表:`sys_user`、`component`、`interface_definition_v2`、`interface_definition_revision`、`sys_operation_log`;组件资源索引代码已移除。
用户删除保留数据及手机号唯一索引,已删除用户的手机号不能重新注册。组件删除沿用物理删除;接口删除保留历史修订,原接口 ID 不能再次创建。删除操作不会清理 MinIO 中的资源对象。
用户删除保留数据及手机号唯一索引,已删除用户的手机号不能重新注册。组件删除沿用物理删除;接口删除保留历史修订,原接口 ID 不能再次创建。删除操作不会清理 OSS 中的资源对象。
### 接口重构补充 SQL
@@ -77,7 +77,7 @@ DROP TABLE IF EXISTS component_asset;
```bash
cp .env.example .env
# 填写 .env 中的数据库和 MinIO 参数。
# 填写 .env 中的数据库和阿里云 OSS 参数。
set -a
source .env
set +a
@@ -91,17 +91,21 @@ java -jar cadworld-adminapi/target/cadworld-adminapi-0.0.1-SNAPSHOT.jar
| --- | --- |
| APP_DB_URL / APP_DB_USERNAME / APP_DB_PASSWORD | MySQL 连接配置 |
| SERVER_PORT | 后台端口,默认 8992 |
| APP_STORAGE_PROVIDER | 默认 `minio`;支持 `local` 用于本地开发和测试 |
| APP_MINIO_ENDPOINT / APP_MINIO_ACCESS_KEY / APP_MINIO_SECRET_KEY / APP_MINIO_BUCKET | MinIO 连接及 bucket,均需配置 |
| OSS_ACCESS_KEY_ID / OSS_ACCESS_KEY_SECRET | 阿里云 OSS 凭证,必填;仅通过环境变量或 Git 忽略的 `.env` 提供 |
| OSS_REGION / OSS_BUCKET | 默认 `oss-cn-beijing / linkerbot`;签名地域自动转换为 `cn-beijing` |
| OSS_ROOT_PREFIX | 桶内根目录;`test` 默认 `cadworld_dev`,`prod` 默认 `cadworld`,各部署环境可独立覆盖 |
| APP_STORAGE_PUBLIC_API_BASE_URL | 生成上传地址所用的后台外部地址 |
| APP_STORAGE_PUBLIC_BASE_URL | 生成下载地址的资源前缀,默认 `/admin-api/component-assets` |
| APP_STORAGE_LOCAL_ROOT | 本地存储目录,默认 `./data/assets` |
| APP_STORAGE_MAX_UPLOAD_BYTES | 文件上传大小限制,默认 104857600 字节;原始 PUT 流同样检查 |
| APP_UPLOAD_SESSION_SECRET | 至少 32 个 UTF-8 字节的上传签名密钥;留空时生成进程内随机密钥,重启后原上传会话失效 |
| APP_ADMIN_OPERATOR_ID / APP_ADMIN_OPERATOR_NAME | 测试操作人,默认 `0 / TEST_OPERATOR` |
| LOG_PATH | 运行日志目录,默认 `logs` |
资源通过后台上传和读取,无需客户端直连 MinIO。MinIO 实现首次使用时检查 bucket,不存在时尝试创建,因此账号需有对应权限。线上地址配置应包含实际域名及反向代理路径,上传和下载地址均使用 `/admin-api` 前缀。
运行时只使用阿里云 OSS,通过 HTTPS 和 V4 签名访问已有存储桶,不自动创建桶。账号需要对应目录下的上传、读取和删除权限。资源继续通过后台上传和读取,无需客户端直连 OSS;线上地址配置应包含实际域名及反向代理路径,上传和下载地址均使用 `/admin-api` 前缀。
数据库资源字段及 URDF 资源清单只保存桶内相对对象键,例如 `cadworld_dev/components/urdf/.../robot.urdf`;访问 URL 由后台生成,不入库。所有上传、入库和存储读写都校验当前环境根目录,完整 URL、跨环境路径或目录越界返回 `422 / INVALID_STORAGE_OBJECT_KEY`。根目录不要配置成完整 URL。新增路径包含根目录后的总长度仍受原字段限制。
本次切换不迁移历史 MinIO 文件或数据库记录,旧资源需要重新上传;切换后旧上传会话也需要重新创建。更改根目录会使原目录对象不能被当前环境访问。
接口文档:`http://127.0.0.1:8992/swagger-ui.html`;OpenAPI:`/api-docs`。日志配置集中在 `cadworld-common`,按应用名输出 `cadworld-adminapi.log` 和独立的 `cadworld-adminapi-error.log`,并携带请求编号。普通日志按天及 100 MB 滚动,保留 30 天、最多 10 GB;ERROR 日志按天及 50 MB 滚动,保留 60 天、最多 5 GB。归档文件保存在 `logs/archive`(支持 `LOG_PATH` 覆盖)。测试环境项目包日志为 DEBUG,生产环境为 INFO;Mapper 日志保持 INFO,避免 SQL 参数输出敏感数据。
@@ -165,15 +169,23 @@ mvn clean verify
cadworld-adminapi/.venv/bin/python -m unittest discover -s cadworld-adminapi/scripts/tests
```
默认 Java 测试使用 Mock/H2 和本地临时资源目录,不连接共享数据库或 MinIO。可选集成测试使用专用可清空的 `cadworld_verification` 数据库,并执行初始化、升级、重复执行及数据保留检查,再将相同的用户/组件/接口/日志/上传用例运行于 MySQL 和 MinIO。
默认 Java 测试使用 SDK Mock、H2 和仅存在于测试代码中的内存资源替身,不连接共享数据库或真实 OSS。Python 测试包含程序生成的真实 STEP 文件及无效文件解析;缺少外部原始模型时,对应回归用例跳过。
Python 测试包含程序生成的真实 STEP 文件及无效文件解析;另有一个依赖外部原始模型的回归用例,缺少该模型时跳过,不会复制来源项目的业务资源。
准备独立 MySQL(默认 `127.0.0.1:13306`,数据库 `cadworld_verification`,root 空密码)及独立 MinIO(默认 `127.0.0.1:19000`,账号 `cadworld-test`,密码 `cadworld-test-secret`),执行:
MySQL 集成测试使用可清空的专用 `cadworld_verification` 数据库,验证初始化、升级、重复执行及数据保留,并运行业务接口用例。资源仍使用内存替身。准备独立 MySQL(默认 `127.0.0.1:13306`,root 空密码)后执行:
```bash
mvn -P integration verify
```
可通过 `-Dcadworld.it.db-url=...`、`-Dcadworld.it.db-user=...`、`-Dcadworld.it.db-password=...`、`-Dcadworld.it.minio-endpoint=...`、`-Dcadworld.it.minio-user=...`、`-Dcadworld.it.minio-password=...` 覆盖测试服务配置。集成测试会重建该专用数据库中的业务表,并拒绝使用其他数据库名称;MinIO 使用专用 `cadworld-verification` bucket。
可通过 `-Dcadworld.it.db-url=...`、`-Dcadworld.it.db-user=...`、`-Dcadworld.it.db-password=...` 覆盖配置。测试会重建专用数据库中的业务表,并拒绝使用其他数据库名称。
真实 OSS 测试使用独立 Spring 上下文,不加载数据库,只通过以下入口显式启用:
```bash
# 先按启动说明加载 .env 中的 OSS 凭证。
mvn -P oss-integration verify
```
默认仅访问 `cadworld_dev/verification/<本次UUID>/`,可通过 `-Dcadworld.it.oss-root-prefix=...` 设置其他测试根目录;禁止使用默认生产目录 `cadworld`。测试验证上传、元数据、下载、未知长度超限保护及删除,仅清理本次创建的对象,不创建或删除存储桶。
若真实测试清理失败,未清理对象键会记录到 `cadworld-adminapi/logs/oss-verification-pending/`,供后续清理且不会被 `mvn clean` 删除。删除返回 `AccessDenied` 时,请检查当前凭证是否具有该目录的 `oss:DeleteObject` 权限,以及桶策略或保留策略是否限制删除;补齐权限后按记录清理。
+47 -43
View File
@@ -726,7 +726,7 @@ curl -X POST 'http://127.0.0.1:8992/admin-api/interfaces/mount-2-hole/assets?geo
{
"code": "OK",
"message": "success",
"data": "interfaces/6d6f756e742d322d686f6c65/1/073b7cf3dbe11a465f34da53b9b5e765a489e970cbb0f6c3c2d8cd01553aedc7/22222222-2222-4222-8222-222222222222/plate.step",
"data": "cadworld_dev/interfaces/6d6f756e742d322d686f6c65/1/073b7cf3dbe11a465f34da53b9b5e765a489e970cbb0f6c3c2d8cd01553aedc7/22222222-2222-4222-8222-222222222222/plate.step",
"requestId": "req-demo-001"
}
```
@@ -802,7 +802,7 @@ curl -X POST 'http://127.0.0.1:8992/admin-api/interfaces/mount-2-hole/assets?geo
"机械臂/末端工具",
"移动底盘/附件"
],
"sourceAssetKey": "interfaces/6d6f756e742d322d686f6c65/1/073b7cf3dbe11a465f34da53b9b5e765a489e970cbb0f6c3c2d8cd01553aedc7/22222222-2222-4222-8222-222222222222/plate.step",
"sourceAssetKey": "cadworld_dev/interfaces/6d6f756e742d322d686f6c65/1/073b7cf3dbe11a465f34da53b9b5e765a489e970cbb0f6c3c2d8cd01553aedc7/22222222-2222-4222-8222-222222222222/plate.step",
"sourceGeometryHash": "sha256:073b7cf3dbe11a465f34da53b9b5e765a489e970cbb0f6c3c2d8cd01553aedc7",
"editRevision": 2
},
@@ -1307,9 +1307,9 @@ X-Request-Id: req-demo-001
"robotType": "humanoid",
"company": "示例厂商",
"model": "BASE-001",
"source": "components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"sourceUrl": "http://127.0.0.1:8992/admin-api/component-assets/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"source": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"sourceUrl": "http://127.0.0.1:8992/admin-api/component-assets/cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"description": "双孔固定底座",
"assemblyMetadata": {
"tags": [
@@ -1371,10 +1371,10 @@ X-Request-Id: req-demo-001
"robotType": "humanoid",
"company": "示例厂商",
"model": "BASE-001",
"source": "components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"sourceUrl": "http://127.0.0.1:8992/admin-api/component-assets/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"source": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"sourceUrl": "http://127.0.0.1:8992/admin-api/component-assets/cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"description": "双孔固定底座",
"componentJson": {
"urdfSource": {
@@ -1520,8 +1520,8 @@ Content-Type: application/json
"robotType": "humanoid",
"company": "示例厂商",
"model": "BASE-001",
"source": "components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"source": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"description": "双孔固定底座",
"componentJson": {
"urdfSource": {
@@ -1652,10 +1652,10 @@ Content-Type: application/json
"robotType": "humanoid",
"company": "示例厂商",
"model": "BASE-001",
"source": "components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"sourceUrl": "http://127.0.0.1:8992/admin-api/component-assets/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"source": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"sourceUrl": "http://127.0.0.1:8992/admin-api/component-assets/cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"description": "双孔固定底座",
"componentJson": {
"urdfSource": {
@@ -1801,8 +1801,8 @@ Content-Type: application/json
"robotType": "humanoid",
"company": "示例厂商",
"model": "BASE-001",
"source": "components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"source": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"description": "更新后的组件说明",
"componentJson": {
"urdfSource": {
@@ -1934,10 +1934,10 @@ Content-Type: application/json
"robotType": "humanoid",
"company": "示例厂商",
"model": "BASE-001",
"source": "components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"sourceUrl": "http://127.0.0.1:8992/admin-api/component-assets/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"source": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"sourceUrl": "http://127.0.0.1:8992/admin-api/component-assets/cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"coverImageKey": "cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"description": "更新后的组件说明",
"componentJson": {
"urdfSource": {
@@ -2065,7 +2065,7 @@ Content-Type: application/json
`DELETE /admin-api/components/{id}`
物理删除组件及关联资源索引,不删除 MinIO 中已经上传的对象。
物理删除组件及关联资源索引,不删除阿里云 OSS 中已经上传的对象。
**请求示例**
@@ -2125,7 +2125,7 @@ X-Request-Id: req-demo-001
"message": "success",
"data": {
"uploadSessionToken": "<COVER_IMAGE_UPLOAD_SESSION_TOKEN>",
"keyPrefix": "components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/"
"keyPrefix": "cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/"
},
"requestId": "req-demo-001"
}
@@ -2167,8 +2167,8 @@ Content-Type: application/json
"code": "OK",
"message": "success",
"data": {
"uploadUrl": "http://127.0.0.1:8992/admin-api/component-assets/upload?objectKey=components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"objectKey": "components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"uploadUrl": "http://127.0.0.1:8992/admin-api/component-assets/upload?objectKey=cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"objectKey": "cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"uploadHeaders": {
"X-Upload-Session-Token": "<uploadSessionToken>"
}
@@ -2205,7 +2205,7 @@ Content-Type: application/json
{
"uploadSessionToken": "<COVER_IMAGE_UPLOAD_SESSION_TOKEN>",
"objectKeys": [
"components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png"
"cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png"
]
}
```
@@ -2217,8 +2217,8 @@ Content-Type: application/json
"code": "OK",
"message": "success",
"data": {
"coverImageKey": "components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png"
"coverImageKey": "cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png",
"coverImageUrl": "http://127.0.0.1:8992/admin-api/component-assets/cadworld_dev/components/cover-images/0/20261009/22222222-2222-4222-8222-222222222222/cover.png"
},
"requestId": "req-demo-001"
}
@@ -2248,7 +2248,7 @@ X-Request-Id: req-demo-001
"message": "success",
"data": {
"uploadSessionToken": "<URDF_UPLOAD_SESSION_TOKEN>",
"keyPrefix": "components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/"
"keyPrefix": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/"
},
"requestId": "req-demo-001"
}
@@ -2290,8 +2290,8 @@ Content-Type: application/json
"code": "OK",
"message": "success",
"data": {
"uploadUrl": "http://127.0.0.1:8992/admin-api/component-assets/upload?objectKey=components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"objectKey": "components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"uploadUrl": "http://127.0.0.1:8992/admin-api/component-assets/upload?objectKey=cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"objectKey": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf",
"uploadHeaders": {
"X-Upload-Session-Token": "<uploadSessionToken>"
}
@@ -2330,7 +2330,7 @@ Content-Type: application/json
{
"uploadSessionToken": "<URDF_UPLOAD_SESSION_TOKEN>",
"objectKeys": [
"components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf"
"cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf"
]
}
```
@@ -2342,7 +2342,7 @@ Content-Type: application/json
"code": "OK",
"message": "success",
"data": {
"source": "components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf"
"source": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf"
},
"requestId": "req-demo-001"
}
@@ -2352,7 +2352,11 @@ Content-Type: application/json
## 4. 资源上传与下载
这些接口通过后台读写 MinIO,前端不需要 MinIO Access Key、Secret Key 或直连存储桶。
这些接口通过后台读写阿里云 OSS,前端不需要 OSS Access Key、Secret Key 或直连存储桶。
本文对象键示例使用测试环境默认根目录 `cadworld_dev/`,生产环境默认 `cadworld/`。两种环境通过 `app.storage.root-prefix` 分别配置,也可在各自部署环境中设置 `OSS_ROOT_PREFIX` 覆盖;前端应使用服务端返回的对象键,不自行硬编码根目录。
保存组件时,`source`、`coverImageKey` 及 URDF 资源清单的 `sourceKey` 必须是当前环境根目录下的相对对象键;接口源文件的 `sourceAssetKey` 也保存相对对象键。`uploadUrl`、`sourceUrl`、`coverImageUrl` 是完整后台访问地址,不作为资源字段入库。完整 URL、跨环境目录及非法路径返回 `422 / INVALID_STORAGE_OBJECT_KEY`。旧 MinIO 文件和数据库记录不自动迁移,需要重新上传。
### 4.1 上传原始文件流
@@ -2371,7 +2375,7 @@ Content-Type: application/json
```bash
curl -X PUT \
'http://127.0.0.1:8992/admin-api/component-assets/upload?objectKey=components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf' \
'http://127.0.0.1:8992/admin-api/component-assets/upload?objectKey=cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf' \
-H 'Content-Type: application/xml' \
-H 'X-Upload-Session-Token: <uploadSessionToken>' \
-H 'X-Request-Id: req-demo-001' \
@@ -2400,7 +2404,7 @@ X-Request-Id: req-demo-001
**请求示例**
```http
GET /admin-api/component-assets/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf HTTP/1.1
GET /admin-api/component-assets/cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/robot.urdf HTTP/1.1
Host: 127.0.0.1:8992
X-Request-Id: req-demo-001
```
@@ -2418,7 +2422,7 @@ X-Request-Id: req-demo-001
```
图片等文件返回对应 MIME 类型和二进制内容,服务端在知道大小时返回 `Content-Length`。读取时使用 `text()`、`blob()` 或 `arrayBuffer()`,不要统一按 JSON 解析。资源不存在时返回 JSON 错误响应,例如 `404 / OSS_OBJECT_NOT_FOUND`。
如需从 Key 手动拼路径,对每个路径段编码后用 `/` 连接;不要把整个带斜杠的路径当作一个普通路径段。
<a id="step"></a>
@@ -3097,7 +3101,7 @@ async function uploadModel(files) {
uploadSessionToken: session.uploadSessionToken,
objectKeys
}); // { source: 'cadworld_dev/components/urdf/...' },随后保存组件
}
}
```
当前应用没有统一启用跨域配置。前端开发代理、网关入口或服务端跨域设置需与后端协调;上传 URL 和下载 URL 也应使用实际可访问的统一入口,不只代理列表接口。
@@ -3128,7 +3132,7 @@ async function uploadModel(files) {
{
"reference": "meshes/base.stl",
"sourceKey": "cadworld_dev/components/urdf/0/20261009/11111111-1111-4111-8111-111111111111/meshes/base.stl",
"sha256": "<RESOURCE_BYTES_SHA256_HEX>"
"sha256": "<RESOURCE_BYTES_SHA256_HEX>"
}
```
@@ -3215,10 +3219,10 @@ sourceGeometryHash = SHA256(documentHash + "\n" + manifest)
| 503 | INTERFACE_BREP_UNAVAILABLE | 解析服务、脚本或 Python/OCP 依赖不可用,联系后端 |
| 504 | INTERFACE_BREP_TIMEOUT | STEP 解析超时,拆分文件后重试 |
| 500 | OSS_CONFIG_ERROR / STORAGE_CONFIG_ERROR | 存储配置缺失或非法,联系后端 |
| 502 | MINIO_BUCKET_FAILED / MINIO_UPLOAD_FAILED / MINIO_OBJECT_CHECK_FAILED / MINIO_DOWNLOAD_FAILED / MINIO_DELETE_FAILED | 存储连接、权限或读写失败,联系后端 |
| 404 | MINIO_OBJECT_NOT_FOUND | 资源对象不存在 |
| 422 | INVALID_STORAGE_OBJECT_KEY | 存储对象 Key 不合法 |
| 503 | DATABASE_UNAVAILABLE | 数据库连接不可用,联系后端 |
| 502 | OSS_UPLOAD_FAILED / OSS_OBJECT_CHECK_FAILED / OSS_DOWNLOAD_FAILED / OSS_DELETE_FAILED | 存储连接、权限或读写失败,联系后端 |
| 404 | OSS_OBJECT_NOT_FOUND | 资源对象不存在 |
| 422 | INVALID_STORAGE_OBJECT_KEY | 存储对象 Key 不合法、完整 URL、跨环境目录或路径越界 |
| 503 | DATABASE_UNAVAILABLE | 数据库连接不可用,联系后端 |
| 500 | INTERNAL_ERROR | 未分类系统异常,反馈 requestId |
| 404 | NOT_FOUND | 路由不存在,检查路径及前缀 |
+19
View File
@@ -67,6 +67,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/OssObjectStorageIT.java</exclude>
</excludes>
<systemPropertyVariables>
<cadworld.project-root>${maven.multiModuleProjectDirectory}</cadworld.project-root>
</systemPropertyVariables>
@@ -75,5 +78,21 @@
</plugins>
</build>
</profile>
<profile>
<id>oss-integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/OssObjectStorageIT.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
@@ -7,6 +7,10 @@ spring:
hikari:
maximum-pool-size: 20
app:
storage:
root-prefix: ${OSS_ROOT_PREFIX:cadworld}
springdoc:
api-docs:
enabled: false
@@ -10,6 +10,10 @@ spring:
maximum-pool-size: 10
connection-timeout: 5000
app:
storage:
root-prefix: ${OSS_ROOT_PREFIX:cadworld_dev}
springdoc:
api-docs:
enabled: true
@@ -43,19 +43,18 @@ app:
# Empty means a random, process-local signing key. Set a stable key for restarts/multiple instances.
secret: ${APP_UPLOAD_SESSION_SECRET:}
storage:
provider: ${APP_STORAGE_PROVIDER:minio}
root-prefix: ${OSS_ROOT_PREFIX:cadworld}
urdf-prefix: components/urdf
cover-image-prefix: components/cover-images
public-api-base-url: ${APP_STORAGE_PUBLIC_API_BASE_URL:http://127.0.0.1:${SERVER_PORT:8992}}
public-base-url: ${APP_STORAGE_PUBLIC_BASE_URL:http://127.0.0.1:${SERVER_PORT:8992}/admin-api/component-assets}
local-root: ${APP_STORAGE_LOCAL_ROOT:./data/assets}
upload-session-ttl: PT30M
max-upload-bytes: ${APP_STORAGE_MAX_UPLOAD_BYTES:104857600}
minio:
endpoint: ${APP_MINIO_ENDPOINT:}
access-key: ${APP_MINIO_ACCESS_KEY:}
secret-key: ${APP_MINIO_SECRET_KEY:}
bucket: ${APP_MINIO_BUCKET:}
oss:
access-key-id: ${OSS_ACCESS_KEY_ID:}
access-key-secret: ${OSS_ACCESS_KEY_SECRET:}
region: ${OSS_REGION:oss-cn-beijing}
bucket: ${OSS_BUCKET:linkerbot}
interface-brep:
python: ${APP_INTERFACE_BREP_PYTHON:python3}
script: ${APP_INTERFACE_BREP_SCRIPT:classpath:scripts/step_brep_topology.py}
@@ -24,9 +24,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
"spring.datasource.url=jdbc:h2:mem:admin;MODE=MySQL;DB_CLOSE_DELAY=-1",
"spring.datasource.driver-class-name=org.h2.Driver", "spring.datasource.username=sa", "spring.datasource.password=",
"spring.sql.init.mode=always", "spring.sql.init.schema-locations=classpath:test-schema.sql",
"app.storage.provider=local", "app.storage.local-root=target/test-assets", "logging.file.path=target/test-logs"
"app.storage.root-prefix=cadworld_dev", "logging.file.path=target/test-logs"
})
@ActiveProfiles("verification")
@org.springframework.context.annotation.Import(InMemoryObjectStorageConfiguration.class)
@AutoConfigureMockMvc
class AdminApiTest {
@Autowired MockMvc mvc;
@@ -38,8 +39,11 @@ class AdminApiTest {
@org.springframework.beans.factory.annotation.Qualifier("requestMappingHandlerMapping")
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping mappings;
@Autowired InMemoryObjectStorageConfiguration.InMemoryObjectStorageClient memoryStorage;
@BeforeEach
void clean() {
memoryStorage.clear();
TestSchemaSupport.prepare(jdbc);
for (String table : new String[]{"component", "interface_definition_revision", "interface_definition_v2", "sys_user", "sys_operation_log"}) {
jdbc.update("DELETE FROM " + table);
@@ -207,6 +211,80 @@ class AdminApiTest {
assertThat(jdbc.queryForList("SELECT * FROM sys_operation_log").toString()).doesNotContain(token);
}
@Test
void modelAndCoverUploadKeysAreStoredAsRelativePathsAndDownloadUrlsWork() throws Exception {
JsonNode session = send("/admin-api/components/urdf-upload-sessions", null).path("data");
assertThat(session.path("keyPrefix").asText()).startsWith("cadworld_dev/components/urdf/");
String token = session.path("uploadSessionToken").asText();
JsonNode upload = send("/admin-api/components/urdf-upload-sessions/upload-url",
Map.of("uploadSessionToken", token, "objectPath", "模型 robot.urdf", "contentType", "application/xml")).path("data");
String key = upload.path("objectKey").asText();
byte[] bytes = "<robot name=\"demo\"><link name=\"base\"/></robot>".getBytes(java.nio.charset.StandardCharsets.UTF_8);
mvc.perform(put("/admin-api/component-assets/upload").param("objectKey", key)
.header("X-Upload-Session-Token", token).contentType("application/xml").content(bytes))
.andExpect(status().isNoContent());
JsonNode complete = send("/admin-api/components/urdf-upload-sessions/complete",
Map.of("uploadSessionToken", token, "objectKeys", new String[]{key})).path("data");
assertThat(complete.path("source").asText()).isEqualTo(key);
JsonNode coverSession = send("/admin-api/components/cover-image-upload-sessions", null).path("data");
String coverToken = coverSession.path("uploadSessionToken").asText();
String coverKey = send("/admin-api/components/cover-image-upload-sessions/upload-url",
Map.of("uploadSessionToken", coverToken, "objectPath", "cover.png", "contentType", "image/png"))
.path("data").path("objectKey").asText();
assertThat(coverKey).startsWith("cadworld_dev/components/cover-images/");
mvc.perform(put("/admin-api/component-assets/upload").param("objectKey", coverKey)
.header("X-Upload-Session-Token", coverToken).contentType("image/png").content(new byte[]{1, 2}))
.andExpect(status().isNoContent());
send("/admin-api/components/cover-image-upload-sessions/complete",
Map.of("uploadSessionToken", coverToken, "objectKeys", new String[]{coverKey}));
var body = json.valueToTree(component("relative-path-component"));
((com.fasterxml.jackson.databind.node.ObjectNode) body).put("source", key).put("coverImageKey", coverKey);
JsonNode result = send("/admin-api/components", body).path("data");
long id = result.path("id").asLong();
assertThat(jdbc.queryForObject("SELECT source FROM component WHERE id=?", String.class, id)).isEqualTo(key);
assertThat(jdbc.queryForObject("SELECT cover_image_key FROM component WHERE id=?", String.class, id)).isEqualTo(coverKey);
assertThat(result.path("sourceUrl").asText()).contains("/admin-api/component-assets/cadworld_dev/");
mvc.perform(get(java.net.URI.create(result.path("sourceUrl").asText())))
.andExpect(status().isOk()).andExpect(content().bytes(bytes))
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_XML))
.andExpect(header().longValue("Content-Length", bytes.length));
mvc.perform(get(java.net.URI.create(result.path("coverImageUrl").asText())))
.andExpect(status().isOk()).andExpect(content().bytes(new byte[]{1, 2}));
}
@Test
void saveAndDownloadRejectFullUrlsAndOtherEnvironmentKeys() throws Exception {
for (String invalid : new String[]{"https://linkerbot.oss-cn-beijing.aliyuncs.com/cadworld_dev/model.rfs",
"cadworld/model.rfs", "cadworld_dev/../model.rfs"}) {
var body = (com.fasterxml.jackson.databind.node.ObjectNode) json.valueToTree(component("invalid-path"));
body.put("source", invalid);
mvc.perform(post("/admin-api/components").contentType(MediaType.APPLICATION_JSON).content(body.toString()))
.andExpect(status().isUnprocessableEntity()).andExpect(jsonPath("$.code").value("INVALID_STORAGE_OBJECT_KEY"));
body.put("source", "cadworld_dev/components/sample.rfs").put("coverImageKey", invalid);
mvc.perform(post("/admin-api/components").contentType(MediaType.APPLICATION_JSON).content(body.toString()))
.andExpect(status().isUnprocessableEntity()).andExpect(jsonPath("$.code").value("INVALID_STORAGE_OBJECT_KEY"));
body.remove("coverImageKey");
((com.fasterxml.jackson.databind.node.ObjectNode) body.path("componentJson"))
.putObject("urdfSource").putArray("resources").addObject().put("sourceKey", invalid);
mvc.perform(post("/admin-api/components").contentType(MediaType.APPLICATION_JSON).content(body.toString()))
.andExpect(status().isUnprocessableEntity()).andExpect(jsonPath("$.code").value("INVALID_STORAGE_OBJECT_KEY"));
}
assertThat(jdbc.queryForObject("SELECT COUNT(*) FROM component", Long.class)).isZero();
JsonNode created = send("/admin-api/components", component("preserved-path")).path("data");
var update = (com.fasterxml.jackson.databind.node.ObjectNode) json.valueToTree(component("preserved-path"));
update.set("componentJson", created.path("componentJson"));
update.put("source", created.path("sourceUrl").asText());
mvc.perform(put("/admin-api/components/{id}", created.path("id").asLong())
.contentType(MediaType.APPLICATION_JSON).content(update.toString()))
.andExpect(status().isUnprocessableEntity()).andExpect(jsonPath("$.code").value("INVALID_STORAGE_OBJECT_KEY"));
assertThat(jdbc.queryForObject("SELECT source FROM component WHERE id=?", String.class, created.path("id").asLong()))
.isEqualTo(created.path("source").asText());
mvc.perform(get("/admin-api/component-assets/cadworld/production.rfs"))
.andExpect(status().isUnprocessableEntity()).andExpect(jsonPath("$.code").value("INVALID_STORAGE_OBJECT_KEY"));
}
@Test
void sourceUploadBindsHashAndRetriesWithoutChangingGeometryOrLeakingNewKeys() throws Exception {
send("/admin-api/interfaces", interfaceRequest("source-upload"));
@@ -372,7 +450,7 @@ class AdminApiTest {
mvc.perform(put("/admin-api/component-assets/upload").param("objectKey", key)
.header("X-Upload-Session-Token", expired).content(new byte[]{1}))
.andExpect(status().isUnprocessableEntity());
mvc.perform(put("/admin-api/component-assets/upload").param("objectKey", "components/cover-images/0/other/cover.png")
mvc.perform(put("/admin-api/component-assets/upload").param("objectKey", "cadworld_dev/components/cover-images/0/other/cover.png")
.header("X-Upload-Session-Token", session.path("uploadSessionToken").asText()).content(new byte[]{1}))
.andExpect(status().isUnprocessableEntity());
assertThat(logCount()).isEqualTo(5);
@@ -407,7 +485,7 @@ class AdminApiTest {
}
private Map<String, Object> component(String name) {
return Map.of("componentName", name, "robotType", "humanoid", "company", "test", "model", "m1", "source", "components/sample.rfs",
return Map.of("componentName", name, "robotType", "humanoid", "company", "test", "model", "m1", "source", "cadworld_dev/components/sample.rfs",
"componentJson", Map.of("interfaces", new Object[0]), "assemblyMetadata", Map.of("tags", new String[]{"test"}, "capabilities", new Object[0], "dimensions", Map.of()));
}
@@ -6,8 +6,6 @@ import java.nio.charset.StandardCharsets;
import java.sql.DriverManager;
import java.util.regex.Pattern;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.ByteArrayResource;
@@ -15,38 +13,15 @@ import org.springframework.jdbc.datasource.init.ScriptUtils;
import static org.assertj.core.api.Assertions.*;
/** Opt-in integration profile: dedicated disposable MySQL schema and MinIO bucket only. */
/** 显式启用的 MySQL 集成测试;资源使用测试代码中的内存替身。 */
@SpringBootTest(properties = {
"spring.datasource.url=${cadworld.it.db-url:jdbc:mysql://127.0.0.1:13306/cadworld_verification?serverTimezone=Asia/Shanghai}",
"spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver",
"spring.datasource.username=${cadworld.it.db-user:root}", "spring.datasource.password=${cadworld.it.db-password:}",
"spring.sql.init.mode=never", "app.storage.provider=minio",
"app.minio.endpoint=${cadworld.it.minio-endpoint:http://127.0.0.1:19000}",
"app.minio.access-key=${cadworld.it.minio-user:cadworld-test}", "app.minio.secret-key=${cadworld.it.minio-password:cadworld-test-secret}",
"app.minio.bucket=cadworld-verification", "logging.file.path=target/integration-logs"
"spring.sql.init.mode=never", "app.storage.root-prefix=cadworld_dev",
"logging.file.path=target/integration-logs"
})
class ExternalServicesIT extends AdminApiTest {
@Autowired com.linkerbot.cadworld.service.client.ObjectStorageClient storage;
@Test
void minioStreamLimitPreservesExistingObjectAndMapsTo413() throws Exception {
String key = "limit-verification/" + java.util.UUID.randomUUID() + "/model.rfs";
try {
storage.putObject(key, "application/octet-stream", new java.io.ByteArrayInputStream(new byte[]{1, 2}), 2);
var bounded = com.linkerbot.cadworld.service.support.UploadLimits.bounded(
new java.io.ByteArrayInputStream(new byte[9]), 8);
assertThatThrownBy(() -> storage.putObject(key, "application/octet-stream", bounded, -1))
.isInstanceOf(com.linkerbot.cadworld.common.exception.BizException.class)
.extracting("httpStatus").isEqualTo(413);
var downloaded = new java.io.ByteArrayOutputStream();
storage.writeObjectTo(key, downloaded);
assertThat(downloaded.toByteArray()).containsExactly((byte) 1, (byte) 2);
} finally {
storage.deleteObject(key);
}
assertThat(storage.objectExists(key)).isFalse();
}
@BeforeAll
static void prepareAndVerifySql() throws Exception {
String url = System.getProperty("cadworld.it.db-url", "jdbc:mysql://127.0.0.1:13306/cadworld_verification?serverTimezone=Asia/Shanghai");
@@ -0,0 +1,87 @@
package com.linkerbot.cadworld;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.StorageProperties;
import com.linkerbot.cadworld.service.client.ObjectStorageClient;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import com.linkerbot.cadworld.service.support.UploadLimits;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.web.util.UriComponentsBuilder;
/** 仅用于自动化业务测试;运行时始终使用真实 OSS。 */
@TestConfiguration(proxyBeanMethods = false)
class InMemoryObjectStorageConfiguration {
@Bean
@Primary
InMemoryObjectStorageClient inMemoryObjectStorageClient(StorageProperties properties, StorageObjectKeys keys) {
return new InMemoryObjectStorageClient(properties, keys);
}
static class InMemoryObjectStorageClient implements ObjectStorageClient {
private record Entry(String contentType, byte[] bytes) { }
private final Map<String, Entry> objects = new ConcurrentHashMap<>();
private final StorageProperties properties;
private final StorageObjectKeys keys;
InMemoryObjectStorageClient(StorageProperties properties, StorageObjectKeys keys) {
this.properties = properties;
this.keys = keys;
}
void clear() { objects.clear(); }
@Override
public String createUploadUrl(String key, String contentType, Duration duration) {
return UriComponentsBuilder.fromUriString(properties.publicApiBaseUrl())
.path("/admin-api/component-assets/upload")
.queryParam("objectKey", keys.requireKey(key)).build().encode().toUriString();
}
@Override
public void putObject(String key, String contentType, InputStream input, long length) {
String normalized = keys.requireKey(key);
UploadLimits.checkLength(length, properties.maxUploadBytes());
try {
byte[] bytes = UploadLimits.bounded(input, properties.maxUploadBytes()).readAllBytes();
objects.put(normalized, new Entry(contentType, bytes));
} catch (IOException exception) {
throw new IllegalStateException(exception);
}
}
@Override
public boolean objectExists(String key) { return objects.containsKey(keys.requireKey(key)); }
@Override
public void deleteObject(String key) { objects.remove(keys.requireKey(key)); }
@Override
public StoredObject statObject(String key) {
Entry entry = require(key);
return new StoredObject(entry.contentType(), entry.bytes().length);
}
@Override
public void writeObjectTo(String key, OutputStream output) {
try {
output.write(require(key).bytes());
} catch (IOException exception) {
throw new IllegalStateException(exception);
}
}
private Entry require(String key) {
Entry entry = objects.get(keys.requireKey(key));
if (entry == null) throw new BizException(404, "OSS_OBJECT_NOT_FOUND", "文件不存在");
return entry;
}
}
}
@@ -0,0 +1,85 @@
package com.linkerbot.cadworld;
import static org.assertj.core.api.Assertions.*;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.OssClientConfiguration;
import com.linkerbot.cadworld.config.OssProperties;
import com.linkerbot.cadworld.config.StorageProperties;
import com.linkerbot.cadworld.service.client.OssObjectStorageClient;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
/** 通过 -P oss-integration 单独启用;不加载数据库或业务测试内存替身。 */
@SpringBootTest(classes = OssObjectStorageIT.Config.class, properties = {
"spring.main.web-application-type=none", "app.storage.max-upload-bytes=8",
"logging.file.path=target/oss-integration-logs"
})
@ActiveProfiles("test")
class OssObjectStorageIT {
private static final String RUN_ID = UUID.randomUUID().toString();
@Autowired OssObjectStorageClient storage;
@Autowired StorageObjectKeys keys;
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({StorageProperties.class, OssProperties.class})
@Import({OssClientConfiguration.class, OssObjectStorageClient.class, StorageObjectKeys.class})
static class Config { }
@DynamicPropertySource
static void isolateObjects(DynamicPropertyRegistry registry) {
String root = System.getProperty("cadworld.it.oss-root-prefix", "cadworld_dev").trim().replaceAll("/+$", "");
if (root.equals("cadworld") || root.startsWith("cadworld/")) {
throw new IllegalArgumentException("OSS 集成测试不能使用默认生产目录 cadworld");
}
registry.add("app.storage.root-prefix", () -> root + "/verification/" + RUN_ID);
}
@Test
void realOssUploadMetadataDownloadLimitAndCleanup() throws IOException {
String key = keys.underRoot("model.rfs");
Path journal = Path.of("logs", "oss-verification-pending", RUN_ID + ".txt");
Files.createDirectories(journal.getParent());
Files.writeString(journal, key + "\n");
Throwable originalFailure = null;
try {
storage.putObject(key, "application/octet-stream", new ByteArrayInputStream(new byte[]{1, 2}), 2);
assertThat(storage.objectExists(key)).isTrue();
assertThat(storage.statObject(key).contentLength()).isEqualTo(2);
assertThat(storage.statObject(key).contentType()).isEqualTo("application/octet-stream");
assertThatThrownBy(() -> storage.putObject(key, null, new ByteArrayInputStream(new byte[9]), -1))
.isInstanceOf(BizException.class).extracting("httpStatus").isEqualTo(413);
ByteArrayOutputStream output = new ByteArrayOutputStream();
storage.writeObjectTo(key, output);
assertThat(output.toByteArray()).containsExactly((byte) 1, (byte) 2);
} catch (RuntimeException | Error failure) {
originalFailure = failure;
throw failure;
} finally {
try {
storage.deleteObject(key);
Files.deleteIfExists(journal);
} catch (RuntimeException | IOException cleanupFailure) {
if (originalFailure == null) throw cleanupFailure;
originalFailure.addSuppressed(cleanupFailure);
}
}
assertThat(storage.objectExists(key)).isFalse();
assertThatThrownBy(() -> storage.statObject(key))
.isInstanceOf(BizException.class).extracting("code").isEqualTo("OSS_OBJECT_NOT_FOUND");
}
}
@@ -0,0 +1,43 @@
package com.linkerbot.cadworld;
import static org.assertj.core.api.Assertions.*;
import com.linkerbot.cadworld.config.StorageProperties;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;
class StorageEnvironmentTest {
final ApplicationContextRunner runner = new ApplicationContextRunner()
.withInitializer(new ConfigDataApplicationContextInitializer())
.withUserConfiguration(Config.class);
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(StorageProperties.class)
static class Config { }
@Test
void testAndProductionHaveSeparateDefaultsAndDefaultProfileIsTest() {
runner.run(context -> assertThat(context.getBean(StorageProperties.class).rootPrefix()).isEqualTo("cadworld_dev"));
runner.withPropertyValues("spring.profiles.active=test").run(context ->
assertThat(context.getBean(StorageProperties.class).rootPrefix()).isEqualTo("cadworld_dev"));
runner.withPropertyValues("spring.profiles.active=prod").run(context ->
assertThat(context.getBean(StorageProperties.class).rootPrefix()).isEqualTo("cadworld"));
}
@Test
void rootOverrideWorksInBothProfilesAndIsAddedOnce() {
for (String profile : new String[]{"test", "prod"}) {
runner.withPropertyValues("spring.profiles.active=" + profile, "OSS_ROOT_PREFIX=custom/assets/")
.run(context -> {
StorageObjectKeys keys = new StorageObjectKeys(context.getBean(StorageProperties.class));
String key = keys.underRoot("components/urdf/model.rfs");
assertThat(key).isEqualTo("custom/assets/components/urdf/model.rfs");
assertThat(keys.requireKey(key)).isEqualTo(key);
});
}
}
}
@@ -21,9 +21,10 @@ import static org.assertj.core.api.Assertions.*;
"spring.sql.init.mode=always", "spring.sql.init.schema-locations=classpath:test-schema.sql",
"spring.servlet.multipart.max-file-size=1KB", "spring.servlet.multipart.max-request-size=2KB",
"app.storage.max-upload-bytes=1024",
"app.storage.provider=local", "app.storage.local-root=target/test-http-assets", "logging.file.path=target/test-logs"
"app.storage.root-prefix=cadworld_dev", "logging.file.path=target/test-logs"
})
@ActiveProfiles("verification")
@org.springframework.context.annotation.Import(InMemoryObjectStorageConfiguration.class)
class UploadHttpTest {
@Autowired TestRestTemplate http;
@Autowired JdbcTemplate jdbc;
@@ -58,16 +58,17 @@ class InterfaceBoundaryTest {
byte[] bytes="real mesh bytes".getBytes(StandardCharsets.UTF_8);
String hash=HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));
ObjectStorageClient storage=mock(ObjectStorageClient.class);
doAnswer(invocation->{invocation.<OutputStream>getArgument(1).write(bytes);return null;}).when(storage).writeObjectTo(eq("objects/mesh"),any());
UrdfAssetVerifier verifier=new UrdfAssetVerifier(storage);
ObjectNode source=mapper.createObjectNode();source.putArray("resources").addObject().put("reference","a.stl").put("sourceKey","objects/mesh").put("sha256",hash);
doAnswer(invocation->{invocation.<OutputStream>getArgument(1).write(bytes);return null;}).when(storage).writeObjectTo(eq("cadworld/objects/mesh"),any());
UrdfAssetVerifier verifier=new UrdfAssetVerifier(storage, new com.linkerbot.cadworld.service.support.StorageObjectKeys(
new com.linkerbot.cadworld.config.StorageProperties("cadworld", null, null, null, null, java.time.Duration.ofMinutes(30))));
ObjectNode source=mapper.createObjectNode();source.putArray("resources").addObject().put("reference","a.stl").put("sourceKey","cadworld/objects/mesh").put("sha256",hash);
verifier.verify(source);
((ObjectNode)source.path("resources").get(0)).put("sha256","tampered");
assertThatThrownBy(()->verifier.verify(source)).hasMessageContaining("已变化");
clearInvocations(storage);
for(String key:List.of("https://remote.test/a","/etc/passwd","objects/../secret","objects\\secret")) {
((ObjectNode)source.path("resources").get(0)).put("sourceKey",key);
assertThatThrownBy(()->verifier.verify(source)).hasMessageContaining("存储对象");
assertThatThrownBy(()->verifier.verify(source)).hasMessageContaining("存储对象路径非法");
}
verifyNoInteractions(storage);
}
+39 -2
View File
@@ -30,8 +30,45 @@
<artifactId>mybatis-plus-jsqlparser</artifactId>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- OSS SDK 使用 javax JAXB;显式版本避免被 Spring Boot 的 Jakarta JAXB 覆盖。 -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
<exclusions>
<exclusion>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</exclusion>
<exclusion>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>txw2</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
@@ -1,12 +0,0 @@
package com.linkerbot.cadworld.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "app.minio")
public record MinioProperties(
String endpoint,
String accessKey,
String secretKey,
String bucket
) {
}
@@ -0,0 +1,28 @@
package com.linkerbot.cadworld.config;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
@Configuration(proxyBeanMethods = false)
public class OssClientConfiguration {
@Bean(destroyMethod = "shutdown")
@Lazy
public OSS ossClient(OssProperties properties) {
properties.validate();
ClientBuilderConfiguration configuration = new ClientBuilderConfiguration();
configuration.setSignatureVersion(SignVersion.V4);
return OSSClientBuilder.create()
.endpoint(properties.endpoint())
.region(properties.regionId())
.credentialsProvider(new DefaultCredentialProvider(
properties.accessKeyId().trim(), properties.accessKeySecret().trim()))
.clientConfiguration(configuration)
.build();
}
}
@@ -0,0 +1,40 @@
package com.linkerbot.cadworld.config;
import com.linkerbot.cadworld.common.exception.BizException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;
@ConfigurationProperties(prefix = "app.oss")
public record OssProperties(String accessKeyId, String accessKeySecret, String region, String bucket) {
public void validate() {
requireText(accessKeyId, "OSS_ACCESS_KEY_ID");
requireText(accessKeySecret, "OSS_ACCESS_KEY_SECRET");
requireText(bucket, "OSS_BUCKET");
regionId();
}
public String regionId() {
String value = requireText(region, "OSS_REGION");
String normalized = value.startsWith("oss-") ? value.substring(4) : value;
if (!normalized.matches("[a-z][a-z0-9-]+")) {
throw new BizException(500, "OSS_CONFIG_ERROR", "OSS_REGION 配置非法");
}
return normalized;
}
public String endpoint() {
return "https://oss-" + regionId() + ".aliyuncs.com";
}
private String requireText(String value, String name) {
if (!StringUtils.hasText(value)) {
throw new BizException(500, "OSS_CONFIG_ERROR", "OSS 配置缺失: " + name);
}
return value.trim();
}
@Override
public String toString() {
return "OssProperties[region=" + region + ", bucket=" + bucket + "]";
}
}
@@ -7,27 +7,19 @@ import org.springframework.boot.context.properties.bind.DefaultValue;
@ConfigurationProperties(prefix = "app.storage")
public record StorageProperties(
String provider,
@DefaultValue("cadworld") String rootPrefix,
String urdfPrefix,
String coverImagePrefix,
String publicBaseUrl,
String publicApiBaseUrl,
String localRoot,
Duration uploadSessionTtl,
@DefaultValue("104857600") long maxUploadBytes
) {
@ConstructorBinding
public StorageProperties { }
public StorageProperties(String provider, String urdfPrefix, String coverImagePrefix,
String publicBaseUrl, String publicApiBaseUrl, String localRoot,
Duration uploadSessionTtl) {
this(provider, urdfPrefix, coverImagePrefix, publicBaseUrl, publicApiBaseUrl,
localRoot, uploadSessionTtl, 104857600L);
}
public StorageProperties(String provider, String urdfPrefix, String coverImagePrefix,
public StorageProperties(String rootPrefix, String urdfPrefix, String coverImagePrefix,
String publicBaseUrl, String publicApiBaseUrl, Duration uploadSessionTtl) {
this(provider, urdfPrefix, coverImagePrefix, publicBaseUrl, publicApiBaseUrl, "./data/assets", uploadSessionTtl);
this(rootPrefix, urdfPrefix, coverImagePrefix, publicBaseUrl, publicApiBaseUrl, uploadSessionTtl, 104857600L);
}
}
@@ -2,6 +2,7 @@ package com.linkerbot.cadworld.mechanicalinterface;
import com.fasterxml.jackson.databind.JsonNode;
import com.linkerbot.cadworld.service.client.ObjectStorageClient;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import java.io.OutputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
@@ -15,13 +16,12 @@ import org.springframework.stereotype.Service;
@Service @RequiredArgsConstructor
public class UrdfAssetVerifier {
private final ObjectStorageClient storage;
private final StorageObjectKeys storageObjectKeys;
public void verify(JsonNode source) {
Map<String,String> checked = new HashMap<>();
for (JsonNode resource : source.path("resources")) {
String key=resource.path("sourceKey").asText(), expected=resource.path("sha256").asText();
if (key.isBlank() || key.startsWith("/") || key.contains(":") || key.contains("\\")
|| java.util.Arrays.asList(key.split("/")).contains(".."))
throw new IllegalArgumentException("URDF 资源必须引用已上传的存储对象");
String key=storageObjectKeys.requireKey(resource.path("sourceKey").asText());
String expected=resource.path("sha256").asText();
try {
String digest=checked.get(key);
if(digest==null) {
@@ -5,6 +5,7 @@ import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.StorageProperties;
import com.linkerbot.cadworld.service.client.ObjectStorageClient;
import com.linkerbot.cadworld.service.support.UploadLimits;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@@ -26,6 +27,7 @@ public class InterfaceAssetService {
private final InterfaceDefinitionService definitions;
private final ObjectStorageClient storage;
private final StorageProperties properties;
private final StorageObjectKeys storageObjectKeys;
public String upload(String definitionId, int geometryRevision, MultipartFile file) {
if (file == null || file.isEmpty()) throw new BizException(422, "INVALID_INTERFACE_ASSET", "接口源文件不能为空");
@@ -45,7 +47,7 @@ public class InterfaceAssetService {
String hash = "sha256:" + HexFormat.of().formatHex(digest.digest());
String oldKey = current.path("sourceAssetKey").asText();
if (hash.equals(current.path("sourceGeometryHash").asText())
&& !oldKey.isBlank() && storage.objectExists(oldKey)) {
&& oldKey.startsWith(storageObjectKeys.rootPrefix()) && storage.objectExists(oldKey)) {
// Revalidate under the database lock even for an idempotent retry.
definitions.attachAsset(definitionId, geometryRevision, oldKey, hash, oldKey);
return oldKey;
@@ -54,10 +56,11 @@ public class InterfaceAssetService {
&& !hash.equals(current.path("sourceGeometryHash").asText())) {
throw new BizException(422, "INVALID_INTERFACE_ASSET", "该修订已有源文件,请创建新几何修订");
}
String key = "interfaces/" + HexFormat.of().formatHex(definitionId.getBytes(StandardCharsets.UTF_8))
String key = storageObjectKeys.rootPrefix() + "interfaces/" + HexFormat.of().formatHex(definitionId.getBytes(StandardCharsets.UTF_8))
+ "/" + geometryRevision + "/" + hash.substring(7) + "/" + UUID.randomUUID()
+ "/" + filename(file.getOriginalFilename());
if (key.length() > 512) throw new BizException(422, "INVALID_INTERFACE_ASSET", "源文件对象路径超过512字符,请缩短文件名或接口ID");
storageObjectKeys.requireKey(key);
// Each request owns its object; compensation cannot delete another upload's object.
uploadedKey = key;
try (var input = Files.newInputStream(temporary)) {
@@ -89,8 +92,8 @@ public class InterfaceAssetService {
try {
storage.deleteObject(key);
} catch (RuntimeException exception) {
log.error("接口源文件补偿清理失败 definitionId={} geometryRevision={} requestId={} objectKey={}",
definitionId, revision, RequestIdHolder.get(), key, exception);
log.error("接口源文件补偿清理失败 definitionId={} geometryRevision={} requestId={} keySummary={} exceptionType={}",
definitionId, revision, RequestIdHolder.get(), StorageObjectKeys.summary(key), exception.getClass().getSimpleName());
}
}
@@ -1,150 +0,0 @@
package com.linkerbot.cadworld.service.client;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.StorageProperties;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
@Slf4j
@Component
@ConditionalOnProperty(name = "app.storage.provider", havingValue = "local")
public class LocalObjectStorageClient implements ObjectStorageClient {
private final StorageProperties properties;
private final Path root;
public LocalObjectStorageClient(StorageProperties properties) {
this.properties = properties;
this.root = Path.of(StringUtils.hasText(properties.localRoot()) ? properties.localRoot() : "./data/assets")
.toAbsolutePath().normalize();
try {
Files.createDirectories(root);
} catch (IOException exception) {
throw new IllegalStateException("无法创建本地资源目录: " + root, exception);
}
}
@Override
public String createUploadUrl(String objectKey, String contentType, Duration duration) {
if (duration == null || duration.isZero() || duration.isNegative()) {
throw new BizException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "STORAGE_CONFIG_ERROR", "上传会话有效期配置非法");
}
String base = StringUtils.hasText(properties.publicApiBaseUrl())
? properties.publicApiBaseUrl().trim() : "http://127.0.0.1:8992";
return UriComponentsBuilder.fromUriString(base.replaceAll("/+$", ""))
.path("/admin-api/component-assets/upload")
.queryParam("objectKey", normalizeKey(objectKey))
.build().encode().toUriString();
}
@Override
public void putObject(String objectKey, String contentType, InputStream inputStream, long contentLength) {
Path target = resolve(objectKey);
Path temporary = null;
try {
Files.createDirectories(Objects.requireNonNull(target.getParent()));
temporary = Files.createTempFile(target.getParent(), ".upload-", ".tmp");
Files.copy(inputStream, temporary, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
try {
Files.move(temporary, target, java.nio.file.StandardCopyOption.ATOMIC_MOVE,
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
} catch (java.nio.file.AtomicMoveNotSupportedException exception) {
Files.move(temporary, target, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException exception) {
log.error("Failed to write local object {}", objectKey, exception);
throw new BizException(500, "LOCAL_STORAGE_WRITE_FAILED", "文件写入本地目录失败");
} finally {
if (temporary != null) {
try {
Files.deleteIfExists(temporary);
} catch (IOException exception) {
log.warn("Failed to clean local upload temporary file", exception);
}
}
}
}
@Override
public void deleteObject(String objectKey) {
try {
Files.deleteIfExists(resolve(objectKey));
} catch (IOException exception) {
throw new BizException(500, "LOCAL_STORAGE_DELETE_FAILED", "清理本地文件失败");
}
}
@Override
public boolean objectExists(String objectKey) {
return Files.isRegularFile(resolve(objectKey), LinkOption.NOFOLLOW_LINKS);
}
@Override
public StoredObject statObject(String objectKey) {
Path file = resolve(objectKey);
try {
if (!Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)) {
throw notFound();
}
return new StoredObject(Files.probeContentType(file), Files.size(file));
} catch (IOException exception) {
throw notFound();
}
}
@Override
public void writeObjectTo(String objectKey, OutputStream outputStream) {
Path file = resolve(objectKey);
try {
if (!Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)) throw notFound();
try (InputStream input = Files.newInputStream(file)) {
input.transferTo(outputStream);
}
} catch (IOException exception) {
throw notFound();
}
}
private Path resolve(String key) {
String normalizedKey = normalizeKey(key);
Path candidate = root.resolve(normalizedKey).normalize();
if (!candidate.startsWith(root)) {
throw new BizException(HttpStatus.UNPROCESSABLE_ENTITY.value(), "INVALID_STORAGE_OBJECT_KEY", "对象路径非法");
}
// Check every existing path component so a symlink in the bundle cannot
// redirect uploads or downloads outside the configured storage root.
Path current = root;
for (String segment : normalizedKey.split("/")) {
current = current.resolve(segment);
if (Files.isSymbolicLink(current)) {
throw new BizException(HttpStatus.UNPROCESSABLE_ENTITY.value(), "INVALID_STORAGE_OBJECT_KEY", "对象路径不能包含符号链接");
}
}
return candidate;
}
private String normalizeKey(String key) {
String normalized = key == null ? "" : key.trim().replace('\\', '/');
while (normalized.startsWith("/")) normalized = normalized.substring(1);
if (!StringUtils.hasText(normalized) || normalized.equals("..") || normalized.contains("../")) {
throw new BizException(HttpStatus.UNPROCESSABLE_ENTITY.value(), "INVALID_STORAGE_OBJECT_KEY", "对象路径非法");
}
return normalized;
}
private BizException notFound() {
return new BizException(HttpStatus.NOT_FOUND.value(), "LOCAL_OBJECT_NOT_FOUND", "文件不存在");
}
}
@@ -1,280 +0,0 @@
package com.linkerbot.cadworld.service.client;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.MinioProperties;
import com.linkerbot.cadworld.config.StorageProperties;
import io.minio.BucketExistsArgs;
import io.minio.GetObjectArgs;
import io.minio.GetObjectResponse;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import com.linkerbot.cadworld.service.support.UploadLimits;
import io.minio.StatObjectArgs;
import io.minio.StatObjectResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.time.Duration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
@Slf4j
@Component
@ConditionalOnProperty(name = "app.storage.provider", havingValue = "minio", matchIfMissing = true)
public class MinioObjectStorageClient implements ObjectStorageClient {
private static final long UNKNOWN_SIZE_PART_SIZE = 10L * 1024 * 1024;
private final MinioProperties properties;
private final StorageProperties storageProperties;
private volatile MinioClient minioClient;
private volatile boolean bucketReady;
public MinioObjectStorageClient(MinioProperties properties, StorageProperties storageProperties) {
this.properties = properties;
this.storageProperties = storageProperties;
}
@Override
public String createUploadUrl(String objectKey, String contentType, Duration duration) {
validateConfiguration();
if (duration == null || duration.isZero() || duration.isNegative()) {
throw configError("MinIO 上传会话有效期配置非法");
}
String baseUrl = requireText(storageProperties.publicApiBaseUrl(), "APP_STORAGE_PUBLIC_API_BASE_URL");
return UriComponentsBuilder.fromUriString(trimTrailingSlash(baseUrl))
.path("/admin-api/component-assets/upload")
.queryParam("objectKey", normalizeObjectKey(objectKey))
.build()
.encode()
.toUriString();
}
@Override
public void putObject(String objectKey, String contentType, InputStream inputStream, long contentLength) {
validateConfiguration();
ensureBucket();
try {
long objectSize = contentLength >= 0 ? contentLength : -1;
long partSize = contentLength >= 0 ? -1 : UNKNOWN_SIZE_PART_SIZE;
getMinioClient().putObject(PutObjectArgs.builder()
.bucket(properties.bucket().trim())
.object(normalizeObjectKey(objectKey))
.contentType(normalizeContentType(contentType))
.stream(inputStream, objectSize, partSize)
.build());
} catch (Exception exception) {
UploadLimits.rethrowIfExceeded(exception);
restoreInterrupt(exception);
log.error("Failed to upload object to MinIO. objectKey={}", objectKey, exception);
throw new BizException(HttpStatus.BAD_GATEWAY.value(), "MINIO_UPLOAD_FAILED", "文件上传到 MinIO 失败");
}
}
@Override
public boolean objectExists(String objectKey) {
validateConfiguration();
ensureBucket();
try {
getMinioClient().statObject(StatObjectArgs.builder()
.bucket(properties.bucket().trim())
.object(normalizeObjectKey(objectKey))
.build());
return true;
} catch (io.minio.errors.ErrorResponseException exception) {
String code = exception.errorResponse() == null ? "" : exception.errorResponse().code();
if ("NoSuchKey".equals(code) || "NoSuchObject".equals(code) || "NotFound".equals(code)) {
return false;
}
log.error("Failed to check MinIO object. objectKey={}", objectKey, exception);
throw new BizException(HttpStatus.BAD_GATEWAY.value(), "MINIO_OBJECT_CHECK_FAILED", "校验 MinIO 上传结果失败");
} catch (Exception exception) {
restoreInterrupt(exception);
log.error("Failed to check MinIO object. objectKey={}", objectKey, exception);
throw new BizException(HttpStatus.BAD_GATEWAY.value(), "MINIO_OBJECT_CHECK_FAILED", "校验 MinIO 上传结果失败");
}
}
@Override
public void deleteObject(String objectKey) {
validateConfiguration();
try {
getMinioClient().removeObject(RemoveObjectArgs.builder()
.bucket(properties.bucket().trim()).object(normalizeObjectKey(objectKey)).build());
} catch (Exception exception) {
restoreInterrupt(exception);
throw new BizException(502, "MINIO_DELETE_FAILED", "清理 MinIO 文件失败");
}
}
@Override
public StoredObject statObject(String objectKey) {
validateConfiguration();
ensureBucket();
String normalizedKey = normalizeObjectKey(objectKey);
try {
StatObjectResponse stat = getMinioClient().statObject(StatObjectArgs.builder()
.bucket(properties.bucket().trim())
.object(normalizedKey)
.build());
return new StoredObject(stat.contentType(), stat.size());
} catch (io.minio.errors.ErrorResponseException exception) {
throw mapDownloadException(objectKey, exception);
} catch (Exception exception) {
throw mapDownloadException(objectKey, exception);
}
}
@Override
public void writeObjectTo(String objectKey, OutputStream outputStream) {
validateConfiguration();
ensureBucket();
String normalizedKey = normalizeObjectKey(objectKey);
try (GetObjectResponse input = getMinioClient().getObject(GetObjectArgs.builder()
.bucket(properties.bucket().trim())
.object(normalizedKey)
.build())) {
input.transferTo(outputStream);
} catch (io.minio.errors.ErrorResponseException exception) {
throw mapDownloadException(objectKey, exception);
} catch (Exception exception) {
throw mapDownloadException(objectKey, exception);
}
}
private BizException mapDownloadException(String objectKey, Exception exception) {
restoreInterrupt(exception);
if (exception instanceof io.minio.errors.ErrorResponseException errorResponseException) {
String code = errorResponseException.errorResponse() == null ? "" : errorResponseException.errorResponse().code();
if ("NoSuchKey".equals(code) || "NoSuchObject".equals(code) || "NotFound".equals(code)) {
return new BizException(HttpStatus.NOT_FOUND.value(), "MINIO_OBJECT_NOT_FOUND", "文件不存在");
}
}
log.error("Failed to download MinIO object. objectKey={}", objectKey, exception);
return new BizException(HttpStatus.BAD_GATEWAY.value(), "MINIO_DOWNLOAD_FAILED", "从 MinIO 读取文件失败");
}
private void ensureBucket() {
if (bucketReady) {
return;
}
synchronized (this) {
if (bucketReady) {
return;
}
try {
String bucket = properties.bucket().trim();
if (!getMinioClient().bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) {
getMinioClient().makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
}
bucketReady = true;
} catch (Exception exception) {
restoreInterrupt(exception);
log.error("Failed to initialize MinIO bucket. bucket={}, endpoint={}",
properties.bucket(), properties.endpoint(), exception);
throw new BizException(
HttpStatus.BAD_GATEWAY.value(),
"MINIO_BUCKET_FAILED",
"MinIO Bucket 初始化失败:" + classifyBucketFailure(exception));
}
}
}
private String classifyBucketFailure(Exception exception) {
if (exception instanceof io.minio.errors.ErrorResponseException errorResponseException
&& errorResponseException.errorResponse() != null) {
String code = errorResponseException.errorResponse().code();
if ("AccessDenied".equals(code) || "InvalidAccessKeyId".equals(code)
|| "SignatureDoesNotMatch".equals(code)) {
return "MinIO 账号或 Bucket 权限不足 (" + code + ")";
}
return "MinIO 返回错误 (" + code + ")";
}
Throwable cause = exception;
while (cause.getCause() != null && cause.getCause() != cause) {
cause = cause.getCause();
}
if (cause instanceof java.net.ConnectException) {
return "无法连接 MinIO,请检查 endpoint 和网络";
}
if (cause instanceof java.net.UnknownHostException) {
return "无法解析 MinIO 地址,请检查 Docker 网络和容器名";
}
return "请检查 MinIO endpoint、账号和 Bucket 权限";
}
private MinioClient getMinioClient() {
MinioClient client = minioClient;
if (client != null) {
return client;
}
synchronized (this) {
if (minioClient == null) {
minioClient = MinioClient.builder()
.endpoint(normalizeEndpoint(properties.endpoint()))
.credentials(properties.accessKey().trim(), properties.secretKey().trim())
.build();
}
return minioClient;
}
}
private void validateConfiguration() {
requireText(properties.endpoint(), "APP_MINIO_ENDPOINT");
requireText(properties.accessKey(), "APP_MINIO_ACCESS_KEY");
requireText(properties.secretKey(), "APP_MINIO_SECRET_KEY");
requireText(properties.bucket(), "APP_MINIO_BUCKET");
}
private String requireText(String value, String property) {
if (!StringUtils.hasText(value)) {
throw configError("MinIO 配置缺失: " + property);
}
return value.trim();
}
private BizException configError(String message) {
return new BizException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "MINIO_CONFIG_ERROR", message);
}
private static String normalizeEndpoint(String endpoint) {
String value = endpoint.trim();
URI uri = URI.create(value.startsWith("http://") || value.startsWith("https://") ? value : "http://" + value);
return uri.toString();
}
private static String normalizeObjectKey(String objectKey) {
String normalized = objectKey == null ? "" : objectKey.trim().replace("\\", "/");
while (normalized.startsWith("/")) {
normalized = normalized.substring(1);
}
if (!StringUtils.hasText(normalized) || normalized.contains("../") || normalized.equals("..")) {
throw new BizException(HttpStatus.UNPROCESSABLE_ENTITY.value(), "INVALID_STORAGE_OBJECT_KEY", "对象路径非法");
}
return normalized;
}
private static String normalizeContentType(String contentType) {
return StringUtils.hasText(contentType) ? contentType.trim() : "application/octet-stream";
}
private static String trimTrailingSlash(String value) {
String result = value.trim();
while (result.endsWith("/")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
private static void restoreInterrupt(Exception exception) {
if (exception instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
}
}
@@ -0,0 +1,188 @@
package com.linkerbot.cadworld.service.client;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.linkerbot.cadworld.common.api.RequestIdHolder;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.OssProperties;
import com.linkerbot.cadworld.config.StorageProperties;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import com.linkerbot.cadworld.service.support.UploadLimits;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
@Slf4j
@Component
public class OssObjectStorageClient implements ObjectStorageClient {
private final OSS oss;
private final OssProperties properties;
private final StorageProperties storageProperties;
private final StorageObjectKeys keys;
public OssObjectStorageClient(@Lazy OSS oss, OssProperties properties,
StorageProperties storageProperties, StorageObjectKeys keys) {
this.oss = oss;
this.properties = properties;
this.storageProperties = storageProperties;
this.keys = keys;
}
@Override
public String createUploadUrl(String objectKey, String contentType, Duration duration) {
String key = keys.requireKey(objectKey);
properties.validate();
if (duration == null || duration.isZero() || duration.isNegative()
|| !StringUtils.hasText(storageProperties.publicApiBaseUrl())) {
throw new BizException(500, "STORAGE_CONFIG_ERROR", "上传会话有效期或后台访问地址配置非法");
}
return UriComponentsBuilder.fromUriString(storageProperties.publicApiBaseUrl().trim().replaceAll("/+$", ""))
.path("/admin-api/component-assets/upload")
.queryParam("objectKey", key).build().encode().toUriString();
}
@Override
public void putObject(String objectKey, String contentType, InputStream inputStream, long contentLength) {
String key = keys.requireKey(objectKey);
properties.validate();
UploadLimits.checkLength(contentLength, storageProperties.maxUploadBytes());
Path temporary = null;
long started = System.nanoTime();
try {
InputStream bounded = UploadLimits.bounded(inputStream, storageProperties.maxUploadBytes());
if (contentLength < 0) {
// 在请求 OSS 之前完整检查未知长度流,超限不能覆盖原有对象。
temporary = Files.createTempFile("cadworld-oss-", ".upload");
Files.copy(bounded, temporary, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
try (InputStream staged = Files.newInputStream(temporary)) {
upload(key, contentType, staged, Files.size(temporary));
}
} else {
upload(key, contentType, bounded, contentLength);
}
log.info("OSS 上传成功 bucket={} keySummary={} requestId={} durationMs={}",
properties.bucket().trim(), StorageObjectKeys.summary(key), RequestIdHolder.get(),
(System.nanoTime() - started) / 1_000_000);
} catch (Exception exception) {
throw mapFailure("UPLOAD", key, exception, false);
} finally {
if (temporary != null) {
try {
Files.deleteIfExists(temporary);
} catch (IOException exception) {
log.warn("OSS 上传临时文件清理失败 keySummary={} requestId={} exceptionType={}",
StorageObjectKeys.summary(key), RequestIdHolder.get(), exception.getClass().getSimpleName());
}
}
}
}
private void upload(String key, String contentType, InputStream input, long length) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(length);
metadata.setContentType(StringUtils.hasText(contentType) ? contentType.trim() : "application/octet-stream");
oss.putObject(new PutObjectRequest(properties.bucket().trim(), key, input, metadata));
}
@Override
public boolean objectExists(String objectKey) {
String key = keys.requireKey(objectKey);
properties.validate();
try {
oss.getObjectMetadata(properties.bucket().trim(), key);
return true;
} catch (OSSException exception) {
if (isMissingObject(exception)) return false;
throw mapFailure("OBJECT_CHECK", key, exception, false);
} catch (RuntimeException exception) {
throw mapFailure("OBJECT_CHECK", key, exception, false);
}
}
@Override
public StoredObject statObject(String objectKey) {
String key = keys.requireKey(objectKey);
properties.validate();
try {
ObjectMetadata metadata = oss.getObjectMetadata(properties.bucket().trim(), key);
return new StoredObject(metadata.getContentType(), metadata.getContentLength());
} catch (RuntimeException exception) {
throw mapFailure("DOWNLOAD", key, exception, true);
}
}
@Override
public void writeObjectTo(String objectKey, OutputStream outputStream) {
String key = keys.requireKey(objectKey);
properties.validate();
long started = System.nanoTime();
try (OSSObject object = oss.getObject(properties.bucket().trim(), key)) {
object.getObjectContent().transferTo(outputStream);
log.info("OSS 下载成功 bucket={} keySummary={} requestId={} durationMs={}",
properties.bucket().trim(), StorageObjectKeys.summary(key), RequestIdHolder.get(),
(System.nanoTime() - started) / 1_000_000);
} catch (Exception exception) {
throw mapFailure("DOWNLOAD", key, exception, true);
}
}
@Override
public void deleteObject(String objectKey) {
String key = keys.requireKey(objectKey);
properties.validate();
try {
oss.deleteObject(properties.bucket().trim(), key);
log.info("OSS 清理成功 bucket={} keySummary={} requestId={}",
properties.bucket().trim(), StorageObjectKeys.summary(key), RequestIdHolder.get());
} catch (RuntimeException exception) {
throw mapFailure("DELETE", key, exception, false);
}
}
private boolean isMissingObject(OSSException exception) {
return "NoSuchKey".equals(exception.getErrorCode());
}
private BizException mapFailure(String operation, String key, Exception exception, boolean mapNotFound) {
UploadLimits.rethrowIfExceeded(exception);
OSSException ossError = null;
String causeType = exception.getClass().getSimpleName();
for (Throwable cause = exception; cause != null; cause = cause.getCause()) {
causeType = cause.getClass().getSimpleName();
if (cause instanceof InterruptedException
|| (cause instanceof InterruptedIOException && !(cause instanceof SocketTimeoutException))) {
Thread.currentThread().interrupt();
}
if (cause instanceof OSSException error) ossError = error;
if (cause instanceof BizException failure) return failure;
}
if (mapNotFound && ossError != null && isMissingObject(ossError)) {
return new BizException(404, "OSS_OBJECT_NOT_FOUND", "文件不存在");
}
// SDK 异常消息/原始响应可能包含认证信息,仅输出可定位的安全字段。
log.error("OSS 操作失败 operation={} bucket={} keySummary={} requestId={} ossCode={} ossRequestId={} exceptionType={} causeType={}",
operation, properties.bucket().trim(), StorageObjectKeys.summary(key), RequestIdHolder.get(),
ossError == null ? "-" : ossError.getErrorCode(),
ossError == null ? "-" : ossError.getRequestId(), exception.getClass().getSimpleName(), causeType);
String message = switch (operation) {
case "UPLOAD" -> "文件上传到 OSS 失败";
case "OBJECT_CHECK" -> "校验 OSS 上传结果失败";
case "DELETE" -> "清理 OSS 文件失败";
default -> "从 OSS 读取文件失败";
};
return new BizException(502, "OSS_" + operation + "_FAILED", message);
}
}
@@ -16,6 +16,7 @@ import com.linkerbot.cadworld.entity.Component;
import com.linkerbot.cadworld.mapper.ComponentMapper;
import com.linkerbot.cadworld.service.ComponentService;
import com.linkerbot.cadworld.mechanicalinterface.ComponentPortValidationService;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
@@ -35,6 +36,7 @@ public class ComponentServiceImpl implements ComponentService {
private final ObjectStorageUrlResolver objectStorageUrlResolver;
private final ObjectMapper objectMapper;
private final ComponentPortValidationService portValidation;
private final StorageObjectKeys storageObjectKeys;
@Override
@Transactional
@@ -143,12 +145,16 @@ public class ComponentServiceImpl implements ComponentService {
component.setRobotType(robotType.trim());
component.setCompany(company.trim());
component.setModel(model.trim());
component.setSource(source.trim());
component.setCoverImageKey(normalizeOptionalText(coverImageKey));
component.setSource(storageObjectKeys.requireKey(source));
component.setCoverImageKey(storageObjectKeys.optionalKey(coverImageKey));
component.setDescription(normalizeOptionalText(description));
int previousRevision = component.getComponentJson() == null ? 0
: component.getComponentJson().path("componentRevision").asInt(0);
ObjectNode validated = (ObjectNode) portValidation.validate(componentJson);
for (JsonNode resource : validated.path("urdfSource").path("resources")) {
String key = storageObjectKeys.requireKey(resource.path("sourceKey").asText());
((ObjectNode) resource).put("sourceKey", key);
}
validated.put("componentRevision", Math.incrementExact(previousRevision));
component.setComponentJson(validated);
component.setAssemblyMetadata(objectMapper.valueToTree(
@@ -14,6 +14,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.linkerbot.cadworld.service.support.UploadLimits;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@@ -27,6 +28,7 @@ public class ComponentUploadSupport {
private final StorageProperties storageProperties;
private final ObjectStorageClient objectStorageClient;
private final ComponentUploadSessionTokenProvider componentUploadSessionTokenProvider;
private final StorageObjectKeys storageObjectKeys;
public PreparedUploadSession prepareUploadSession(Long userId, ComponentUploadType uploadType, String keyPrefixRoot) {
Duration uploadSessionTtl = resolveUploadSessionTtl();
@@ -50,6 +52,7 @@ public class ComponentUploadSupport {
if (!StringUtils.hasText(claims.keyPrefix())) {
throw invalidUploadSession(invalidCode, "上传会话缺少对象前缀");
}
storageObjectKeys.requirePrefix(claims.keyPrefix());
if (!userId.equals(claims.userId())) {
throw invalidUploadSession(invalidCode, "上传会话与当前用户不匹配");
}
@@ -63,10 +66,7 @@ public class ComponentUploadSupport {
if (!StringUtils.hasText(keyPrefix) || submittedObjectKeys == null || submittedObjectKeys.isEmpty()) {
throw invalidUploadObject("上传文件列表不能为空");
}
String normalizedPrefix = normalizeObjectKey(keyPrefix);
if (!normalizedPrefix.endsWith("/")) {
normalizedPrefix += "/";
}
String normalizedPrefix = storageObjectKeys.requirePrefix(keyPrefix);
final String requiredPrefix = normalizedPrefix;
List<String> objectKeys = submittedObjectKeys.stream()
@@ -139,8 +139,8 @@ public class ComponentUploadSupport {
String normalizedKey = normalizeObjectKey(objectKey);
validateSessionObjectKey(claims.keyPrefix(), normalizedKey);
String userSegment = "/" + userId + "/";
String urdfPrefix = normalizeConfiguredPrefix(storageProperties.urdfPrefix(), "components/urdf") + userSegment;
String coverPrefix = normalizeConfiguredPrefix(storageProperties.coverImagePrefix(), "components/cover-images") + userSegment;
String urdfPrefix = storageObjectKeys.underRoot(normalizeConfiguredPrefix(storageProperties.urdfPrefix(), "components/urdf")) + userSegment;
String coverPrefix = storageObjectKeys.underRoot(normalizeConfiguredPrefix(storageProperties.coverImagePrefix(), "components/cover-images")) + userSegment;
if (!normalizedKey.startsWith(urdfPrefix) && !normalizedKey.startsWith(coverPrefix)) {
throw invalidUploadObject("无权上传到该对象路径");
}
@@ -170,7 +170,7 @@ public class ComponentUploadSupport {
if (!StringUtils.hasText(objectKey)) {
throw invalidUploadObject("上传文件路径不能为空");
}
return objectKey.trim().replace("\\", "/");
return storageObjectKeys.requireKey(objectKey);
}
private void validateSessionObjectKey(String keyPrefix, String objectKey) {
@@ -189,7 +189,7 @@ public class ComponentUploadSupport {
}
private String buildKeyPrefix(String keyPrefixRoot, Long userId) {
return keyPrefixRoot + "/"
return storageObjectKeys.underRoot(keyPrefixRoot) + "/"
+ userId + "/"
+ LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE) + "/"
+ UUID.randomUUID() + "/";
@@ -212,7 +212,7 @@ public class ComponentUploadSupport {
if (objectKey.length() > 512) {
throw new BizException(HttpStatus.UNPROCESSABLE_ENTITY.value(), "INVALID_COMPONENT_UPLOAD_OBJECT", "上传文件路径过长");
}
return objectKey;
return storageObjectKeys.requireKey(objectKey);
}
public record PreparedUploadSession(
@@ -14,6 +14,7 @@ import com.linkerbot.cadworld.mapper.InterfaceDefinitionMapper;
import com.linkerbot.cadworld.mapper.InterfaceDefinitionRevisionMapper;
import com.linkerbot.cadworld.mechanicalinterface.MechanicalInterfaceSolver;
import com.linkerbot.cadworld.service.InterfaceDefinitionService;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
@@ -28,6 +29,7 @@ public class InterfaceDefinitionServiceImpl implements InterfaceDefinitionServic
private final InterfaceDefinitionRevisionMapper revisionMapper;
private final ObjectMapper objectMapper;
private final MechanicalInterfaceSolver solver;
private final StorageObjectKeys storageObjectKeys;
@Override
public List<JsonNode> list(String keyword) {
@@ -81,6 +83,7 @@ public class InterfaceDefinitionServiceImpl implements InterfaceDefinitionServic
private JsonNode save(InterfaceDefinitionEntity entity, SaveInterfaceDefinitionRequest request,
String id, boolean existing) {
storageObjectKeys.optionalKey(request.sourceAssetKey());
Definition proposed = new Definition(id, existing ? entity.getGeometryRevision() : 1,
request.name().trim(), request.geometry(), request.tolerances(),
request.connectableTargets(), request.displayForward());
@@ -159,7 +162,7 @@ public class InterfaceDefinitionServiceImpl implements InterfaceDefinitionServic
if (!boundKey.equals(expectedAssetKey)) {
throw new BizException(409, "INTERFACE_REVISION_CONFLICT", "源文件绑定已变化,请刷新后重试");
}
entity.setSourceAssetKey(key);
entity.setSourceAssetKey(storageObjectKeys.requireKey(key));
entity.setSourceGeometryHash(hash);
entity.setEditRevision(Math.incrementExact(entity.getEditRevision()));
if (definitionMapper.updateById(entity) != 1) {
@@ -0,0 +1,77 @@
package com.linkerbot.cadworld.service.support;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.StorageProperties;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/** 对象键是桶内的相对路径;环境根目录只在生成对象键时添加一次。 */
@Component
public class StorageObjectKeys {
private final String rootPrefix;
public StorageObjectKeys(StorageProperties properties) {
String root = properties.rootPrefix() == null ? "" : properties.rootPrefix().trim();
root = root.replaceAll("/+$", "");
if (!validRelativePath(root)) {
throw new BizException(500, "STORAGE_CONFIG_ERROR", "对象存储根目录配置非法");
}
this.rootPrefix = root + "/";
}
public String rootPrefix() {
return rootPrefix;
}
public String underRoot(String relativePath) {
if (!validRelativePath(relativePath)) throw invalidKey();
return requireKey(rootPrefix + relativePath);
}
public String requireKey(String key) {
if (key != null && key.chars().anyMatch(Character::isISOControl)) throw invalidKey();
String normalized = key == null ? "" : key.trim();
if (!validRelativePath(normalized) || !normalized.startsWith(rootPrefix)
|| normalized.length() > 512) {
throw invalidKey();
}
return normalized;
}
public String optionalKey(String key) {
return StringUtils.hasText(key) ? requireKey(key) : "";
}
public String requirePrefix(String prefix) {
String value = prefix == null ? "" : prefix.trim();
return requireKey(value.replaceAll("/+$", "")) + "/";
}
private static boolean validRelativePath(String path) {
if (!StringUtils.hasText(path) || path.contains(":") || path.contains("\\")
|| path.contains("?") || path.contains("#") || path.chars().anyMatch(Character::isISOControl)) {
return false;
}
for (String segment : path.split("/", -1)) {
if (!StringUtils.hasText(segment) || segment.equals(".") || segment.equals("..")) return false;
}
return true;
}
private static BizException invalidKey() {
return new BizException(422, "INVALID_STORAGE_OBJECT_KEY", "存储对象路径非法,必须使用当前环境根目录下的相对路径");
}
public static String summary(String key) {
try {
byte[] bytes = (key == null ? "" : key).getBytes(StandardCharsets.UTF_8);
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)).substring(0, 16);
} catch (NoSuchAlgorithmException exception) {
throw new IllegalStateException("SHA-256 不可用", exception);
}
}
}
@@ -18,8 +18,9 @@ import org.springframework.mock.web.MockMultipartFile;
class InterfaceAssetServiceTest {
final InterfaceDefinitionService definitions = mock(InterfaceDefinitionService.class);
final ObjectStorageClient storage = mock(ObjectStorageClient.class);
final InterfaceAssetService service = new InterfaceAssetService(definitions, storage,
new StorageProperties("local", "models", "covers", "http://localhost", "http://localhost", ".", Duration.ofMinutes(30), 16));
final StorageProperties properties = new StorageProperties("cadworld", "models", "covers", "http://localhost", "http://localhost", Duration.ofMinutes(30), 16);
final InterfaceAssetService service = new InterfaceAssetService(definitions, storage, properties,
new com.linkerbot.cadworld.service.support.StorageObjectKeys(properties));
@BeforeEach
void currentDefinition() {
@@ -31,7 +32,7 @@ class InterfaceAssetServiceTest {
var file = spy(new MockMultipartFile("file", "source.step", "application/octet-stream", new byte[]{1, 2, 3}));
doThrow(new AssertionError("getBytes must not be used")).when(file).getBytes();
String key = service.upload("mount", 1, file);
assertThat(key).startsWith("interfaces/6d6f756e74/1/").endsWith("/source.step");
assertThat(key).startsWith("cadworld/interfaces/6d6f756e74/1/").endsWith("/source.step");
verify(file, never()).getBytes();
verify(definitions).attachAsset(eq("mount"), eq(1), eq(key), startsWith("sha256:"), eq(""));
verify(storage, never()).deleteObject(anyString());
@@ -0,0 +1,38 @@
package com.linkerbot.cadworld.service;
import static org.assertj.core.api.Assertions.*;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.StorageProperties;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class StorageObjectKeysTest {
final StorageObjectKeys keys = new StorageObjectKeys(properties("cadworld_dev"));
@ParameterizedTest
@ValueSource(strings = {"https://bucket.test/cadworld_dev/a", "//bucket.test/a", "/cadworld_dev/a", "cadworld/a",
"cadworld_dev_other/a", "cadworld_dev/../a", "cadworld_dev/./a", "cadworld_dev//a",
"cadworld_dev/a/", "cadworld_dev\\a", "cadworld_dev/a?token=x", "cadworld_dev/a#fragment", "cadworld_dev/a\n"})
void rejectsUrlsOtherEnvironmentsAndUnsafePaths(String key) {
assertThatThrownBy(() -> keys.requireKey(key)).isInstanceOf(BizException.class)
.extracting("code", "httpStatus").containsExactly("INVALID_STORAGE_OBJECT_KEY", 422);
}
@Test
void keepsRelativeObjectKeyAndEnforcesLengthAndOptionalCover() {
assertThat(keys.requireKey("cadworld_dev/模型/a b.urdf")).isEqualTo("cadworld_dev/模型/a b.urdf");
assertThat(keys.optionalKey(null)).isEmpty();
assertThat(keys.optionalKey(" ")).isEmpty();
assertThatThrownBy(() -> keys.requireKey("cadworld_dev/" + "a".repeat(512))).isInstanceOf(BizException.class);
assertThatThrownBy(() -> new StorageObjectKeys(properties("../cadworld_dev")))
.isInstanceOf(BizException.class).extracting("code").isEqualTo("STORAGE_CONFIG_ERROR");
}
private StorageProperties properties(String root) {
return new StorageProperties(root, null, null, null, null, Duration.ofMinutes(30));
}
}
@@ -1,42 +1,53 @@
package com.linkerbot.cadworld.service;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.*;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.StorageProperties;
import com.linkerbot.cadworld.config.UploadSessionProperties;
import com.linkerbot.cadworld.enums.ComponentUploadType;
import com.linkerbot.cadworld.service.client.LocalObjectStorageClient;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.linkerbot.cadworld.config.OssProperties;
import com.linkerbot.cadworld.service.client.OssObjectStorageClient;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import com.linkerbot.cadworld.service.impl.ComponentUploadSupport;
import com.linkerbot.cadworld.service.support.ComponentUploadSessionTokenProvider;
import java.io.ByteArrayInputStream;
import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicReference;
import java.time.Duration;
import java.time.Instant;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class UploadIntegrityTest {
@TempDir Path root;
final String secret = "test-upload-signing-key-32-bytes-long";
@Test
void unknownLengthUploadIsBoundedAndCannotDestroyExistingObject() throws Exception {
var properties = new StorageProperties("local", "components/urdf", "components/cover-images", "http://localhost",
"http://localhost", root.toString(), Duration.ofMinutes(30), 8);
var storage = new LocalObjectStorageClient(properties);
var properties = new StorageProperties("cadworld_dev", "components/urdf", "components/cover-images", "http://localhost",
"http://localhost", Duration.ofMinutes(30), 8);
var keys = new StorageObjectKeys(properties);
var sdk = mock(OSS.class);
var stored = new AtomicReference<byte[]>();
when(sdk.putObject(any(PutObjectRequest.class))).thenAnswer(call -> {
PutObjectRequest request = call.getArgument(0);
stored.set(request.getInputStream().readAllBytes());
return null;
});
var storage = new OssObjectStorageClient(sdk,
new OssProperties("test-key", "test-secret", "oss-cn-beijing", "test-bucket"), properties, keys);
var tokens = new ComponentUploadSessionTokenProvider(new UploadSessionProperties("upload", secret));
var support = new ComponentUploadSupport(properties, storage, tokens);
String prefix = "components/urdf/0/session/";
var support = new ComponentUploadSupport(properties, storage, tokens, keys);
String prefix = "cadworld_dev/components/urdf/0/session/";
String key = prefix + "model.rfs";
String token = tokens.generateToken(0L, ComponentUploadType.URDF, prefix, Instant.now().plusSeconds(60));
support.uploadObject(0L, token, key, "application/octet-stream", new ByteArrayInputStream(new byte[]{1, 2}), -1);
assertThatThrownBy(() -> support.uploadObject(0L, token, key, "application/octet-stream", new ByteArrayInputStream(new byte[9]), -1))
.isInstanceOf(BizException.class).extracting("httpStatus").isEqualTo(413);
assertThat(java.nio.file.Files.readAllBytes(root.resolve(key))).containsExactly((byte) 1, (byte) 2);
try (var files = java.nio.file.Files.walk(root)) {
assertThat(files.filter(java.nio.file.Files::isRegularFile).count()).isEqualTo(1);
}
assertThat(stored.get()).containsExactly((byte) 1, (byte) 2);
verify(sdk, times(1)).putObject(any(PutObjectRequest.class));
}
@Test
@@ -18,15 +18,15 @@ class UploadSessionTest {
void tokenTypeOwnerExpiryAndPathAreCheckedIndependentlyOfUserAuthentication() {
var tokens = new ComponentUploadSessionTokenProvider(new UploadSessionProperties("test", "test-upload-signing-key-32-bytes-long"));
var storage = mock(ObjectStorageClient.class);
var properties = new StorageProperties("local", "components/urdf", "components/cover-images", "http://localhost/admin-api/component-assets", "http://localhost", Duration.ofMinutes(30));
var support = new ComponentUploadSupport(properties, storage, tokens);
var properties = new StorageProperties("cadworld", "components/urdf", "components/cover-images", "http://localhost/admin-api/component-assets", "http://localhost", Duration.ofMinutes(30));
var support = new ComponentUploadSupport(properties, storage, tokens, new com.linkerbot.cadworld.service.support.StorageObjectKeys(properties));
ComponentUploadType type = ComponentUploadType.values()[0];
String token = tokens.generateToken(0L, type, "components/urdf/0/session/", Instant.now().plusSeconds(60));
String token = tokens.generateToken(0L, type, "cadworld/components/urdf/0/session/", Instant.now().plusSeconds(60));
assertThat(support.requireUploadSession(0L, token, type, "INVALID").userId()).isZero();
assertThatThrownBy(() -> support.requireUploadSession(1L, token, type, "INVALID")).hasMessageContaining("用户");
ComponentUploadType other = ComponentUploadType.values()[1];
assertThatThrownBy(() -> support.requireUploadSession(0L, token, other, "INVALID")).hasMessageContaining("类型");
String expired = tokens.generateToken(0L, type, "components/urdf/0/session/", Instant.now().minusSeconds(60));
String expired = tokens.generateToken(0L, type, "cadworld/components/urdf/0/session/", Instant.now().minusSeconds(60));
assertThatThrownBy(() -> support.requireUploadSession(0L, expired, type, "INVALID")).hasMessageContaining("过期");
for (String path : new String[]{"../escape", "/absolute", "a/../../escape", "a//b"}) {
assertThatThrownBy(() -> support.prepareObjectUpload(0L, token, type, "INVALID", path, "application/octet-stream"))
@@ -1,34 +0,0 @@
package com.linkerbot.cadworld.service.client;
import com.linkerbot.cadworld.config.StorageProperties;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.file.Path;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class LocalObjectStorageClientTest {
@TempDir
Path tempDir;
@Test
void writesReadsAndRejectsTraversal() {
LocalObjectStorageClient client = new LocalObjectStorageClient(new StorageProperties(
"local", "components/urdf", "components/cover-images", "", "", tempDir.toString(), Duration.ofMinutes(5)));
client.putObject("components/urdf/1/model.rfs", "application/json",
new ByteArrayInputStream("{}".getBytes()), 2);
ByteArrayOutputStream output = new ByteArrayOutputStream();
client.writeObjectTo("components/urdf/1/model.rfs", output);
assertArrayEquals("{}".getBytes(), output.toByteArray());
assertTrue(client.objectExists("components/urdf/1/model.rfs"));
assertThrows(RuntimeException.class, () -> client.objectExists("../outside"));
}
}
@@ -0,0 +1,189 @@
package com.linkerbot.cadworld.service.client;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.*;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.linkerbot.cadworld.common.exception.BizException;
import com.linkerbot.cadworld.config.OssProperties;
import com.linkerbot.cadworld.config.StorageProperties;
import com.linkerbot.cadworld.service.support.StorageObjectKeys;
import com.linkerbot.cadworld.service.support.UploadLimits;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.LoggerFactory;
class OssObjectStorageClientTest {
final OSS sdk = mock(OSS.class);
final StorageProperties storage = new StorageProperties("cadworld_dev", "components/urdf", "components/cover-images",
"https://admin.test/admin-api/component-assets", "https://admin.test", Duration.ofMinutes(30), 8);
final StorageObjectKeys keys = new StorageObjectKeys(storage);
final OssProperties properties = new OssProperties("PRIVATE_ACCESS_KEY", "PRIVATE_SECRET", "oss-cn-beijing", "test-bucket");
final OssObjectStorageClient client = new OssObjectStorageClient(sdk, properties, storage, keys);
final String key = "cadworld_dev/components/urdf/0/session/model.rfs";
@Test
void java21JaxbRuntimeCanParseSdkErrorModels() throws Exception {
var context = javax.xml.bind.JAXBContext.newInstance(com.aliyun.oss.internal.model.OSSErrorResult.class);
Object parsed = context.createUnmarshaller().unmarshal(new StringReader(
"<Error><Code>NoSuchKey</Code><Message>missing</Message><RequestId>test-id</RequestId><HostId>oss.test</HostId></Error>"));
assertThat(parsed).isInstanceOf(com.aliyun.oss.internal.model.OSSErrorResult.class);
var sdk = new com.linkerbot.cadworld.config.OssClientConfiguration().ossClient(properties);
sdk.shutdown();
}
@Test
void regionCredentialsAndBackendUploadUrlAreConfiguredWithoutNetwork() {
assertThat(properties.regionId()).isEqualTo("cn-beijing");
assertThat(properties.endpoint()).isEqualTo("https://oss-cn-beijing.aliyuncs.com");
assertThat(new OssProperties("a", "b", "cn-beijing", "c").endpoint()).isEqualTo(properties.endpoint());
assertThat(properties.toString()).doesNotContain(properties.accessKeyId(), properties.accessKeySecret());
assertThat(client.createUploadUrl(key, null, Duration.ofMinutes(1)))
.isEqualTo("https://admin.test/admin-api/component-assets/upload?objectKey=" + key);
assertThatThrownBy(() -> client.createUploadUrl(key, null, Duration.ZERO))
.isInstanceOf(BizException.class).extracting("code").isEqualTo("STORAGE_CONFIG_ERROR");
assertThatThrownBy(() -> new OssProperties("", "secret", "oss-cn-beijing", "bucket").validate())
.isInstanceOf(BizException.class).hasMessageContaining("OSS_ACCESS_KEY_ID");
verifyNoInteractions(sdk);
}
@Test
void uploadsExactKeyMetadataAndBytes() {
when(sdk.putObject(any(PutObjectRequest.class))).thenAnswer(call -> {
PutObjectRequest request = call.getArgument(0);
assertThat(request.getBucketName()).isEqualTo("test-bucket");
assertThat(request.getKey()).isEqualTo(key);
assertThat(request.getMetadata().getContentLength()).isEqualTo(2);
assertThat(request.getMetadata().getContentType()).isEqualTo("application/octet-stream");
assertThat(request.getInputStream().readAllBytes()).containsExactly((byte) 1, (byte) 2);
return null;
});
client.putObject(key, null, new ByteArrayInputStream(new byte[]{1, 2}), 2);
verify(sdk).putObject(any(PutObjectRequest.class));
}
@Test
void streamsDownloadClosesOssResponseAndKeepsOutputOpen() throws Exception {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(2);
metadata.setContentType("image/png");
when(sdk.getObjectMetadata("test-bucket", key)).thenReturn(metadata);
assertThat(client.objectExists(key)).isTrue();
assertThat(client.statObject(key)).isEqualTo(new ObjectStorageClient.StoredObject("image/png", 2));
OSSObject response = spy(new OSSObject());
InputStream input = spy(new ByteArrayInputStream(new byte[]{1, 2}));
response.setObjectContent(input);
when(sdk.getObject("test-bucket", key)).thenReturn(response);
ByteArrayOutputStream output = spy(new ByteArrayOutputStream());
client.writeObjectTo(key, output);
assertThat(output.toByteArray()).containsExactly((byte) 1, (byte) 2);
verify(response).close();
verify(input).close();
verify(output, never()).close();
client.deleteObject(key);
verify(sdk).deleteObject("test-bucket", key);
}
@Test
void missingObjectReturnsFalseOr404() {
when(sdk.getObjectMetadata("test-bucket", key)).thenThrow(error("NoSuchKey"));
assertThat(client.objectExists(key)).isFalse();
assertThatThrownBy(() -> client.statObject(key)).isInstanceOf(BizException.class)
.extracting("code", "httpStatus").containsExactly("OSS_OBJECT_NOT_FOUND", 404);
when(sdk.getObject("test-bucket", key)).thenThrow(error("NoSuchKey"));
assertThatThrownBy(() -> client.writeObjectTo(key, new ByteArrayOutputStream())).isInstanceOf(BizException.class)
.extracting("code", "httpStatus").containsExactly("OSS_OBJECT_NOT_FOUND", 404);
}
@ParameterizedTest
@ValueSource(strings = {"AccessDenied", "NoSuchBucket", "InvalidAccessKeyId", "SignatureDoesNotMatch"})
void bucketAndPermissionFailuresAreNotMistakenForMissingObjects(String code) {
when(sdk.getObjectMetadata("test-bucket", key)).thenThrow(error(code));
assertThatThrownBy(() -> client.objectExists(key)).isInstanceOf(BizException.class)
.extracting("code", "httpStatus").containsExactly("OSS_OBJECT_CHECK_FAILED", 502);
}
@Test
void errorLogsContainDiagnosticIdsWithoutRawCredentialBearingMessage() {
Logger logger = (Logger) LoggerFactory.getLogger(OssObjectStorageClient.class);
ListAppender<ILoggingEvent> capture = new ListAppender<>();
capture.start();
logger.addAppender(capture);
try {
when(sdk.putObject(any(PutObjectRequest.class))).thenThrow(error("AccessDenied"));
assertThatThrownBy(() -> client.putObject(key, null, new ByteArrayInputStream(new byte[2]), 2))
.isInstanceOf(BizException.class).extracting("code").isEqualTo("OSS_UPLOAD_FAILED");
doThrow(error("AccessDenied")).when(sdk).deleteObject("test-bucket", key);
assertThatThrownBy(() -> client.deleteObject(key)).isInstanceOf(BizException.class)
.extracting("code").isEqualTo("OSS_DELETE_FAILED");
String messages = capture.list.stream().map(ILoggingEvent::getFormattedMessage).collect(Collectors.joining("\n"));
assertThat(messages).contains("operation=UPLOAD", "operation=DELETE", "ossCode=AccessDenied", "ossRequestId=oss-test-id", "keySummary=");
assertThat(messages).doesNotContain("PRIVATE_ACCESS_KEY", "PRIVATE_SECRET", "PRIVATE_TOKEN", key);
assertThat(capture.list).allSatisfy(event -> assertThat(event.getThrowableProxy()).isNull());
} finally {
logger.detachAppender(capture);
capture.stop();
}
}
@Test
void uploadSizeFailureWrappedBySdkRemains413AndTemporaryFilesAreRemoved() throws Exception {
Set<Path> before = uploadTemporaryFiles();
when(sdk.putObject(any(PutObjectRequest.class))).thenAnswer(call -> {
try {
UploadLimits.bounded(new ByteArrayInputStream(new byte[9]), 8).readAllBytes();
} catch (BizException failure) {
throw new ClientException("wrapped", failure);
}
return null;
});
assertThatThrownBy(() -> client.putObject(key, null, new ByteArrayInputStream(new byte[]{1}), -1))
.isInstanceOf(BizException.class).extracting("httpStatus").isEqualTo(413);
assertThat(uploadTemporaryFiles()).isEqualTo(before);
}
@Test
void oversizedUploadsAndInvalidKeysNeverReachSdk() {
assertThatThrownBy(() -> client.putObject(key, null, new ByteArrayInputStream(new byte[9]), 9))
.isInstanceOf(BizException.class).extracting("httpStatus").isEqualTo(413);
for (String invalid : new String[]{"cadworld/model.rfs", "https://bucket.test/a", "cadworld_dev/../escape"}) {
assertThatThrownBy(() -> client.objectExists(invalid)).isInstanceOf(BizException.class)
.extracting("code").isEqualTo("INVALID_STORAGE_OBJECT_KEY");
assertThatThrownBy(() -> client.putObject(invalid, null, new ByteArrayInputStream(new byte[0]), 0))
.isInstanceOf(BizException.class);
assertThatThrownBy(() -> client.statObject(invalid)).isInstanceOf(BizException.class);
assertThatThrownBy(() -> client.writeObjectTo(invalid, new ByteArrayOutputStream())).isInstanceOf(BizException.class);
assertThatThrownBy(() -> client.deleteObject(invalid)).isInstanceOf(BizException.class);
}
verifyNoInteractions(sdk);
}
private Set<Path> uploadTemporaryFiles() throws Exception {
try (var paths = Files.list(Path.of(System.getProperty("java.io.tmpdir")))) {
return paths.filter(path -> path.getFileName().toString().startsWith("cadworld-oss-")).collect(Collectors.toSet());
}
}
private OSSException error(String code) {
return new OSSException("PRIVATE_ACCESS_KEY PRIVATE_SECRET PRIVATE_TOKEN", code, "oss-test-id", "oss.test", "", "", "");
}
}
@@ -42,7 +42,9 @@ class ComponentServiceImplTest {
new ObjectMapper(), new com.linkerbot.cadworld.mechanicalinterface.ComponentPortValidationService(
MechanicalTestFixtures.definitions(),MechanicalTestFixtures.SOLVER,new ObjectMapper(),
new com.linkerbot.cadworld.mechanicalinterface.UrdfSourceValidation(),
org.mockito.Mockito.mock(com.linkerbot.cadworld.mechanicalinterface.UrdfAssetVerifier.class)));
org.mockito.Mockito.mock(com.linkerbot.cadworld.mechanicalinterface.UrdfAssetVerifier.class)),
new com.linkerbot.cadworld.service.support.StorageObjectKeys(
new com.linkerbot.cadworld.config.StorageProperties("cadworld", null, null, null, null, java.time.Duration.ofMinutes(30))));
}
@Test
@@ -159,7 +161,7 @@ class ComponentServiceImplTest {
var metadata = new com.linkerbot.cadworld.dto.component.ComponentAssemblyMetadata(
java.util.List.of("工具", "末端执行器,铲取"), java.util.List.of(), null);
var request = new CreateComponentRequest("铲子", null, "humanoid", "未指定", "未指定",
"shovel.rfs", "", null, new ObjectMapper().createObjectNode(), metadata);
"cadworld/shovel.rfs", "", null, new ObjectMapper().createObjectNode(), metadata);
try (var factory = jakarta.validation.Validation.buildDefaultValidatorFactory()) {
assertThat(factory.getValidator().validate(request)).isEmpty();
}
@@ -180,7 +182,7 @@ class ComponentServiceImplTest {
robotType,
"示例公司",
"M1",
"components/urdf/arm.urdf",
"cadworld/components/urdf/arm.urdf",
"",
"示例组件",
new ObjectMapper().createObjectNode()
@@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test;
class ComponentUploadSupportTest {
private static final String KEY_PREFIX = "components/urdf/1/20260920/session-id/";
private static final String KEY_PREFIX = "cadworld/components/urdf/1/20260920/session-id/";
@Test
void verifiesExactObjectsWithoutListingTheBucket() {
@@ -41,7 +41,7 @@ class ComponentUploadSupportTest {
BizException exception = assertThrows(BizException.class, () -> support.verifyUploadedObjectKeys(
KEY_PREFIX,
List.of("components/urdf/1/another-session/LeKiwi.rfs")
List.of("cadworld/components/urdf/1/another-session/LeKiwi.rfs")
));
assertEquals("INVALID_COMPONENT_UPLOAD_OBJECT", exception.getCode());
@@ -62,7 +62,9 @@ class ComponentUploadSupportTest {
}
private ComponentUploadSupport createSupport(ObjectStorageClient storageClient) {
return new ComponentUploadSupport(null, storageClient, null);
return new ComponentUploadSupport(null, storageClient, null,
new com.linkerbot.cadworld.service.support.StorageObjectKeys(
new com.linkerbot.cadworld.config.StorageProperties("cadworld", null, null, null, null, Duration.ofMinutes(30))));
}
private static final class RecordingObjectStorageClient implements ObjectStorageClient {
@@ -18,7 +18,9 @@ class InterfaceDefinitionRevisionTest {
final ObjectMapper json=new ObjectMapper();
final InterfaceDefinitionMapper definitions=mock(InterfaceDefinitionMapper.class);
final InterfaceDefinitionRevisionMapper revisions=mock(InterfaceDefinitionRevisionMapper.class);
final InterfaceDefinitionServiceImpl service=new InterfaceDefinitionServiceImpl(definitions,revisions,json,new MechanicalInterfaceSolver());
final InterfaceDefinitionServiceImpl service=new InterfaceDefinitionServiceImpl(definitions,revisions,json,new MechanicalInterfaceSolver(),
new com.linkerbot.cadworld.service.support.StorageObjectKeys(
new com.linkerbot.cadworld.config.StorageProperties("cadworld", null, null, null, null, java.time.Duration.ofMinutes(30))));
final MountGeometry geometry=new MountGeometry(new Plane(new double[3],new double[]{0,0,1}),List.of(
new Hole("a",new double[]{-.02,0,0},new double[]{0,0,1},.004),
new Hole("b",new double[]{.02,0,0},new double[]{0,0,1},.004)));
@@ -26,7 +28,7 @@ class InterfaceDefinitionRevisionTest {
InterfaceDefinitionEntity e=new InterfaceDefinitionEntity();
e.setId("mount");e.setName("mount");e.setGeometryRevision(1);e.setEditRevision(1L);
e.setGeometryJson(json.valueToTree(geometry));e.setTolerancesJson(json.valueToTree(Tolerances.defaults()));
e.setSourceGeometryHash("sha256:old");e.setSourceAssetKey("interfaces/old.step");
e.setSourceGeometryHash("sha256:old");e.setSourceAssetKey("cadworld/interfaces/old.step");
when(definitions.selectOne(any())).thenReturn(e);
when(definitions.updateById(any(InterfaceDefinitionEntity.class))).thenReturn(1);
when(revisions.updateById(any(InterfaceDefinitionRevisionEntity.class))).thenReturn(1);return e;
@@ -38,15 +40,15 @@ class InterfaceDefinitionRevisionTest {
@Test void editsAppendRevisionAndDoNotOverwriteOldSnapshot(){
current();var result=service.update("mount",dto(changedGeometry()));
assertThat(result.path("geometryRevision").asInt()).isEqualTo(2);
assertThat(result.path("sourceAssetKey").asText()).isEqualTo("interfaces/old.step");
assertThat(result.path("sourceAssetKey").asText()).isEqualTo("cadworld/interfaces/old.step");
ArgumentCaptor<InterfaceDefinitionRevisionEntity> capture=ArgumentCaptor.forClass(InterfaceDefinitionRevisionEntity.class);
verify(revisions).insert(capture.capture());assertThat(capture.getValue().getId()).isEqualTo("mount:2");
verify(revisions,never()).updateById(any(InterfaceDefinitionRevisionEntity.class));
}
@Test void staleEditsAndAssetUploadsAreRejected(){
current();assertThatThrownBy(()->service.update("mount",dto(request().put("geometryRevision",2)))).hasMessageContaining("刷新");
assertThatThrownBy(()->service.attachAsset("mount",2,"other.step","sha256:new","interfaces/old.step")).hasMessageContaining("修订");
assertThatThrownBy(()->service.attachAsset("mount",1,"other.step","sha256:new","interfaces/old.step")).hasMessageContaining("新几何修订");
assertThatThrownBy(()->service.attachAsset("mount",2,"other.step","sha256:new","cadworld/interfaces/old.step")).hasMessageContaining("修订");
assertThatThrownBy(()->service.attachAsset("mount",1,"other.step","sha256:new","cadworld/interfaces/old.step")).hasMessageContaining("新几何修订");
verify(definitions,never()).updateById(any(InterfaceDefinitionEntity.class));
}
@Test void displayOnlyEditPersistsWithoutInvalidatingGeometryRevision(){
@@ -66,10 +68,10 @@ class InterfaceDefinitionRevisionTest {
assertThat(result.path("sourceAssetKey").asText()).isEmpty();
assertThat(e.getSourceGeometryHash()).isEmpty();
InterfaceDefinitionRevisionEntity snapshot=new InterfaceDefinitionRevisionEntity();snapshot.setId("mount:2");
service.attachAsset("mount",2,"interfaces/new.step","sha256:new","");
service.attachAsset("mount",2,"cadworld/interfaces/new.step","sha256:new","");
ArgumentCaptor<InterfaceDefinitionRevisionEntity> capture=ArgumentCaptor.forClass(InterfaceDefinitionRevisionEntity.class);
verify(revisions).updateById(capture.capture());
assertThat(capture.getValue().getDefinitionJson().path("sourceAssetKey").asText()).isEqualTo("interfaces/new.step");
assertThat(capture.getValue().getDefinitionJson().path("sourceAssetKey").asText()).isEqualTo("cadworld/interfaces/new.step");
assertThat(capture.getValue().getDefinitionJson().path("geometryRevision").asInt()).isEqualTo(2);
}
ObjectNode changedGeometry() {
@@ -94,10 +96,10 @@ class InterfaceDefinitionRevisionTest {
@Test void concurrentUploadsCannotReplaceAnotherSuccessfulBinding() {
InterfaceDefinitionEntity entity = current();
assertThatThrownBy(() -> service.attachAsset("mount", 1, "interfaces/loser.step", "sha256:old", ""))
assertThatThrownBy(() -> service.attachAsset("mount", 1, "cadworld/interfaces/loser.step", "sha256:old", ""))
.isInstanceOf(com.linkerbot.cadworld.common.exception.BizException.class)
.hasMessageContaining("绑定已变化");
assertThat(entity.getSourceAssetKey()).isEqualTo("interfaces/old.step");
assertThat(entity.getSourceAssetKey()).isEqualTo("cadworld/interfaces/old.step");
verify(definitions, never()).updateById(any(InterfaceDefinitionEntity.class));
}
}
+4 -4
View File
@@ -30,7 +30,7 @@
<java.version>21</java.version>
<mybatis-plus.version>3.5.11</mybatis-plus.version>
<jjwt.version>0.12.6</jjwt.version>
<minio.version>8.5.17</minio.version>
<aliyun-oss.version>3.18.4</aliyun-oss.version>
<springdoc.version>2.8.6</springdoc.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
@@ -54,9 +54,9 @@
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.version}</version>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>${aliyun-oss.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>