Search in sources :

Example 1 with SftpFile

use of com.orion.remote.channel.sftp.SftpFile in project orion-kit by lijiahangmax.

the class SftpExecutorTests method update.

@Test
public void update() {
    String path = "/root/1";
    SftpFile file = e.getFile(path);
    e.setModifyTime(path, new Date());
    e.chmod(path, 777);
    e.chown(path, 1);
    e.chgrp(path, 1);
    file.setPermission(444);
    e.setFileAttribute(file);
}
Also used : SftpFile(com.orion.remote.channel.sftp.SftpFile) Date(java.util.Date) Test(org.junit.Test)

Example 2 with SftpFile

use of com.orion.remote.channel.sftp.SftpFile in project orion-ops by lijiahangmax.

the class SftpServiceImpl method transferReTransfer.

/**
 * 重新传输
 */
private void transferReTransfer(String fileToken, boolean isUpload) {
    // 获取请求文件
    FileTransferLogDO transferLog = this.getTransferLogByToken(fileToken);
    Valid.notNull(transferLog, MessageConst.UNSELECTED_TRANSFER_LOG);
    Long machineId = transferLog.getMachineId();
    // 暂停
    IFileTransferProcessor processor = transferProcessorManager.getProcessor(transferLog.getFileToken());
    if (processor != null) {
        processor.stop();
    }
    if (isUpload) {
        // 删除远程文件
        SftpExecutor executor = sftpBasicExecutorHolder.getBasicExecutor(machineId);
        SftpFile file = executor.getFile(transferLog.getRemoteFile());
        if (file != null) {
            executor.rmFile(transferLog.getRemoteFile());
        }
    } else {
        // 删除本地文件
        String loacalPath = Files1.getPath(SystemEnvAttr.SWAP_PATH.getValue(), transferLog.getLocalFile());
        Files1.delete(loacalPath);
    }
    // 修改进度
    FileTransferLogDO update = new FileTransferLogDO();
    update.setId(transferLog.getId());
    update.setTransferStatus(SftpTransferStatus.WAIT.getStatus());
    update.setNowProgress(0D);
    update.setCurrentSize(0L);
    fileTransferLogDAO.updateById(update);
    // 通知进度
    FileTransferNotifyDTO.FileTransferNotifyProgress progress = FileTransferNotifyDTO.progress(Strings.EMPTY, Files1.getSize(transferLog.getFileSize()), "0");
    transferProcessorManager.notifySessionProgressEvent(transferLog.getUserId(), machineId, transferLog.getFileToken(), progress);
    // 通知状态
    transferProcessorManager.notifySessionStatusEvent(transferLog.getUserId(), machineId, transferLog.getFileToken(), SftpTransferStatus.WAIT.getStatus());
    // 提交下载
    IFileTransferProcessor.of(FileTransferHint.transfer(transferLog)).exec();
}
Also used : SftpFile(com.orion.remote.channel.sftp.SftpFile) SftpExecutor(com.orion.remote.channel.sftp.SftpExecutor) FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO) FileTransferNotifyDTO(com.orion.ops.entity.dto.FileTransferNotifyDTO) IFileTransferProcessor(com.orion.ops.handler.sftp.IFileTransferProcessor)

Example 3 with SftpFile

use of com.orion.remote.channel.sftp.SftpFile in project orion-ops by lijiahangmax.

the class SftpServiceImpl method listDir.

@Override
public FileListVO listDir(FileListRequest request) {
    SftpExecutor executor = sftpBasicExecutorHolder.getBasicExecutor(request.getSessionToken());
    synchronized (executor) {
        String path = request.getPath();
        List<SftpFile> fileList = executor.listDirs(path);
        // 返回
        FileListVO fileListVO = new FileListVO();
        List<FileDetailVO> files = Converts.toList(fileList, FileDetailVO.class);
        fileListVO.setPath(path);
        fileListVO.setFiles(files);
        return fileListVO;
    }
}
Also used : SftpFile(com.orion.remote.channel.sftp.SftpFile) SftpExecutor(com.orion.remote.channel.sftp.SftpExecutor) FileListVO(com.orion.ops.entity.vo.sftp.FileListVO) FileDetailVO(com.orion.ops.entity.vo.sftp.FileDetailVO)

Example 4 with SftpFile

use of com.orion.remote.channel.sftp.SftpFile in project orion-ops by lijiahangmax.

the class SftpServiceImpl method download.

