Spaces:
Sleeping
Sleeping
File size: 1,556 Bytes
abf6602 | 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import os
from fastapi import UploadFile
import cloudinary
import cloudinary.uploader
from cloudinary.utils import cloudinary_url
cloudinary.config(
cloud_name = os.getenv("CLOUDINARY_CLOUD_NAME", ""),
api_key = os.getenv("CLOUDINARY_API_KEY", ""),
api_secret = os.getenv("CLOUDINARY_API_SECRET", ""),
secure = True
)
async def upload_image_to_cloudinary(file: UploadFile, folder: str = "smafit/avatars") -> str:
"""
Uploads an image to Cloudinary and returns the public secure URL.
"""
try:
file_data = await file.read()
result = cloudinary.uploader.upload(
file_data,
folder=folder,
resource_type="image"
)
return result["secure_url"]
except Exception as e:
print(f"Error uploading to Cloudinary: {e}")
raise Exception("Failed to upload image to Cloudinary")
finally:
await file.seek(0)
async def upload_video_to_cloudinary(file: UploadFile, folder: str = "smafit/exercises") -> str:
"""
Uploads a video to Cloudinary and returns the public secure URL.
"""
try:
file_data = await file.read()
result = cloudinary.uploader.upload(
file_data,
folder=folder,
resource_type="video"
)
return result["secure_url"]
except Exception as e:
print(f"Error uploading video to Cloudinary: {e}")
raise Exception("Failed to upload video to Cloudinary")
finally:
await file.seek(0)
|