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);
}
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();
}
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;
}
}
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());
}
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;
}
}
Aggregations