Merge branch 'master' of http://1.14.125.6:3000/mianbin/ops-pro
# Conflicts: # modules/module-ci-process-api/src/main/java/cd/casic/ci/api/SftpFileController.java
This commit is contained in:
commit
f983c3d2c8
@ -1,9 +1,20 @@
|
|||||||
package cd.casic.module.execute.docker.api;
|
package cd.casic.module.execute.docker.api;
|
||||||
|
|
||||||
|
import cd.casic.framework.commons.pojo.CommonResult;
|
||||||
|
import cd.casic.framework.commons.pojo.PageResult;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.dto.DockerImageDo;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.model.DockerImage;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.vo.DockerImagePageReqVO;
|
||||||
|
import cd.casic.module.execute.docker.service.IImageService;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import static cd.casic.framework.commons.pojo.CommonResult.success;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: TODO
|
* @description: TODO
|
||||||
@ -14,7 +25,26 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@Tag(name = "docker管理 - 镜像管理")
|
@Tag(name = "docker管理 - 镜像管理")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@RestController
|
@RestController
|
||||||
|
@Validated
|
||||||
@RequestMapping("/api/images")
|
@RequestMapping("/api/images")
|
||||||
public class DockerImageController {
|
public class DockerImageController {
|
||||||
|
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IImageService imageService;
|
||||||
|
|
||||||
|
@PostMapping("/localUpload")
|
||||||
|
@Operation(summary = "镜像上传")
|
||||||
|
@PreAuthorize("@ss.hasPermission('docker:images:localUpload')")
|
||||||
|
public CommonResult localImageUpload(@RequestBody DockerImage dockerImage) {
|
||||||
|
return success(imageService.localImageUpload(dockerImage));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/page")
|
||||||
|
@Operation(summary = "本地镜像分页")
|
||||||
|
@PreAuthorize("@ss.hasPermission('docker:images:query')")
|
||||||
|
public CommonResult<PageResult<DockerImageDo>> getLocalImagePage(@Valid DockerImagePageReqVO pageVO) {
|
||||||
|
return success(imageService.getLocalImagePage(pageVO));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package cd.casic.module.execute.docker.dao;
|
||||||
|
|
||||||
|
import cd.casic.framework.mybatis.core.mapper.BaseMapperX;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.dto.DockerImageDo;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface DockerImageDao extends BaseMapperX<DockerImageDo> {
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package cd.casic.module.execute.docker.dataobject.convert;
|
||||||
|
|
||||||
|
import cd.casic.module.execute.docker.dataobject.dto.DockerImageDo;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.model.DockerImage;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Yuru Pu
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2025/7/23 17:16
|
||||||
|
*/
|
||||||
|
@Mapper(componentModel = "spring")
|
||||||
|
public interface DockerImageConvert {
|
||||||
|
DockerImageConvert INSTANCE = Mappers.getMapper(DockerImageConvert.class);
|
||||||
|
|
||||||
|
DockerImageDo convert(DockerImage dockerImage);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
package cd.casic.module.execute.docker.dataobject.dto;
|
||||||
|
|
||||||
|
import cd.casic.framework.commons.dataobject.BaseDO;
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本地镜像
|
||||||
|
*
|
||||||
|
* @author Yuru Pu
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2025/7/23 16:22
|
||||||
|
*/
|
||||||
|
@TableName("pipeline_image")
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class DockerImageDo extends BaseDO {
|
||||||
|
|
||||||
|
@TableId(type = IdType.ASSIGN_ID)
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件大小
|
||||||
|
*/
|
||||||
|
private Integer size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件版本
|
||||||
|
*/
|
||||||
|
private String version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用途
|
||||||
|
*/
|
||||||
|
private String purpose;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 地址路径
|
||||||
|
*/
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 描述
|
||||||
|
*/
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 状态 0 可用
|
||||||
|
*/
|
||||||
|
private Boolean state;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
package cd.casic.module.execute.docker.dataobject.model;
|
||||||
|
|
||||||
|
import cd.casic.framework.commons.dataobject.BaseDO;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本地镜像
|
||||||
|
*
|
||||||
|
* @author Yuru Pu
|
||||||
|
* @version 1.0
|
||||||
|
* @since 2025/7/23 16:22
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class DockerImage extends BaseDO {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件大小
|
||||||
|
*/
|
||||||
|
private Integer size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件版本
|
||||||
|
*/
|
||||||
|
private String version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用途
|
||||||
|
*/
|
||||||
|
private String purpose;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 地址路径
|
||||||
|
*/
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 描述
|
||||||
|
*/
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 状态 1 可用
|
||||||
|
*/
|
||||||
|
private Boolean state;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package cd.casic.module.execute.docker.dataobject.vo;
|
||||||
|
|
||||||
|
import cd.casic.framework.commons.pojo.PageParam;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: Paul
|
||||||
|
* @create: 2025-07-24 13:49
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DockerImagePageReqVO extends PageParam {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
}
|
@ -1,5 +1,9 @@
|
|||||||
package cd.casic.module.execute.docker.service;
|
package cd.casic.module.execute.docker.service;
|
||||||
|
|
||||||
|
import cd.casic.framework.commons.pojo.PageResult;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.dto.DockerImageDo;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.model.DockerImage;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.vo.DockerImagePageReqVO;
|
||||||
import com.github.dockerjava.api.command.InspectImageResponse;
|
import com.github.dockerjava.api.command.InspectImageResponse;
|
||||||
import com.github.dockerjava.api.model.Image;
|
import com.github.dockerjava.api.model.Image;
|
||||||
import jakarta.annotation.Nonnull;
|
import jakarta.annotation.Nonnull;
|
||||||
@ -102,4 +106,7 @@ public interface IImageService {
|
|||||||
*/
|
*/
|
||||||
Boolean pushImage(@Nonnull String clientId, String imageId);
|
Boolean pushImage(@Nonnull String clientId, String imageId);
|
||||||
|
|
||||||
|
int localImageUpload(DockerImage dockerImage);
|
||||||
|
|
||||||
|
PageResult<DockerImageDo> getLocalImagePage(DockerImagePageReqVO pageVO);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,13 @@
|
|||||||
package cd.casic.module.execute.docker.service.impl;
|
package cd.casic.module.execute.docker.service.impl;
|
||||||
|
|
||||||
|
import cd.casic.framework.commons.pojo.PageResult;
|
||||||
|
import cd.casic.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||||
import cd.casic.module.execute.docker.DockerClientFactory;
|
import cd.casic.module.execute.docker.DockerClientFactory;
|
||||||
|
import cd.casic.module.execute.docker.dao.DockerImageDao;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.convert.DockerImageConvert;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.dto.DockerImageDo;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.model.DockerImage;
|
||||||
|
import cd.casic.module.execute.docker.dataobject.vo.DockerImagePageReqVO;
|
||||||
import cd.casic.module.execute.docker.service.IImageService;
|
import cd.casic.module.execute.docker.service.IImageService;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
@ -11,11 +18,12 @@ import com.github.dockerjava.api.exception.NotFoundException;
|
|||||||
import com.github.dockerjava.api.model.Image;
|
import com.github.dockerjava.api.model.Image;
|
||||||
import com.github.dockerjava.api.model.PruneType;
|
import com.github.dockerjava.api.model.PruneType;
|
||||||
import jakarta.annotation.Nonnull;
|
import jakarta.annotation.Nonnull;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -35,6 +43,9 @@ public class ImageService implements IImageService {
|
|||||||
|
|
||||||
private final DockerClientFactory dockerClientFactory;
|
private final DockerClientFactory dockerClientFactory;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DockerImageDao dockerImageDao;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Image> list(@Nonnull String clientId) {
|
public List<Image> list(@Nonnull String clientId) {
|
||||||
DockerClient dockerClient = dockerClientFactory.getdockerClient(clientId);
|
DockerClient dockerClient = dockerClientFactory.getdockerClient(clientId);
|
||||||
@ -130,4 +141,16 @@ public class ImageService implements IImageService {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
@Override
|
||||||
|
public int localImageUpload(DockerImage dockerImage) {
|
||||||
|
return dockerImageDao.insert(DockerImageConvert.INSTANCE.convert(dockerImage));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PageResult<DockerImageDo> getLocalImagePage(DockerImagePageReqVO pageVO) {
|
||||||
|
LambdaQueryWrapperX<DockerImageDo> queryWrapperX = new LambdaQueryWrapperX<>();
|
||||||
|
PageResult<DockerImageDo> page = dockerImageDao.selectPage(pageVO, queryWrapperX.likeIfPresent(DockerImageDo::getName, pageVO.getName()).orderByDesc(DockerImageDo::getCreateTime));
|
||||||
|
return page;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
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;
|
||||||
@ -28,8 +27,6 @@ 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(
|
||||||
@ -37,14 +34,9 @@ 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(
|
||||||
fileUploadProperties.getRemoteHost(),
|
file, remoteDir, remoteFileName);
|
||||||
fileUploadProperties.getRemotePort(),
|
|
||||||
fileUploadProperties.getUsername(),
|
|
||||||
fileUploadProperties.getPassword(),
|
|
||||||
fileUploadProperties.getSshKeyPath(),
|
|
||||||
localFilePath, remoteDir, remoteFileName);
|
|
||||||
|
|
||||||
return CommonResult.success(uploadFilePath);
|
return CommonResult.success(uploadFilePath);
|
||||||
}
|
}
|
||||||
@ -53,13 +45,8 @@ public class SftpFileController {
|
|||||||
public void downloadFile(
|
public void downloadFile(
|
||||||
@RequestParam String remoteFilePath,
|
@RequestParam String remoteFilePath,
|
||||||
HttpServletResponse response) {
|
HttpServletResponse response) {
|
||||||
remoteFilePath = URLDecoder.decode(remoteFilePath, StandardCharsets.UTF_8);
|
|
||||||
sftpFileService.downloadFile(
|
sftpFileService.downloadFile(
|
||||||
fileUploadProperties.getRemoteHost(),
|
|
||||||
fileUploadProperties.getRemotePort(),
|
|
||||||
fileUploadProperties.getUsername(),
|
|
||||||
fileUploadProperties.getPassword(),
|
|
||||||
fileUploadProperties.getSshKeyPath(),
|
|
||||||
remoteFilePath, response);
|
remoteFilePath, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,21 +57,8 @@ 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,7 +20,6 @@ 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;
|
||||||
|
|
||||||
@ -134,9 +133,13 @@ public class TargetController {
|
|||||||
|
|
||||||
|
|
||||||
@PostMapping(path="/fileDownload")
|
@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);
|
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")
|
||||||
|
@ -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 {
|
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 jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -14,9 +15,9 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface SftpFileService{
|
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.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;
|
||||||
@ -25,10 +29,18 @@ 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(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String localFilePath, String remoteDir, String remoteFileName) {
|
public String uploadFile(MultipartFile file, String remoteDir, String remoteFileName) {
|
||||||
try (SftpClientUtils client = new SftpClientUtils(remoteHost, remotePort, username, password, sshKeyPath)) {
|
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);
|
return client.uploadFile(localFilePath, remoteDir, remoteFileName);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
|
throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
|
||||||
@ -36,8 +48,12 @@ public class SftpFileServiceImpl implements SftpFileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void downloadFile(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, String remoteFilePath, HttpServletResponse response) {
|
public void downloadFile(String remoteFilePath, HttpServletResponse response) {
|
||||||
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())) {
|
||||||
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() + "\"");
|
||||||
|
|
||||||
@ -50,8 +66,12 @@ public class SftpFileServiceImpl implements SftpFileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void downloadFilesAsZip(String remoteHost, Integer remotePort, String username, String password, String sshKeyPath, List<String> remoteFilePaths, String zipFileName, HttpServletResponse response) {
|
public void downloadFilesAsZip(List<String> remoteFilePaths, String zipFileName, HttpServletResponse response) {
|
||||||
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())) {
|
||||||
response.setContentType("application/zip");
|
response.setContentType("application/zip");
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"");
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + zipFileName + "\"");
|
||||||
|
|
||||||
@ -73,4 +93,16 @@ 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,7 +12,6 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,5 +40,7 @@ public interface TargetManagerService extends IService<TargetManager> {
|
|||||||
|
|
||||||
void updateVersion(@Valid TargetVersionUpdateReq req);
|
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.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;
|
||||||
@ -66,6 +67,9 @@ 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;
|
||||||
@ -278,16 +282,40 @@ public class TargetManagerServiceImpl extends ServiceImpl<TargetManagerDao, Targ
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fileDownload(TargetDownloadReq req, HttpServletResponse response) throws SftpUploadUtil.SftpUploadException, IOException {
|
public void fileDownload(BaseIdReq req, HttpServletResponse response){
|
||||||
SftpUploadUtil.downloadFileSftp(
|
if (ObjectUtils.isEmpty(req.getId())){
|
||||||
req.getRemoteHost(),
|
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标id不能为空");
|
||||||
req.getRemotePort(),
|
}
|
||||||
req.getUsername(),
|
//根据versionId查询对应的版本信息,获取版本保存地址
|
||||||
req.getPassword(),
|
TargetVersion targetVersion = targetVersionDao.selectById(req.getId());
|
||||||
req.getSshKeyPath(),
|
|
||||||
req.getRemoteFilePath(),
|
if (ObjectUtils.isEmpty(targetVersion) || ObjectUtils.isEmpty(targetVersion.getFilePath())){
|
||||||
response
|
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