优化文件下载.

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

@@ -21,6 +21,8 @@ server {
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
# web history 模式 404
try_files $uri $uri/ /index.html;
} }
location /orion/api { location /orion/api {

View File

@@ -1,5 +1,6 @@
package com.orion.ops.module.asset.handler.host.terminal.handler; 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.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.SftpBaseRequest; 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); ISftpSession session = terminalManager.getSession(channel.getId(), sessionId);
String path = payload.getPath(); String path = payload.getPath();
log.info("SftpGetContentHandler-handle start sessionId: {}, path: {}", sessionId, path); log.info("SftpGetContentHandler-handle start sessionId: {}, path: {}", sessionId, path);
String content = null; String content = Const.EMPTY;
Exception ex = null; Exception ex = null;
// 获取内容 // 获取内容
try { try {

View File

@@ -116,10 +116,22 @@ public class SftpSession extends TerminalSession implements ISftpSession {
@Override @Override
public String getContent(String path) { public String getContent(String path) {
path = Valid.checkNormalize(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()); return Streams.toString(in, config.getFileContentCharset());
} catch (Exception e) { } catch (Exception e) {
throw Exceptions.ioRuntime(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); ITransferHostSession session = sessions.get(sessionKey);
if (session == null) { 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); SessionStore sessionStore = hostTerminalService.openSessionStore(connectInfo);
// 打开会话并初始化 // 打开会话并初始化
if (TransferOperatorType.UPLOAD.equals(type.getOperator())) { if (TransferOperatorType.UPLOAD.equals(type.getOperator())) {

View File

@@ -46,6 +46,12 @@ public class DownloadSession extends TransferHostSession implements IDownloadSes
// 检查文件是否存在 // 检查文件是否存在
SftpFile file = executor.getFile(path); SftpFile file = executor.getFile(path);
Valid.notNull(file, ErrorMessage.FILE_ABSENT); 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); this.inputStream = executor.openInputStream(path);
log.info("DownloadSession.startDownload open success channelId: {}, path: {}", channelId, path); log.info("DownloadSession.startDownload open success channelId: {}, path: {}", channelId, path);
@@ -92,7 +98,7 @@ public class DownloadSession extends TransferHostSession implements IDownloadSes
@Override @Override
protected void closeStream() { protected void closeStream() {
// 关闭 inputStream 会被阻塞 ??..?? 只能关闭 executor // 关闭 inputStream 可能会被阻塞 ??..?? 只能关闭 executor
Streams.close(this.executor); Streams.close(this.executor);
this.executor = null; this.executor = null;
this.inputStream = null; this.inputStream = null;

View File

@@ -260,7 +260,7 @@
setLoading(false); setLoading(false);
Message.success('修改成功'); Message.success('修改成功');
// 回调 props // 回调 props
emits('submitted', { ...formModel.value }); emits('submitted', { ...props.content, config: { ...formModel.value } });
} catch (e) { } catch (e) {
} finally { } finally {
setLoading(false); setLoading(false);

View File

@@ -30,8 +30,8 @@ const columns = [
}, { }, {
title: '操作', title: '操作',
slotName: 'handle', slotName: 'handle',
width: 180, width: 162,
align: 'right', align: 'center',
fixed: 'right', fixed: 'right',
}, },
] as TableColumnData[]; ] as TableColumnData[];

View File

@@ -186,8 +186,8 @@
:deep(.arco-tabs-tab) { :deep(.arco-tabs-tab) {
height: 100%; height: 100%;
margin: 0; margin: 0 !important;
padding: 0; padding: 0 !important;
color: var(--color-panel-text-1); color: var(--color-panel-text-1);
background: var(--color-bg-panel-tabs); background: var(--color-bg-panel-tabs);
position: relative; position: relative;

View File

@@ -68,7 +68,7 @@
</span> </span>
</a-tooltip> </a-tooltip>
<!-- 编辑内容 --> <!-- 编辑内容 -->
<a-tooltip v-if="canEditable(record.size, record.isDir)" <a-tooltip v-if="canEditable(record.size, record.attr)"
position="top" position="top"
:mini="true" :mini="true"
:overlay-inverse="true" :overlay-inverse="true"
@@ -204,14 +204,15 @@
}; };
// 是否可编辑 // 是否可编辑
const canEditable = (size: number, isDir: boolean) => { const canEditable = (size: number, attr: string) => {
// 非文件夹并且文件小于 配置大小(MB) 可以编辑 // 是普通文件 && 文件小于 配置大小(MB) 可以编辑
return !isDir && size <= previewSize * 1024 * 1024; return FILE_TYPE.NORMAL_FILE.value == formatFileType(attr).value
&& size <= previewSize * 1024 * 1024;
}; };
// 点击文件名称 // 点击文件名称
const clickFilename = (record: TableData) => { const clickFilename = (record: TableData) => {
if (FILE_TYPE.DIRECTORY.value === formatFileType(record.attr).value) { if (record.isDir) {
// 进入文件夹 // 进入文件夹
emits('loadFile', record.path); emits('loadFile', record.path);
} else { } else {

View File

@@ -121,6 +121,7 @@
// 编辑文件 // 编辑文件
const editFile = (name: string, path: string) => { const editFile = (name: string, path: string) => {
setEditorLoading(true); setEditorLoading(true);
setTableLoading(true);
splitSize.value = 0.6; splitSize.value = 0.6;
editorView.value = true; editorView.value = true;
editorFileName.value = name; editorFileName.value = name;
@@ -208,6 +209,7 @@
// 接收获取文件内容响应 // 接收获取文件内容响应
const resolveSftpGetContent = (path: string, result: string, content: string) => { const resolveSftpGetContent = (path: string, result: string, content: string) => {
setTableLoading(false);
setEditorLoading(false); setEditorLoading(false);
// 检查结果 // 检查结果
if (!checkResult(result, '加载失败')) { if (!checkResult(result, '加载失败')) {