o
    ¤š
j&  ã                   @   sB   d dl Z d dlZd dlZd dlT d dlmZ dedefdd„ZdS )é    N)Ú*)ÚMinimaxRequestErrorÚ	image_urlÚreturnc              
   C   s¼  |   d¡r| dd… } |   d¡r| S |   d¡rozAt | ¡}| ¡  |j}|j dd¡ ¡ }d|v s5d	|v r8d}nd
|v r?d
}n	d|v rFd}nd}t |¡ 	d¡}d|› d|› W S  tj
yn } z	tdt|ƒ› ƒ‚d}~ww tj | ¡s|td| › ƒ‚zLt| dƒ<}| ¡ }d}|  ¡  d¡r“d
}n|  ¡  d¡rd}n	|  ¡  d¡r¦d}t |¡ 	d¡}d|› d|› W  d  ƒ W S 1 sÁw   Y  W dS  tyÝ } z	tdt|ƒ› ƒ‚d}~ww )aG  
    Process image URL and convert to base64 data URL format.
    
    This function handles three types of image inputs:
    1. HTTP/HTTPS URLs: Downloads the image and converts to base64
    2. Base64 data URLs: Passes through as-is
    3. Local file paths: Reads the file and converts to base64
    
    Args:
        image_url (str): The image URL, data URL, or local file path
        
    Returns:
        str: Base64 data URL in format "data:image/{format};base64,{data}"
        
    Raises:
        MinimaxRequestError: If image cannot be downloaded, read, or processed
    ú@é   Nzdata:)zhttp://zhttps://zcontent-typeÚ ÚjpegÚjpgÚpngÚwebpzutf-8zdata:image/z;base64,z#Failed to download image from URL: z!Local image file does not exist: Úrbz.pngz.webp)z.jpgz.jpegz!Failed to read local image file: )Ú
startswithÚrequestsÚgetÚraise_for_statusÚcontentÚheadersÚlowerÚbase64Ú	b64encodeÚdecodeÚRequestExceptionr   ÚstrÚosÚpathÚexistsÚopenÚreadÚendswithÚIOError)r   Úimage_responseÚ
image_dataÚcontent_typeÚimage_formatÚbase64_dataÚeÚf© r(   úD/home/kuhnn/.local/lib/python3.10/site-packages/minimax_mcp/utils.pyÚprocess_image_url	   sT   



€ÿ(ó€ÿr*   )r   r   r   Úminimax_mcp.constÚminimax_mcp.exceptionsr   r   r*   r(   r(   r(   r)   Ú<module>   s    