
    lbi              	          d dl Z d dlZ d dlmZmZmZ d dlmZ d dlZd dl	m
Z
  e
              ej        dd          Z ej        dd          Z ej        d	d
          Zereres ed           e j        eee           h dZdZdeddfdZ	 	 ddedee         dee         defdZdedefdZdedee         fdZdS )    N)HTTPExceptionstatus
UploadFile)Optional)load_dotenvCLOUDINARY_CLOUD_NAME	dnwxrqvthCLOUDINARY_API_KEY658588413175913CLOUDINARY_API_SECRETrSbOvPyQormfGojL1JiGlzyNX6QzCloudinary credentials not configured. Please set CLOUDINARY_CLOUD_NAME, CLOUDINARY_API_KEY, and CLOUDINARY_API_SECRET environment variables.)
cloud_nameapi_key
api_secret>   	image/gif	image/jpg	image/png
image/jpeg
image/webpi  P filereturnc                    | j         sL| j        pd}|                                                    d          st	          t
          j        d          n2| j         t          vr$t	          t
          j        d| j          d          	 | j        	                    dd           | j        
                                }| j        	                    d           |t          k    r#t	          t
          j        d	|d
z  dd          |dk    rt	          t
          j        d          dS # t          $ rF}t          |t                    r t	          t
          j        dt          |                     d}~ww xY w)z!Validate image file type and size )z.jpgz.jpegz.pngz.webpz.gifz\File type could not be determined. Please ensure the file is an image (JPEG, PNG, WebP, GIF)status_codedetailzInvalid file type: z%. Allowed types: JPEG, PNG, WebP, GIFr      z+File size exceeds 5MB limit. Current size: i   z.2fMBzFile is emptyzError reading file: N)content_typefilenamelowerendswithr   r   HTTP_400_BAD_REQUESTALLOWED_IMAGE_TYPESr   seektellMAX_FILE_SIZE	Exception
isinstancestr)r   r    	file_sizees       (D:\Nbitz\app\utility\cloudinary_utils.pyvalidate_image_filer.   "   s    
=&B~~(()STT 	"7u   	
 
	"5	5	53a):aaa
 
 
 	

	q!INN$$		q}$$"7fYR]E^ffff    >>"7&    >
  
 
 
a'' 	32#a&&22
 
 
 	

s   BD& &
E60AE11E6folder	public_idc                    t          |            	 | j                            d           | j                                        }| j                            d           |st	          t
          j        d          ddi}|r||d<   |r||d<   t          j        j	        |fi |}|st	          t
          j
        d          |                    d	          p|                    d
          }|st	          t
          j
        d          |S # t          $ r  t          $ rU}t          |          }d|v sd|v rt	          t
          j
        d          t	          t
          j
        d|           d}~ww xY w)a  
    Upload an image file to Cloudinary
    
    Args:
        file: FastAPI UploadFile object
        folder: Optional folder path in Cloudinary
        public_id: Optional public ID for the image
    
    Returns:
        str: URL of the uploaded image
    r   z"File is empty or could not be readr   resource_typeimager/   r0   z$Cloudinary upload returned no result
secure_urlurlz3Cloudinary upload succeeded but no URL was returnedzInvalid API Key401zDCloudinary authentication failed. Please check your API credentials.z%Error uploading image to Cloudinary: N)r.   r   r%   readr   r   r#   
cloudinaryuploaderuploadHTTP_500_INTERNAL_SERVER_ERRORgetr(   r*   )	r   r/   r0   file_contentupload_optionsresult	image_urlr,   	error_msgs	            r-   upload_image_to_cloudinaryrB   K   s     9
	qy~~''	q 	"7;    W
  	.'-N8$ 	4*3N;' $+
 

 

  	"A=   
 JJ|,,A

50A0A	 	"AL   
     
 
 
FF		))Ui-?-?"A]    =F9FF
 
 
 	

s   C8D
 
E3AE..E3c                     	 t           j                            |           }|                    d          dk    S # t          $ r Y dS w xY w)z
    Delete an image from Cloudinary
    
    Args:
        public_id: Public ID of the image to delete
    
    Returns:
        bool: True if deletion was successful
    r?   okF)r8   r9   destroyr<   r(   )r0   r?   s     r-   delete_image_from_cloudinaryrF      sV    $,,Y77zz(##t++   uus   7: 
AAr5   c                    	 d| vsd| vrdS |                      d          }|                    d          }|dz   t          |          k    rdS d                    ||dz   d                   }|                    d          r4d|v r0d                    |                     d          dd                   }d|v r2d                    |                    dd          dd	                   }n|}|r|ndS # t          $ r Y nw xY wdS )
z
    Extract public_id from Cloudinary URL
    
    Args:
        url: Cloudinary image URL
    
    Returns:
        Optional[str]: Public ID if found, None otherwise
    zcloudinary.comzimage/uploadN/r:      v.)splitindexlenjoin
startswithrsplitr(   )r5   partsupload_indexpath_after_uploadr0   s        r-   extract_public_id_from_urlrV      sD   
 3&&.*C*C4		#{{8,,!s5zz))4  HHU<!+;+<+<%=>> '',, 	K8I1I1I #):)@)@)E)Eabb)I J J ###!2!9!9#q!A!A#2#!FGGII)I%/yy4/   4s   C5 A C5 B&C5 5
DD)NN)r8   cloudinary.uploaderfastapir   r   r   typingr   osdotenvr   getenv
CLOUD_NAMEAPI_KEY
API_SECRETRuntimeErrorconfigr$   r'   r.   r*   rB   boolrF   rV        r-   <module>re      s           5 5 5 5 5 5 5 5 5 5       				        RY.<<

")(*;
<
<RY.0MNN
   
 
,	O   
     ZYY &
j &
T &
 &
 &
 &
V !#K
 K

K
SMK
 }K
 		K
 K
 K
 K
\C D    "'C 'HSM ' ' ' ' ' 'rd   