fix(platform): 汇总知识库问答与图表展示修复
This commit is contained in:
1 parent
f615e56bb9
commit
55c6467d83
41 files changed
+4569
-770
No files matched your search
@@ -1,18 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* shared/components/FilePreviewModal.vue
|
||||
* 文件预览弹窗(图片/PDF 等)。
|
||||
*/
|
||||
|
||||
/**
|
||||
* FilePreviewModal.vue
|
||||
* 文件用途:文件预览弹窗,用于在聊天页预览来源文件内容
|
||||
* 引用文件:vue, marked
|
||||
* 逻辑关系:
|
||||
* - 接收 ChatView 传入的 file(name/url)与弹窗显隐状态
|
||||
* - 根据扩展名选择预览模式(markdown/image/iframe/office)
|
||||
* - Excel 预览优先走 Office 风格渲染,尽量还原原始表格观感;失败时再降级为简化表格
|
||||
* - 对 Office 文档在可公网 HTTPS 访问时走 Office 在线预览,否则降级为打开/下载
|
||||
* 用途:聊天页源文件预览弹窗(图片/PDF/Office/Markdown/表格)。
|
||||
* 依赖:vue、marked、xlsx、@vue-office/*、lucide-vue-next。
|
||||
* 逻辑:ChatViewOverlays 传入 file/files;按扩展名选择模式;源文件缺失时尊重后端 X-Preview-Actual-Ext 回退文本预览。
|
||||
*/
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
@@ -382,7 +373,38 @@ const fallbackToSheetPreview = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const loadPdfContent = async (): Promise<'iframe' | 'office' | 'sheet'> => {
|
||||
/**
|
||||
* 判断预览载荷是否为 Office/PDF 等二进制,避免 UTF-8 解码后 Markdown 乱码。
|
||||
*/
|
||||
const isBinaryPreviewPayload = (buffer: ArrayBuffer): boolean => {
|
||||
const bytes = new Uint8Array(buffer);
|
||||
if (bytes.length >= 5 && bytes[0] === 0x25 && bytes[1] === 0x50 && bytes[2] === 0x44 && bytes[3] === 0x46 && bytes[4] === 0x2d) {
|
||||
return true; // %PDF-
|
||||
}
|
||||
if (bytes.length >= 4 && bytes[0] === 0x50 && bytes[1] === 0x4b && bytes[2] === 0x03 && bytes[3] === 0x04) {
|
||||
return true; // ZIP / OOXML
|
||||
}
|
||||
if (
|
||||
bytes.length >= 8
|
||||
&& bytes[0] === 0xd0 && bytes[1] === 0xcf && bytes[2] === 0x11 && bytes[3] === 0xe0
|
||||
&& bytes[4] === 0xa1 && bytes[5] === 0xb1 && bytes[6] === 0x1a && bytes[7] === 0xe1
|
||||
) {
|
||||
return true; // OLE Compound Document (.doc/.xls/.ppt)
|
||||
}
|
||||
const sample = bytes.subarray(0, Math.min(bytes.length, 512));
|
||||
let nul = 0;
|
||||
for (let i = 0; i < sample.length; i += 1) {
|
||||
if (sample[i] === 0) nul += 1;
|
||||
}
|
||||
return nul > sample.length * 0.02;
|
||||
};
|
||||
|
||||
/**
|
||||
* 拉取 PDF/Office 预览内容;若后端因源文件缺失回退到文本,则按 X-Preview-Actual-Ext 渲染 Markdown。
|
||||
* 核心逻辑:请求 /preview-file-raw,根据实际扩展名在 iframe / office / sheet / markdown 间切换。
|
||||
* @returns 最终预览模式
|
||||
*/
|
||||
const loadPdfContent = async (): Promise<'iframe' | 'office' | 'sheet' | 'markdown'> => {
|
||||
if (!props.file?.url) return 'iframe';
|
||||
if (!props.agentId) throw new Error('agent_id is required for pdf preview');
|
||||
|
||||
@@ -413,6 +435,16 @@ const loadPdfContent = async (): Promise<'iframe' | 'office' | 'sheet'> => {
|
||||
const actualExt = (res.headers.get('X-Preview-Actual-Ext') || '').toLowerCase();
|
||||
const blobType = (blob.type || '').toLowerCase();
|
||||
const effectiveExt = actualExt || ext.value;
|
||||
const binaryPayload = isBinaryPreviewPayload(buffer);
|
||||
// 源文件丢失时后端可能回退到清洗稿/分段文本;二进制载荷绝不能当 Markdown 解码
|
||||
if (
|
||||
!binaryPayload
|
||||
&& (['md', 'markdown', 'txt'].includes(actualExt) || blobType.includes('markdown'))
|
||||
) {
|
||||
const text = new TextDecoder('utf-8').decode(buffer);
|
||||
markdownHtml.value = marked.parse(text) as string;
|
||||
return 'markdown';
|
||||
}
|
||||
const isDocxPayload = actualExt === 'docx' || blobType.includes('wordprocessingml.document');
|
||||
const isExcelPayload = ['xlsx', 'xls'].includes(actualExt) || blobType.includes('spreadsheetml.sheet') || blobType.includes('application/vnd.ms-excel');
|
||||
if (isExcelPayload) {
|
||||
@@ -431,6 +463,15 @@ const loadPdfContent = async (): Promise<'iframe' | 'office' | 'sheet'> => {
|
||||
officeKind.value = 'docx';
|
||||
return 'office';
|
||||
}
|
||||
// 旧版 .doc 仍是 OLE:后端应转 PDF;若仍返回原始 OLE,给出明确错误而非乱码
|
||||
if (
|
||||
binaryPayload
|
||||
&& (effectiveExt === 'doc' || ext.value === 'doc')
|
||||
&& !blobType.includes('pdf')
|
||||
&& !(actualExt === 'pdf')
|
||||
) {
|
||||
throw new Error('旧版 Word(.doc)未能转换为可预览格式,请安装 LibreOffice/Word 后重试,或改为上传 .docx/.pdf');
|
||||
}
|
||||
if (!blobType.includes('pdf') && officePreviewExts.has(effectiveExt)) {
|
||||
throw new Error(t('filePreview.unsupportedRawPreview'));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user