Compare commits

...

2 Commits

4 changed files with 61 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package cd.casic.ci.api;
import cd.casic.ci.process.dto.req.target.*;
import cd.casic.ci.process.dto.resp.target.TargetManagerDownloadResp;
import cd.casic.ci.process.dto.resp.target.TargetManagerResp;
import cd.casic.ci.process.dto.resp.target.TargetVersionResp;
import cd.casic.ci.process.process.dataObject.base.BaseIdReq;
@ -138,8 +139,8 @@ public class TargetController {
}
@PostMapping(path="/zipDownload")
public void zipDownload(@RequestBody @Valid BaseIdReq req, HttpServletResponse response){
targetManagerService.zipDownload(req,response);
public CommonResult<TargetManagerDownloadResp> zipDownload(@RequestBody @Valid BaseIdReq req, HttpServletResponse response){
return CommonResult.success(targetManagerService.zipDownload(req,response));
}
@PostMapping(path = "/uploadFolder")

View File

@ -0,0 +1,9 @@
package cd.casic.ci.process.dto.resp.target;
import lombok.Data;
@Data
public class TargetManagerDownloadResp {
private String fileName;
private String file;
}

View File

@ -2,6 +2,7 @@ package cd.casic.ci.process.process.service.target;
import cd.casic.ci.process.dto.req.target.*;
import cd.casic.ci.process.dto.resp.target.TargetManagerDownloadResp;
import cd.casic.ci.process.dto.resp.target.TargetManagerResp;
import cd.casic.ci.process.process.dataObject.base.BaseIdReq;
import cd.casic.ci.process.process.dataObject.target.TargetManager;
@ -42,5 +43,5 @@ public interface TargetManagerService extends IService<TargetManager> {
void fileDownload(@Valid BaseIdReq req, HttpServletResponse response);
void zipDownload(@Valid BaseIdReq req, HttpServletResponse response);
TargetManagerDownloadResp zipDownload(@Valid BaseIdReq req, HttpServletResponse response);
}

View File

@ -2,6 +2,7 @@ package cd.casic.ci.process.process.service.target.impl;
import cd.casic.ci.process.dto.req.target.*;
import cd.casic.ci.process.dto.resp.target.TargetManagerDownloadResp;
import cd.casic.ci.process.dto.resp.target.TargetManagerResp;
import cd.casic.ci.process.dto.resp.target.TargetVersionResp;
import cd.casic.ci.process.process.converter.TargetConverter;
@ -24,9 +25,13 @@ import cd.casic.framework.tenant.core.service.AdminUserServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -37,10 +42,9 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author HopeLi
@ -296,7 +300,7 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
}
@Override
public void zipDownload(BaseIdReq req, HttpServletResponse response){
public TargetManagerDownloadResp zipDownload(BaseIdReq req, HttpServletResponse response){
if (ObjectUtils.isEmpty(req.getId())){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标管理id不能为空");
}
@ -304,18 +308,47 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
if (ObjectUtils.isEmpty(targetManager)){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标不存在");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (ZipOutputStream zipOutputStream = new ZipOutputStream(bos)){
JSch jsch = new JSch();
Session session = jsch.getSession(fileUploadProperties.getUsername(), fileUploadProperties.getRemoteHost(), fileUploadProperties.getRemotePort());
session.setPassword(fileUploadProperties.getPassword());
//根据targetId查询对应的全部版本信息获取版本保存地址
QueryWrapper<TargetVersion> wrapper = new QueryWrapper<>();
wrapper.eq("target_id",req.getId());
List<TargetVersion> targetVersionList = targetVersionDao.selectList(wrapper);
// 跳过 host key 检查
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
if (ObjectUtils.isEmpty(targetVersionList)){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标版本不存在");
session.connect();
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
//根据targetId查询对应的全部版本信息获取版本保存地址
QueryWrapper<TargetVersion> wrapper = new QueryWrapper<>();
wrapper.eq("target_id",req.getId());
List<TargetVersion> targetVersionList = targetVersionDao.selectList(wrapper);
if (ObjectUtils.isEmpty(targetVersionList)){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标版本不存在");
}
for (TargetVersion targetVersion : targetVersionList) {
InputStream inputStream = sftp.get(targetVersion.getFilePath());
String fileName = targetVersion.getFileName();
ZipEntry zipEntry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(zipEntry);
IOUtils.copy(inputStream,zipOutputStream);
zipOutputStream.closeEntry();
inputStream.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
List<String> filePathList = targetVersionList.stream().map(TargetVersion::getFilePath).toList();
sftpFileService.downloadFilesAsZip(filePathList,targetManager.getTargetName(),response);
byte[] byteArray = bos.toByteArray();
String file = Base64.getEncoder().encodeToString(byteArray);
TargetManagerDownloadResp targetManagerDownloadResp = new TargetManagerDownloadResp();
targetManagerDownloadResp.setFile(file);
targetManagerDownloadResp.setFileName(targetManager.getTargetName());
return targetManagerDownloadResp;
}