mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
feat: upload file
This commit is contained in:
commit
10ba28c2a8
4 changed files with 87 additions and 3 deletions
|
|
@ -1,5 +1,5 @@
|
|||
from fastapi import APIRouter
|
||||
from api.v1 import user, templates, agents, voices, internal, chat
|
||||
from api.v1 import user, templates, agents, voices, internal, files, chat
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
|
|
@ -38,6 +38,13 @@ api_router.include_router(
|
|||
tags=["Internal"]
|
||||
)
|
||||
|
||||
# File upload routes (for App only)
|
||||
api_router.include_router(
|
||||
files.router,
|
||||
prefix="/files",
|
||||
tags=["Files"]
|
||||
)
|
||||
|
||||
# Chat routes
|
||||
api_router.include_router(
|
||||
chat.router,
|
||||
|
|
|
|||
71
main/live-agent-api/api/v1/files.py
Normal file
71
main/live-agent-api/api/v1/files.py
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
"""
|
||||
File upload API endpoints (for App only)
|
||||
"""
|
||||
from fastapi import APIRouter, File, UploadFile, Depends, Form, HTTPException
|
||||
|
||||
from infra import get_s3
|
||||
from repositories import FileRepository
|
||||
from utils.response import success_response
|
||||
from api.auth import get_current_user_id
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/upload", summary="Upload file to S3 (App only)")
|
||||
async def upload_file(
|
||||
file: UploadFile = File(..., description="File to upload (image, PDF, etc.)"),
|
||||
agent_id: str = Form(..., description="Agent ID (required, for organizing files by agent)"),
|
||||
current_user_id: str = Depends(get_current_user_id), # need user authentication
|
||||
s3 = Depends(get_s3)
|
||||
):
|
||||
"""
|
||||
Upload file to S3 storage (for App clients only)
|
||||
|
||||
Headers:
|
||||
- Authorization: Bearer <token> (required)
|
||||
|
||||
Form Data:
|
||||
- file: File to upload (required)
|
||||
- agent_id: Agent ID (required, for organizing files by agent)
|
||||
|
||||
Returns:
|
||||
- url: Public URL of uploaded file
|
||||
- filename: Original filename
|
||||
- size: File size in bytes
|
||||
|
||||
Example:
|
||||
POST /api/live_agent/v1/files/upload
|
||||
Headers:
|
||||
Authorization: Bearer eyJhbGc...
|
||||
Form Data:
|
||||
file: [binary data]
|
||||
agent_id: agent_01JD8X0000ABC
|
||||
"""
|
||||
# Validate agent_id format (basic validation)
|
||||
if not agent_id or len(agent_id.strip()) == 0:
|
||||
raise HTTPException(status_code=400, detail="agent_id cannot be empty")
|
||||
|
||||
folder = f"files/{agent_id}"
|
||||
|
||||
# Upload to S3 using FileRepository
|
||||
name = file.filename.split('.')[0] if '.' in file.filename else file.filename
|
||||
file_url = await FileRepository.upload(
|
||||
s3=s3,
|
||||
file=file,
|
||||
folder=folder,
|
||||
custom_filename=name
|
||||
)
|
||||
|
||||
# file size
|
||||
await file.seek(0)
|
||||
content = await file.read()
|
||||
file_size = len(content)
|
||||
|
||||
return success_response(
|
||||
data={
|
||||
"url": file_url,
|
||||
"filename": file.filename,
|
||||
"size": file_size
|
||||
},
|
||||
message="File uploaded successfully"
|
||||
)
|
||||
|
|
@ -10,7 +10,7 @@ import enum
|
|||
class MessageBody(BaseModel):
|
||||
"""Individual message content part (text, audio, image, or file)"""
|
||||
message_type: Literal["text", "audio", "image", "file"]
|
||||
message_content: str | bytes # Text content or S3 URL
|
||||
message_content: str # Text content or S3 URL
|
||||
|
||||
class ChatRole(enum.Enum):
|
||||
USER = 1
|
||||
|
|
|
|||
|
|
@ -47,10 +47,16 @@ class ChatService:
|
|||
if message.message_type != "audio":
|
||||
continue
|
||||
|
||||
# Decode base64 audio to bytes (service layer handles data transformation)
|
||||
try:
|
||||
audio_bytes = base64.b64decode(message.message_content)
|
||||
except Exception as e:
|
||||
raise ValueError(f"Invalid base64 audio data: {e}")
|
||||
|
||||
# Upload to S3 using FileRepository with message_id
|
||||
audio_url = await FileRepository.upload_chat_audio(
|
||||
s3,
|
||||
message.message_content,
|
||||
audio_bytes,
|
||||
request.agent_id,
|
||||
message_id=message_id,
|
||||
file_ext="opus"
|
||||
|
|
|
|||
Loading…
Reference in a new issue