"""角色头像 API。 上传与获取角色头像图片文件。 """ from fastapi import APIRouter, Depends, File, UploadFile from app.api.v1.deps import get_current_user from app.schemas.auth import CurrentUser from app.services.avatar import ( get_avatar_response, upload_avatar, ) router = APIRouter(prefix="/characters", tags=["characters"]) @router.post("/{character_id}/avatar") async def upload_character_avatar( character_id: str, file: UploadFile = File(..., description="头像图片,支持 JPG/PNG/WebP,最大 5MB。"), current_user: CurrentUser = Depends(get_current_user), ) -> dict: """上传角色头像。 支持的格式:JPEG、PNG、WebP。最大 5MB。 新头像会替换旧头像(同角色只保留一张)。 """ content = await file.read() avatar_url = await upload_avatar( character_id=character_id, content=content, content_type=file.content_type or "image/jpeg", owner_scope=current_user.owner_scope, ) return {"avatar_url": avatar_url} @router.get("/{character_id}/avatar") async def get_character_avatar( character_id: str, ) -> object: """获取角色头像图片。 返回原始图片文件。无头像时返回 404。 """ return await get_avatar_response(character_id)