From ab326b3d73252c01794435941c7d8e7484b9a8b2 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Tue, 6 Aug 2024 10:54:25 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B):=20?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=AF=BC=E5=87=BA=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/FunctionalCaseController.java | 9 +++++++ .../service/FunctionalCaseFileService.java | 25 ++++++++++++++++++- .../FunctionalCaseControllerTests.java | 9 +++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseController.java b/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseController.java index cdb0d7c9fc..5ac5beb299 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseController.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseController.java @@ -35,6 +35,7 @@ import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.constraints.NotBlank; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -272,4 +273,12 @@ public class FunctionalCaseController { public FunctionalCaseExportColumns getExportColumns(@PathVariable String projectId) { return functionalCaseFileService.getExportColumns(projectId); } + + + @GetMapping(value = "/download/file/{projectId}/{fileId}") + @Operation(summary = "用例管理-功能用例-下载文件") + @RequiresPermissions(PermissionConstants.FUNCTIONAL_CASE_READ_EXPORT) + public ResponseEntity downloadImgById(@PathVariable String projectId, @PathVariable String fileId) { + return functionalCaseFileService.downloadFile(projectId, fileId); + } } diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseFileService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseFileService.java index 534394087d..62f216c74f 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseFileService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseFileService.java @@ -54,6 +54,9 @@ import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.jetbrains.annotations.NotNull; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -99,6 +102,7 @@ public class FunctionalCaseFileService { private FunctionalCaseLogService functionalCaseLogService; @Resource private SystemParameterMapper systemParameterMapper; + private static final String EXPORT_FILE_NAME = "case_export"; /** * 下载excel导入模板 @@ -364,7 +368,7 @@ public class FunctionalCaseFileService { private void uploadFileToMinio(File file, String fileId) { FileRequest fileRequest = new FileRequest(); - fileRequest.setFileName(file.getName()); + fileRequest.setFileName(EXPORT_FILE_NAME); fileRequest.setFolder(DefaultRepositoryDir.getExportExcelTempDir() + "/" + fileId); fileRequest.setStorage(StorageType.MINIO.name()); try { @@ -734,4 +738,23 @@ public class FunctionalCaseFileService { functionalCaseExportColumns.initCustomColumns(headerCustomFields); return functionalCaseExportColumns; } + + public ResponseEntity downloadFile(String projectId, String fileId) { + Project project = projectMapper.selectByPrimaryKey(projectId); + byte[] bytes; + FileRequest fileRequest = new FileRequest(); + fileRequest.setFileName(EXPORT_FILE_NAME); + fileRequest.setFolder(DefaultRepositoryDir.getExportExcelTempDir() + "/" + fileId); + fileRequest.setStorage(StorageType.MINIO.name()); + try { + bytes = fileService.download(fileRequest); + } catch (Exception e) { + throw new MSException("get file error"); + } + + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/octet-stream")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "Metersphere_case_" + project.getName() + "\"") + .body(bytes); + } } diff --git a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java index b2a5a05ded..f13dd64ac5 100644 --- a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java +++ b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java @@ -89,6 +89,7 @@ public class FunctionalCaseControllerTests extends BaseTest { public static final String EXPORT_EXCEL_URL = "/functional/case/export/excel"; public static final String DOWNLOAD_XMIND_TEMPLATE_URL = "/functional/case/download/xmind/template/"; public static final String EXPORT_COLUMNS_URL = "/functional/case/export/columns/"; + public static final String DOWNLOAD_FILE_URL = "/functional/case/download/file/"; @Resource private NotificationMapper notificationMapper; @@ -849,4 +850,12 @@ public class FunctionalCaseControllerTests extends BaseTest { public void getExportColumns() throws Exception { this.requestGetExcel(EXPORT_COLUMNS_URL + DEFAULT_PROJECT_ID); } + + @Test + @Order(24) + public void downloadFile() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get(DOWNLOAD_FILE_URL + DEFAULT_PROJECT_ID + "/" + "123142342") + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken)); + } }