Fix avatars upload
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, HTTPException, status, UploadFile, File
|
||||
from fastapi import APIRouter, HTTPException, status, UploadFile, File, Response
|
||||
from sqlalchemy import select, func
|
||||
|
||||
from app.api.deps import DbSession, CurrentUser
|
||||
@@ -30,6 +30,34 @@ async def get_user(user_id: int, db: DbSession):
|
||||
return UserPublic.model_validate(user)
|
||||
|
||||
|
||||
@router.get("/{user_id}/avatar")
|
||||
async def get_user_avatar(user_id: int, db: DbSession):
|
||||
"""Stream user avatar from storage"""
|
||||
result = await db.execute(select(User).where(User.id == user_id))
|
||||
user = result.scalar_one_or_none()
|
||||
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
if not user.avatar_path:
|
||||
raise HTTPException(status_code=404, detail="User has no avatar")
|
||||
|
||||
# Get file from storage
|
||||
file_data = await storage_service.get_file(user.avatar_path, "avatars")
|
||||
if not file_data:
|
||||
raise HTTPException(status_code=404, detail="Avatar not found in storage")
|
||||
|
||||
content, content_type = file_data
|
||||
|
||||
return Response(
|
||||
content=content,
|
||||
media_type=content_type,
|
||||
headers={
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@router.patch("/me", response_model=UserPublic)
|
||||
async def update_me(data: UserUpdate, current_user: CurrentUser, db: DbSession):
|
||||
if data.nickname is not None:
|
||||
|
||||
Reference in New Issue
Block a user