fix(knowledgeBase): 知识库下载文件命名不对

This commit is contained in:
HTEWJ\26070045 committed 2026-09-09 15:43:14 +08:00
1 parent dabf140e41
commit a768eb9024
13 files changed
+299 -28

No files matched your search

+11 -6
View File
@@ -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 {