mirror of
https://gitee.com/infiniflow/ragflow.git
synced 2025-12-06 07:19:03 +08:00
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) <img width="700" alt="image" src="https://github.com/user-attachments/assets/15cf69ee-3698-4e18-8e8f-bb75c321334d" /> #### Performance (After) 
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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<DocPreviewerProps> = ({
|
||||
className,
|
||||
url,
|
||||
}) => {
|
||||
// const url = useGetDocumentUrl();
|
||||
const [htmlContent, setHtmlContent] = useState<string>('');
|
||||
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(`
|
||||
<div class="flex h-full items-center justify-center">
|
||||
<div class="border border-dashed border-border-normal rounded-xl p-8 max-w-2xl text-center">
|
||||
<p class="text-2xl font-bold mb-4">
|
||||
Preview not available for .doc files
|
||||
</p>
|
||||
<p class="italic text-sm text-muted-foreground leading-relaxed">
|
||||
Mammoth does not support <code>.doc</code> documents.<br/>
|
||||
Inline preview is unavailable.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
`);
|
||||
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<DocPreviewerProps> = ({
|
||||
|
||||
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<DocPreviewerProps> = ({
|
||||
fetchDocument();
|
||||
}
|
||||
}, [url]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={classNames(
|
||||
|
||||
Reference in New Issue
Block a user