Files
secondary-school-grade-archive/backend/app/services/student_avatar.py
T
dekun 530a8b70a1 学生资料设置、头像与自动备份恢复。
首页卡片支持修改/删除;详情页设置 Tab 可维护学校、年级与头像;系统设置新增数据备份下载与恢复;备份默认存 /root/grade-archive-backups,详见 docs/BACKUP.md。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-28 17:56:09 +08:00

33 lines
893 B
Python

import uuid
from io import BytesIO
from pathlib import Path
from PIL import Image
from app.core.config import settings
def avatar_rel_path(user_id: str, student_id: str) -> str:
return f"{user_id}/avatars/{student_id}.jpg"
def save_avatar(user_id: str, student_id: str, content: bytes) -> str:
rel = avatar_rel_path(user_id, student_id)
full = Path(settings.UPLOAD_DIR) / rel
full.parent.mkdir(parents=True, exist_ok=True)
image = Image.open(BytesIO(content))
if image.mode not in ("RGB", "L"):
image = image.convert("RGB")
image.thumbnail((256, 256), Image.Resampling.LANCZOS)
image.save(full, format="JPEG", quality=85, optimize=True)
return rel
def delete_avatar_file(avatar_path: str | None) -> None:
if not avatar_path:
return
full = Path(settings.UPLOAD_DIR) / avatar_path
if full.is_file():
full.unlink()