From 0196744ce9bb0995afe4f2e365b474570e417b6b Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 13 Nov 2023 18:31:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=A1=B9=E7=9B=AE=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=8E=AF=E5=A2=83=E6=97=A0=E6=B3=95=E9=A2=84?= =?UTF-8?q?=E8=A7=88csv=E6=96=87=E4=BB=B6=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1032782 --user=王孝刚 【项目设置】github##27503,项目环境上传csv文件保存后,文件预览显示为空 https://www.tapd.cn/55049933/s/1435969 --- .../environment/EnvironmentHttpConfig.vue | 1 - .../metersphere/dto/FileOperationRequest.java | 11 ++++++++ .../service/BaseProjectService.java | 23 ++++++++++++++++ .../controller/PerformanceTestController.java | 1 + .../controller/remote/ApiTestController.java | 27 +++++++++++++++++++ .../controller/ApiTestController.java | 27 +++++++++++++++++++ 6 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 framework/sdk-parent/sdk/src/main/java/io/metersphere/dto/FileOperationRequest.java create mode 100644 project-management/backend/src/main/java/io/metersphere/controller/remote/ApiTestController.java create mode 100644 system-setting/backend/src/main/java/io/metersphere/controller/ApiTestController.java diff --git a/framework/sdk-parent/frontend/src/components/environment/EnvironmentHttpConfig.vue b/framework/sdk-parent/frontend/src/components/environment/EnvironmentHttpConfig.vue index 9284875f05..4c1568ebf1 100644 --- a/framework/sdk-parent/frontend/src/components/environment/EnvironmentHttpConfig.vue +++ b/framework/sdk-parent/frontend/src/components/environment/EnvironmentHttpConfig.vue @@ -143,7 +143,6 @@ export default { }, }, created() { - console.log(this.httpConfig) this.list(); }, data() { diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/dto/FileOperationRequest.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/dto/FileOperationRequest.java new file mode 100644 index 0000000000..39c317ca11 --- /dev/null +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/dto/FileOperationRequest.java @@ -0,0 +1,11 @@ +package io.metersphere.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class FileOperationRequest { + private String id; + private String name; +} diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseProjectService.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseProjectService.java index 24293e2420..7005e17618 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseProjectService.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseProjectService.java @@ -10,8 +10,11 @@ import io.metersphere.base.mapper.ext.BaseUserGroupMapper; import io.metersphere.base.mapper.ext.BaseUserMapper; import io.metersphere.commons.constants.ProjectApplicationType; import io.metersphere.commons.exception.MSException; +import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.JSON; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.SessionUtils; +import io.metersphere.dto.FileOperationRequest; import io.metersphere.dto.ProjectConfig; import io.metersphere.dto.ProjectDTO; import io.metersphere.dto.WorkspaceMemberDTO; @@ -31,6 +34,9 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; import java.util.*; import java.util.stream.Collectors; @@ -391,4 +397,21 @@ public class BaseProjectService { List projectUserId = baseUserGroupMapper.getProjectUserId(projectId); return projectUserId.contains(userId); } + + public byte[] loadFileAsBytes(FileOperationRequest fileOperationRequest) { + if (fileOperationRequest.getId().contains("/") || fileOperationRequest.getName().contains("/")) + MSException.throwException(Translator.get("invalid_parameter")); + File file = new File(FileUtils.BODY_FILE_DIR + "/" + fileOperationRequest.getId() + "_" + fileOperationRequest.getName()); + try (FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);) { + byte[] b = new byte[1000]; + int n; + while ((n = fis.read(b)) != -1) { + bos.write(b, 0, n); + } + return bos.toByteArray(); + } catch (Exception ex) { + LogUtil.error(ex); + } + return null; + } } diff --git a/performance-test/backend/src/main/java/io/metersphere/controller/PerformanceTestController.java b/performance-test/backend/src/main/java/io/metersphere/controller/PerformanceTestController.java index 7cab19e0a5..38f8b6536e 100644 --- a/performance-test/backend/src/main/java/io/metersphere/controller/PerformanceTestController.java +++ b/performance-test/backend/src/main/java/io/metersphere/controller/PerformanceTestController.java @@ -13,6 +13,7 @@ import io.metersphere.commons.utils.Pager; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.consul.CacheNode; import io.metersphere.consul.ConsulService; +import io.metersphere.dto.FileOperationRequest; import io.metersphere.dto.*; import io.metersphere.log.annotation.MsAuditLog; import io.metersphere.log.annotation.MsRequestLog; diff --git a/project-management/backend/src/main/java/io/metersphere/controller/remote/ApiTestController.java b/project-management/backend/src/main/java/io/metersphere/controller/remote/ApiTestController.java new file mode 100644 index 0000000000..904d5dfd3a --- /dev/null +++ b/project-management/backend/src/main/java/io/metersphere/controller/remote/ApiTestController.java @@ -0,0 +1,27 @@ +package io.metersphere.controller.remote; + +import io.metersphere.dto.FileOperationRequest; +import io.metersphere.service.BaseProjectService; +import jakarta.annotation.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(path = { + "/api/automation", +}) +public class ApiTestController { + + @Resource + private BaseProjectService baseProjectService; + @PostMapping("/file/download") + public ResponseEntity download(@RequestBody FileOperationRequest request){ + byte[] bytes = baseProjectService.loadFileAsBytes(request); + return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/octet-stream")).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + request.getName() + "\"").body(bytes); + } +} diff --git a/system-setting/backend/src/main/java/io/metersphere/controller/ApiTestController.java b/system-setting/backend/src/main/java/io/metersphere/controller/ApiTestController.java new file mode 100644 index 0000000000..228e32a0ab --- /dev/null +++ b/system-setting/backend/src/main/java/io/metersphere/controller/ApiTestController.java @@ -0,0 +1,27 @@ +package io.metersphere.controller; + +import io.metersphere.dto.FileOperationRequest; +import io.metersphere.service.BaseProjectService; +import jakarta.annotation.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(path = { + "/api/automation", +}) +public class ApiTestController { + + @Resource + private BaseProjectService baseProjectService; + @PostMapping("/file/download") + public ResponseEntity download(@RequestBody FileOperationRequest request){ + byte[] bytes = baseProjectService.loadFileAsBytes(request); + return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/octet-stream")).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + request.getName() + "\"").body(bytes); + } +}