From dd05e7f5c7817f5b9c92ef9e2179eabc4d9594e6 Mon Sep 17 00:00:00 2001 From: lorsan Date: Mon, 26 Jan 2026 13:25:18 +0300 Subject: [PATCH] Edit get avatar --- backend/app/core/config.py | 1 + backend/app/main.py | 14 ++++++++++++++ backend/app/users/service.py | 2 +- backend/app/utils/S3_client.py | 2 +- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/backend/app/core/config.py b/backend/app/core/config.py index f64325b..0dc6b82 100755 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -13,6 +13,7 @@ class Settings(BaseSettings): BACKEND_PORT: int WORKERS: int FRONTEND_URL: str + BACKEND_URL: str FIRST_SUPER_USER_EMAIL: str FIRST_SUPER_USER_PASS: str diff --git a/backend/app/main.py b/backend/app/main.py index 8d0ea00..0d0bec7 100755 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -15,6 +15,7 @@ from app.chats.router import router as chat_router from app.core.log_config import set_logging from app.core.config import settings from app.services.messenger_service import PubSubMessenger +from app.utils.S3_client import s3_client set_logging() log = logging.getLogger(__name__) @@ -42,6 +43,19 @@ api_router.include_router(chat_router) async def test_health(): return {"status": True} +@api_router.get("/files/{filename}") +async def get_file(filename: str): + file_data = await s3_client.download_file(filename) + + content_type = "image/jpeg" + if filename.endswith(".png"): + content_type = "image/png" + + # Возвращаем файл напрямую из памяти + return Response( + content=file_data, + media_type=content_type + ) app = FastAPI( title=settings.COMPANY_NAME, description="## Backend messenger aether", diff --git a/backend/app/users/service.py b/backend/app/users/service.py index f0b061a..7da5f97 100644 --- a/backend/app/users/service.py +++ b/backend/app/users/service.py @@ -234,7 +234,7 @@ class UserService: @classmethod async def upload_avatar(cls, user: UserModel, avatar: UploadFile) -> User: async with async_session_maker() as session: - allowed_types = ["image/jpeg", "image/png", "image/gif"] + allowed_types = ["image/jpeg", "image/png"] if not avatar.content_type in allowed_types: log.warning("Using not allowed type photo", extra={"user_id": user.id}) diff --git a/backend/app/utils/S3_client.py b/backend/app/utils/S3_client.py index f649e46..abd143e 100644 --- a/backend/app/utils/S3_client.py +++ b/backend/app/utils/S3_client.py @@ -59,7 +59,7 @@ class S3Client: log.error(f"Unexpected S3 error: {str(e)}", extra={"object_name": object_name}) raise HTTPException(status.HTTP_503_SERVICE_UNAVAILABLE, detail="Unexpected S3 error") - return f"{self.endpoint_url}/{self.bucket_name}/{object_name}" + return f"{settings.BACKEND_URL}/files/{object_name}" async def download_file(