mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-17 05:07:23 +08:00
Consolidateion of document upload API (#14106)
### What problem does this PR solve? Consolidation WEB API & HTTP API for document upload Before consolidation Web API: POST /v1/document/upload Http API - POST /api/v1/datasets/<dataset_id>/documents After consolidation, Restful API -- POST /api/v1/datasets/<dataset_id>/documents ### Type of change - [x] Refactoring
This commit is contained in:
@@ -19,6 +19,7 @@ import { EMPTY_METADATA_FIELD } from '@/pages/dataset/dataset/use-select-filters
|
||||
import kbService, {
|
||||
listDocument,
|
||||
renameDocument,
|
||||
uploadDocument,
|
||||
} from '@/services/knowledge-service';
|
||||
import api, { restAPIv1, webAPI } from '@/utils/api';
|
||||
import { getSearchValue } from '@/utils/common-util';
|
||||
@@ -66,22 +67,24 @@ export const useUploadNextDocument = () => {
|
||||
} = useMutation<ResponseType<IDocumentInfo[]>, Error, File[]>({
|
||||
mutationKey: [DocumentApiAction.UploadDocument],
|
||||
mutationFn: async (fileList) => {
|
||||
if (!id) {
|
||||
return { code: 500, message: 'Dataset ID is required' };
|
||||
}
|
||||
const formData = new FormData();
|
||||
formData.append('kb_id', id!);
|
||||
fileList.forEach((file: any) => {
|
||||
formData.append('file', file);
|
||||
});
|
||||
|
||||
try {
|
||||
const ret = await kbService.documentUpload(formData);
|
||||
const code = get(ret, 'data.code');
|
||||
const ret = await uploadDocument(id, formData);
|
||||
const code = get(ret, 'code');
|
||||
|
||||
if (code === 0 || code === 500) {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [DocumentApiAction.FetchDocumentList],
|
||||
});
|
||||
}
|
||||
return ret?.data;
|
||||
return ret;
|
||||
} catch (error) {
|
||||
console.warn(error);
|
||||
return {
|
||||
|
||||
@@ -20,29 +20,36 @@ export const useHandleUploadDocument = () => {
|
||||
async ({ fileList, parseOnCreation }: UploadFormSchemaType) => {
|
||||
if (fileList.length > 0) {
|
||||
const ret = await uploadDocument(fileList);
|
||||
if (typeof ret?.message !== 'string') {
|
||||
|
||||
// Check for success (code === 0) or partial success (code === 500 with some files)
|
||||
const isSuccess = ret?.code === 0;
|
||||
const isPartialSuccess = ret?.code === 500 && ret?.message;
|
||||
|
||||
if (!isSuccess && !isPartialSuccess) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ret.code === 0 && parseOnCreation) {
|
||||
if (isSuccess && parseOnCreation) {
|
||||
runDocumentByIds({
|
||||
documentIds: ret.data.map((x) => x.id),
|
||||
documentIds: ret.data.map((x: any) => x.id),
|
||||
run: 1,
|
||||
shouldDelete: false,
|
||||
});
|
||||
}
|
||||
|
||||
const count = getUnSupportedFilesCount(ret?.message);
|
||||
/// 500 error code indicates that some file types are not supported
|
||||
let code = ret?.code;
|
||||
if (
|
||||
ret?.code === 0 ||
|
||||
(ret?.code === 500 && count !== fileList.length) // Some files were not uploaded successfully, but some were uploaded successfully.
|
||||
) {
|
||||
code = 0;
|
||||
if (isSuccess) {
|
||||
hideDocumentUploadModal();
|
||||
return 0;
|
||||
}
|
||||
return code;
|
||||
|
||||
// For partial success (code 500), check if any files were uploaded
|
||||
const count = getUnSupportedFilesCount(ret?.message);
|
||||
if (count !== fileList.length) {
|
||||
hideDocumentUploadModal();
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ret?.code;
|
||||
}
|
||||
},
|
||||
[uploadDocument, runDocumentByIds, hideDocumentUploadModal],
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Authorization } from '@/constants/authorization';
|
||||
import { IRenameTag } from '@/interfaces/database/knowledge';
|
||||
import {
|
||||
IFetchDocumentListRequestBody,
|
||||
@@ -5,8 +6,10 @@ import {
|
||||
} from '@/interfaces/request/knowledge';
|
||||
import { ProcessingType } from '@/pages/dataset/dataset-overview/dataset-common';
|
||||
import api from '@/utils/api';
|
||||
import { getAuthorization } from '@/utils/authorization-util';
|
||||
import registerServer from '@/utils/register-server';
|
||||
import request, { post } from '@/utils/request';
|
||||
import axios from 'axios';
|
||||
|
||||
const {
|
||||
createKb,
|
||||
@@ -246,6 +249,17 @@ export const listDocument = (
|
||||
export const documentFilter = (kb_id: string) =>
|
||||
request.post(api.getDatasetFilter, { kb_id });
|
||||
|
||||
// Custom upload function that handles dynamic URL using axios directly
|
||||
export const uploadDocument = async (datasetId: string, formData: FormData) => {
|
||||
const url = api.documentUpload(datasetId);
|
||||
const response = await axios.post(url, formData, {
|
||||
headers: {
|
||||
[Authorization]: getAuthorization(),
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const renameDocument = (
|
||||
datasetId: string,
|
||||
documentId: string,
|
||||
|
||||
@@ -119,7 +119,8 @@ export default {
|
||||
getDocumentFile: `${webAPI}/document/get`,
|
||||
getDocumentFileDownload: (docId: string) =>
|
||||
`${webAPI}/document/download/${docId}`,
|
||||
documentUpload: `${webAPI}/document/upload`,
|
||||
documentUpload: (datasetId: string) =>
|
||||
`${restAPIv1}/datasets/${datasetId}/documents`,
|
||||
webCrawl: `${webAPI}/document/web_crawl`,
|
||||
documentInfos: `${webAPI}/document/infos`,
|
||||
uploadAndParse: `${webAPI}/document/upload_info`,
|
||||
|
||||
Reference in New Issue
Block a user