From cf8c4fa0d6db79e3c1fe54e641ea0878c607fec9 Mon Sep 17 00:00:00 2001 From: Artur Date: Sun, 3 May 2026 17:51:19 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=81=D1=82=D0=B0=D1=80=D1=8B=D1=85=20=D0=B0=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B0=D1=80=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srv/app/api/endpoints/users.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/srv/app/api/endpoints/users.py b/srv/app/api/endpoints/users.py index 5e8f347..098c560 100644 --- a/srv/app/api/endpoints/users.py +++ b/srv/app/api/endpoints/users.py @@ -1,9 +1,12 @@ +import os + from fastapi import Depends, APIRouter, HTTPException, Depends, Request from sqlalchemy.orm import Session from app.db import models from app.core.security import get_current_user from app.api import schemas +from app.core.config import config from sqlalchemy import or_, and_, exists from sqlalchemy.exc import IntegrityError from app.websocket import connection_manager @@ -19,6 +22,28 @@ def get_db(): db.close() +def _delete_old_avatar_file(file_id: str, db: Session): + upload_path = os.path.join('uploads', f"{file_id}.enc") + if os.path.exists(upload_path): + try: + os.remove(upload_path) + except OSError: + pass + + cloud_item = db.query(models.CloudMediaItem).filter( + models.CloudMediaItem.file_id == file_id, + ).all() + for item in cloud_item: + cloud_path = os.path.join(config.CLOUD_MEDIA_CACHE_FOLDER, item.local_filename) + if os.path.exists(cloud_path): + try: + os.remove(cloud_path) + except OSError: + pass + db.delete(item) + db.commit() + + usersRouter = APIRouter( prefix="/users", tags=[], @@ -327,6 +352,10 @@ async def update_user_avatar( user_to_update = db.merge(current_user) avatar_file_id = data.get("avatar_file_id") if avatar_file_id: + old_avatar_file_id = user_to_update.avatar_file_id + if old_avatar_file_id and old_avatar_file_id != avatar_file_id: + _delete_old_avatar_file(old_avatar_file_id, db) + user_to_update.avatar_file_id = avatar_file_id db.commit() print(