mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
add files
This commit is contained in:
parent
d11aec58b1
commit
6eb397e9bc
2 changed files with 75 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from api.v1 import user, templates, agents, voices, internal
|
from api.v1 import user, templates, agents, voices, internal, files
|
||||||
|
|
||||||
api_router = APIRouter()
|
api_router = APIRouter()
|
||||||
|
|
||||||
|
|
@ -38,3 +38,10 @@ api_router.include_router(
|
||||||
tags=["Internal"]
|
tags=["Internal"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# File upload routes (for App only)
|
||||||
|
api_router.include_router(
|
||||||
|
files.router,
|
||||||
|
prefix="/files",
|
||||||
|
tags=["Files"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
|
||||||
67
main/live-agent-api/api/v1/files.py
Normal file
67
main/live-agent-api/api/v1/files.py
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
"""
|
||||||
|
File upload API endpoints (for App only)
|
||||||
|
"""
|
||||||
|
from fastapi import APIRouter, File, UploadFile, Depends, Header
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
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: Optional[str] = Header(None, alias="Agent-Id", description="Agent ID (optional)"),
|
||||||
|
current_user_id: str = Depends(get_current_user_id), # 需要用户认证
|
||||||
|
s3 = Depends(get_s3)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Upload file to S3 storage (for App clients only)
|
||||||
|
|
||||||
|
Headers:
|
||||||
|
- Authorization: Bearer <token> (required)
|
||||||
|
- Agent-Id: <agent_id> (optional, 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...
|
||||||
|
Agent-Id: agent_01JD8X0000ABC
|
||||||
|
Body:
|
||||||
|
file: [binary data]
|
||||||
|
"""
|
||||||
|
# 根据 agent_id 组织文件夹结构
|
||||||
|
if agent_id:
|
||||||
|
folder = f"vision/users/{current_user_id}/agents/{agent_id}"
|
||||||
|
else:
|
||||||
|
folder = f"vision/users/{current_user_id}"
|
||||||
|
|
||||||
|
# Upload to S3 using FileRepository
|
||||||
|
file_url = await FileRepository.upload(
|
||||||
|
s3=s3,
|
||||||
|
file=file,
|
||||||
|
folder=folder
|
||||||
|
)
|
||||||
|
|
||||||
|
# 获取文件大小
|
||||||
|
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"
|
||||||
|
)
|
||||||
Loading…
Reference in a new issue