chat中源文件没用的问题

This commit is contained in:
runny committed 2026-05-19 04:23:38 +08:00
1 parent 3d4b2647ae
commit b745a9c803
6 files changed
+490 -99

No files matched your search

+46 -13
View File
@@ -93,7 +93,9 @@ const needsOfficeFallback = computed(() => {
* - 其余返回原始 URL,由浏览器自行处理。
*/
const iframeSrc = computed(() => {
if (!props.file?.url || !props.agentId) return '';
if (!props.file?.url) return '';
if (officeObjectUrl.value) return officeObjectUrl.value;
if (!props.agentId) return '';
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.
@@ -102,6 +104,8 @@ const iframeSrc = computed(() => {
return props.file.url;
});
const imageSrc = computed(() => officeObjectUrl.value || props.file?.url || '');
const close = () => emit('update:modelValue', false);
const isDownloading = ref(false);
@@ -198,19 +202,34 @@ const loadPdfContent = async () => {
throw new Error(err?.error || `HTTP ${res.status}`);
}
const buf = await res.arrayBuffer();
// 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');
// }
const blob = await res.blob();
clearOfficeObjectUrl();
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';
let fallbackType = 'application/octet-stream';
if (ext.value === 'pdf' || ['doc', 'ppt', 'pptx'].includes(ext.value)) fallbackType = 'application/pdf';
else if (ext.value === 'docx') fallbackType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
const effectiveBlob = blob.type ? blob : new Blob([blob], { type: fallbackType });
officeObjectUrl.value = URL.createObjectURL(effectiveBlob);
};
const blob = new Blob([buf], { type });
const loadBinaryContent = async () => {
if (!props.file?.url) return;
if (!props.agentId) throw new Error('agent_id is required for binary preview');
const params = new URLSearchParams({
agent_id: String(props.agentId),
url: props.file.url,
name: props.file.name || '',
});
const res = await fetch(`/api/chat/preview-file-raw?${params.toString()}`, {
headers: props.authHeaders || {},
});
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err?.error || `HTTP ${res.status}`);
}
const blob = await res.blob();
clearOfficeObjectUrl();
officeObjectUrl.value = URL.createObjectURL(blob);
};
@@ -297,6 +316,20 @@ const loadContent = async () => {
return;
}
if (preferredMode.value === 'image' || (preferredMode.value === 'iframe' && !/^https?:\/\//i.test(props.file.url || ''))) {
isLoading.value = true;
try {
await loadBinaryContent();
resolvedMode.value = preferredMode.value === 'image' ? 'image' : 'iframe';
} catch (e: any) {
error.value = e?.message || 'Failed to load file preview';
resolvedMode.value = preferredMode.value === 'image' ? 'image' : 'iframe';
} finally {
isLoading.value = false;
}
return;
}
if (preferredMode.value !== 'markdown') return;
isLoading.value = true;
@@ -435,7 +468,7 @@ watch(
/>
</div>
<div v-else-if="resolvedMode === 'image'" class="h-full flex items-center justify-center bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-700 rounded-lg p-2">
<img :src="file.url" :alt="file.name" class="max-w-full max-h-full object-contain" />
<img :src="imageSrc" :alt="file.name" class="max-w-full max-h-full object-contain" />
</div>
<div
v-else-if="['pdf', 'doc', 'ppt', 'pptx'].includes(ext) && officeObjectUrl"