Refactor: migrate doc upload info used in chat (#14359)

### What problem does this PR solve?

Before migration: POST /v1/document/upload_info/
After migration: POST /api/v1/documentss/upload/

### Type of change

- [x] Refactoring
This commit is contained in:
Jack
2026-04-27 16:58:42 +08:00
committed by GitHub
parent c446c403de
commit 61a24a2c14
7 changed files with 165 additions and 116 deletions

View File

@@ -41,7 +41,6 @@ from common import settings
from common.constants import RetCode, TaskStatus
from common.file_utils import get_project_base_directory
from common.misc_utils import thread_pool_exec
from common.ssrf_guard import assert_url_is_safe
from deepdoc.parser.html_parser import RAGFlowHtmlParser
from rag.nlp import search
@@ -214,7 +213,6 @@ async def run():
except Exception as e:
return server_error_response(e)
@manager.route("/get/<doc_id>", methods=["GET"]) # noqa: F821
@login_required
async def get(doc_id):
@@ -400,38 +398,3 @@ async def parse():
txt = FileService.parse_docs(file_objs, current_user.id)
return get_json_result(data=txt)
@manager.route("/upload_info", methods=["POST"]) # noqa: F821
@login_required
async def upload_info():
files = await request.files
file_objs = files.getlist("file") if files and files.get("file") else []
url = request.args.get("url")
if file_objs and url:
return get_json_result(
data=False,
message="Provide either multipart file(s) or ?url=..., not both.",
code=RetCode.BAD_REQUEST,
)
if not file_objs and not url:
return get_json_result(
data=False,
message="Missing input: provide multipart file(s) or url",
code=RetCode.BAD_REQUEST,
)
try:
if url and not file_objs:
assert_url_is_safe(url)
return get_json_result(data=FileService.upload_info(current_user.id, None, url))
if len(file_objs) == 1:
return get_json_result(data=FileService.upload_info(current_user.id, file_objs[0], None))
results = [FileService.upload_info(current_user.id, f, None) for f in file_objs]
return get_json_result(data=results)
except Exception as e:
return server_error_response(e)

View File

@@ -45,11 +45,76 @@ from common import settings
from common.constants import ParserType, RetCode, SANDBOX_ARTIFACT_BUCKET, TaskStatus
from common.metadata_utils import convert_conditions, meta_filter, turn2jsonschema
from common.misc_utils import get_uuid, thread_pool_exec
from common.ssrf_guard import assert_url_is_safe
from api.utils.file_utils import filename_type, thumbnail
from api.utils.web_utils import html2pdf, is_valid_url
from rag.nlp import search
from api.utils.web_utils import apply_safe_file_response_headers
@manager.route("/documents/upload", methods=["POST"]) # noqa: F821
@login_required
@add_tenant_id_to_kwargs
async def upload_info(tenant_id: str):
"""
Upload a document and get its parsed info.
---
tags:
- Documents
security:
- ApiKeyAuth: []
parameters:
- in: header
name: Authorization
type: string
required: true
description: Bearer token for authentication.
- in: formData
name: file
type: file
required: false
description: File to upload.
- in: query
name: url
type: string
required: false
description: URL to fetch file from.
responses:
200:
description: Successful operation.
"""
files = await request.files
file_objs = files.getlist("file") if files and files.get("file") else []
url = request.args.get("url")
if file_objs and url:
return get_error_argument_result("Provide either multipart file(s) or ?url=..., not both.")
if not file_objs and not url:
return get_error_argument_result("Missing input: provide multipart file(s) or url")
try:
if url and not file_objs:
try:
assert_url_is_safe(url)
except ValueError as ve:
logging.warning("upload_info: rejected unsafe url: %s", ve)
return get_error_argument_result(str(ve))
data = await thread_pool_exec(FileService.upload_info, tenant_id, None, url)
return get_result(data=data)
if len(file_objs) == 1:
data = await thread_pool_exec(FileService.upload_info, tenant_id, file_objs[0], None)
return get_result(data=data)
results = [await thread_pool_exec(FileService.upload_info, tenant_id, f, None) for f in file_objs]
return get_result(data=results)
except Exception as e:
logging.exception("upload_info failed")
return server_error_response(e)
@manager.route("/datasets/<dataset_id>/documents/<document_id>", methods=["PATCH"]) # noqa: F821
@login_required
@add_tenant_id_to_kwargs