🔨 下载文件.
This commit is contained in:
@@ -5,7 +5,7 @@ import com.orion.lang.utils.collect.Lists;
|
||||
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.SftpDownloadFlatDirectoryRequest;
|
||||
import com.orion.ops.module.asset.handler.host.terminal.model.response.SftpDownloadDirectoryFlatResponse;
|
||||
import com.orion.ops.module.asset.handler.host.terminal.model.response.SftpDownloadFlatDirectoryResponse;
|
||||
import com.orion.ops.module.asset.handler.host.terminal.model.response.SftpFileVO;
|
||||
import com.orion.ops.module.asset.handler.host.terminal.session.ISftpSession;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -44,7 +44,7 @@ public class SftpDownloadFlatDirectoryHandler extends AbstractTerminalHandler<Sf
|
||||
// 返回
|
||||
this.send(channel,
|
||||
OutputTypeEnum.SFTP_DOWNLOAD_FLAT_DIRECTORY,
|
||||
SftpDownloadDirectoryFlatResponse.builder()
|
||||
SftpDownloadFlatDirectoryResponse.builder()
|
||||
.sessionId(payload.getSessionId())
|
||||
.currentPath(payload.getCurrentPath())
|
||||
.body(JSON.toJSONString(files))
|
||||
|
||||
@@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* sftp 下载文件夹 flat 实体对象
|
||||
* sftp 下载文件夹展开文件 实体对象
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
@@ -19,8 +19,8 @@ import lombok.experimental.SuperBuilder;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Schema(name = "SftpDownloadDirectoryFlatResponse", description = "sftp 下载文件夹 flat 实体对象")
|
||||
public class SftpDownloadDirectoryFlatResponse extends SftpBaseResponse {
|
||||
@Schema(name = "SftpDownloadFlatDirectoryResponse", description = "sftp 下载文件夹展开文件 实体对象")
|
||||
public class SftpDownloadFlatDirectoryResponse extends SftpBaseResponse {
|
||||
|
||||
@Schema(description = "currentPath")
|
||||
private String currentPath;
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.orion.ops.module.asset.handler.host.transfer.session;
|
||||
|
||||
import com.orion.lang.utils.Threads;
|
||||
import com.orion.lang.utils.Valid;
|
||||
import com.orion.lang.utils.io.Streams;
|
||||
import com.orion.net.host.SessionStore;
|
||||
@@ -43,28 +44,35 @@ public class DownloadSession extends TransferHostSession implements IDownloadSes
|
||||
// 打开输入流
|
||||
this.inputStream = executor.openInputStream(path);
|
||||
} catch (Exception e) {
|
||||
log.error("DownloadSession.uploadStart error", e);
|
||||
log.error("DownloadSession.startDownload error", e);
|
||||
// 响应结果
|
||||
TransferUtils.sendMessage(this.channel, TransferReceiverType.DOWNLOAD_ERROR, e);
|
||||
return;
|
||||
}
|
||||
// 异步读取文件内容 FIXME bug
|
||||
AssetThreadPools.TERMINAL_SCHEDULER.execute(() -> {
|
||||
// 异步读取文件内容
|
||||
AssetThreadPools.SFTP_DOWNLOAD_SCHEDULER.execute(() -> {
|
||||
Exception ex = null;
|
||||
try {
|
||||
byte[] buffer = new byte[Const.BUFFER_KB_32];
|
||||
int len;
|
||||
// 响应文件内容
|
||||
while ((len = this.inputStream.read(buffer)) != -1) {
|
||||
while (this.inputStream != null && (len = this.inputStream.read(buffer)) != -1) {
|
||||
this.channel.sendMessage(new BinaryMessage(buffer, 0, len, true));
|
||||
}
|
||||
// 响应结果
|
||||
TransferUtils.sendMessage(this.channel, TransferReceiverType.DOWNLOAD_FINISH, null);
|
||||
log.info("DownloadSession.startDownload finish");
|
||||
} catch (Exception e) {
|
||||
log.error("DownloadSession.transfer error", e);
|
||||
// 响应结果
|
||||
TransferUtils.sendMessage(this.channel, TransferReceiverType.DOWNLOAD_ERROR, e);
|
||||
} finally {
|
||||
this.closeStream();
|
||||
log.error("DownloadSession.startDownload error", e);
|
||||
ex = e;
|
||||
}
|
||||
// 关闭等待 jsch 内部处理
|
||||
Threads.sleep(100);
|
||||
this.closeStream();
|
||||
Threads.sleep(100);
|
||||
// 响应结果
|
||||
if (ex == null) {
|
||||
TransferUtils.sendMessage(this.channel, TransferReceiverType.DOWNLOAD_FINISH, null);
|
||||
} else {
|
||||
TransferUtils.sendMessage(this.channel, TransferReceiverType.DOWNLOAD_ERROR, ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -73,23 +81,22 @@ public class DownloadSession extends TransferHostSession implements IDownloadSes
|
||||
public void abortDownload() {
|
||||
log.info("DownloadSession.abortDownload");
|
||||
// 关闭流
|
||||
Streams.close(inputStream);
|
||||
// 响应结果
|
||||
TransferUtils.sendMessage(this.channel, TransferReceiverType.DOWNLOAD_FINISH, null);
|
||||
this.closeStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
this.closeStream();
|
||||
super.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭流
|
||||
*/
|
||||
private void closeStream() {
|
||||
// 关闭流
|
||||
Streams.close(inputStream);
|
||||
// 关闭 inputStream 会被阻塞 ??..?? 只能关闭 executor
|
||||
Streams.close(this.executor);
|
||||
this.executor = null;
|
||||
this.inputStream = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@ public class UploadSession extends TransferHostSession implements IUploadSession
|
||||
|
||||
@Override
|
||||
public void uploadFinish() {
|
||||
log.info("UploadSession.uploadFinish");
|
||||
this.closeStream();
|
||||
// 响应结果
|
||||
TransferUtils.sendMessage(this.channel, TransferReceiverType.UPLOAD_FINISH, null);
|
||||
@@ -76,6 +77,7 @@ public class UploadSession extends TransferHostSession implements IUploadSession
|
||||
|
||||
@Override
|
||||
public void uploadError() {
|
||||
log.error("UploadSession.uploadError");
|
||||
this.closeStream();
|
||||
// 响应结果
|
||||
TransferUtils.sendMessage(this.channel, TransferReceiverType.UPLOAD_ERROR, new InvalidArgumentException((String) null));
|
||||
@@ -83,8 +85,8 @@ public class UploadSession extends TransferHostSession implements IUploadSession
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
this.closeStream();
|
||||
super.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
<!-- 左侧图标 -->
|
||||
<div class="transfer-item-left">
|
||||
<span class="file-icon">
|
||||
<icon-upload />
|
||||
<icon-upload v-if="item.type === TransferType.UPLOAD" />
|
||||
<icon-download v-else-if="item.type === TransferType.DOWNLOAD" />
|
||||
</span>
|
||||
</div>
|
||||
<!-- 中间信息 -->
|
||||
@@ -116,7 +117,7 @@
|
||||
import useVisible from '@/hooks/visible';
|
||||
import { useTerminalStore } from '@/store';
|
||||
import { getFileSize } from '@/utils/file';
|
||||
import { TransferStatus } from '../../types/terminal.const';
|
||||
import { TransferStatus, TransferType } from '../../types/terminal.const';
|
||||
|
||||
const { transferManager } = useTerminalStore();
|
||||
const { visible, setVisible } = useVisible();
|
||||
|
||||
Reference in New Issue
Block a user