fix: 支持原文件下载,同时将postgres地址由localhost变更为8.152.207.171

This commit is contained in:
sophie committed 2026-04-03 11:30:49 +08:00
1 parent 70f95e9f5c
commit c380248502
12 files changed
+1273 -273

No files matched your search

+97 -1
View File
@@ -1,4 +1,13 @@
<script setup lang="ts">
/**
* FilePreviewModal.vue
* 文件用途:文件预览弹窗,用于在聊天页预览来源文件内容
* 引用文件:vue, marked
* 逻辑关系:
* - 接收 ChatView 传入的 file(name/url)与弹窗显隐状态
* - 根据扩展名选择预览模式(markdown/image/iframe)
* - 对 Office 文档在可公网 HTTPS 访问时走 Office 在线预览,否则降级为打开/下载
*/
import { computed, ref, watch } from 'vue';
import { marked } from 'marked';
@@ -20,6 +29,11 @@ const isLoading = ref(false);
const error = ref('');
const markdownHtml = ref('');
/**
* Extract extension from URL or filename.
* - Drops query string first (for signed URLs).
* - Returns lowercase extension without dot.
*/
const getExt = (raw: string) => {
const clean = (raw.split('?')[0] ?? '').toLowerCase();
const i = clean.lastIndexOf('.');
@@ -40,9 +54,50 @@ const preferredMode = computed<'markdown' | 'image' | 'iframe'>(() => {
});
const resolvedMode = ref<'markdown' | 'image' | 'iframe'>('iframe');
const isOfficeDoc = computed(() => ['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx'].includes(ext.value));
/**
* Decide whether Office Online Viewer can be used safely.
* Requirements (practical):
* - Office-like file type.
* - HTTPS URL.
* - Publicly reachable host (not localhost/private subnet).
*/
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(() => isOfficeDoc.value && !canUseOfficeOnlineViewer.value);
/**
* Compute iframe source.
* - Office docs with public HTTPS: use Office Online embed endpoint.
* - Otherwise: fallback to raw file URL (browser/native handling).
*/
const iframeSrc = computed(() => {
if (!props.file?.url) return '';
if (['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx'].includes(ext.value)) {
if (isOfficeDoc.value && canUseOfficeOnlineViewer.value) {
return `https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(props.file.url)}`;
}
return props.file.url;
@@ -50,6 +105,14 @@ const iframeSrc = computed(() => {
const close = () => emit('update:modelValue', false);
/**
* Load preview content when modal/file changes.
* Strategy:
* 1) Reset state and choose preferred mode by extension.
* 2) Only actively fetch for markdown/text mode.
* 3) Auto-switch mode by response content-type (image/binary/text).
* 4) Parse markdown to HTML on success; set error on failure.
*/
const loadContent = async () => {
error.value = '';
markdownHtml.value = '';
@@ -87,6 +150,12 @@ const loadContent = async () => {
}
};
/**
* Reactively reload preview when:
* - modal visibility changes
* - file URL changes
* - file name changes
*/
watch(
() => [props.modelValue, props.file?.url, props.file?.name],
() => {
@@ -121,6 +190,33 @@ watch(
<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" />
</div>
<div
v-else-if="needsOfficeFallback"
class="h-full rounded-lg border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 p-6"
>
<!-- Graceful fallback for private/non-HTTPS Office links:
show actions instead of an empty iframe. -->
<div class="text-sm text-slate-700 dark:text-slate-200 font-semibold">Preview not available for this file URL</div>
<div class="mt-2 text-sm text-slate-500 dark:text-slate-400">
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="file.url"
target="_blank"
class="px-3 py-2 rounded-lg bg-brand-600 text-white text-sm hover:bg-brand-700"
>
Open in new tab
</a>
<a
:href="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>
</div>
</div>
<iframe
v-else
:src="iframeSrc"