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)
This commit is contained in:
49
web/src/hooks/use-document-request.ts
Normal file
49
web/src/hooks/use-document-request.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import kbService from '@/services/knowledge-service';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { get } from 'lodash';
|
||||
import { useParams } from 'umi';
|
||||
|
||||
export const enum DocumentApiAction {
|
||||
UploadDocument = 'uploadDocument',
|
||||
FetchDocumentList = 'fetchDocumentList',
|
||||
}
|
||||
|
||||
export const useUploadNextDocument = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { id } = useParams();
|
||||
|
||||
const {
|
||||
data,
|
||||
isPending: loading,
|
||||
mutateAsync,
|
||||
} = useMutation({
|
||||
mutationKey: [DocumentApiAction.UploadDocument],
|
||||
mutationFn: async (fileList: File[]) => {
|
||||
const formData = new FormData();
|
||||
formData.append('kb_id', id!);
|
||||
fileList.forEach((file: any) => {
|
||||
formData.append('file', file);
|
||||
});
|
||||
|
||||
try {
|
||||
const ret = await kbService.document_upload(formData);
|
||||
const code = get(ret, 'data.code');
|
||||
|
||||
if (code === 0 || code === 500) {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [DocumentApiAction.FetchDocumentList],
|
||||
});
|
||||
}
|
||||
return ret?.data;
|
||||
} catch (error) {
|
||||
console.warn(error);
|
||||
return {
|
||||
code: 500,
|
||||
message: error + '',
|
||||
};
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return { uploadDocument: mutateAsync, loading, data };
|
||||
};
|
||||
48
web/src/hooks/use-file-request.ts
Normal file
48
web/src/hooks/use-file-request.ts
Normal file
@ -0,0 +1,48 @@
|
||||
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 };
|
||||
};
|
||||
Reference in New Issue
Block a user