mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-06-03 02:43:59 +08:00
### What problem does this PR solve? fix: display specific error message when previewing file error #868 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
parent
3cae87a902
commit
b62a20816e
@ -5,7 +5,7 @@ import { useFetchDocx } from '../hooks';
|
|||||||
import styles from './index.less';
|
import styles from './index.less';
|
||||||
|
|
||||||
const Docx = ({ filePath }: { filePath: string }) => {
|
const Docx = ({ filePath }: { filePath: string }) => {
|
||||||
const { succeed, containerRef } = useFetchDocx(filePath);
|
const { succeed, containerRef, error } = useFetchDocx(filePath);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -16,7 +16,7 @@ const Docx = ({ filePath }: { filePath: string }) => {
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
) : (
|
) : (
|
||||||
<FileError></FileError>
|
<FileError>{error}</FileError>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
@ -3,7 +3,7 @@ import FileError from '../file-error';
|
|||||||
import { useFetchExcel } from '../hooks';
|
import { useFetchExcel } from '../hooks';
|
||||||
|
|
||||||
const Excel = ({ filePath }: { filePath: string }) => {
|
const Excel = ({ filePath }: { filePath: string }) => {
|
||||||
const { status, containerRef } = useFetchExcel(filePath);
|
const { status, containerRef, error } = useFetchExcel(filePath);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@ -11,7 +11,7 @@ const Excel = ({ filePath }: { filePath: string }) => {
|
|||||||
ref={containerRef}
|
ref={containerRef}
|
||||||
style={{ height: '100%', width: '100%' }}
|
style={{ height: '100%', width: '100%' }}
|
||||||
>
|
>
|
||||||
{status || <FileError></FileError>}
|
{status || <FileError>{error}</FileError>}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -3,18 +3,38 @@ import axios from 'axios';
|
|||||||
import mammoth from 'mammoth';
|
import mammoth from 'mammoth';
|
||||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
const useFetchDocument = () => {
|
export const useCatchError = (api: string) => {
|
||||||
const fetchDocument = useCallback((api: string) => {
|
const [error, setError] = useState('');
|
||||||
return axios.get(api, { responseType: 'arraybuffer' });
|
const fetchDocument = useCallback(async () => {
|
||||||
|
const ret = await axios.get(api);
|
||||||
|
const { data } = ret;
|
||||||
|
if (!(data instanceof ArrayBuffer) && data.retcode !== 0) {
|
||||||
|
setError(data.retmsg);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}, [api]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchDocument();
|
||||||
|
}, [fetchDocument]);
|
||||||
|
|
||||||
|
return { fetchDocument, error };
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useFetchDocument = () => {
|
||||||
|
const fetchDocument = useCallback(async (api: string) => {
|
||||||
|
const ret = await axios.get(api, { responseType: 'arraybuffer' });
|
||||||
|
return ret;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return fetchDocument;
|
return { fetchDocument };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useFetchExcel = (filePath: string) => {
|
export const useFetchExcel = (filePath: string) => {
|
||||||
const [status, setStatus] = useState(true);
|
const [status, setStatus] = useState(true);
|
||||||
const fetchDocument = useFetchDocument();
|
const { fetchDocument } = useFetchDocument();
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
|
const { error } = useCatchError(filePath);
|
||||||
|
|
||||||
const fetchDocumentAsync = useCallback(async () => {
|
const fetchDocumentAsync = useCallback(async () => {
|
||||||
let myExcelPreviewer;
|
let myExcelPreviewer;
|
||||||
@ -39,13 +59,14 @@ export const useFetchExcel = (filePath: string) => {
|
|||||||
fetchDocumentAsync();
|
fetchDocumentAsync();
|
||||||
}, [fetchDocumentAsync]);
|
}, [fetchDocumentAsync]);
|
||||||
|
|
||||||
return { status, containerRef };
|
return { status, containerRef, error };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useFetchDocx = (filePath: string) => {
|
export const useFetchDocx = (filePath: string) => {
|
||||||
const [succeed, setSucceed] = useState(true);
|
const [succeed, setSucceed] = useState(true);
|
||||||
const fetchDocument = useFetchDocument();
|
const { fetchDocument } = useFetchDocument();
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
|
const { error } = useCatchError(filePath);
|
||||||
|
|
||||||
const fetchDocumentAsync = useCallback(async () => {
|
const fetchDocumentAsync = useCallback(async () => {
|
||||||
const jsonFile = await fetchDocument(filePath);
|
const jsonFile = await fetchDocument(filePath);
|
||||||
@ -64,9 +85,8 @@ export const useFetchDocx = (filePath: string) => {
|
|||||||
container.innerHTML = docEl.outerHTML;
|
container.innerHTML = docEl.outerHTML;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((a) => {
|
.catch(() => {
|
||||||
setSucceed(false);
|
setSucceed(false);
|
||||||
console.warn('alexei: something went wrong', a);
|
|
||||||
});
|
});
|
||||||
}, [filePath, fetchDocument]);
|
}, [filePath, fetchDocument]);
|
||||||
|
|
||||||
@ -74,5 +94,5 @@ export const useFetchDocx = (filePath: string) => {
|
|||||||
fetchDocumentAsync();
|
fetchDocumentAsync();
|
||||||
}, [fetchDocumentAsync]);
|
}, [fetchDocumentAsync]);
|
||||||
|
|
||||||
return { succeed, containerRef };
|
return { succeed, containerRef, error };
|
||||||
};
|
};
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
import { Skeleton } from 'antd';
|
import { Skeleton } from 'antd';
|
||||||
import { PdfHighlighter, PdfLoader } from 'react-pdf-highlighter';
|
import { PdfHighlighter, PdfLoader } from 'react-pdf-highlighter';
|
||||||
import FileError from '../file-error';
|
import FileError from '../file-error';
|
||||||
|
import { useCatchError } from '../hooks';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
url: string;
|
url: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DocumentPreviewer = ({ url }: IProps) => {
|
const PdfPreviewer = ({ url }: IProps) => {
|
||||||
|
const { error } = useCatchError(url);
|
||||||
const resetHash = () => {};
|
const resetHash = () => {};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -15,7 +17,7 @@ const DocumentPreviewer = ({ url }: IProps) => {
|
|||||||
url={url}
|
url={url}
|
||||||
beforeLoad={<Skeleton active />}
|
beforeLoad={<Skeleton active />}
|
||||||
workerSrc="/pdfjs-dist/pdf.worker.min.js"
|
workerSrc="/pdfjs-dist/pdf.worker.min.js"
|
||||||
errorMessage={<FileError></FileError>}
|
errorMessage={<FileError>{error}</FileError>}
|
||||||
onError={(e) => {
|
onError={(e) => {
|
||||||
console.warn(e);
|
console.warn(e);
|
||||||
}}
|
}}
|
||||||
@ -40,4 +42,4 @@ const DocumentPreviewer = ({ url }: IProps) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default DocumentPreviewer;
|
export default PdfPreviewer;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user