From 6fc7def5623799841a2ecd737d311c35599f9bd5 Mon Sep 17 00:00:00 2001 From: Billy Bao Date: Wed, 3 Dec 2025 12:22:01 +0800 Subject: [PATCH] Feat: optimize the information displayed when .doc preview is unavailable (#11684) ### What problem does this PR solve? Feat: optimize the information displayed when .doc preview is unavailable #11605 ### Type of change - [X] New Feature (non-breaking change which adds functionality) #### Performance (Before) image #### Performance (After) ![img_v3_02sk_c0fcaf74-4a26-4b6c-b0e0-8f8929426d9g](https://github.com/user-attachments/assets/8c8eea3e-2c8e-457c-ab2b-5ef205806f42) --- common/misc_utils.py | 4 +- .../document-preview/doc-preview.tsx | 46 ++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/common/misc_utils.py b/common/misc_utils.py index daebf4c8c..694b8b494 100644 --- a/common/misc_utils.py +++ b/common/misc_utils.py @@ -173,8 +173,8 @@ def install_mineru() -> None: Logging is used to indicate status. """ # Check if MinerU is enabled - use_mineru = os.getenv("USE_MINERU", "").strip().lower() - if use_mineru == "false": + use_mineru = os.getenv("USE_MINERU", "false").strip().lower() + if use_mineru != "true": logging.info("USE_MINERU=%r. Skipping MinerU installation.", use_mineru) return diff --git a/web/src/components/document-preview/doc-preview.tsx b/web/src/components/document-preview/doc-preview.tsx index 470765f6e..5f8dd0cf9 100644 --- a/web/src/components/document-preview/doc-preview.tsx +++ b/web/src/components/document-preview/doc-preview.tsx @@ -1,7 +1,5 @@ import message from '@/components/ui/message'; import { Spin } from '@/components/ui/spin'; -import { Authorization } from '@/constants/authorization'; -import { getAuthorization } from '@/utils/authorization-util'; import request from '@/utils/request'; import classNames from 'classnames'; import mammoth from 'mammoth'; @@ -16,22 +14,55 @@ export const DocPreviewer: React.FC = ({ className, url, }) => { - // const url = useGetDocumentUrl(); const [htmlContent, setHtmlContent] = useState(''); const [loading, setLoading] = useState(false); + const fetchDocument = async () => { + if (!url) return; + setLoading(true); + const res = await request(url, { method: 'GET', responseType: 'blob', - headers: { [Authorization]: getAuthorization() }, onError: () => { message.error('Document parsing failed'); console.error('Error loading document:', url); }, }); + try { - const arrayBuffer = await res.data.arrayBuffer(); + const blob: Blob = res.data; + const contentType: string = + blob.type || (res as any).headers?.['content-type'] || ''; + + // ---- Detect legacy .doc via MIME or URL ---- + const cleanUrl = url.split(/[?#]/)[0].toLowerCase(); + const isDocMime = /application\/msword/i.test(contentType); + const isLegacyDocByUrl = + cleanUrl.endsWith('.doc') && !cleanUrl.endsWith('.docx'); + const isLegacyDoc = isDocMime || isLegacyDocByUrl; + + if (isLegacyDoc) { + // Do not call mammoth and do not throw an error; instead, show a note in the preview area + setHtmlContent(` +
+
+

+ Preview not available for .doc files +

+

+ Mammoth does not support .doc documents.
+ Inline preview is unavailable. +

+
+
+ `); + return; + } + + // ---- Standard .docx preview path ---- + const arrayBuffer = await blob.arrayBuffer(); const result = await mammoth.convertToHtml( { arrayBuffer }, { includeDefaultStyleMap: true }, @@ -43,10 +74,12 @@ export const DocPreviewer: React.FC = ({ setHtmlContent(styledContent); } catch (err) { + // Only errors from the mammoth conversion path should surface here message.error('Document parsing failed'); console.error('Error parsing document:', err); + } finally { + setLoading(false); } - setLoading(false); }; useEffect(() => { @@ -54,6 +87,7 @@ export const DocPreviewer: React.FC = ({ fetchDocument(); } }, [url]); + return (