Browse Source

大文件下载优化

dev-opt
chenzhihang 8 months ago
parent
commit
43bb8fc9f7
3 changed files with 76 additions and 40 deletions
  1. +6
    -4
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/model/NewModelFromGitController.java
  2. +4
    -2
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ModelsService.java
  3. +66
    -34
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java

+ 6
- 4
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/model/NewModelFromGitController.java View File

@@ -21,6 +21,8 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@@ -72,14 +74,14 @@ public class NewModelFromGitController extends BaseController {

@GetMapping("/downloadAllFiles")
@ApiOperation(value = "下载同一版本下所有模型,并打包")
public ResponseEntity<InputStreamResource> downloadAllDatasetFiles(@RequestParam("name") String name, @RequestParam("identifier") String identifier, @RequestParam("id") Integer id, @RequestParam("owner") String owner, @RequestParam("version") String version, @RequestParam("is_public") Boolean isPublic) throws Exception {
return modelsService.downloadAllModelFilesNew(name, identifier, id, owner, version, isPublic);
public void downloadAllDatasetFiles(HttpServletRequest req, HttpServletResponse resp, @RequestParam("name") String name, @RequestParam("identifier") String identifier, @RequestParam("id") Integer id, @RequestParam("owner") String owner, @RequestParam("version") String version, @RequestParam("is_public") Boolean isPublic) throws Exception {
modelsService.downloadAllModelFilesNew(req, resp, name, identifier, id, owner, version, isPublic);
}

@GetMapping("/downloadSingleFile")
@ApiOperation(value = "下载单个模型文件", notes = "根据模型版本表id下载单个模型文件")
public ResponseEntity<InputStreamResource> downloadDatasetlocal(@RequestParam("url") String url) throws Exception {
return modelsService.downloadDatasetlocal(url);
public AjaxResult downloadDatasetlocal(@RequestParam("url") String url) throws Exception {
return AjaxResult.success(modelsService.downloadDatasetlocal(url));
}

@GetMapping("/queryModels")


+ 4
- 2
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ModelsService.java View File

@@ -14,6 +14,8 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@@ -72,7 +74,7 @@ public interface ModelsService {

ResponseEntity<InputStreamResource> downloadModels(Integer id) throws Exception;

ResponseEntity<InputStreamResource> downloadDatasetlocal(String filePath) throws Exception;
String downloadDatasetlocal(String filePath) throws Exception;

List<Map<String, String>> uploadModels(MultipartFile[] files, String uuid) throws Exception;

@@ -98,7 +100,7 @@ public interface ModelsService {

List<Map<String, String>> uploadModelLocal(MultipartFile[] files, String uuid) throws Exception;

ResponseEntity<InputStreamResource> downloadAllModelFilesNew(String name, String identifier, Integer id, String owner, String version, Boolean isPublic) throws IOException, Exception;
void downloadAllModelFilesNew(HttpServletRequest req, HttpServletResponse resp, String name, String identifier, Integer id, String owner, String version, Boolean isPublic) throws IOException, Exception;

Page<ModelsVo> newPubilcQueryByPage(ModelsVo modelsVo, PageRequest pageRequest, String accessToken) throws Exception;



+ 66
- 34
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java View File

@@ -38,6 +38,8 @@ import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder;
@@ -110,8 +112,12 @@ public class ModelsServiceImpl implements ModelsService {
private RemoteInfoPublishService remoteInfoPublishService;

// 固定存储桶名
@Value("${minio.endpointIp}")
String endpointIp;
@Value("${minio.dataReleaseBucketName}")
private String bucketName;
@Value("${minio.platformDataBucketName}")
private String platformDataBucketName;
@Resource
private MinioUtil minioUtil;

@@ -289,25 +295,27 @@ public class ModelsServiceImpl implements ModelsService {
}

@Override
public ResponseEntity<InputStreamResource> downloadDatasetlocal(String filePath) throws Exception {
File file = new File(filePath);

if (!file.exists()) {
throw new FileNotFoundException("File not found: " + filePath);
}

InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(file.length()));
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

return ResponseEntity.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
public String downloadDatasetlocal(String filePath) throws Exception {
// File file = new File(filePath);
//
// if (!file.exists()) {
// throw new FileNotFoundException("File not found: " + filePath);
// }
//
// InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
//
// HttpHeaders headers = new HttpHeaders();
// headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
// headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(file.length()));
// headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
//
// return ResponseEntity.ok()
// .headers(headers)
// .contentLength(file.length())
// .contentType(MediaType.APPLICATION_OCTET_STREAM)
// .body(resource);
String[] split = filePath.split("/");
return endpointIp + "/" + bucketName + "/" + platformDataBucketName + "/" + String.join("/", Arrays.copyOfRange(split, 3, split.length));
}


@@ -919,7 +927,7 @@ public class ModelsServiceImpl implements ModelsService {
}

@Override
public ResponseEntity<InputStreamResource> downloadAllModelFilesNew(String name, String identifier, Integer id, String owner, String version, Boolean isPublic) throws Exception {
public void downloadAllModelFilesNew(HttpServletRequest req, HttpServletResponse resp, String name, String identifier, Integer id, String owner, String version, Boolean isPublic) throws Exception {
// 命令行操作 git clone 项目地址
String localPath1 = localPath + owner + "/model/" + id + "/" + identifier + "/" + version;
// 打包 data 文件夹
@@ -944,13 +952,32 @@ public class ModelsServiceImpl implements ModelsService {

// 返回压缩文件的输入流
File zipFile = new File(zipFilePath);
InputStreamResource resource = new InputStreamResource(new FileInputStream(zipFile));

return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + name + "_" + version + "_model.zip")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.contentLength(zipFile.length())
.body(resource);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
try {
bis = new BufferedInputStream(new FileInputStream(zipFile));
fos = resp.getOutputStream();
bos = new BufferedOutputStream(fos);
ServletUtils.setFileDownloadHeader(req, resp, name);
int byteRead = 0;
byte[] buffer = new byte[8192];
while ((byteRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, byteRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bos.flush();
bis.close();
fos.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

@Override
@@ -979,7 +1006,8 @@ public class ModelsServiceImpl implements ModelsService {


@Override
public Page<ModelsVo> newPubilcQueryByPage(ModelsVo modelsVo, PageRequest pageRequest, String accessToken) throws Exception {
public Page<ModelsVo> newPubilcQueryByPage(ModelsVo modelsVo, PageRequest pageRequest, String accessToken) throws
Exception {
String token;
if (StringUtils.isNotEmpty(accessToken)) {
token = accessToken;
@@ -1007,7 +1035,8 @@ public class ModelsServiceImpl implements ModelsService {
}

@Override
public Page<Map<String, Object>> queryVersions(PageRequest pageRequest, String identifier, String owner, String type) throws Exception {
public Page<Map<String, Object>> queryVersions(PageRequest pageRequest, String identifier, String owner, String
type) throws Exception {
String token = gitService.checkoutToken();
List<Map<String, Object>> collect = gitService.getBrancheList(token, owner, identifier);
List<Map<String, Object>> result = collect.stream()
@@ -1119,7 +1148,8 @@ public class ModelsServiceImpl implements ModelsService {
}

@Override
public ModelsVo getModelDetail(Integer id, String identifier, String owner, String version, Boolean isPublic) throws Exception {
public ModelsVo getModelDetail(Integer id, String identifier, String owner, String version, Boolean isPublic) throws
Exception {
if (StringUtils.isEmpty(version)) {
version = "origin";
}
@@ -1220,7 +1250,8 @@ public class ModelsServiceImpl implements ModelsService {
}

@Override
public void deleteVersion(Integer repoId, String identifier, String owner, String version, String relativePath) throws Exception {
public void deleteVersion(Integer repoId, String identifier, String owner, String version, String relativePath) throws
Exception {
List<AssetWorkflow> assetWorkflow = assetWorkflowDao.getAssetWorkflow(Long.valueOf(repoId), Constant.Asset_Type_Model, version);
if (assetWorkflow != null && !assetWorkflow.isEmpty()) {
String workflows = String.join(",", assetWorkflow.stream().map(AssetWorkflow::getWorkflowName).collect(Collectors.toSet()));
@@ -1305,7 +1336,8 @@ public class ModelsServiceImpl implements ModelsService {
}

@Override
public List<ModelsVo> convert(List<Map<String, Object>> lst, String modelTopic, String modelTagName, String modelTypeName) {
public List<ModelsVo> convert(List<Map<String, Object>> lst, String modelTopic, String
modelTagName, String modelTypeName) {
if (lst != null && lst.size() > 0) {
List<ModelsVo> result = new ArrayList<>();
List<ModelsVo> newModelVos = ConvertUtil.convertListMapToObjectList(lst, ModelsVo.class);
@@ -1364,7 +1396,7 @@ public class ModelsServiceImpl implements ModelsService {
}

@Override
public void privateToPublic(ModelsVo modelsVo) throws Exception {
public void privateToPublic(ModelsVo modelsVo) throws Exception {
//获取版本列表信息,每一个版本的配置文件都要更改
String token = gitService.checkoutToken();
List<Map<String, Object>> brancheList = gitService.getBrancheList(token, modelsVo.getOwner(), modelsVo.getIdentifier());
@@ -1390,7 +1422,7 @@ public class ModelsServiceImpl implements ModelsService {
Map<String, Object> param = new HashMap<>();
param.put("id", projectDetail.get("project_id"));
param.put("is_public", true);
gitService.updateProjectDetail(param,modelsVo.getOwner(),modelsVo.getIdentifier(),token);
gitService.updateProjectDetail(param, modelsVo.getOwner(), modelsVo.getIdentifier(), token);
}


@@ -1401,7 +1433,7 @@ public class ModelsServiceImpl implements ModelsService {
//无需审批,直接通过
return "发布成功";
}
ModelsVo modelsVotDesc = getModelDetail(modelsVo.getId(), modelsVo.getIdentifier(),modelsVo.getOwner(),modelsVo.getVersion(), false);
ModelsVo modelsVotDesc = getModelDetail(modelsVo.getId(), modelsVo.getIdentifier(), modelsVo.getOwner(), modelsVo.getVersion(), false);
String username = loginUser.getUsername();
Long userid = loginUser.getUserid();
ApprovalRequest approvalRequest = new ApprovalRequest();


Loading…
Cancel
Save