From 5da6c464b9f19cede3cd385678c10bf7845327d8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-JKIE562\\PC" <1013397054@qq.com> Date: Tue, 12 May 2026 12:09:00 +0800 Subject: [PATCH] pdf/doc --- backend/app/api/chat.py | 10 +++++----- frontend/src/components/FilePreviewModal.vue | 8 +++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/app/api/chat.py b/backend/app/api/chat.py index 3c67729d..8e9fd1c9 100644 --- a/backend/app/api/chat.py +++ b/backend/app/api/chat.py @@ -62,7 +62,7 @@ def _is_allowed_preview_url(target_url: str) -> bool: return False -def _fetch_preview_source_bytes(file_url: str): +def _fetch_preview_source_bytes(file_url: str, max_size: int | None = 15 * 1024 * 1024): """ 从上游文件 URL 拉取二进制内容(供预览接口复用)。 如果 file_url 是内部 integration API 下载链接且包含过期 token,则尝试重新获取最新链接。 @@ -178,8 +178,7 @@ def _fetch_preview_source_bytes(file_url: str): return None, None, (f'upstream returned status {upstream.status_code}', 502) content = upstream.content or b'' - max_size = 15 * 1024 * 1024 - if len(content) > max_size: + if max_size is not None and len(content) > max_size: return None, None, ('file too large for online preview', 413) content_type = (upstream.headers.get('content-type') or '').split(';')[0].strip().lower() @@ -717,7 +716,9 @@ def preview_file_raw(): name = (request.args.get('name') or '').strip() name_ext = (name.split('.')[-1] if '.' in name else '').lower() ext = url_path_ext or name_ext - content, content_type, err = _fetch_preview_source_bytes(file_url) + is_download = request.args.get('download') == '1' + max_size = None if is_download else 15 * 1024 * 1024 + content, content_type, err = _fetch_preview_source_bytes(file_url, max_size=max_size) if err: msg, code = err return jsonify({'error': msg}), code @@ -755,7 +756,6 @@ def preview_file_raw(): encoded_filename = quote(filename) ascii_filename = filename.encode('ascii', 'ignore').decode('ascii') or f'preview.{ext or "bin"}' - is_download = request.args.get('download') == '1' disposition_type = 'attachment' if is_download else 'inline' resp = Response(content, status=200, mimetype=mime) diff --git a/frontend/src/components/FilePreviewModal.vue b/frontend/src/components/FilePreviewModal.vue index ae914505..247d7368 100644 --- a/frontend/src/components/FilePreviewModal.vue +++ b/frontend/src/components/FilePreviewModal.vue @@ -105,6 +105,7 @@ const iframeSrc = computed(() => { const close = () => emit('update:modelValue', false); const isDownloading = ref(false); +const previewTooLarge = computed(() => /file too large for online preview/i.test(error.value)); const downloadFile = async () => { if (!props.file?.url || !props.agentId) return; @@ -373,7 +374,12 @@ watch(