diff --git a/backend/src/main/java/io/metersphere/controller/TestPlanController.java b/backend/src/main/java/io/metersphere/controller/TestPlanController.java index 48d4f27dca..b8b639f7eb 100644 --- a/backend/src/main/java/io/metersphere/controller/TestPlanController.java +++ b/backend/src/main/java/io/metersphere/controller/TestPlanController.java @@ -1,12 +1,18 @@ package io.metersphere.controller; +import io.metersphere.requests.testplan.FileOperationRequest; import io.metersphere.service.FileService; +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; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; @RestController @@ -16,7 +22,22 @@ public class TestPlanController { private FileService fileService; @PostMapping("/file/upload") - public void upload(MultipartFile file) throws IOException { + public void uploadJmx(MultipartFile file) throws IOException { fileService.upload(file.getOriginalFilename(), file); } + + @PostMapping("/file/delete") + public void deleteJmx(@RequestBody FileOperationRequest request) { + System.out.println(String.format("delete %s", request.getName())); + } + + @PostMapping("/file/download") + public ResponseEntity downloadJmx(@RequestBody FileOperationRequest fileOperationRequest, HttpServletResponse response) { + org.springframework.core.io.Resource resource = fileService.loadFileAsResource(fileOperationRequest.getName()); + + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/octet-stream")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileOperationRequest.getName() + "\"") + .body(resource); + } } diff --git a/backend/src/main/java/io/metersphere/requests/testplan/FileOperationRequest.java b/backend/src/main/java/io/metersphere/requests/testplan/FileOperationRequest.java new file mode 100644 index 0000000000..88ff096c6a --- /dev/null +++ b/backend/src/main/java/io/metersphere/requests/testplan/FileOperationRequest.java @@ -0,0 +1,13 @@ +package io.metersphere.requests.testplan; + +public class FileOperationRequest { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/backend/src/main/java/io/metersphere/service/FileService.java b/backend/src/main/java/io/metersphere/service/FileService.java index f6b7090a73..3c0d7d6dfc 100644 --- a/backend/src/main/java/io/metersphere/service/FileService.java +++ b/backend/src/main/java/io/metersphere/service/FileService.java @@ -1,18 +1,41 @@ package io.metersphere.service; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @Service public class FileService { + // 将上传的文件保存在内存,方便测试 + private Map fileMap = new ConcurrentHashMap<>(); + public void upload(String name, MultipartFile file) throws IOException { String result = new BufferedReader(new InputStreamReader(file.getInputStream())) .lines().collect(Collectors.joining("\n")); System.out.println(String.format("upload file: %s, content: \n%s", name, result)); + + fileMap.put(name, file); + } + + public Resource loadFileAsResource(String name) { + final MultipartFile file = fileMap.get(name); + + if (file != null) { + try { + return new InputStreamResource(file.getInputStream()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return null; } } \ No newline at end of file diff --git a/frontend/src/performance/components/common/filter/TimestampFormatDateFilter.js b/frontend/src/performance/components/common/filter/TimestampFormatDateFilter.js new file mode 100644 index 0000000000..678f3b9de3 --- /dev/null +++ b/frontend/src/performance/components/common/filter/TimestampFormatDateFilter.js @@ -0,0 +1,27 @@ +let timestampFormatDate = function (timestamp) { + if (!timestamp) { + return timestamp + } + + let date = new Date(timestamp); + + let y = date.getFullYear(); + + let MM = date.getMonth() + 1; + MM = MM < 10 ? ('0' + MM) : MM; + + let d = date.getDate(); + d = d < 10 ? ('0' + d) : d; + + let h = date.getHours(); + h = h < 10 ? ('0' + h) : h; + + let m = date.getMinutes(); + m = m < 10 ? ('0' + m) : m; + + let s = date.getSeconds(); + s = s < 10 ? ('0' + s) : s; + + return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s +}; +export default timestampFormatDate diff --git a/frontend/src/performance/components/testPlan/CreateTestPlan.vue b/frontend/src/performance/components/testPlan/CreateTestPlan.vue index 3be9afc501..2a1cd96af5 100644 --- a/frontend/src/performance/components/testPlan/CreateTestPlan.vue +++ b/frontend/src/performance/components/testPlan/CreateTestPlan.vue @@ -1,11 +1,29 @@