0724 ljc 修改文件服务器接口逻辑,新增目标管理下的文件下载功能接口
This commit is contained in:
parent
e36d596bc7
commit
36d342cb4a
@ -1,15 +1,12 @@
|
||||
package cd.casic.ci.api;
|
||||
|
||||
import cd.casic.ci.process.process.service.sftpFile.SftpFileService;
|
||||
import cd.casic.ci.process.properties.TargetFileUploadProperties;
|
||||
import cd.casic.framework.commons.pojo.CommonResult;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -26,8 +23,6 @@ public class SftpFileController {
|
||||
@Resource
|
||||
private SftpFileService sftpFileService;
|
||||
|
||||
@Resource
|
||||
private TargetFileUploadProperties fileUploadProperties;
|
||||
|
||||
@PostMapping("/upload")
|
||||
public CommonResult<String> uploadFile(
|
||||
@ -35,14 +30,9 @@ public class SftpFileController {
|
||||
@RequestParam String remoteFileName,
|
||||
@RequestParam MultipartFile file) {
|
||||
|
||||
String localFilePath = saveTempFile(file);
|
||||
|
||||
String uploadFilePath = sftpFileService.uploadFile(
|
||||
fileUploadProperties.getRemoteHost(),
|
||||
fileUploadProperties.getRemotePort(),
|
||||
fileUploadProperties.getUsername(),
|
||||
fileUploadProperties.getPassword(),
|
||||
fileUploadProperties.getSshKeyPath(),
|
||||
localFilePath, remoteDir, remoteFileName);
|
||||
file, remoteDir, remoteFileName);
|
||||
|
||||
return CommonResult.success(uploadFilePath);
|
||||
}
|
||||
@ -53,11 +43,6 @@ public class SftpFileController {
|
||||
HttpServletResponse response) {
|
||||
|
||||
sftpFileService.downloadFile(
|
||||
fileUploadProperties.getRemoteHost(),
|
||||
fileUploadProperties.getRemotePort(),
|
||||
fileUploadProperties.getUsername(),
|
||||
fileUploadProperties.getPassword(),
|
||||
fileUploadProperties.getSshKeyPath(),
|
||||
remoteFilePath, response);
|
||||
}
|
||||
|
||||
@ -68,21 +53,8 @@ public class SftpFileController {
|
||||
HttpServletResponse response) {
|
||||
|
||||
sftpFileService.downloadFilesAsZip(
|
||||
fileUploadProperties.getRemoteHost(),
|
||||
fileUploadProperties.getRemotePort(),
|
||||
fileUploadProperties.getUsername(),
|
||||
fileUploadProperties.getPassword(),
|
||||
fileUploadProperties.getSshKeyPath(),
|
||||
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,7 +20,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -134,9 +133,13 @@ public class TargetController {
|
||||
|
||||
|
||||
@PostMapping(path="/fileDownload")
|
||||
public void fileDownload(@RequestBody @Valid TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException {
|
||||
public void fileDownload(@RequestBody @Valid BaseIdReq req, HttpServletResponse response){
|
||||
targetManagerService.fileDownload(req,response);
|
||||
}
|
||||
|
||||
@PostMapping(path="/zipDownload")
|
||||
public void zipDownload(@RequestBody @Valid BaseIdReq req, HttpServletResponse response){
|
||||
targetManagerService.zipDownload(req,response);
|
||||
}
|
||||
|
||||
@PostMapping(path = "/uploadFolder")
|
||||
|
@ -1,31 +0,0 @@
|
||||
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,10 +156,6 @@ public class SftpClientUtils implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public InputStream downloadFileToStream(String remoteFilePath) throws SftpException, SftpUploadUtil.SftpUploadException {
|
||||
|
@ -2,6 +2,7 @@ package cd.casic.ci.process.process.service.sftpFile;
|
||||
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -14,9 +15,9 @@ import java.util.List;
|
||||
*/
|
||||
public interface SftpFileService{
|
||||
|
||||
String uploadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String localFilePath, String remoteDir, String remoteFileName);
|
||||
String uploadFile(MultipartFile file, String remoteDir, String remoteFileName);
|
||||
|
||||
void downloadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String remoteFilePath, HttpServletResponse response);
|
||||
void downloadFile(String remoteFilePath, HttpServletResponse response);
|
||||
|
||||
void downloadFilesAsZip(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, List<String> remoteFilePaths, String zipFileName, HttpServletResponse response);
|
||||
void downloadFilesAsZip(List<String> remoteFilePaths, String zipFileName, HttpServletResponse response);
|
||||
}
|
||||
|
@ -3,11 +3,15 @@ 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.SftpFileService;
|
||||
import cd.casic.ci.process.properties.TargetFileUploadProperties;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
@ -25,10 +29,18 @@ import java.util.zip.ZipOutputStream;
|
||||
@Slf4j
|
||||
public class SftpFileServiceImpl implements SftpFileService {
|
||||
|
||||
@Resource
|
||||
private TargetFileUploadProperties fileUploadProperties;
|
||||
|
||||
|
||||
@Override
|
||||
public String uploadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String localFilePath, String remoteDir, String remoteFileName) {
|
||||
try (SftpClientUtils client = new SftpClientUtils(remoteHost, remotePort, username, password, sshKeyPath)) {
|
||||
public String uploadFile(MultipartFile file, String remoteDir, String remoteFileName) {
|
||||
String localFilePath = saveTempFile(file);
|
||||
try (SftpClientUtils client = new SftpClientUtils(fileUploadProperties.getRemoteHost(),
|
||||
fileUploadProperties.getRemotePort(),
|
||||
fileUploadProperties.getUsername(),
|
||||
fileUploadProperties.getPassword(),
|
||||
fileUploadProperties.getSshKeyPath())) {
|
||||
return client.uploadFile(localFilePath, remoteDir, remoteFileName);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
|
||||
@ -36,8 +48,12 @@ public class SftpFileServiceImpl implements SftpFileService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String remoteFilePath, HttpServletResponse response) {
|
||||
try (SftpClientUtils client = new SftpClientUtils(remoteHost, remotePort, username, password, sshKeyPath)) {
|
||||
public void downloadFile(String remoteFilePath, HttpServletResponse response) {
|
||||
try (SftpClientUtils client = new SftpClientUtils(fileUploadProperties.getRemoteHost(),
|
||||
fileUploadProperties.getRemotePort(),
|
||||
fileUploadProperties.getUsername(),
|
||||
fileUploadProperties.getPassword(),
|
||||
fileUploadProperties.getSshKeyPath())) {
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + new File(remoteFilePath).getName() + "\"");
|
||||
|
||||
@ -50,8 +66,12 @@ public class SftpFileServiceImpl implements SftpFileService {
|
||||
}
|
||||
|
||||
@Override
|
||||
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(remoteHost, remotePort, username, password, sshKeyPath)) {
|
||||
public void downloadFilesAsZip(List<String> remoteFilePaths, String zipFileName, HttpServletResponse response) {
|
||||
try (SftpClientUtils client = new SftpClientUtils(fileUploadProperties.getRemoteHost(),
|
||||
fileUploadProperties.getRemotePort(),
|
||||
fileUploadProperties.getUsername(),
|
||||
fileUploadProperties.getPassword(),
|
||||
fileUploadProperties.getSshKeyPath())) {
|
||||
response.setContentType("application/zip");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"");
|
||||
|
||||
@ -73,4 +93,16 @@ public class SftpFileServiceImpl implements SftpFileService {
|
||||
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,7 +12,6 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -41,5 +40,7 @@ public interface TargetManagerService extends IService<TargetManager> {
|
||||
|
||||
void updateVersion(@Valid TargetVersionUpdateReq req);
|
||||
|
||||
void fileDownload(@Valid TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException;
|
||||
void fileDownload(@Valid BaseIdReq req, HttpServletResponse response);
|
||||
|
||||
void zipDownload(@Valid BaseIdReq req, HttpServletResponse response);
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ 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.TargetVersion;
|
||||
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.properties.TargetFileUploadProperties;
|
||||
import cd.casic.ci.process.util.SftpUploadUtil;
|
||||
@ -66,6 +67,9 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
|
||||
@Resource
|
||||
private TargetFileUploadProperties fileUploadProperties;
|
||||
|
||||
@Resource
|
||||
private SftpFileServiceImpl sftpFileService;
|
||||
|
||||
|
||||
@Resource
|
||||
private PipelineService pipelineService;
|
||||
@ -278,16 +282,40 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fileDownload(TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException {
|
||||
SftpUploadUtil.downloadFileSftp(
|
||||
req.getRemoteHost(),
|
||||
req.getRemotePort(),
|
||||
req.getUsername(),
|
||||
req.getPassword(),
|
||||
req.getSshKeyPath(),
|
||||
req.getRemoteFilePath(),
|
||||
response
|
||||
);
|
||||
public void fileDownload(BaseIdReq req, HttpServletResponse response){
|
||||
if (ObjectUtils.isEmpty(req.getId())){
|
||||
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标id不能为空");
|
||||
}
|
||||
//根据versionId查询对应的版本信息,获取版本保存地址
|
||||
TargetVersion targetVersion = targetVersionDao.selectById(req.getId());
|
||||
|
||||
if (ObjectUtils.isEmpty(targetVersion) || ObjectUtils.isEmpty(targetVersion.getFilePath())){
|
||||
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标版本或路径不存在");
|
||||
}
|
||||
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