Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions backend/routes/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,36 @@ async def update_profile_sample(
return sample


AVATAR_MAX_FILE_SIZE = 10 * 1024 * 1024 # 10 MB
AVATAR_UPLOAD_CHUNK_SIZE = 1024 * 1024 # 1 MB


@router.post("/profiles/{profile_id}/avatar", response_model=models.VoiceProfileResponse)
async def upload_profile_avatar(
profile_id: str,
file: UploadFile = File(...),
db: Session = Depends(get_db),
):
"""Upload or update avatar image for a profile."""
with tempfile.NamedTemporaryFile(delete=False, suffix=Path(file.filename).suffix) as tmp:
content = await file.read()
tmp.write(content)
_ALLOWED_IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".svg"}
_raw_ext = Path(file.filename or "").suffix.lower()
if _raw_ext and _raw_ext not in _ALLOWED_IMAGE_EXTS:
raise HTTPException(
status_code=400,
detail=f"Unsupported image format '{_raw_ext}'. Allowed: {sorted(_ALLOWED_IMAGE_EXTS)}",
)
suffix = _raw_ext if _raw_ext in _ALLOWED_IMAGE_EXTS else ".png"
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
total_size = 0
while chunk := await file.read(AVATAR_UPLOAD_CHUNK_SIZE):
total_size += len(chunk)
if total_size > AVATAR_MAX_FILE_SIZE:
Path(tmp.name).unlink(missing_ok=True)
raise HTTPException(
status_code=413,
detail=f"Avatar file too large (max {AVATAR_MAX_FILE_SIZE // (1024 * 1024)} MB)",
)
tmp.write(chunk)
tmp_path = tmp.name

try:
Expand Down