fix(测试跟踪): 用例及缺陷关联附件的下载格式问题

--bug=1016969 --user=宋昌昌 【测试跟踪】-功能用例附件中关联gitlab文件jpg格式,下载该附件文件格式错误 https://www.tapd.cn/55049933/s/1246735
This commit is contained in:
song-cc-rock 2022-09-20 14:24:16 +08:00 committed by 刘瑞斌
parent b6d07fa0d3
commit 727c7159b2
3 changed files with 22 additions and 20 deletions

View File

@ -6,11 +6,15 @@ import io.metersphere.base.mapper.ext.ExtFileMetadataMapper;
import io.metersphere.commons.constants.FileType; import io.metersphere.commons.constants.FileType;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*; import io.metersphere.commons.utils.*;
import io.metersphere.metadata.utils.MetadataUtils;
import io.metersphere.performance.request.QueryProjectFileRequest; import io.metersphere.performance.request.QueryProjectFileRequest;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import io.metersphere.xmind.utils.FileUtil; import io.metersphere.xmind.utils.FileUtil;
import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -51,6 +55,17 @@ public class FileService {
return FileUtils.fileToByte(attachmentFile); return FileUtils.fileToByte(attachmentFile);
} }
public ResponseEntity<byte[]> 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) { public MultipartFile getAttachmentMultipartFile(String id) {
FileAttachmentMetadata fileAttachmentMetadata = fileAttachmentMetadataMapper.selectByPrimaryKey(id); FileAttachmentMetadata fileAttachmentMetadata = fileAttachmentMetadataMapper.selectByPrimaryKey(id);
File attachmentFile = new File(fileAttachmentMetadata.getFilePath() + "/" + fileAttachmentMetadata.getName()); File attachmentFile = new File(fileAttachmentMetadata.getFilePath() + "/" + fileAttachmentMetadata.getName());

View File

@ -5,7 +5,6 @@ import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.metadata.vo.AttachmentDumpRequest; import io.metersphere.metadata.vo.AttachmentDumpRequest;
import io.metersphere.service.FileService; import io.metersphere.service.FileService;
import io.metersphere.track.request.attachment.AttachmentRequest; import io.metersphere.track.request.attachment.AttachmentRequest;
import io.metersphere.track.request.testplan.FileOperationRequest;
import io.metersphere.track.service.AttachmentService; import io.metersphere.track.service.AttachmentService;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -14,8 +13,6 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -53,20 +50,14 @@ public class AttachmentController {
.body(bytes); .body(bytes);
} }
@PostMapping("/download") @GetMapping("/download/{id}/{isLocal}")
public ResponseEntity<byte[]> downloadAttachment(@RequestBody FileOperationRequest fileOperationRequest) { public ResponseEntity<byte[]> downloadAttachment(@PathVariable("id") String fileId, @PathVariable("isLocal") Boolean isLocal) {
byte[] bytes; if (isLocal) {
if (fileOperationRequest.getIsLocal()) { return fileService.downloadLocalAttachment(fileId);
bytes = fileService.getAttachmentBytes(fileOperationRequest.getId());
} else { } else {
String refId = attachmentService.getRefIdByAttachmentId(fileOperationRequest.getId()); String refId = attachmentService.getRefIdByAttachmentId(fileId);
bytes = fileMetadataService.loadFileAsBytes(refId); 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}") @GetMapping("/delete/{attachmentType}/{attachmentId}")

View File

@ -138,11 +138,7 @@ export default {
return fileType === 'JPG' || fileType === 'JPEG' || fileType === 'PDF' || fileType === 'PNG'; return fileType === 'JPG' || fileType === 'JPEG' || fileType === 'PDF' || fileType === 'PNG';
}, },
handleDownload(file) { handleDownload(file) {
this.$fileDownloadPost('/attachment/download', { this.$fileDownload('/attachment/download/' + file.id + "/" + file.isLocal, file.name);
name: file.name,
id: file.id,
isLocal: file.isLocal
});
}, },
handleUpload(file) { handleUpload(file) {
this.$emit("handleDump", file); this.$emit("handleDump", file);