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 1e722cd22f
commit 09636279a8
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.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<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) {
FileAttachmentMetadata fileAttachmentMetadata = fileAttachmentMetadataMapper.selectByPrimaryKey(id);
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.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<byte[]> downloadAttachment(@RequestBody FileOperationRequest fileOperationRequest) {
byte[] bytes;
if (fileOperationRequest.getIsLocal()) {
bytes = fileService.getAttachmentBytes(fileOperationRequest.getId());
@GetMapping("/download/{id}/{isLocal}")
public ResponseEntity<byte[]> 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}")

View File

@ -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);