fix: use raw POST body for file upload instead of multipart

This commit is contained in:
yumoqing 2026-07-22 18:13:58 +08:00
parent c25351a65e
commit ef783dc97f

23
init.py
View File

@ -73,26 +73,15 @@ async def doc_upload_handler(request, params_kw, *args, **kwargs):
try:
userorgid = await env.get_userorgid()
kb_id = params_kw.get("kb_id", "")
folder = params_kw.get("folder", "/")
file_name = params_kw.get("file_name", "upload.bin")
if not kb_id:
return json.dumps({"error": "kb_id required"})
# Read uploaded file from request
reader = await request.multipart()
file_data = None
file_name = None
content_type = None
while True:
part = await reader.next()
if part is None:
break
if part.name == "file":
file_name = part.filename or "upload.bin"
file_data = await part.read()
content_type = part.headers.get("Content-Type", "application/octet-stream")
# Read raw file data from request body
file_data = await request.read()
if not file_data:
return json.dumps({"error": "no file uploaded"})
return json.dumps({"error": "no file data"})
# Save file
doc_id = uuid.uuid4().hex[:16]
@ -105,7 +94,7 @@ async def doc_upload_handler(request, params_kw, *args, **kwargs):
f.write(file_data)
file_size = len(file_data)
file_type = _detect_file_type(file_name, content_type)
file_type = _detect_file_type(file_name, "application/octet-stream")
# Create document record
async with get_sor_context(env, 'rag') as sor:
@ -114,7 +103,7 @@ async def doc_upload_handler(request, params_kw, *args, **kwargs):
"VALUES (${id}$, ${kb_id}$, ${file_name}$, ${file_type}$, ${file_size}$, ${file_path}$, ${mime_type}$, 'pending', ${org_id}$, NOW(), NOW())",
{"id": doc_id, "kb_id": kb_id, "file_name": file_name, "file_type": file_type,
"file_size": file_size, "file_path": "/idfile/files/" + saved_name,
"mime_type": content_type, "org_id": userorgid})
"mime_type": "application/octet-stream", "org_id": userorgid})
# Update KB stats
await sor.sqlExe(