Compare commits
No commits in common. "771be286e6e91a044a16b048f5763be79e265208" and "b4a4e0fe58cd5c8c4a502197e33a6355608636f0" have entirely different histories.
771be286e6
...
b4a4e0fe58
@ -1,12 +1,15 @@
|
|||||||
package cd.casic.ci.api;
|
package cd.casic.ci.api;
|
||||||
|
|
||||||
import cd.casic.ci.process.process.service.sftpFile.SftpFileService;
|
import cd.casic.ci.process.process.service.sftpFile.SftpFileService;
|
||||||
|
import cd.casic.ci.process.properties.TargetFileUploadProperties;
|
||||||
import cd.casic.framework.commons.pojo.CommonResult;
|
import cd.casic.framework.commons.pojo.CommonResult;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,6 +26,8 @@ public class SftpFileController {
|
|||||||
@Resource
|
@Resource
|
||||||
private SftpFileService sftpFileService;
|
private SftpFileService sftpFileService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private TargetFileUploadProperties fileUploadProperties;
|
||||||
|
|
||||||
@PostMapping("/upload")
|
@PostMapping("/upload")
|
||||||
public CommonResult<String> uploadFile(
|
public CommonResult<String> uploadFile(
|
||||||
@ -30,9 +35,14 @@ public class SftpFileController {
|
|||||||
@RequestParam String remoteFileName,
|
@RequestParam String remoteFileName,
|
||||||
@RequestParam MultipartFile file) {
|
@RequestParam MultipartFile file) {
|
||||||
|
|
||||||
|
String localFilePath = saveTempFile(file);
|
||||||
String uploadFilePath = sftpFileService.uploadFile(
|
String uploadFilePath = sftpFileService.uploadFile(
|
||||||
file, remoteDir, remoteFileName);
|
fileUploadProperties.getRemoteHost(),
|
||||||
|
fileUploadProperties.getRemotePort(),
|
||||||
|
fileUploadProperties.getUsername(),
|
||||||
|
fileUploadProperties.getPassword(),
|
||||||
|
fileUploadProperties.getSshKeyPath(),
|
||||||
|
localFilePath, remoteDir, remoteFileName);
|
||||||
|
|
||||||
return CommonResult.success(uploadFilePath);
|
return CommonResult.success(uploadFilePath);
|
||||||
}
|
}
|
||||||
@ -43,6 +53,11 @@ public class SftpFileController {
|
|||||||
HttpServletResponse response) {
|
HttpServletResponse response) {
|
||||||
|
|
||||||
sftpFileService.downloadFile(
|
sftpFileService.downloadFile(
|
||||||
|
fileUploadProperties.getRemoteHost(),
|
||||||
|
fileUploadProperties.getRemotePort(),
|
||||||
|
fileUploadProperties.getUsername(),
|
||||||
|
fileUploadProperties.getPassword(),
|
||||||
|
fileUploadProperties.getSshKeyPath(),
|
||||||
remoteFilePath, response);
|
remoteFilePath, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,8 +68,21 @@ public class SftpFileController {
|
|||||||
HttpServletResponse response) {
|
HttpServletResponse response) {
|
||||||
|
|
||||||
sftpFileService.downloadFilesAsZip(
|
sftpFileService.downloadFilesAsZip(
|
||||||
|
fileUploadProperties.getRemoteHost(),
|
||||||
|
fileUploadProperties.getRemotePort(),
|
||||||
|
fileUploadProperties.getUsername(),
|
||||||
|
fileUploadProperties.getPassword(),
|
||||||
|
fileUploadProperties.getSshKeyPath(),
|
||||||
remoteFilePaths, zipFileName, response);
|
remoteFilePaths, zipFileName, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String saveTempFile(MultipartFile file) {
|
||||||
|
try {
|
||||||
|
File tempFile = File.createTempFile("upload-", ".tmp");
|
||||||
|
file.transferTo(tempFile);
|
||||||
|
return tempFile.getAbsolutePath();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("保存临时文件失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -133,13 +134,9 @@ public class TargetController {
|
|||||||
|
|
||||||
|
|
||||||
@PostMapping(path="/fileDownload")
|
@PostMapping(path="/fileDownload")
|
||||||
public void fileDownload(@RequestBody @Valid BaseIdReq req, HttpServletResponse response){
|
public void fileDownload(@RequestBody @Valid TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException {
|
||||||
targetManagerService.fileDownload(req,response);
|
targetManagerService.fileDownload(req,response);
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(path="/zipDownload")
|
|
||||||
public void zipDownload(@RequestBody @Valid BaseIdReq req, HttpServletResponse response){
|
|
||||||
targetManagerService.zipDownload(req,response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(path = "/uploadFolder")
|
@PostMapping(path = "/uploadFolder")
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package cd.casic.ci.process.dto.req.target;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName PipelineQueryReq
|
||||||
|
* @Author hopeli
|
||||||
|
* @Date 2025/5/10 9:54
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class TargetDownloadReq {
|
||||||
|
|
||||||
|
//远程服务器IP或主机名
|
||||||
|
private String remoteHost = "175.6.27.252";
|
||||||
|
|
||||||
|
//远程服务器端口 (通常是 22),为 null 或 <= 0 时使用默认端口 22
|
||||||
|
private Integer remotePort = 22;
|
||||||
|
|
||||||
|
//远程服务器用户名
|
||||||
|
private String username = "roots";
|
||||||
|
|
||||||
|
//远程服务器密码 (如果使用密码认证)
|
||||||
|
private String password = "hnidc0327cn!@#xhh";
|
||||||
|
|
||||||
|
//SSH Key 文件路径 (如果使用密钥认证,password 参数可以为 null)
|
||||||
|
private String sshKeyPath;
|
||||||
|
|
||||||
|
//文件地址(包含文件名)
|
||||||
|
private String remoteFilePath;
|
||||||
|
}
|
@ -156,6 +156,10 @@ public class SftpClientUtils implements AutoCloseable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream downloadFileToStream(String remoteFilePath) throws SftpException, SftpUploadUtil.SftpUploadException {
|
public InputStream downloadFileToStream(String remoteFilePath) throws SftpException, SftpUploadUtil.SftpUploadException {
|
||||||
|
@ -2,7 +2,6 @@ package cd.casic.ci.process.process.service.sftpFile;
|
|||||||
|
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -15,9 +14,9 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface SftpFileService{
|
public interface SftpFileService{
|
||||||
|
|
||||||
String uploadFile(MultipartFile file, String remoteDir, String remoteFileName);
|
String uploadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String localFilePath, String remoteDir, String remoteFileName);
|
||||||
|
|
||||||
void downloadFile(String remoteFilePath, HttpServletResponse response);
|
void downloadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String remoteFilePath, HttpServletResponse response);
|
||||||
|
|
||||||
void downloadFilesAsZip(List<String> remoteFilePaths, String zipFileName, HttpServletResponse response);
|
void downloadFilesAsZip(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, List<String> remoteFilePaths, String zipFileName, HttpServletResponse response);
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,11 @@ package cd.casic.ci.process.process.service.sftpFile.impl;
|
|||||||
|
|
||||||
import cd.casic.ci.process.process.service.sftpFile.SftpClientUtils;
|
import cd.casic.ci.process.process.service.sftpFile.SftpClientUtils;
|
||||||
import cd.casic.ci.process.process.service.sftpFile.SftpFileService;
|
import cd.casic.ci.process.process.service.sftpFile.SftpFileService;
|
||||||
import cd.casic.ci.process.properties.TargetFileUploadProperties;
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -29,18 +25,10 @@ import java.util.zip.ZipOutputStream;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class SftpFileServiceImpl implements SftpFileService {
|
public class SftpFileServiceImpl implements SftpFileService {
|
||||||
|
|
||||||
@Resource
|
|
||||||
private TargetFileUploadProperties fileUploadProperties;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String uploadFile(MultipartFile file, String remoteDir, String remoteFileName) {
|
public String uploadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String localFilePath, String remoteDir, String remoteFileName) {
|
||||||
String localFilePath = saveTempFile(file);
|
try (SftpClientUtils client = new SftpClientUtils(remoteHost, remotePort, username, password, sshKeyPath)) {
|
||||||
try (SftpClientUtils client = new SftpClientUtils(fileUploadProperties.getRemoteHost(),
|
|
||||||
fileUploadProperties.getRemotePort(),
|
|
||||||
fileUploadProperties.getUsername(),
|
|
||||||
fileUploadProperties.getPassword(),
|
|
||||||
fileUploadProperties.getSshKeyPath())) {
|
|
||||||
return client.uploadFile(localFilePath, remoteDir, remoteFileName);
|
return client.uploadFile(localFilePath, remoteDir, remoteFileName);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
|
throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
|
||||||
@ -48,12 +36,8 @@ public class SftpFileServiceImpl implements SftpFileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void downloadFile(String remoteFilePath, HttpServletResponse response) {
|
public void downloadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String remoteFilePath, HttpServletResponse response) {
|
||||||
try (SftpClientUtils client = new SftpClientUtils(fileUploadProperties.getRemoteHost(),
|
try (SftpClientUtils client = new SftpClientUtils(remoteHost, remotePort, username, password, sshKeyPath)) {
|
||||||
fileUploadProperties.getRemotePort(),
|
|
||||||
fileUploadProperties.getUsername(),
|
|
||||||
fileUploadProperties.getPassword(),
|
|
||||||
fileUploadProperties.getSshKeyPath())) {
|
|
||||||
response.setContentType("application/octet-stream");
|
response.setContentType("application/octet-stream");
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + new File(remoteFilePath).getName() + "\"");
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + new File(remoteFilePath).getName() + "\"");
|
||||||
|
|
||||||
@ -66,12 +50,8 @@ public class SftpFileServiceImpl implements SftpFileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void downloadFilesAsZip(List<String> remoteFilePaths, String zipFileName, HttpServletResponse response) {
|
public void downloadFilesAsZip(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, List<String> remoteFilePaths, String zipFileName, HttpServletResponse response) {
|
||||||
try (SftpClientUtils client = new SftpClientUtils(fileUploadProperties.getRemoteHost(),
|
try (SftpClientUtils client = new SftpClientUtils(remoteHost, remotePort, username, password, sshKeyPath)) {
|
||||||
fileUploadProperties.getRemotePort(),
|
|
||||||
fileUploadProperties.getUsername(),
|
|
||||||
fileUploadProperties.getPassword(),
|
|
||||||
fileUploadProperties.getSshKeyPath())) {
|
|
||||||
response.setContentType("application/zip");
|
response.setContentType("application/zip");
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"");
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"");
|
||||||
|
|
||||||
@ -93,16 +73,4 @@ public class SftpFileServiceImpl implements SftpFileService {
|
|||||||
throw new RuntimeException("批量下载并打包 ZIP 失败: " + e.getMessage(), e);
|
throw new RuntimeException("批量下载并打包 ZIP 失败: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private String saveTempFile(MultipartFile file) {
|
|
||||||
try {
|
|
||||||
File tempFile = File.createTempFile("upload-", ".tmp");
|
|
||||||
file.transferTo(tempFile);
|
|
||||||
return tempFile.getAbsolutePath();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("保存临时文件失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
|||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,7 +41,5 @@ public interface TargetManagerService extends IService<TargetManager> {
|
|||||||
|
|
||||||
void updateVersion(@Valid TargetVersionUpdateReq req);
|
void updateVersion(@Valid TargetVersionUpdateReq req);
|
||||||
|
|
||||||
void fileDownload(@Valid BaseIdReq req, HttpServletResponse response);
|
void fileDownload(@Valid TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException;
|
||||||
|
|
||||||
void zipDownload(@Valid BaseIdReq req, HttpServletResponse response);
|
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,6 @@ import cd.casic.ci.process.process.dataObject.base.BaseIdReq;
|
|||||||
import cd.casic.ci.process.process.dataObject.target.TargetManager;
|
import cd.casic.ci.process.process.dataObject.target.TargetManager;
|
||||||
import cd.casic.ci.process.process.dataObject.target.TargetVersion;
|
import cd.casic.ci.process.process.dataObject.target.TargetVersion;
|
||||||
import cd.casic.ci.process.process.service.pipeline.PipelineService;
|
import cd.casic.ci.process.process.service.pipeline.PipelineService;
|
||||||
import cd.casic.ci.process.process.service.sftpFile.impl.SftpFileServiceImpl;
|
|
||||||
import cd.casic.ci.process.process.service.target.TargetManagerService;
|
import cd.casic.ci.process.process.service.target.TargetManagerService;
|
||||||
import cd.casic.ci.process.properties.TargetFileUploadProperties;
|
import cd.casic.ci.process.properties.TargetFileUploadProperties;
|
||||||
import cd.casic.ci.process.util.SftpUploadUtil;
|
import cd.casic.ci.process.util.SftpUploadUtil;
|
||||||
@ -67,9 +66,6 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
|
|||||||
@Resource
|
@Resource
|
||||||
private TargetFileUploadProperties fileUploadProperties;
|
private TargetFileUploadProperties fileUploadProperties;
|
||||||
|
|
||||||
@Resource
|
|
||||||
private SftpFileServiceImpl sftpFileService;
|
|
||||||
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private PipelineService pipelineService;
|
private PipelineService pipelineService;
|
||||||
@ -282,40 +278,16 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fileDownload(BaseIdReq req, HttpServletResponse response){
|
public void fileDownload(TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException {
|
||||||
if (ObjectUtils.isEmpty(req.getId())){
|
SftpUploadUtil.downloadFileSftp(
|
||||||
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标id不能为空");
|
req.getRemoteHost(),
|
||||||
}
|
req.getRemotePort(),
|
||||||
//根据versionId查询对应的版本信息,获取版本保存地址
|
req.getUsername(),
|
||||||
TargetVersion targetVersion = targetVersionDao.selectById(req.getId());
|
req.getPassword(),
|
||||||
|
req.getSshKeyPath(),
|
||||||
if (ObjectUtils.isEmpty(targetVersion) || ObjectUtils.isEmpty(targetVersion.getFilePath())){
|
req.getRemoteFilePath(),
|
||||||
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标版本或路径不存在");
|
response
|
||||||
}
|
);
|
||||||
sftpFileService.downloadFile(targetVersion.getFilePath(),response);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void zipDownload(BaseIdReq req, HttpServletResponse response){
|
|
||||||
if (ObjectUtils.isEmpty(req.getId())){
|
|
||||||
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标管理id不能为空");
|
|
||||||
}
|
|
||||||
TargetManager targetManager = targetManagerDao.selectById(req.getId());
|
|
||||||
if (ObjectUtils.isEmpty(targetManager)){
|
|
||||||
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
//根据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(),"目标版本不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> filePathList = targetVersionList.stream().map(TargetVersion::getFilePath).toList();
|
|
||||||
sftpFileService.downloadFilesAsZip(filePathList,targetManager.getTargetName(),response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user