chat_back/app/images/router.py
2024-07-16 18:18:28 +04:00

71 lines
2.3 KiB
Python

from fastapi import APIRouter, UploadFile, status, Depends
from app.images.shemas import SImageUrl, Responses
from app.services.image_service import ImageService
from app.users.dependencies import get_verificated_user
router = APIRouter(prefix="/images", tags=["Загрузка картинок"])
@router.post(
"/upload_avatar",
status_code=status.HTTP_200_OK,
response_model=SImageUrl,
responses={
status.HTTP_401_UNAUTHORIZED: {
"model": Responses.STokenMissingException,
"description": "Токен отсутствует"
},
status.HTTP_403_FORBIDDEN: {
"model": Responses.SNotAuthenticated,
"description": "Not authenticated"
},
status.HTTP_404_NOT_FOUND: {
"model": Responses.SUserNotFoundException,
"description": "Юзер не найден"
},
status.HTTP_409_CONFLICT: {
"model": Responses.SUserMustConfirmEmailException,
"description": "Сначала подтвердите почту"
},
status.HTTP_500_INTERNAL_SERVER_ERROR: {
"model": Responses.SBlackPhoenixException,
"description": "Внутренняя ошибка сервера"
},
},
)
async def upload_avatar(file: UploadFile, _=Depends(get_verificated_user)):
image_url = await ImageService.upload_file_returning_str(file, "upload_avatar")
return {"image_url": image_url}
@router.post(
"/upload_image",
status_code=status.HTTP_200_OK,
response_model=SImageUrl,
responses={
status.HTTP_401_UNAUTHORIZED: {
"model": Responses.STokenMissingException,
"description": "Токен отсутствует"
},
status.HTTP_403_FORBIDDEN: {
"model": Responses.SNotAuthenticated,
"description": "Not authenticated"
},
status.HTTP_404_NOT_FOUND: {
"model": Responses.SUserNotFoundException,
"description": "Юзер не найден"
},
status.HTTP_409_CONFLICT: {
"model": Responses.SUserMustConfirmEmailException,
"description": "Сначала подтвердите почту"
},
status.HTTP_500_INTERNAL_SERVER_ERROR: {
"model": Responses.SBlackPhoenixException,
"description": "Внутренняя ошибка сервера"
},
},
)
async def upload_image(file: UploadFile, _=Depends(get_verificated_user)):
image_url = await ImageService.upload_file_returning_str(file, "upload_image")
return {"image_url": image_url}