From 727c7159b22af156168187077fc05f96002300d3 Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Tue, 20 Sep 2022 14:24:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E5=8F=8A=E7=BC=BA=E9=99=B7=E5=85=B3=E8=81=94?= =?UTF-8?q?=E9=99=84=E4=BB=B6=E7=9A=84=E4=B8=8B=E8=BD=BD=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1016969 --user=宋昌昌 【测试跟踪】-功能用例附件中关联gitlab文件jpg格式,下载该附件文件格式错误 https://www.tapd.cn/55049933/s/1246735 --- .../io/metersphere/service/FileService.java | 15 +++++++++++++ .../controller/AttachmentController.java | 21 ++++++------------- .../case/components/TestCaseAttachment.vue | 6 +----- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/backend/src/main/java/io/metersphere/service/FileService.java b/backend/src/main/java/io/metersphere/service/FileService.java index e7da233c53..0430cf5c52 100644 --- a/backend/src/main/java/io/metersphere/service/FileService.java +++ b/backend/src/main/java/io/metersphere/service/FileService.java @@ -6,11 +6,15 @@ import io.metersphere.base.mapper.ext.ExtFileMetadataMapper; import io.metersphere.commons.constants.FileType; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.*; +import io.metersphere.metadata.utils.MetadataUtils; import io.metersphere.performance.request.QueryProjectFileRequest; import org.apache.commons.collections.CollectionUtils; import io.metersphere.xmind.utils.FileUtil; import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -51,6 +55,17 @@ public class FileService { return FileUtils.fileToByte(attachmentFile); } + public ResponseEntity downloadLocalAttachment(String id) { + FileAttachmentMetadata fileAttachmentMetadata = fileAttachmentMetadataMapper.selectByPrimaryKey(id); + File attachmentFile = new File(fileAttachmentMetadata.getFilePath() + "/" + fileAttachmentMetadata.getName()); + byte[] bytes = FileUtils.fileToByte(attachmentFile); + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/octet-stream")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + MetadataUtils.getFileName(fileAttachmentMetadata.getName(), fileAttachmentMetadata.getType()) + "\"") + .contentLength(bytes.length) + .body(bytes); + } + public MultipartFile getAttachmentMultipartFile(String id) { FileAttachmentMetadata fileAttachmentMetadata = fileAttachmentMetadataMapper.selectByPrimaryKey(id); File attachmentFile = new File(fileAttachmentMetadata.getFilePath() + "/" + fileAttachmentMetadata.getName()); diff --git a/backend/src/main/java/io/metersphere/track/controller/AttachmentController.java b/backend/src/main/java/io/metersphere/track/controller/AttachmentController.java index 5c5cadae6a..38ec808374 100644 --- a/backend/src/main/java/io/metersphere/track/controller/AttachmentController.java +++ b/backend/src/main/java/io/metersphere/track/controller/AttachmentController.java @@ -5,7 +5,6 @@ import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.metadata.vo.AttachmentDumpRequest; import io.metersphere.service.FileService; import io.metersphere.track.request.attachment.AttachmentRequest; -import io.metersphere.track.request.testplan.FileOperationRequest; import io.metersphere.track.service.AttachmentService; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -14,8 +13,6 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -53,20 +50,14 @@ public class AttachmentController { .body(bytes); } - @PostMapping("/download") - public ResponseEntity downloadAttachment(@RequestBody FileOperationRequest fileOperationRequest) { - byte[] bytes; - if (fileOperationRequest.getIsLocal()) { - bytes = fileService.getAttachmentBytes(fileOperationRequest.getId()); + @GetMapping("/download/{id}/{isLocal}") + public ResponseEntity downloadAttachment(@PathVariable("id") String fileId, @PathVariable("isLocal") Boolean isLocal) { + if (isLocal) { + return fileService.downloadLocalAttachment(fileId); } else { - String refId = attachmentService.getRefIdByAttachmentId(fileOperationRequest.getId()); - bytes = fileMetadataService.loadFileAsBytes(refId); + String refId = attachmentService.getRefIdByAttachmentId(fileId); + return fileMetadataService.getFile(refId); } - return ResponseEntity.ok() - .contentType(MediaType.parseMediaType("application/octet-stream")) - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + URLEncoder.encode(fileOperationRequest.getName(), StandardCharsets.UTF_8) + "\"") - .contentLength(bytes.length) - .body(bytes); } @GetMapping("/delete/{attachmentType}/{attachmentId}") diff --git a/frontend/src/business/components/track/case/components/TestCaseAttachment.vue b/frontend/src/business/components/track/case/components/TestCaseAttachment.vue index 4ddc79dd41..e92aabccba 100644 --- a/frontend/src/business/components/track/case/components/TestCaseAttachment.vue +++ b/frontend/src/business/components/track/case/components/TestCaseAttachment.vue @@ -138,11 +138,7 @@ export default { return fileType === 'JPG' || fileType === 'JPEG' || fileType === 'PDF' || fileType === 'PNG'; }, handleDownload(file) { - this.$fileDownloadPost('/attachment/download', { - name: file.name, - id: file.id, - isLocal: file.isLocal - }); + this.$fileDownload('/attachment/download/' + file.id + "/" + file.isLocal, file.name); }, handleUpload(file) { this.$emit("handleDump", file);