fix(项目设置): 修复环境无法预览csv文件的缺陷

--bug=1032782 --user=王孝刚 【项目设置】github##27503,项目环境上传csv文件保存后,文件预览显示为空 https://www.tapd.cn/55049933/s/1435969
This commit is contained in:
wxg0103 2023-11-13 18:31:13 +08:00 committed by wxg0103
parent ff8517db35
commit 0196744ce9
6 changed files with 89 additions and 1 deletions

View File

@ -143,7 +143,6 @@ export default {
},
},
created() {
console.log(this.httpConfig)
this.list();
},
data() {

View File

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

View File

@ -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<String> 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;
}
}

View File

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

View File

@ -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<byte[]> 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);
}
}

View File

@ -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<byte[]> 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);
}
}