component表的step_object_key、urdf_main_object_key、urdf_prefix不保存环境根目录
This commit is contained in:
@@ -185,6 +185,8 @@ PUT 是全量表单更新,前端应提交完整表单,每次都必须提交
|
||||
|
||||
后端根据实际对象生成 `urdfPrefix`、`urdfMainObjectKey` 和 `urdfFiles`,清单保留请求顺序,元素为 `relativePath` 和 `sizeBytes`。无需提交这些数据库维护字段。
|
||||
|
||||
数据库中的 `step_object_key`、`urdf_main_object_key`、`urdf_prefix` 仅保存从 `components/` 开始的相对路径,不包含 `app.storage.root-prefix` 环境根目录;目录前缀保留末尾 `/`。上传授权、保存请求和详情响应中的对象键仍包含环境根目录,访问 URL 根据当前环境配置生成,前端用法不变。兼容读取带当前环境根目录的旧记录,组件再次成功保存时统一转换这三个字段(仅修改业务信息也会转换),读取不会修改数据库,无需批量更新旧数据。
|
||||
|
||||
检查全部通过才写数据库。失败不切换原资源引用。旧批次、上传后未保存及删除组件后的文件暂不自动清理。校验覆盖清单的结构和对象存在性,不解析 URDF XML 内部的依赖引用。
|
||||
|
||||
### 4.3 旋转轴字段部署
|
||||
|
||||
@@ -363,6 +363,72 @@ class AdminApiTest {
|
||||
assertThat(count("component")).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyStoragePathsAreReadWithoutWritesAndConvertedOnlyAfterSuccessfulSave() throws Exception {
|
||||
JsonNode created = create(uploaded());
|
||||
long id = created.path("id").asLong();
|
||||
jdbc.update("UPDATE component SET step_object_key=?,urdf_prefix=?,urdf_main_object_key=? WHERE id=?",
|
||||
created.path("stepObjectKey").asText(), created.path("urdfPrefix").asText(),
|
||||
created.path("urdfMainObjectKey").asText(), id);
|
||||
Map<String, Object> legacy = storedComponentPaths(id);
|
||||
JsonNode queried = data(mvc.perform(get("/admin-api/components/{id}", id))
|
||||
.andExpect(status().isOk()).andReturn());
|
||||
assertThat(queried).isEqualTo(created);
|
||||
mvc.perform(get("/admin-api/components").param("keyword", created.path("componentCode").asText()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.items[0].stepUrl").value(created.path("stepUrl").asText()))
|
||||
.andExpect(jsonPath("$.data.items[0].urdfUrl").value(created.path("urdfUrl").asText()));
|
||||
assertThat(storedComponentPaths(id)).isEqualTo(legacy);
|
||||
|
||||
expectUpdateError(id, update(2), 409, "COMPONENT_REVISION_CONFLICT");
|
||||
JsonNode session = sign(Map.of("componentId", id));
|
||||
Map<String, Object> missingUpload = update(1);
|
||||
missingUpload.put("uploadSessionToken", session.path("uploadSessionToken").asText());
|
||||
missingUpload.put("stepObjectKey", session.path("step").path("keyPrefix").asText() + "missing.step");
|
||||
expectUpdateError(id, missingUpload, 422, "COMPONENT_UPLOAD_OBJECT_MISSING");
|
||||
assertThat(storedComponentPaths(id)).isEqualTo(legacy);
|
||||
|
||||
JsonNode changed = data(mvc.perform(put("/admin-api/components/{id}", id)
|
||||
.contentType(MediaType.APPLICATION_JSON).content(json.writeValueAsString(update(1))))
|
||||
.andExpect(status().isOk()).andReturn());
|
||||
assertRelativeComponentPaths(changed);
|
||||
assertThat(changed.path("version").asLong()).isEqualTo(2);
|
||||
for (String field : List.of("stepObjectKey", "urdfPrefix", "urdfMainObjectKey", "stepUrl", "urdfUrl", "urdfBaseUrl", "urdfFiles")) {
|
||||
assertThat(changed.get(field)).as(field).isEqualTo(created.get(field));
|
||||
}
|
||||
JsonNode savedAgain = data(mvc.perform(put("/admin-api/components/{id}", id)
|
||||
.contentType(MediaType.APPLICATION_JSON).content(json.writeValueAsString(update(2))))
|
||||
.andExpect(status().isOk()).andReturn());
|
||||
assertRelativeComponentPaths(savedAgain);
|
||||
assertThat(savedAgain.path("stepUrl")).isEqualTo(created.path("stepUrl"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void storesNestedUrdfRelativePathsAndReturnsEncodedDownloadableUrls() throws Exception {
|
||||
JsonNode session = sign(Map.of());
|
||||
String step = session.path("step").path("keyPrefix").asText() + "模型 one.STEP";
|
||||
String main = session.path("urdf").path("keyPrefix").asText() + "urdf/机器人 one.urdf";
|
||||
String mesh = session.path("urdf").path("keyPrefix").asText() + "meshes/底座 one.stl";
|
||||
storage.simulateDirectUpload(step, "application/octet-stream", new byte[]{1, 2});
|
||||
storage.simulateDirectUpload(main, "application/xml", new byte[]{3});
|
||||
storage.simulateDirectUpload(mesh, "application/octet-stream", new byte[]{4});
|
||||
JsonNode created = create(Map.of("uploadSessionToken", session.path("uploadSessionToken").asText(),
|
||||
"stepObjectKey", step, "urdfObjectKeys", List.of(main, mesh)));
|
||||
assertThat(created.path("stepObjectKey").asText()).isEqualTo(step);
|
||||
assertThat(created.path("urdfMainObjectKey").asText()).isEqualTo(main);
|
||||
assertThat(created.path("stepUrl").asText()).contains("/cadworld_dev/components/").endsWith("%E6%A8%A1%E5%9E%8B%20one.STEP");
|
||||
assertThat(created.path("urdfFiles").get(0).path("relativePath").asText()).isEqualTo("urdf/机器人 one.urdf");
|
||||
assertThat(created.path("urdfUrl").asText()).contains("/urdf/").endsWith("%E6%9C%BA%E5%99%A8%E4%BA%BA%20one.urdf");
|
||||
mvc.perform(get(java.net.URI.create(created.path("stepUrl").asText())))
|
||||
.andExpect(status().isOk()).andExpect(content().bytes(new byte[]{1, 2}));
|
||||
mvc.perform(get(java.net.URI.create(created.path("urdfUrl").asText())))
|
||||
.andExpect(status().isOk()).andExpect(content().bytes(new byte[]{3}));
|
||||
mvc.perform(get("/admin-api/components").param("keyword", created.path("componentCode").asText()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.items[0].stepUrl").value(created.path("stepUrl").asText()))
|
||||
.andExpect(jsonPath("$.data.items[0].urdfUrl").value(created.path("urdfUrl").asText()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stepAndUploadSessionRemainRequiredWhenOtherFieldsAreOmitted() throws Exception {
|
||||
JsonNode session = sign(Map.of());
|
||||
@@ -509,12 +575,13 @@ class AdminApiTest {
|
||||
form.put("stepObjectKey", newKey);
|
||||
expectUpdateError(id, form, 422, "COMPONENT_UPLOAD_OBJECT_MISSING");
|
||||
assertThat(jdbc.queryForObject("SELECT step_object_key FROM component WHERE id=?",String.class,id))
|
||||
.isEqualTo(created.path("stepObjectKey").asText());
|
||||
.isEqualTo(created.path("stepObjectKey").asText().substring("cadworld_dev/".length()));
|
||||
storage.simulateDirectUpload(newKey,"application/octet-stream",new byte[]{4,5});
|
||||
JsonNode changed = data(mvc.perform(put("/admin-api/components/{id}",id).contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json.writeValueAsString(form))).andExpect(status().isOk()).andReturn());
|
||||
assertThat(changed.path("stepObjectKey").asText()).isEqualTo(newKey);
|
||||
assertThat(changed.path("urdfMainObjectKey")).isEqualTo(created.path("urdfMainObjectKey"));
|
||||
assertRelativeComponentPaths(changed);
|
||||
assertThat(storage.objectExists(created.path("stepObjectKey").asText())).isTrue();
|
||||
Map<String,Object> another = uploaded();
|
||||
form.put("version",2);
|
||||
@@ -537,13 +604,14 @@ class AdminApiTest {
|
||||
storage.simulateDirectUpload(main, "application/xml", new byte[]{1});
|
||||
expectUpdateError(id, form, 422, "COMPONENT_UPLOAD_OBJECT_MISSING");
|
||||
assertThat(jdbc.queryForObject("SELECT urdf_prefix FROM component WHERE id=?", String.class, id))
|
||||
.isEqualTo(created.path("urdfPrefix").asText());
|
||||
.isEqualTo(created.path("urdfPrefix").asText().substring("cadworld_dev/".length()));
|
||||
storage.simulateDirectUpload(mesh, "application/octet-stream", new byte[]{2,3,4,5});
|
||||
JsonNode changed = data(mvc.perform(put("/admin-api/components/{id}", id).contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json.writeValueAsString(form))).andExpect(status().isOk()).andReturn());
|
||||
assertThat(changed.path("stepObjectKey")).isEqualTo(created.path("stepObjectKey"));
|
||||
assertThat(changed.path("urdfPrefix").asText()).isEqualTo(prefix);
|
||||
assertThat(changed.path("urdfMainObjectKey").asText()).isEqualTo(main);
|
||||
assertRelativeComponentPaths(changed);
|
||||
assertThat(changed.path("urdfFiles").get(1).path("relativePath").asText()).isEqualTo("meshes/new.stl");
|
||||
assertThat(changed.path("urdfFiles").get(1).path("sizeBytes").asLong()).isEqualTo(4);
|
||||
assertThat(storage.objectExists(created.path("urdfMainObjectKey").asText())).isTrue();
|
||||
@@ -580,6 +648,7 @@ class AdminApiTest {
|
||||
assertThat(changed.path("stepUrl").asText()).endsWith("replacement.STEP");
|
||||
assertThat(changed.path("urdfPrefix").asText()).isEqualTo(prefix);
|
||||
assertThat(changed.path("urdfMainObjectKey").asText()).isEqualTo(main);
|
||||
assertRelativeComponentPaths(changed);
|
||||
assertThat(changed.path("urdfFiles").get(1).path("relativePath").asText()).isEqualTo("meshes/new.stl");
|
||||
assertThat(changed.path("urdfFiles").get(1).path("sizeBytes").asLong()).isEqualTo(3);
|
||||
assertThat(storage.objectExists(created.path("stepObjectKey").asText())).isTrue();
|
||||
@@ -937,8 +1006,24 @@ class AdminApiTest {
|
||||
}
|
||||
}
|
||||
JsonNode create(Map<String,?> body) throws Exception {
|
||||
return data(mvc.perform(post("/admin-api/components").contentType(MediaType.APPLICATION_JSON)
|
||||
JsonNode created = data(mvc.perform(post("/admin-api/components").contentType(MediaType.APPLICATION_JSON)
|
||||
.content(json.writeValueAsString(body))).andExpect(status().isOk()).andReturn());
|
||||
assertRelativeComponentPaths(created);
|
||||
return created;
|
||||
}
|
||||
Map<String, Object> storedComponentPaths(long id) {
|
||||
return jdbc.queryForMap("SELECT step_object_key,urdf_prefix,urdf_main_object_key FROM component WHERE id=?", id);
|
||||
}
|
||||
void assertRelativeComponentPaths(JsonNode response) {
|
||||
Map<String, Object> stored = storedComponentPaths(response.path("id").asLong());
|
||||
Map<String, String> fields = Map.of("step_object_key", "stepObjectKey", "urdf_prefix", "urdfPrefix",
|
||||
"urdf_main_object_key", "urdfMainObjectKey");
|
||||
for (Map.Entry<String, String> field : fields.entrySet()) {
|
||||
String objectKey = response.path(field.getValue()).asText();
|
||||
if (!objectKey.isEmpty()) assertThat(objectKey).startsWith("cadworld_dev/components/");
|
||||
String expectedPath = objectKey.isEmpty() ? "" : objectKey.substring("cadworld_dev/".length());
|
||||
assertThat(stored.get(field.getKey())).as(field.getKey()).isEqualTo(expectedPath);
|
||||
}
|
||||
}
|
||||
void expectCreateError(Map<String,?> body,int status,String code) throws Exception {
|
||||
mvc.perform(post("/admin-api/components").contentType(MediaType.APPLICATION_JSON)
|
||||
|
||||
Reference in New Issue
Block a user