Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(magent-ui knowledge): upload component use custom request #59

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ async def delete_knowledge(knowledge_id: str):

@router.post("/knowledge/upload")
async def upload_knowledge_file(knowledge_id: Annotated[str, Body()], file: UploadFile = File(...)):
print('here')
return await AsyncTask.to_thread(KnowledgeService.upload_knowledge_file, knowledge_id, file)
18 changes: 18 additions & 0 deletions web-apps/ui/src/modules/knowledge/knowledge-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,22 @@ export class KnowledgeManager {
this.cache.set(tool.id, tool);
return tool;
};

uploadFile = async (options: {
knowledge_id: string;
file: File;
}): Promise<boolean> => {
// create FormData object for multipart/form-data upload
const formData = new FormData();
formData.append('knowledge_id', options.knowledge_id); // add knowledge_id
formData.append('file', options.file); // add file

// send request via axios
const res = await this.axios.post<string>('/api/v1/knowledge/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data', // specify multipart/form-data
},
});
return Boolean(res.data);
};
}
34 changes: 27 additions & 7 deletions web-apps/ui/src/views/knowledge/upload-view.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { InboxOutlined } from '@ant-design/icons';
import { BaseView, inject, singleton, view } from '@difizen/mana-app';
import { message, Upload } from 'antd';
import {
BaseView,
inject,
singleton,
useInject,
view,
ViewInstance,
} from '@difizen/mana-app';
import { message, Upload, UploadProps } from 'antd';
import { forwardRef } from 'react';
import { history } from 'umi';
import { useParams } from 'umi';
Expand All @@ -9,6 +16,7 @@ import { MainView } from '@/modules/base-layout/main-view.js';
import type { NavigatablePage } from '@/modules/base-layout/protocol.js';

import './index.less';
import { KnowledgeManager } from '@/modules/knowledge/knowledge-manager.js';

const { Dragger } = Upload;

Expand All @@ -18,17 +26,16 @@ export const uploadslot = `${viewId}-slot`;
const KnowledgeUploadComponent = forwardRef<HTMLDivElement>(
function KnowledgeUploadComponent() {
const { knowledgeId } = useParams();

const instance = useInject<KnowledgeUploadView>(ViewInstance);

return (
<div className={`${viewId}-wrapper`}>
<Dragger
className={`${viewId}-dragger`}
name="file"
multiple
data={{
knowledge_id: knowledgeId,
}}
method="post"
action="/api/v1/knowledge/upload"
listType="picture"
onChange={(info) => {
const { status } = info.file;
Expand All @@ -38,7 +45,18 @@ const KnowledgeUploadComponent = forwardRef<HTMLDivElement>(
message.error(`${info.file.name} file upload failed.`);
}
}}
// onDrop={(e) => {}}
customRequest={async (info) => {
const { file, onSuccess, onError } = info;
const succ = await instance.manager.uploadFile({
knowledge_id: knowledgeId,
file: file as File,
});
if (succ) {
onSuccess?.('上传成功');
} else {
onError?.(new Error('上传失败'));
}
}}
>
<p className="ant-upload-drag-icon">
<InboxOutlined />
Expand All @@ -59,6 +77,8 @@ const KnowledgeUploadComponent = forwardRef<HTMLDivElement>(
export class KnowledgeUploadView extends BaseView implements NavigatablePage {
@inject(MainView) protected mainView: MainView;

@inject(KnowledgeManager) manager: KnowledgeManager;

override view = KnowledgeUploadComponent;

override onViewUnmount(): void {
Expand Down
Loading