mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Feat: Upload document #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import fileManagerService from '@/services/file-manager-service';
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { message } from 'antd';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useSetPaginationParams } from './route-hook';
|
|
|
|
export const enum FileApiAction {
|
|
UploadFile = 'uploadFile',
|
|
FetchFileList = 'fetchFileList',
|
|
}
|
|
|
|
export const useUploadFile = () => {
|
|
const { setPaginationParams } = useSetPaginationParams();
|
|
const { t } = useTranslation();
|
|
const queryClient = useQueryClient();
|
|
const {
|
|
data,
|
|
isPending: loading,
|
|
mutateAsync,
|
|
} = useMutation({
|
|
mutationKey: [FileApiAction.UploadFile],
|
|
mutationFn: async (params: { fileList: File[]; parentId: string }) => {
|
|
const fileList = params.fileList;
|
|
const pathList = params.fileList.map(
|
|
(file) => (file as any).webkitRelativePath,
|
|
);
|
|
const formData = new FormData();
|
|
formData.append('parent_id', params.parentId);
|
|
fileList.forEach((file: any, index: number) => {
|
|
formData.append('file', file);
|
|
formData.append('path', pathList[index]);
|
|
});
|
|
try {
|
|
const ret = await fileManagerService.uploadFile(formData);
|
|
if (ret?.data.code === 0) {
|
|
message.success(t('message.uploaded'));
|
|
setPaginationParams(1);
|
|
queryClient.invalidateQueries({
|
|
queryKey: [FileApiAction.FetchFileList],
|
|
});
|
|
}
|
|
return ret?.data?.code;
|
|
} catch (error) {}
|
|
},
|
|
});
|
|
|
|
return { data, loading, uploadFile: mutateAsync };
|
|
};
|