优化文件下载逻辑.

This commit is contained in:
lijiahangmax
2024-10-10 23:18:53 +08:00
parent c229029c1d
commit 6e977dabf6
22 changed files with 357 additions and 48 deletions

View File

@@ -63,10 +63,35 @@ export function deleteHostSftpLog(idList: Array<number>) {
});
}
/**
* 获取 SFTP 文件内容
*/
export function getSftpFileContent(token: string) {
return axios.get<string>('/asset/host-sftp/get-content', {
unwrap: true,
params: { token },
timeout: 60000
});
}
/**
* 设置 SFTP 文件内容
*/
export function setSftpFileContent(token: string, content: string) {
const formData = new FormData();
formData.append('token', token);
formData.append('file', new File([content], Date.now() + '', { type: 'text/plain' }));
return axios.post<boolean>('/asset/host-sftp/set-content', formData, {
timeout: 60000,
headers: {
'Content-Type': 'multipart/form-data'
}
});
}
/**
* 下载文件
*/
export function getDownloadTransferUrl(channelId: string, transferToken: string) {
return `${httpBaseUrl}/asset/host-sftp/download?channelId=${channelId}&transferToken=${transferToken}`;
}

View File

@@ -100,7 +100,7 @@ export const InputProtocol = {
// SFTP 修改内容
SFTP_SET_CONTENT: {
type: 'sc',
template: ['type', 'sessionId', 'path', 'content']
template: ['type', 'sessionId', 'path']
},
};
@@ -181,13 +181,13 @@ export const OutputProtocol = {
// SFTP 获取文件内容
SFTP_GET_CONTENT: {
type: 'gc',
template: ['type', 'sessionId', 'path', 'result', 'msg', 'content'],
template: ['type', 'sessionId', 'result', 'msg', 'token'],
processMethod: 'processSftpGetContent'
},
// SFTP 修改文件内容
SFTP_SET_CONTENT: {
type: 'sc',
template: ['type', 'sessionId', 'result', 'msg'],
template: ['type', 'sessionId', 'result', 'msg', 'token'],
processMethod: 'processSftpSetContent'
},
};

View File

@@ -7,6 +7,22 @@ export function getBase64Data(e: string) {
return e.substring(e.indexOf(',') + 1);
}
/**
* 读取 blob 内容 返回 promise
*/
export function readBlobText(blob: Blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = event => {
resolve(event.target?.result as string);
};
reader.onerror = err => {
reject(err);
};
reader.readAsText(blob);
});
}
/**
* 读取文件内容 返回 promise
*/

View File

@@ -77,6 +77,7 @@
arrow-class="terminal-tooltip-content"
content="编辑内容">
<a-button class="icon-button row-action-icon"
:disabled="editorLoading"
@click="editFile(record)">
<icon-edit />
</a-button>
@@ -158,6 +159,7 @@
session?: ISftpSession;
list: Array<SftpFile>;
loading: boolean;
editorLoading: boolean;
selectedFiles: Array<string>;
}>();

View File

