From 07095ecd5a3559329a4642c66ff589dc2fa55855 Mon Sep 17 00:00:00 2001 From: lan-yonghui Date: Fri, 8 Dec 2023 16:08:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=AE=A1=E7=90=86=E6=A8=A1=E5=9D=97=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/ApiFileResourceService.java | 34 +++++++++++++++++++ .../definition/ApiDefinitionService.java | 7 ++-- .../ApiDefinitionControllerTests.java | 13 ++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiFileResourceService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiFileResourceService.java index 4cd55ae681..61f75dc7aa 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiFileResourceService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiFileResourceService.java @@ -24,6 +24,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -242,4 +243,37 @@ public class ApiFileResourceService { } return fileId; } + + public void copyFileByResourceId(String sourceId, String sourceFolder, String targetId, String targetFolder) { + List files = getByResourceId(sourceId); + if(!files.isEmpty()){ + FileRepository defaultRepository = FileCenter.getDefaultRepository(); + List apiFileResources = new ArrayList<>(); + files.forEach(item->{ + try { + // 按ID建文件夹,避免文件名重复 + FileCopyRequest fileCopyRequest = new FileCopyRequest(); + fileCopyRequest.setCopyFolder(sourceFolder + "/" + item.getFileId()); + fileCopyRequest.setCopyfileName(item.getFileName()); + fileCopyRequest.setFileName(item.getFileName()); + fileCopyRequest.setFolder(targetFolder + "/" + item.getFileId()); + // 将文件从临时目录复制到资源目录 + defaultRepository.copyFile(fileCopyRequest); + ApiFileResource apiFileResource = new ApiFileResource(); + apiFileResource.setFileId(item.getFileId()); + apiFileResource.setResourceId(targetId); + apiFileResource.setResourceType(item.getResourceType()); + apiFileResource.setProjectId(item.getProjectId()); + apiFileResource.setCreateTime(System.currentTimeMillis()); + apiFileResource.setFileName(item.getFileName()); + apiFileResources.add(apiFileResource); + } catch (Exception e) { + LogUtils.error(e); + throw new MSException(Translator.get("file_copy_fail")); + } + }); + + apiFileResourceMapper.batchInsert(apiFileResources); + } + } } 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 73142e1d96..5ff835f084 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 @@ -249,7 +249,10 @@ public class ApiDefinitionService { apiDefinitionBlob.setResponse(copyApiDefinitionBlob.getResponse()); apiDefinitionBlobMapper.insertSelective(apiDefinitionBlob); } - // TODO 复制的时候文件需要复制一份 仅复制接口内容, 不包含用例、mock信息 + + String sourceDir = DefaultRepositoryDir.getApiDefinitionDir(apiDefinition.getProjectId(), request.getId()); + String targetDir = DefaultRepositoryDir.getApiDefinitionDir(apiDefinition.getProjectId(), apiDefinition.getId()); + apiFileResourceService.copyFileByResourceId(request.getId(), sourceDir, apiDefinition.getId(), targetDir); return apiDefinition; } @@ -671,7 +674,7 @@ public class ApiDefinitionService { public ApiDefinitionDocDTO getDocInfo(ApiDefinitionDocRequest request, String userId) { ApiDefinitionDocDTO apiDefinitionDocDTO = new ApiDefinitionDocDTO(); apiDefinitionDocDTO.setType(request.getType()); - // @@TODO 下载所有/一个模块接口文档时,不做分页数据量大的时候会不会有性能问题 + // 下载所有/一个模块接口文档时,不做分页数据量大的时候会不会有性能问题,单独做接口 if (ApiDefinitionDocType.ALL.name().equals(request.getType()) || ApiDefinitionDocType.MODULE.name().equals(request.getType())) { List list = extApiDefinitionMapper.listDoc(request); if (!list.isEmpty()) { diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java index 3027dd5b8c..3b06b19744 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -489,15 +489,26 @@ public class ApiDefinitionControllerTests extends BaseTest { @Order(5) public void copy() throws Exception { LogUtils.info("copy api test"); - ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1001"); ApiDefinitionCopyRequest request = new ApiDefinitionCopyRequest(); request.setId(apiDefinition.getId()); MvcResult mvcResult = this.requestPostWithOkAndReturn(COPY, request); ApiDefinition resultData = getResultData(mvcResult, ApiDefinition.class); // @数据验证 + List sourceFiles = apiFileResourceService.getByResourceId(apiDefinition.getId()); + List copyFiles = apiFileResourceService.getByResourceId(resultData.getId()); + if(!sourceFiles.isEmpty() && !copyFiles.isEmpty()){ + Assertions.assertEquals(sourceFiles.size(), copyFiles.size()); + } Assertions.assertTrue(resultData.getName().contains("copy_")); // @@校验日志 checkLog(resultData.getId(), OperationLogType.UPDATE); + + request.setId("1001"); + MvcResult mvcResultCopy = this.requestPostWithOkAndReturn(COPY, request); + ApiDefinition resultDataCopy = getResultData(mvcResultCopy, ApiDefinition.class); + // @数据验证 + Assertions.assertTrue(resultDataCopy.getName().contains("copy_")); + request.setId("121"); assertErrorCode(this.requestPost(COPY, request), API_DEFINITION_NOT_EXIST); // @@校验权限