@Override
public void download(FileDownloadRequest request) {
    // 获取token信息
    Long machineId = this.getMachineId(request.getSessionToken());
    SftpExecutor executor = sftpBasicExecutorHolder.getBasicExecutor(request.getSessionToken());
    UserDTO user = Currents.getUser();
    Long userId = user.getId();
    // 定义文件转换器
    Function<SftpFile, FileTransferLogDO> convert = file -> {
        // 本地保存路径
        String fileToken = ObjectIds.next();
        // 设置传输信息
        FileTransferLogDO download = new FileTransferLogDO();
        download.setUserId(userId);
        download.setUserName(user.getUsername());
        download.setFileToken(fileToken);
        download.setTransferType(SftpTransferType.DOWNLOAD.getType());
        download.setMachineId(machineId);
        download.setRemoteFile(file.getPath());
        download.setLocalFile(PathBuilders.getSftpDownloadFilePath(fileToken));
        download.setCurrentSize(0L);
        download.setFileSize(file.getSize());
        download.setNowProgress(0D);
        download.setTransferStatus(SftpTransferStatus.WAIT.getStatus());
        return download;
    };
    // 初始化下载信息
    List<FileTransferLogDO> downloadFiles = Lists.newList();
    List<String> paths = request.getPaths();
    for (String path : paths) {
        SftpFile file = executor.getFile(path);
        Valid.notNull(file, Strings.format(MessageConst.FILE_NOT_FOUND, path));
        // 如果是文件夹则递归所有文件
        if (file.isDirectory()) {
            List<SftpFile> childFiles = executor.listFiles(path, true, false);
            childFiles.forEach(f -> downloadFiles.add(convert.apply(f)));
        } else {
            downloadFiles.add(convert.apply(file));
        }
    }
    for (FileTransferLogDO downloadFile : downloadFiles) {
        fileTransferLogDAO.insert(downloadFile);
        // 通知添加
        transferProcessorManager.notifySessionAddEvent(userId, machineId, downloadFile.getFileToken(), downloadFile);
    }
    // 提交下载任务
    for (FileTransferLogDO downloadFile : downloadFiles) {
        IFileTransferProcessor.of(FileTransferHint.transfer(downloadFile)).exec();
    }
    // 设置日志参数
    EventParamsHolder.addParam(EventKeys.MACHINE_ID, machineId);
    EventParamsHolder.addParam(EventKeys.PATHS, paths);
    EventParamsHolder.addParam(EventKeys.COUNT, downloadFiles.size());
}
Also used : SftpFile(com.orion.remote.channel.sftp.SftpFile) EventKeys(com.orion.ops.consts.event.EventKeys) IFileTransferProcessor(com.orion.ops.handler.sftp.IFileTransferProcessor) MessageConst(com.orion.ops.consts.MessageConst) EventParamsHolder(com.orion.ops.consts.event.EventParamsHolder) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) ObjectIds(com.orion.id.ObjectIds) Function(java.util.function.Function) TransferProcessorManager(com.orion.ops.handler.sftp.TransferProcessorManager) FileTransferHint(com.orion.ops.handler.sftp.hint.FileTransferHint) PathBuilders(com.orion.ops.utils.PathBuilders) FileTransferLogVO(com.orion.ops.entity.vo.FileTransferLogVO) SftpBasicExecutorHolder(com.orion.ops.handler.sftp.SftpBasicExecutorHolder) Service(org.springframework.stereotype.Service) Arrays1(com.orion.utils.Arrays1) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) FileOpenVO(com.orion.ops.entity.vo.sftp.FileOpenVO) KeyConst(com.orion.ops.consts.KeyConst) Const(com.orion.ops.consts.Const) SftpFile(com.orion.remote.channel.sftp.SftpFile) FileDetailVO(com.orion.ops.entity.vo.sftp.FileDetailVO) SftpTransferType(com.orion.ops.consts.sftp.SftpTransferType) SftpTransferStatus(com.orion.ops.consts.sftp.SftpTransferStatus) com.orion.ops.entity.request.sftp(com.orion.ops.entity.request.sftp) UUIds(com.orion.id.UUIds) Resource(javax.annotation.Resource) SftpService(com.orion.ops.service.api.SftpService) Valid(com.orion.ops.utils.Valid) Collectors(java.util.stream.Collectors) UserDTO(com.orion.ops.entity.dto.UserDTO) Files1(com.orion.utils.io.Files1) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Converts(com.orion.utils.convert.Converts) List(java.util.List) SftpPackageType(com.orion.ops.consts.sftp.SftpPackageType) SystemEnvAttr(com.orion.ops.consts.system.SystemEnvAttr) FileTransferLogDAO(com.orion.ops.dao.FileTransferLogDAO) FileListVO(com.orion.ops.entity.vo.sftp.FileListVO) FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO) Lists(com.orion.utils.collect.Lists) MachineInfoService(com.orion.ops.service.api.MachineInfoService) Currents(com.orion.ops.utils.Currents) FileTransferNotifyDTO(com.orion.ops.entity.dto.FileTransferNotifyDTO) SftpExecutor(com.orion.remote.channel.sftp.SftpExecutor) Strings(com.orion.utils.Strings) SftpExecutor(com.orion.remote.channel.sftp.SftpExecutor) FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO) UserDTO(com.orion.ops.entity.dto.UserDTO)

Example 5 with SftpFile

use of com.orion.remote.channel.sftp.SftpFile in project orion-ops by lijiahangmax.

the class SftpServiceImpl method list.