@@ -25,6 +25,7 @@
:session="session"
:list="fileList"
:loading="tableLoading"
:editor-loading="editorLoading"
@load-file="loadFiles"
@edit-file="editFile"
@delete-file="deleteFile"
@@ -70,6 +71,8 @@
import { Message } from '@arco-design/web-vue';
import useLoading from '@/hooks/loading';
import { openSftpCreateModalKey, openSftpMoveModalKey, openSftpChmodModalKey, openSftpUploadModalKey } from '../../types/const';
import { getSftpFileContent, setSftpFileContent } from '@/api/asset/host-sftp';
import { isString } from '@/utils/is';
import SftpTableHeader from './sftp-table-header.vue';
import SftpTable from './sftp-table.vue';
import SftpEditorHeader from './sftp-editor-header.vue';
@@ -135,8 +138,7 @@
// 编辑器保存
const editorSave = () => {
setEditorLoading(true);
const value = editorRef.value?.getValue() || '';
session.value?.setContent(editorFilePath.value, value);
session.value?.setContent(editorFilePath.value);
};
// 关闭编辑器
@@ -231,24 +233,47 @@
};
// 接收获取文件内容响应
const resolveSftpGetContent = (path: string, result: string, msg: string, content: string) => {
const resolveSftpGetContent = (result: string, msg: string, token: string) => {
setTableLoading(false);
setEditorLoading(false);
// 检查结果
if (!checkResult(result, msg)) {
return;
}
editorRef.value?.setValue(content);
setEditorLoading(true);
editorRef.value?.setValue('');
// 读取文件
getSftpFileContent(token).then(async ({ data }) => {
if (isString(data)) {
// 成功为 string
editorRef.value?.setValue(data || '');
} else {
// 失败为 object
Message.error((data as any).msg || '读取失败');
}
setEditorLoading(false);
}).catch(() => {
setEditorLoading(false);
});
};
// 接收修改文件内容响应
const resolveSftpSetContent = (result: string, msg: string) => {
const resolveSftpSetContent = (result: string, msg: string, token: string) => {
setEditorLoading(false);
// 检查结果
if (!checkResult(result, msg)) {
return;
}
Message.success('保存成功');
setEditorLoading(true);
// 获取文本
const value = editorRef.value?.getValue() || '';
// 保存
setSftpFileContent(token, value).then(() => {
setEditorLoading(false);
Message.success('保存成功');
}).catch(() => {
setEditorLoading(false);
});
};
// 接收下载文件夹展开文件响应

View File

@@ -144,11 +144,10 @@ export default class SftpSession extends BaseSession implements ISftpSession {
};
// 修改内容
setContent(path: string, content: string) {
setContent(path: string) {
this.channel.send(InputProtocol.SFTP_SET_CONTENT, {
sessionId: this.sessionId,
path,
content
});
};

View File

@@ -177,17 +177,17 @@ export default class TerminalOutputProcessor implements ITerminalOutputProcessor
}
// 处理 SFTP 获取文件内容
processSftpGetContent({ sessionId, path, result, msg, content }: OutputPayload): void {
processSftpGetContent({ sessionId, result, msg, token }: OutputPayload): void {
// 获取会话
const session = this.sessionManager.getSession<ISftpSession>(sessionId);
session && session.resolver.resolveSftpGetContent(path, result, msg, content);
session && session.resolver.resolveSftpGetContent(result, msg, token);
}
// 处理 SFTP 修改文件内容
processSftpSetContent({ sessionId, result, msg }: OutputPayload) {
processSftpSetContent({ sessionId, result, msg, token }: OutputPayload) {
// 获取会话
const session = this.sessionManager.getSession<ISftpSession>(sessionId);
session && session.resolver.resolveSftpSetContent(result, msg);
session && session.resolver.resolveSftpSetContent(result, msg, token);
}
// 根据类型处理操作

View File

@@ -323,7 +323,7 @@ export interface ISftpSession extends ITerminalSession {
// 获取内容
getContent: (path: string) => void;
// 修改内容
setContent: (path: string, content: string) => void;
setContent: (path: string) => void;
}
// sftp 会话接收器定义
@@ -349,9 +349,9 @@ export interface ISftpSessionResolver {
// 接收下载文件夹展开文件响应
resolveDownloadFlatDirectory: (currentPath: string, result: string, msg: string, list: Array<SftpFile>) => void;
// 接收获取文件内容响应
resolveSftpGetContent: (path: string, result: string, msg: string, content: string) => void;
resolveSftpGetContent: (result: string, msg: string, token: string) => void;
// 接收修改文件内容响应
resolveSftpSetContent: (result: string, msg: string) => void;
resolveSftpSetContent: (result: string, msg: string, token: string) => void;
}
// sftp 文件