| import cloudinary | |
| import cloudinary.uploader | |
| import os | |
| from fastapi import UploadFile | |
| # Configure Cloudinary | |
| cloudinary.config( | |
| cloud_name=os.getenv("CLOUDINARY_CLOUD_NAME"), | |
| api_key=os.getenv("CLOUDINARY_API_KEY"), | |
| api_secret=os.getenv("CLOUDINARY_API_SECRET") | |
| ) | |
| def upload_image_to_cloudinary(file: UploadFile, folder: str = "medispenser") -> str: | |
| """ | |
| Uploads a file directly to Cloudinary and returns the secure URL. | |
| """ | |
| try: | |
| # Upload the file directly from the memory stream | |
| upload_result = cloudinary.uploader.upload( | |
| file.file, | |
| folder=folder, | |
| resource_type="auto" | |
| ) | |
| return upload_result.get("secure_url") | |
| except Exception as e: | |
| print(f"Cloudinary upload failed: {e}") | |
| return "" | |