Merge branch 'dev' into hugo-shipping-notice
This commit is contained in:
commit
472b530be3
397 files changed
+46450
-34171
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,8 +463,17 @@ 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('当前原始文件暂不支持直接预览,已阻止浏览器自动下载');
|
||||
throw new Error(t('filePreview.unsupportedRawPreview'));
|
||||
}
|
||||
let fallbackType = 'application/octet-stream';
|
||||
if (ext.value === 'pdf' || ['doc', 'ppt', 'pptx'].includes(ext.value)) fallbackType = 'application/pdf';
|
||||
@@ -629,7 +670,7 @@ watch(
|
||||
<div class="text-sm font-medium text-slate-500 dark:text-slate-400 tracking-wide">{{ t('filePreview.title') }}</div>
|
||||
<div class="mt-1 font-semibold text-slate-900 dark:text-slate-100 break-words">{{ activeFile.name }}</div>
|
||||
<div v-if="isImageGallery" class="mt-1 text-xs text-slate-500 dark:text-slate-400">
|
||||
第 {{ activeIndex + 1 }} / {{ activeFiles.length }} 张
|
||||
{{ t('filePreview.imageCounter', { current: activeIndex + 1, total: activeFiles.length }) }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex shrink-0 items-center gap-2">
|
||||
@@ -637,7 +678,7 @@ watch(
|
||||
<button
|
||||
type="button"
|
||||
class="btn-icon-ds h-8 w-8"
|
||||
aria-label="缩小"
|
||||
:aria-label="t('filePreview.zoomOut')"
|
||||
:disabled="excelZoom <= EXCEL_ZOOM_MIN"
|
||||
@click="adjustExcelZoom(-EXCEL_ZOOM_STEP)"
|
||||
>
|
||||
@@ -646,7 +687,7 @@ watch(
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-md px-2 py-1 text-xs font-medium text-secondary transition hover:bg-surface-soft"
|
||||
aria-label="重置缩放"
|
||||
:aria-label="t('filePreview.resetZoom')"
|
||||
@click="resetExcelZoom"
|
||||
>
|
||||
<span class="inline-flex items-center gap-1">
|
||||
@@ -657,7 +698,7 @@ watch(
|
||||
<button
|
||||
type="button"
|
||||
class="btn-icon-ds h-8 w-8"
|
||||
aria-label="放大"
|
||||
:aria-label="t('filePreview.zoomIn')"
|
||||
:disabled="excelZoom >= EXCEL_ZOOM_MAX"
|
||||
@click="adjustExcelZoom(EXCEL_ZOOM_STEP)"
|
||||
>
|
||||
@@ -760,7 +801,7 @@ watch(
|
||||
type="button"
|
||||
class="absolute left-3 top-1/2 z-10 inline-flex h-10 w-10 -translate-y-1/2 items-center justify-center rounded-full border border-slate-200 bg-white/90 text-slate-700 shadow transition hover:bg-white disabled:cursor-not-allowed disabled:opacity-40 dark:border-slate-700 dark:bg-slate-900/90 dark:text-slate-100"
|
||||
:disabled="activeIndex <= 0"
|
||||
aria-label="上一张"
|
||||
:aria-label="t('filePreview.prevImage')"
|
||||
@click="showPrevImage"
|
||||
>
|
||||
<ChevronLeft class="h-5 w-5" />
|
||||
@@ -771,7 +812,7 @@ watch(
|
||||
type="button"
|
||||
class="absolute right-3 top-1/2 z-10 inline-flex h-10 w-10 -translate-y-1/2 items-center justify-center rounded-full border border-slate-200 bg-white/90 text-slate-700 shadow transition hover:bg-white disabled:cursor-not-allowed disabled:opacity-40 dark:border-slate-700 dark:bg-slate-900/90 dark:text-slate-100"
|
||||
:disabled="activeIndex >= activeFiles.length - 1"
|
||||
aria-label="下一张"
|
||||
:aria-label="t('filePreview.nextImage')"
|
||||
@click="showNextImage"
|
||||
>
|
||||
<ChevronRight class="h-5 w-5" />
|
||||
|
||||
Reference in new issue
Block a user