Fix:Added support for preview of txt, md, excel, csv, ppt, image, doc and other files (#8712)
### What problem does this PR solve?
Added support for preview of txt, md, excel, csv, ppt, image, doc and
other files [#3221](https://github.com/infiniflow/ragflow/issues/3221)
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
2025-07-08 10:39:18 +08:00
|
|
|
import message from '@/components/ui/message';
|
|
|
|
|
import { Spin } from '@/components/ui/spin';
|
2025-11-25 19:13:00 +08:00
|
|
|
import { Authorization } from '@/constants/authorization';
|
|
|
|
|
import { getAuthorization } from '@/utils/authorization-util';
|
Fix:Added support for preview of txt, md, excel, csv, ppt, image, doc and other files (#8712)
### What problem does this PR solve?
Added support for preview of txt, md, excel, csv, ppt, image, doc and
other files [#3221](https://github.com/infiniflow/ragflow/issues/3221)
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
2025-07-08 10:39:18 +08:00
|
|
|
import request from '@/utils/request';
|
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
import mammoth from 'mammoth';
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
interface DocPreviewerProps {
|
|
|
|
|
className?: string;
|
2025-08-14 12:11:53 +08:00
|
|
|
url: string;
|
Fix:Added support for preview of txt, md, excel, csv, ppt, image, doc and other files (#8712)
### What problem does this PR solve?
Added support for preview of txt, md, excel, csv, ppt, image, doc and
other files [#3221](https://github.com/infiniflow/ragflow/issues/3221)
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
2025-07-08 10:39:18 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 12:11:53 +08:00
|
|
|
export const DocPreviewer: React.FC<DocPreviewerProps> = ({
|
|
|
|
|
className,
|
|
|
|
|
url,
|
|
|
|
|
}) => {
|
|
|
|
|
// const url = useGetDocumentUrl();
|
Fix:Added support for preview of txt, md, excel, csv, ppt, image, doc and other files (#8712)
### What problem does this PR solve?
Added support for preview of txt, md, excel, csv, ppt, image, doc and
other files [#3221](https://github.com/infiniflow/ragflow/issues/3221)
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
2025-07-08 10:39:18 +08:00
|
|
|
const [htmlContent, setHtmlContent] = useState<string>('');
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const fetchDocument = async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
const res = await request(url, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
responseType: 'blob',
|
2025-11-25 19:13:00 +08:00
|
|
|
headers: { [Authorization]: getAuthorization() },
|
Fix:Added support for preview of txt, md, excel, csv, ppt, image, doc and other files (#8712)
### What problem does this PR solve?
Added support for preview of txt, md, excel, csv, ppt, image, doc and
other files [#3221](https://github.com/infiniflow/ragflow/issues/3221)
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
2025-07-08 10:39:18 +08:00
|
|
|
onError: () => {
|
|
|
|
|
message.error('Document parsing failed');
|
|
|
|
|
console.error('Error loading document:', url);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
const arrayBuffer = await res.data.arrayBuffer();
|
|
|
|
|
const result = await mammoth.convertToHtml(
|
|
|
|
|
{ arrayBuffer },
|
|
|
|
|
{ includeDefaultStyleMap: true },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const styledContent = result.value
|
|
|
|
|
.replace(/<p>/g, '<p class="mb-2">')
|
|
|
|
|
.replace(/<h(\d)>/g, '<h$1 class="font-semibold mt-4 mb-2">');
|
|
|
|
|
|
|
|
|
|
setHtmlContent(styledContent);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
message.error('Document parsing failed');
|
|
|
|
|
console.error('Error parsing document:', err);
|
|
|
|
|
}
|
|
|
|
|
setLoading(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (url) {
|
|
|
|
|
fetchDocument();
|
|
|
|
|
}
|
|
|
|
|
}, [url]);
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={classNames(
|
|
|
|
|
'relative w-full h-full p-4 bg-background-paper border border-border-normal rounded-md',
|
|
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{loading && (
|
|
|
|
|
<div className="absolute inset-0 flex items-center justify-center">
|
|
|
|
|
<Spin />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{!loading && <div dangerouslySetInnerHTML={{ __html: htmlContent }} />}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|