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