优化文件下载.

This commit is contained in:
lijiahangmax
2024-02-25 19:29:39 +08:00
parent c3f33e7494
commit b30a48f174
10 changed files with 38 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.asset.handler.host.terminal.handler;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.common.enums.BooleanBit;
import com.orion.ops.module.asset.handler.host.terminal.enums.OutputTypeEnum;
import com.orion.ops.module.asset.handler.host.terminal.model.request.SftpBaseRequest;
@@ -27,7 +28,7 @@ public class SftpGetContentHandler extends AbstractTerminalHandler<SftpBaseReque
ISftpSession session = terminalManager.getSession(channel.getId(), sessionId);
String path = payload.getPath();
log.info("SftpGetContentHandler-handle start sessionId: {}, path: {}", sessionId, path);
String content = null;
String content = Const.EMPTY;
Exception ex = null;
// 获取内容
try {

View File

@@ -116,10 +116,22 @@ public class SftpSession extends TerminalSession implements ISftpSession {
@Override
public String getContent(String path) {
path = Valid.checkNormalize(path);
try (InputStream in = executor.openInputStream(path)) {
try {
// 获取文件
SftpFile file = executor.getFile(path);
if (file == null || file.getSize() == 0L) {
return Const.EMPTY;
}
// 读取文件
InputStream in = executor.openInputStream(path);
return Streams.toString(in, config.getFileContentCharset());
} catch (Exception e) {
throw Exceptions.ioRuntime(e);
} finally {
// 同关闭 transfer downloader
// 关闭 inputStream 可能会被阻塞 ??..?? 只能关闭 executor
Streams.close(this.executor);
this.connect();
}
}

View File

@@ -107,7 +107,7 @@ public class TransferHandler implements ITransferHandler {
ITransferHostSession session = sessions.get(sessionKey);
if (session == null) {
// 获取主机信息
HostTerminalConnectDTO connectInfo = hostTerminalService.getTerminalConnectInfo(hostId, this.userId, HostConnectTypeEnum.SFTP);
HostTerminalConnectDTO connectInfo = hostTerminalService.getTerminalConnectInfo(this.userId, hostId, HostConnectTypeEnum.SFTP);
SessionStore sessionStore = hostTerminalService.openSessionStore(connectInfo);
// 打开会话并初始化
if (TransferOperatorType.UPLOAD.equals(type.getOperator())) {

View File

@@ -46,6 +46,12 @@ public class DownloadSession extends TransferHostSession implements IDownloadSes
// 检查文件是否存在
SftpFile file = executor.getFile(path);
Valid.notNull(file, ErrorMessage.FILE_ABSENT);
if (file.getSize() == 0L) {
// 文件为空
log.info("DownloadSession.startDownload file empty channelId: {}, path: {}", channelId, path);
TransferUtils.sendMessage(this.channel, TransferReceiverType.DOWNLOAD_FINISH, null);
return;
}
// 打开输入流
this.inputStream = executor.openInputStream(path);
log.info("DownloadSession.startDownload open success channelId: {}, path: {}", channelId, path);
@@ -92,7 +98,7 @@ public class DownloadSession extends TransferHostSession implements IDownloadSes
@Override
protected void closeStream() {
// 关闭 inputStream 会被阻塞 ??..?? 只能关闭 executor
// 关闭 inputStream 可能会被阻塞 ??..?? 只能关闭 executor
Streams.close(this.executor);
this.executor = null;
this.inputStream = null;