private FileListVO list(String path, Boolean all, SftpExecutor executor) {
    synchronized (executor) {
        List<SftpFile> fileList;
        if (all == null || !all) {
            // 不查询隐藏文件
            fileList = executor.listFilesFilter(path, f -> !f.getName().startsWith("."), false, true);
        } else {
            // 查询隐藏文件
            fileList = executor.ll(path);
        }
        // 返回
        FileListVO fileListVO = new FileListVO();
        List<FileDetailVO> files = Converts.toList(fileList, FileDetailVO.class);
        fileListVO.setPath(path);
        fileListVO.setFiles(files);
        return fileListVO;
    }
}
Also used : SftpFile(com.orion.remote.channel.sftp.SftpFile) EventKeys(com.orion.ops.consts.event.EventKeys) IFileTransferProcessor(com.orion.ops.handler.sftp.IFileTransferProcessor) MessageConst(com.orion.ops.consts.MessageConst) EventParamsHolder(com.orion.ops.consts.event.EventParamsHolder) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) ObjectIds(com.orion.id.ObjectIds) Function(java.util.function.Function) TransferProcessorManager(com.orion.ops.handler.sftp.TransferProcessorManager) FileTransferHint(com.orion.ops.handler.sftp.hint.FileTransferHint) PathBuilders(com.orion.ops.utils.PathBuilders) FileTransferLogVO(com.orion.ops.entity.vo.FileTransferLogVO) SftpBasicExecutorHolder(com.orion.ops.handler.sftp.SftpBasicExecutorHolder) Service(org.springframework.stereotype.Service) Arrays1(com.orion.utils.Arrays1) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) FileOpenVO(com.orion.ops.entity.vo.sftp.FileOpenVO) KeyConst(com.orion.ops.consts.KeyConst) Const(com.orion.ops.consts.Const) SftpFile(com.orion.remote.channel.sftp.SftpFile) FileDetailVO(com.orion.ops.entity.vo.sftp.FileDetailVO) SftpTransferType(com.orion.ops.consts.sftp.SftpTransferType) SftpTransferStatus(com.orion.ops.consts.sftp.SftpTransferStatus) com.orion.ops.entity.request.sftp(com.orion.ops.entity.request.sftp) UUIds(com.orion.id.UUIds) Resource(javax.annotation.Resource) SftpService(com.orion.ops.service.api.SftpService) Valid(com.orion.ops.utils.Valid) Collectors(java.util.stream.Collectors) UserDTO(com.orion.ops.entity.dto.UserDTO) Files1(com.orion.utils.io.Files1) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Converts(com.orion.utils.convert.Converts) List(java.util.List) SftpPackageType(com.orion.ops.consts.sftp.SftpPackageType) SystemEnvAttr(com.orion.ops.consts.system.SystemEnvAttr) FileTransferLogDAO(com.orion.ops.dao.FileTransferLogDAO) FileListVO(com.orion.ops.entity.vo.sftp.FileListVO) FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO) Lists(com.orion.utils.collect.Lists) MachineInfoService(com.orion.ops.service.api.MachineInfoService) Currents(com.orion.ops.utils.Currents) FileTransferNotifyDTO(com.orion.ops.entity.dto.FileTransferNotifyDTO) SftpExecutor(com.orion.remote.channel.sftp.SftpExecutor) Strings(com.orion.utils.Strings) FileListVO(com.orion.ops.entity.vo.sftp.FileListVO) FileDetailVO(com.orion.ops.entity.vo.sftp.FileDetailVO)

Aggregations

SftpFile (com.orion.remote.channel.sftp.SftpFile)5 SftpExecutor (com.orion.remote.channel.sftp.SftpExecutor)4 FileTransferLogDO (com.orion.ops.entity.domain.FileTransferLogDO)3 FileTransferNotifyDTO (com.orion.ops.entity.dto.FileTransferNotifyDTO)3 FileDetailVO (com.orion.ops.entity.vo.sftp.FileDetailVO)3 FileListVO (com.orion.ops.entity.vo.sftp.FileListVO)3 IFileTransferProcessor (com.orion.ops.handler.sftp.IFileTransferProcessor)3 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)2 ObjectIds (com.orion.id.ObjectIds)2 UUIds (com.orion.id.UUIds)2 Const (com.orion.ops.consts.Const)2 KeyConst (com.orion.ops.consts.KeyConst)2 MessageConst (com.orion.ops.consts.MessageConst)2 EventKeys (com.orion.ops.consts.event.EventKeys)2 EventParamsHolder (com.orion.ops.consts.event.EventParamsHolder)2 SftpPackageType (com.orion.ops.consts.sftp.SftpPackageType)2 SftpTransferStatus (com.orion.ops.consts.sftp.SftpTransferStatus)2 SftpTransferType (com.orion.ops.consts.sftp.SftpTransferType)2 SystemEnvAttr (com.orion.ops.consts.system.SystemEnvAttr)2 FileTransferLogDAO (com.orion.ops.dao.FileTransferLogDAO)2