Spaces:
Sleeping
Sleeping
| """ | |
| Cloudinary client for file uploads | |
| """ | |
| import os | |
| import logging | |
| import cloudinary | |
| import cloudinary.uploader | |
| from typing import Union | |
| logger = logging.getLogger(__name__) | |
| class CloudinaryClient: | |
| """Handles file uploads to Cloudinary""" | |
| def __init__(self): | |
| # Configure Cloudinary | |
| cloudinary.config( | |
| cloud_name=os.environ.get("CLOUDINARY_CLOUD_NAME"), | |
| api_key=os.environ.get("CLOUDINARY_API_KEY"), | |
| api_secret=os.environ.get("CLOUDINARY_API_SECRET") | |
| ) | |
| # Verify configuration | |
| if not all([ | |
| os.environ.get("CLOUDINARY_CLOUD_NAME"), | |
| os.environ.get("CLOUDINARY_API_KEY"), | |
| os.environ.get("CLOUDINARY_API_SECRET") | |
| ]): | |
| logger.warning("β οΈ Cloudinary credentials not fully configured") | |
| else: | |
| logger.info("β Cloudinary client initialized") | |
| def upload_image_from_bytes(self, image_bytes: bytes, public_id: str) -> str: | |
| """Upload image from bytes to Cloudinary""" | |
| try: | |
| logger.info(f"βοΈ Uploading image to Cloudinary: {public_id}") | |
| result = cloudinary.uploader.upload( | |
| image_bytes, | |
| public_id=f"text-to-3d/{public_id}", | |
| resource_type="image", | |
| unique_filename=True, | |
| overwrite=True, | |
| quality="auto" | |
| ) | |
| url = result["secure_url"] | |
| logger.info(f"β Image uploaded: {url}") | |
| return url | |
| except Exception as e: | |
| logger.error(f"β Error uploading image to Cloudinary: {str(e)}") | |
| raise e | |
| def upload_image_from_path(self, file_path: str, public_id: str) -> str: | |
| """Upload image from file path to Cloudinary""" | |
| try: | |
| logger.info(f"βοΈ Uploading image file to Cloudinary: {public_id}") | |
| result = cloudinary.uploader.upload( | |
| file_path, | |
| public_id=f"text-to-3d/{public_id}", | |
| resource_type="image", | |
| unique_filename=True, | |
| overwrite=True, | |
| quality="auto" | |
| ) | |
| url = result["secure_url"] | |
| logger.info(f"β Image file uploaded: {url}") | |
| return url | |
| except Exception as e: | |
| logger.error(f"β Error uploading image file to Cloudinary: {str(e)}") | |
| raise e | |
| def upload_file(self, file_path: str, public_id: str) -> str: | |
| """Upload any file to Cloudinary""" | |
| try: | |
| logger.info(f"βοΈ Uploading file to Cloudinary: {public_id}") | |
| result = cloudinary.uploader.upload( | |
| file_path, | |
| public_id=f"text-to-3d/{public_id}", | |
| resource_type="raw", # For non-image files | |
| unique_filename=True, | |
| overwrite=True | |
| ) | |
| url = result["secure_url"] | |
| logger.info(f"β File uploaded: {url}") | |
| return url | |
| except Exception as e: | |
| logger.error(f"β Error uploading file to Cloudinary: {str(e)}") | |
| raise e | |
| def delete_file(self, public_id: str, resource_type: str = "image") -> bool: | |
| """Delete file from Cloudinary""" | |
| try: | |
| logger.info(f"ποΈ Deleting file from Cloudinary: {public_id}") | |
| result = cloudinary.uploader.destroy( | |
| f"text-to-3d/{public_id}", | |
| resource_type=resource_type | |
| ) | |
| success = result.get("result") == "ok" | |
| if success: | |
| logger.info(f"β File deleted: {public_id}") | |
| else: | |
| logger.warning(f"β οΈ File deletion may have failed: {public_id}") | |
| return success | |
| except Exception as e: | |
| logger.error(f"β Error deleting file from Cloudinary: {str(e)}") | |
| return False |