diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java index 02b43ff7f1..acaf59d400 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java @@ -10,6 +10,8 @@ import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.ApiEditPosRequest; import io.metersphere.api.dto.request.ApiTransferRequest; import io.metersphere.api.dto.request.ImportRequest; +import io.metersphere.api.dto.scenario.ApiFileCopyRequest; +import io.metersphere.api.dto.scenario.ApiScenarioStepFileCopyRequest; import io.metersphere.api.dto.schema.JsonSchemaItem; import io.metersphere.api.mapper.ExtApiDefinitionMapper; import io.metersphere.api.mapper.ExtApiScenarioStepMapper; @@ -49,6 +51,7 @@ import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; import java.util.List; +import java.util.Map; /** @@ -151,6 +154,14 @@ public class ApiDefinitionController { apiDefinitionService.batchUpdate(request, SessionUtils.getUserId()); } + @PostMapping("/file/copy") + @Operation(summary = "接口测试-接口管理-复制接口时,复制文件") + @RequiresPermissions(value = {PermissionConstants.PROJECT_API_DEFINITION_UPDATE, PermissionConstants.PROJECT_API_DEFINITION_ADD}, + logical = Logical.OR) + public Map copyFile(@Validated @RequestBody ApiFileCopyRequest request) { + return apiDefinitionService.copyFile(request); + } + @PostMapping(value = "/copy") @Operation(summary = "接口测试-接口管理-复制接口定义") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_UPDATE) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java index c39a8d6f41..d3cb52602e 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiTestCaseController.java @@ -9,6 +9,7 @@ import io.metersphere.api.dto.ReferenceDTO; import io.metersphere.api.dto.ReferenceRequest; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.ApiTransferRequest; +import io.metersphere.api.dto.scenario.ApiFileCopyRequest; import io.metersphere.api.service.ApiFileResourceService; import io.metersphere.api.service.definition.*; import io.metersphere.plugin.api.spi.AbstractMsTestElement; @@ -42,6 +43,7 @@ import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; import java.util.List; +import java.util.Map; @RestController @@ -140,6 +142,14 @@ public class ApiTestCaseController { return apiTestCaseService.update(request, SessionUtils.getUserId()); } + @PostMapping("/file/copy") + @Operation(summary = "接口测试-接口管理-复制用例时,复制文件") + @RequiresPermissions(value = {PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE, PermissionConstants.PROJECT_API_DEFINITION_CASE_ADD}, + logical = Logical.OR) + public Map copyFile(@Validated @RequestBody ApiFileCopyRequest request) { + return apiTestCaseService.copyFile(request); + } + @GetMapping(value = "/update-priority/{id}/{priority}") @Operation(summary = "接口测试-接口管理-接口用例-更新等级") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiFileCopyRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiFileCopyRequest.java new file mode 100644 index 0000000000..54c8f7d1e7 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiFileCopyRequest.java @@ -0,0 +1,19 @@ +package io.metersphere.api.dto.scenario; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; +import lombok.Data; + +import java.util.List; + +@Data +public class ApiFileCopyRequest { + @Schema(description = "资源id") + @NotBlank + private String resourceId; + + @Schema(description = "文件数组") + @NotEmpty + private List fileIds; +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java index 0da718f01a..2e7d706aca 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiCommonService.java @@ -31,6 +31,7 @@ import io.metersphere.project.service.CustomFunctionService; import io.metersphere.project.service.FileAssociationService; import io.metersphere.project.service.FileMetadataService; import io.metersphere.sdk.constants.ApplicationNumScope; +import io.metersphere.sdk.constants.DefaultRepositoryDir; import io.metersphere.sdk.constants.ExecStatus; import io.metersphere.sdk.constants.TaskItemErrorMessage; import io.metersphere.sdk.dto.api.task.GetRunScriptRequest; @@ -61,10 +62,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -84,6 +82,8 @@ public class ApiCommonService { @Resource private ExecTaskItemMapper execTaskItemMapper; @Resource + private ApiFileResourceService apiFileResourceService; + @Resource private ExecTaskMapper execTaskMapper; /** @@ -618,4 +618,25 @@ public class ApiCommonService { } return null; } + + /** + * 复制文件到临时目录 + * @param fileIds + * @param sourceDir + * @return + */ + public Map copyFiles2TempDir(List fileIds, String sourceDir) { + Map uploadFileMap = new HashMap<>(); + for (String fileId : fileIds) { + String newFileId = IDGenerator.nextStr(); + String targetDir = DefaultRepositoryDir.getSystemTempDir(); + String fileName = apiFileResourceService.getFileNameByFileId(fileId, sourceDir); + // 复制文件到临时目录 + apiFileResourceService.copyFile(sourceDir + "/" + fileId, + targetDir + "/" + newFileId, + fileName); + uploadFileMap.put(fileId, newFileId); + } + return uploadFileMap; + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java index 77e6a40937..3963f0ce5f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java @@ -9,6 +9,7 @@ import io.metersphere.api.dto.debug.ApiResourceRunRequest; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.ApiEditPosRequest; import io.metersphere.api.dto.request.ApiTransferRequest; +import io.metersphere.api.dto.scenario.ApiFileCopyRequest; import io.metersphere.api.dto.schema.JsonSchemaItem; import io.metersphere.api.mapper.*; import io.metersphere.api.service.ApiCommonService; @@ -1354,4 +1355,13 @@ public class ApiDefinitionService extends MoveNodeService { private boolean isObjectItem(JsonSchemaItem jsonSchemaItem) { return StringUtils.equals(jsonSchemaItem.getType(), PropertyConstant.OBJECT); } + + public Map copyFile(ApiFileCopyRequest request) { + // 从接口定义复制 + ApiDefinition apiDefinition = checkApiDefinition(request.getResourceId()); + String sourceDir = DefaultRepositoryDir.getApiDefinitionDir(apiDefinition.getProjectId(), + apiDefinition.getId()); + + return apiCommonService.copyFiles2TempDir(request.getFileIds(), sourceDir); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java index af599aa3b5..4c8b6da56d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseService.java @@ -8,6 +8,7 @@ import io.metersphere.api.dto.debug.ApiFileResourceUpdateRequest; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.request.ApiTransferRequest; import io.metersphere.api.dto.request.http.MsHTTPElement; +import io.metersphere.api.dto.scenario.ApiFileCopyRequest; import io.metersphere.api.mapper.*; import io.metersphere.api.service.ApiCommonService; import io.metersphere.api.service.ApiFileResourceService; @@ -52,6 +53,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.jetbrains.annotations.NotNull; import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -867,4 +869,13 @@ public class ApiTestCaseService extends MoveNodeService { extApiTestCaseMapper.clearApiChange(List.of(request.getId())); return HttpRequestParamDiffUtils.syncRequestDiff(request, apiMsTestElement, apiTestCaseMsTestElement); } + + public Map copyFile(ApiFileCopyRequest request) { + // 从接口定义复制 + ApiTestCase apiTestCase = checkResourceExist(request.getResourceId()); + String sourceDir = DefaultRepositoryDir.getApiCaseDir(apiTestCase.getProjectId(), + apiTestCase.getId()); + + return apiCommonService.copyFiles2TempDir(request.getFileIds(), sourceDir); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java index e057111b7d..62c1ab3ca6 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java @@ -2451,7 +2451,6 @@ public class ApiScenarioService extends MoveNodeService { public Map copyStepFile(ApiScenarioStepFileCopyRequest request) { String sourceDir; - Map uploadFileMap = new HashMap<>(); if (BooleanUtils.isTrue(request.getIsTempFile())) { sourceDir = DefaultRepositoryDir.getSystemTempDir(); } else { @@ -2477,16 +2476,6 @@ public class ApiScenarioService extends MoveNodeService { } } - for (String fileId : request.getFileIds()) { - String newFileId = IDGenerator.nextStr(); - String targetDir = DefaultRepositoryDir.getSystemTempDir(); - String fileName = apiFileResourceService.getFileNameByFileId(fileId, sourceDir); - // 复制文件到临时目录 - apiFileResourceService.copyFile(sourceDir + "/" + fileId, - targetDir + "/" + newFileId, - fileName); - uploadFileMap.put(fileId, newFileId); - } - return uploadFileMap; + return apiCommonService.copyFiles2TempDir(request.getFileIds(), sourceDir); } }