File size: 1,230 Bytes
560a5fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import cloudinary
import cloudinary.uploader
from backend.app.config.settings import settings
if settings.CLOUDINARY_CLOUD_NAME and settings.CLOUDINARY_API_KEY and settings.CLOUDINARY_API_SECRET:
cloudinary.config(
cloud_name= settings.CLOUDINARY_CLOUD_NAME,
api_key= settings.CLOUDINARY_API_KEY,
api_secret= settings.CLOUDINARY_API_SECRET,
secure= True
)
def upload_avatar(file_bytes: bytes, user_id: str) -> str:
"""
Uploads a user avatar to Cloudinary under the 'quantiq/avatars' folder,
using the user's ID as the public ID so it overwrites their old avatar.
Applies an auto-face-detection square crop (150x150).
"""
if not (settings.CLOUDINARY_CLOUD_NAME and settings.CLOUDINARY_API_KEY and settings.CLOUDINARY_API_SECRET):
raise ValueError("Cloudinary credentials are not configured in settings.")
result= cloudinary.uploader.upload(
file_bytes,
folder= "quantiq/avatars",
public_id= f"user_{user_id}",
overwrite= True,
resource_type= "image",
transformation=[
{"width": 150, "height": 150, "crop": "fill", "gravity": "face"}
]
)
return result.get("secure_url") |