Skip to content

Commit f952c24

Browse files
euxxalexcodelf
authored andcommitted
feat: add GET upload file API endpoint to dataset service api (langgenius#11899)
1 parent c7b691b commit f952c24

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed

api/controllers/service_api/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
from . import index
99
from .app import app, audio, completion, conversation, file, message, workflow
10-
from .dataset import dataset, document, hit_testing, segment
10+
from .dataset import dataset, document, hit_testing, segment, upload_file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from werkzeug.exceptions import NotFound
2+
3+
from controllers.service_api import api
4+
from controllers.service_api.wraps import (
5+
DatasetApiResource,
6+
)
7+
from core.file import helpers as file_helpers
8+
from extensions.ext_database import db
9+
from models.dataset import Dataset
10+
from models.model import UploadFile
11+
from services.dataset_service import DocumentService
12+
13+
14+
class UploadFileApi(DatasetApiResource):
15+
def get(self, tenant_id, dataset_id, document_id):
16+
"""Get upload file."""
17+
# check dataset
18+
dataset_id = str(dataset_id)
19+
tenant_id = str(tenant_id)
20+
dataset = db.session.query(Dataset).filter(Dataset.tenant_id == tenant_id, Dataset.id == dataset_id).first()
21+
if not dataset:
22+
raise NotFound("Dataset not found.")
23+
# check document
24+
document_id = str(document_id)
25+
document = DocumentService.get_document(dataset.id, document_id)
26+
if not document:
27+
raise NotFound("Document not found.")
28+
# check upload file
29+
if document.data_source_type != "upload_file":
30+
raise ValueError(f"Document data source type ({document.data_source_type}) is not upload_file.")
31+
data_source_info = document.data_source_info_dict
32+
if data_source_info and "upload_file_id" in data_source_info:
33+
file_id = data_source_info["upload_file_id"]
34+
upload_file = db.session.query(UploadFile).filter(UploadFile.id == file_id).first()
35+
if not upload_file:
36+
raise NotFound("UploadFile not found.")
37+
else:
38+
raise ValueError("Upload file id not found in document data source info.")
39+
40+
url = file_helpers.get_signed_file_url(upload_file_id=upload_file.id)
41+
return {
42+
"id": upload_file.id,
43+
"name": upload_file.name,
44+
"size": upload_file.size,
45+
"extension": upload_file.extension,
46+
"url": url,
47+
"download_url": f"{url}&as_attachment=true",
48+
"mime_type": upload_file.mime_type,
49+
"created_by": upload_file.created_by,
50+
"created_at": upload_file.created_at.timestamp(),
51+
}, 200
52+
53+
54+
api.add_resource(UploadFileApi, "/datasets/<uuid:dataset_id>/documents/<uuid:document_id>/upload-file")

web/app/(commonLayout)/datasets/template/template.en.mdx

+51
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,57 @@ import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from
11061106

11071107
<hr className='ml-0 mr-0' />
11081108

1109+
<Heading
1110+
url='/datasets/{dataset_id}/documents/{document_id}/upload-file'
1111+
method='GET'
1112+
title='Get Upload File'
1113+
name='#get_upload_file'
1114+
/>
1115+
<Row>
1116+
<Col>
1117+
### Path
1118+
<Properties>
1119+
<Property name='dataset_id' type='string' key='dataset_id'>
1120+
Knowledge ID
1121+
</Property>
1122+
<Property name='document_id' type='string' key='document_id'>
1123+
Document ID
1124+
</Property>
1125+
</Properties>
1126+
</Col>
1127+
<Col sticky>
1128+
<CodeGroup
1129+
title="Request"
1130+
tag="GET"
1131+
label="/datasets/{dataset_id}/documents/{document_id}/upload-file"
1132+
targetCode={`curl --location --request GET '${props.apiBaseUrl}/datasets/{dataset_id}/documents/{document_id}/upload-file' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json'`}
1133+
>
1134+
```bash {{ title: 'cURL' }}
1135+
curl --location --request GET '${props.apiBaseUrl}/datasets/{dataset_id}/documents/{document_id}/upload-file' \
1136+
--header 'Authorization: Bearer {api_key}' \
1137+
--header 'Content-Type: application/json'
1138+
```
1139+
</CodeGroup>
1140+
<CodeGroup title="Response">
1141+
```json {{ title: 'Response' }}
1142+
{
1143+
"id": "file_id",
1144+
"name": "file_name",
1145+
"size": 1024,
1146+
"extension": "txt",
1147+
"url": "preview_url",
1148+
"download_url": "download_url",
1149+
"mime_type": "text/plain",
1150+
"created_by": "user_id",
1151+
"created_at": 1728734540,
1152+
}
1153+
```
1154+
</CodeGroup>
1155+
</Col>
1156+
</Row>
1157+
1158+
<hr className='ml-0 mr-0' />
1159+
11091160
<Heading
11101161
url='/datasets/{dataset_id}/retrieve'
11111162
method='POST'

web/app/(commonLayout)/datasets/template/template.zh.mdx

+51
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,57 @@ import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from
11071107

11081108
<hr className='ml-0 mr-0' />
11091109

1110+
<Heading
1111+
url='/datasets/{dataset_id}/documents/{document_id}/upload-file'
1112+
method='GET'
1113+
title='获取上传文件'
1114+
name='#get_upload_file'
1115+
/>
1116+
<Row>
1117+
<Col>
1118+
### Path
1119+
<Properties>
1120+
<Property name='dataset_id' type='string' key='dataset_id'>
1121+
知识库 ID
1122+
</Property>
1123+
<Property name='document_id' type='string' key='document_id'>
1124+
文档 ID
1125+
</Property>
1126+
</Properties>
1127+
</Col>
1128+
<Col sticky>
1129+
<CodeGroup
1130+
title="Request"
1131+
tag="GET"
1132+
label="/datasets/{dataset_id}/documents/{document_id}/upload-file"
1133+
targetCode={`curl --location --request GET '${props.apiBaseUrl}/datasets/{dataset_id}/documents/{document_id}/upload-file' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json'`}
1134+
>
1135+
```bash {{ title: 'cURL' }}
1136+
curl --location --request GET '${props.apiBaseUrl}/datasets/{dataset_id}/documents/{document_id}/upload-file' \
1137+
--header 'Authorization: Bearer {api_key}' \
1138+
--header 'Content-Type: application/json'
1139+
```
1140+
</CodeGroup>
1141+
<CodeGroup title="Response">
1142+
```json {{ title: 'Response' }}
1143+
{
1144+
"id": "file_id",
1145+
"name": "file_name",
1146+
"size": 1024,
1147+
"extension": "txt",
1148+
"url": "preview_url",
1149+
"download_url": "download_url",
1150+
"mime_type": "text/plain",
1151+
"created_by": "user_id",
1152+
"created_at": 1728734540,
1153+
}
1154+
```
1155+
</CodeGroup>
1156+
</Col>
1157+
</Row>
1158+
1159+
<hr className='ml-0 mr-0' />
1160+
11101161
<Heading
11111162
url='/datasets/{dataset_id}/retrieve'
11121163
method='POST'

0 commit comments

Comments
 (0)