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); + } +}