fix(knowledgeBase): 知识库下载文件命名不对
This commit is contained in:
1 parent
dabf140e41
commit
a768eb9024
13 files changed
+299
-28
No files matched your search
@@ -1,12 +1,14 @@
|
||||
/**
|
||||
* features/kb/api/kb.api.ts
|
||||
* 用途:知识库 HTTP API 封装。
|
||||
* 依赖:auth、requestJson/BusinessError、vue-i18n(错误 fallback 文案)。
|
||||
* 依赖:auth、requestJson/BusinessError、vue-i18n(错误 fallback 文案)、shared/utils/download。
|
||||
* 逻辑:KbWorkspace / composables 调用;失败时优先保留 BusinessError.message,否则回退本地化 fallback。
|
||||
* 源文件下载走 ?download=1,文件名由 Content-Disposition filename* 或调用方 source_file_name 决定。
|
||||
*/
|
||||
import i18n from '@/i18n';
|
||||
import { getAuthHeaders } from '@/shared/utils/auth';
|
||||
import { BusinessError, requestJson } from '@/shared/api/client';
|
||||
import { parseContentDispositionFilename } from '@/shared/utils/download';
|
||||
|
||||
const kbT = (key: string) => String(i18n.global.t(key));
|
||||
const kbErrorMessage = (e: unknown, fallbackKey: string) =>
|
||||
@@ -300,11 +302,17 @@ export const getKbDocumentVersions = async (
|
||||
return getJson<KbDocumentVersionItem[]>(`/api/kb/documents/${documentId}/versions`, []);
|
||||
};
|
||||
|
||||
/**
|
||||
* 拉取知识库版本原始文件,供作业面触发本地下载。
|
||||
* 核心逻辑:带 download=1 走附件响应;文件名优先 RFC 5987 filename*,失败由调用方回退 source_file_name。
|
||||
* @param versionId - 文档版本 ID
|
||||
* @returns 成功时含 blob 与解析出的文件名;失败时 ok=false
|
||||
*/
|
||||
export const getKbDocumentVersionSourceFile = async (
|
||||
versionId: number
|
||||
): Promise<{ ok: boolean; error?: string; blob?: Blob; filename?: string; contentType?: string }> => {
|
||||
try {
|
||||
const res = await fetch(`/api/kb/document-versions/${versionId}/source-file`, {
|
||||
const res = await fetch(`/api/kb/document-versions/${versionId}/source-file?download=1`, {
|
||||
headers: getAuthHeaders(),
|
||||
});
|
||||
if (!res.ok) {
|
||||
@@ -318,13 +326,10 @@ export const getKbDocumentVersionSourceFile = async (
|
||||
}
|
||||
return { ok: false, error: message };
|
||||
}
|
||||
const disposition = res.headers.get('content-disposition') || '';
|
||||
const filenameMatch = disposition.match(/filename\*=UTF-8''([^;]+)|filename="?([^"]+)"?/i);
|
||||
const filename = decodeURIComponent(filenameMatch?.[1] || filenameMatch?.[2] || '');
|
||||
return {
|
||||
ok: true,
|
||||
blob: await res.blob(),
|
||||
filename: filename || undefined,
|
||||
filename: parseContentDispositionFilename(res.headers.get('content-disposition')),
|
||||
contentType: res.headers.get('content-type') || undefined,
|
||||
};
|
||||
} catch {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
/**
|
||||
* features/kb/composables/kb-workspace/useKbWorkspaceModals.ts
|
||||
* 记录/来源/Markdown/失败弹窗与原始文件预览。
|
||||
* 用途:记录/来源/Markdown/失败弹窗与原始文件下载。
|
||||
* 依赖:kb.api、kb-workspace state/computed/data、shared/utils/download。
|
||||
* 逻辑:KbWorkspaceOverlays 调用;源文件点击只触发一次 a[download],保存名与上传时一致。
|
||||
*/
|
||||
import { triggerBlobDownload } from '@/shared/utils/download';
|
||||
import type { KbDocumentItem, KbDocumentMarkdown, KbDocumentVersionItem } from '../../api/kb.api';
|
||||
import {
|
||||
getKbDocumentMarkdown,
|
||||
@@ -74,6 +77,11 @@ export function useKbWorkspaceModals(
|
||||
showSourceInfoModal.value = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* 下载当前版本的原始文件,保存名与上传时的源文件名一致。
|
||||
* 核心逻辑:拉 blob 后只触发一次 a[download];不用 window.open blob URL,避免再下一份 UUID 名文件。
|
||||
* @param version - 源文件信息弹窗中的版本项
|
||||
*/
|
||||
const openOriginalSourceFile = async (version: KbDocumentVersionItem) => {
|
||||
openingSourceVersionId.value = version.id;
|
||||
const result = await getKbDocumentVersionSourceFile(version.id);
|
||||
@@ -84,15 +92,8 @@ export function useKbWorkspaceModals(
|
||||
return;
|
||||
}
|
||||
|
||||
const objectUrl = window.URL.createObjectURL(result.blob);
|
||||
const opened = window.open(objectUrl, '_blank', 'noopener,noreferrer');
|
||||
if (!opened) {
|
||||
const link = window.document.createElement('a');
|
||||
link.href = objectUrl;
|
||||
link.download = result.filename || version.source_file_name || `source-${version.id}`;
|
||||
link.click();
|
||||
}
|
||||
window.setTimeout(() => window.URL.revokeObjectURL(objectUrl), 60_000);
|
||||
const filename = version.source_file_name || result.filename || `source-${version.id}`;
|
||||
triggerBlobDownload(result.blob, filename);
|
||||
};
|
||||
|
||||
const openFailureModal = (type: 'upload' | 'clean' | 'sync') => {
|
||||
|
||||
Reference in new issue
Block a user