修复在线预览.doc/.ppt等一些问题
This commit is contained in:
1 parent
66b108f9d8
commit
8647537f2f
6 files changed
+516
-390
No files matched your search
@@ -64,67 +64,26 @@ const getExt = (raw: string) => {
|
||||
};
|
||||
|
||||
const ext = computed(() => {
|
||||
// URL extension has higher priority than display name.
|
||||
const byUrl = getExt(props.file?.url || '');
|
||||
if (byUrl) return byUrl;
|
||||
return getExt(props.file?.name || '');
|
||||
// Display name extension has higher priority than URL.
|
||||
const byName = getExt(props.file?.name || '');
|
||||
if (byName) return byName;
|
||||
return getExt(props.file?.url || '');
|
||||
});
|
||||
|
||||
const preferredMode = computed<'markdown' | 'image' | 'iframe' | 'sheet' | 'office'>(() => {
|
||||
if (['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp'].includes(ext.value)) return 'image';
|
||||
if (['md', 'markdown', 'txt', 'csv', 'vtt', 'properties', 'html', 'htm'].includes(ext.value)) return 'markdown';
|
||||
if (['xlsx', 'xls'].includes(ext.value)) return 'office';
|
||||
if (['docx', 'doc', 'pdf'].includes(ext.value)) return 'iframe';
|
||||
if (['xlsx', 'xls'].includes(ext.value)) return 'sheet';
|
||||
if (['docx'].includes(ext.value)) return 'office';
|
||||
if (['doc', 'ppt', 'pptx', 'pdf'].includes(ext.value)) return 'iframe';
|
||||
return 'iframe';
|
||||
});
|
||||
|
||||
const resolvedMode = ref<'markdown' | 'image' | 'iframe' | 'sheet' | 'office'>('iframe');
|
||||
|
||||
const isOfficeDoc = computed(() => ['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx'].includes(ext.value));
|
||||
|
||||
/**
|
||||
* 判断当前文件是否可使用微软 Office 在线预览。
|
||||
* 实际条件:
|
||||
* - 必须是 Office 文档类型;
|
||||
* - 必须是 HTTPS;
|
||||
* - 主机必须公网可达(非 localhost/私网网段)。
|
||||
*/
|
||||
const canUseOfficeOnlineViewer = computed(() => {
|
||||
const raw = props.file?.url || '';
|
||||
if (!raw || !isOfficeDoc.value) return false;
|
||||
try {
|
||||
const u = new URL(raw, window.location.origin);
|
||||
// Office online viewer usually requires a publicly reachable HTTPS URL.
|
||||
// Private-network or local addresses typically render as blank pages.
|
||||
if (u.protocol !== 'https:') return false;
|
||||
const host = u.hostname;
|
||||
if (
|
||||
host === 'localhost' ||
|
||||
host === '127.0.0.1' ||
|
||||
host === '::1' ||
|
||||
/^10\./.test(host) ||
|
||||
/^192\.168\./.test(host) ||
|
||||
/^172\.(1[6-9]|2\d|3[0-1])\./.test(host)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
const needsOfficeFallback = computed(
|
||||
() => ['ppt', 'pptx'].includes(ext.value) && !canUseOfficeOnlineViewer.value
|
||||
);
|
||||
|
||||
const previewRawUrl = computed(() => {
|
||||
if (!props.file?.url || !props.agentId) return '';
|
||||
const params = new URLSearchParams({
|
||||
agent_id: String(props.agentId),
|
||||
url: props.file.url,
|
||||
name: props.file.name || '',
|
||||
});
|
||||
return `/api/chat/preview-file-raw?${params.toString()}`;
|
||||
const needsOfficeFallback = computed(() => {
|
||||
// We no longer need fallback for doc/ppt/pptx because they are converted to PDF by the backend
|
||||
return false;
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -135,16 +94,11 @@ const previewRawUrl = computed(() => {
|
||||
*/
|
||||
const iframeSrc = computed(() => {
|
||||
if (!props.file?.url || !props.agentId) return '';
|
||||
if (ext.value === 'pdf') {
|
||||
// PDF must use authenticated fetched blob URL; do not fallback to raw URL.
|
||||
if (['pdf', 'doc', 'ppt', 'pptx'].includes(ext.value)) {
|
||||
// These formats must use the authenticated fetch -> blob flow only.
|
||||
// Never fallback to a protected iframe URL, otherwise it returns Unauthorized.
|
||||
return officeObjectUrl.value;
|
||||
}
|
||||
if (isOfficeDoc.value && canUseOfficeOnlineViewer.value) {
|
||||
return `https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(previewRawUrl.value)}`;
|
||||
}
|
||||
if (['doc', 'docx'].includes(ext.value)) {
|
||||
return previewRawUrl.value;
|
||||
}
|
||||
return props.file.url;
|
||||
});
|
||||
|
||||
@@ -244,13 +198,18 @@ const loadPdfContent = async () => {
|
||||
}
|
||||
|
||||
const buf = await res.arrayBuffer();
|
||||
const head = new TextDecoder().decode(buf.slice(0, 5));
|
||||
if (head !== '%PDF-') {
|
||||
throw new Error('Invalid PDF binary from preview API');
|
||||
}
|
||||
// const head = new TextDecoder().decode(buf.slice(0, 5));
|
||||
// 暂时取消对 PDF header 的强校验,因为 docx 等文件也会调用该函数生成 Blob 并渲染在 iframe 或下载中
|
||||
// if (head !== '%PDF-') {
|
||||
// throw new Error('Invalid PDF binary from preview API');
|
||||
// }
|
||||
|
||||
clearOfficeObjectUrl();
|
||||
const blob = new Blob([buf], { type: 'application/pdf' });
|
||||
let type = 'application/octet-stream';
|
||||
if (ext.value === 'pdf' || ['doc', 'ppt', 'pptx'].includes(ext.value)) type = 'application/pdf';
|
||||
else if (ext.value === 'docx') type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
||||
|
||||
const blob = new Blob([buf], { type });
|
||||
officeObjectUrl.value = URL.createObjectURL(blob);
|
||||
};
|
||||
|
||||
@@ -272,13 +231,17 @@ const loadContent = async () => {
|
||||
if (!props.file?.url) return;
|
||||
resolvedMode.value = preferredMode.value;
|
||||
|
||||
if (ext.value === 'pdf') {
|
||||
if (['pdf', 'doc', 'docx', 'ppt', 'pptx'].includes(ext.value) && preferredMode.value === 'iframe') {
|
||||
if (needsOfficeFallback.value) {
|
||||
resolvedMode.value = 'iframe';
|
||||
return;
|
||||
}
|
||||
isLoading.value = true;
|
||||
try {
|
||||
await loadPdfContent();
|
||||
await loadPdfContent(); // This fetches as Blob, and sets officeObjectUrl which will be used in iframeSrc
|
||||
resolvedMode.value = 'iframe';
|
||||
} catch (e: any) {
|
||||
error.value = e?.message || 'Failed to preview pdf';
|
||||
error.value = e?.message || `Failed to preview ${ext.value}`;
|
||||
resolvedMode.value = 'iframe';
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
@@ -286,7 +249,7 @@ const loadContent = async () => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (preferredMode.value === 'office') {
|
||||
if (preferredMode.value === 'office' && !needsOfficeFallback.value) {
|
||||
isLoading.value = true;
|
||||
try {
|
||||
await loadOfficeContent();
|
||||
@@ -469,14 +432,14 @@ watch(
|
||||
<img :src="file.url" :alt="file.name" class="max-w-full max-h-full object-contain" />
|
||||
</div>
|
||||
<div
|
||||
v-else-if="ext === 'pdf' && officeObjectUrl"
|
||||
v-else-if="['pdf', 'doc', 'ppt', 'pptx'].includes(ext) && officeObjectUrl"
|
||||
class="h-full rounded-lg border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 overflow-hidden"
|
||||
>
|
||||
<embed
|
||||
<iframe
|
||||
:src="officeObjectUrl"
|
||||
type="application/pdf"
|
||||
class="w-full h-full min-h-[480px]"
|
||||
/>
|
||||
class="w-full h-full min-h-[480px] border-0"
|
||||
allow="clipboard-read; clipboard-write"
|
||||
></iframe>
|
||||
</div>
|
||||
<div
|
||||
v-else-if="needsOfficeFallback"
|
||||
@@ -489,20 +452,18 @@ watch(
|
||||
This Office file uses a private or non-HTTPS link, so online preview cannot render it.
|
||||
</div>
|
||||
<div class="mt-4 flex gap-3">
|
||||
<a
|
||||
:href="previewRawUrl || file.url"
|
||||
target="_blank"
|
||||
class="px-3 py-2 rounded-lg bg-brand-600 text-white text-sm hover:bg-brand-700"
|
||||
<button
|
||||
type="button"
|
||||
@click="downloadFile"
|
||||
:disabled="isDownloading"
|
||||
class="px-4 py-2 rounded-lg bg-brand-600 text-white text-sm hover:bg-brand-700 disabled:opacity-50 disabled:cursor-not-allowed inline-flex items-center"
|
||||
>
|
||||
Open in new tab
|
||||
</a>
|
||||
<a
|
||||
:href="previewRawUrl || file.url"
|
||||
download
|
||||
class="px-3 py-2 rounded-lg border border-slate-300 dark:border-slate-600 text-sm text-slate-700 dark:text-slate-200 hover:bg-slate-50 dark:hover:bg-slate-800"
|
||||
>
|
||||
Download file
|
||||
</a>
|
||||
<svg v-if="isDownloading" class="animate-spin -ml-1 mr-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
|
||||
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
{{ isDownloading ? '下载中...' : '下载文件' }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<iframe
|
||||
|
||||
Reference in new issue
Block a user