doc preview not available

This commit is contained in:
Billy Bao 2025-12-03 11:55:42 +08:00
parent 5c81e01de5
commit ca1c763f50
2 changed files with 42 additions and 8 deletions

View file

@ -173,8 +173,8 @@ def install_mineru() -> None:
Logging is used to indicate status. Logging is used to indicate status.
""" """
# Check if MinerU is enabled # Check if MinerU is enabled
use_mineru = os.getenv("USE_MINERU", "").strip().lower() use_mineru = os.getenv("USE_MINERU", "false").strip().lower()
if use_mineru == "false": if use_mineru != "true":
logging.info("USE_MINERU=%r. Skipping MinerU installation.", use_mineru) logging.info("USE_MINERU=%r. Skipping MinerU installation.", use_mineru)
return return

View file

@ -1,7 +1,5 @@
import message from '@/components/ui/message'; import message from '@/components/ui/message';
import { Spin } from '@/components/ui/spin'; import { Spin } from '@/components/ui/spin';
import { Authorization } from '@/constants/authorization';
import { getAuthorization } from '@/utils/authorization-util';
import request from '@/utils/request'; import request from '@/utils/request';
import classNames from 'classnames'; import classNames from 'classnames';
import mammoth from 'mammoth'; import mammoth from 'mammoth';
@ -16,22 +14,55 @@ export const DocPreviewer: React.FC<DocPreviewerProps> = ({
className, className,
url, url,
}) => { }) => {
// const url = useGetDocumentUrl();
const [htmlContent, setHtmlContent] = useState<string>(''); const [htmlContent, setHtmlContent] = useState<string>('');
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const fetchDocument = async () => { const fetchDocument = async () => {
if (!url) return;
setLoading(true); setLoading(true);
const res = await request(url, { const res = await request(url, {
method: 'GET', method: 'GET',
responseType: 'blob', responseType: 'blob',
headers: { [Authorization]: getAuthorization() },
onError: () => { onError: () => {
message.error('Document parsing failed'); message.error('Document parsing failed');
console.error('Error loading document:', url); console.error('Error loading document:', url);
}, },
}); });
try { 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( const result = await mammoth.convertToHtml(
{ arrayBuffer }, { arrayBuffer },
{ includeDefaultStyleMap: true }, { includeDefaultStyleMap: true },
@ -43,10 +74,12 @@ export const DocPreviewer: React.FC<DocPreviewerProps> = ({
setHtmlContent(styledContent); setHtmlContent(styledContent);
} catch (err) { } catch (err) {
// Only errors from the mammoth conversion path should surface here
message.error('Document parsing failed'); message.error('Document parsing failed');
console.error('Error parsing document:', err); console.error('Error parsing document:', err);
} finally {
setLoading(false);
} }
setLoading(false);
}; };
useEffect(() => { useEffect(() => {
@ -54,6 +87,7 @@ export const DocPreviewer: React.FC<DocPreviewerProps> = ({
fetchDocument(); fetchDocument();
} }
}, [url]); }, [url]);
return ( return (
<div <div
className={classNames( className={classNames(