
    j1                         d dl Z d dlZd dlZd dlZd dlmZmZmZ ddlm	Z	m
Z
mZ  ej                  e      Z G d de
j                        Zy)    N)AnyOptionalLiteral   )
attachmentdecoderdecoder_helpersc            
       L    e Zd ZdZ	 d	deded   dedeej                     fdZ
y)
Base64AttachmentDecoderzDecodes base64 encoded attachment data.

    This decoder decodes base64 strings, detects MIME types from content, and creates Attachment objects.
    raw_datacontext)inputoutputmetadatakwargsreturnc                    t        |t              st        j                  d       y	 t	        j
                  |d      }t        j                  |      }|r|dv rt        j                  d       yt        j                  |      }t        j                  ||      }t        j                  dd	|
      }|j                  |       |j                          |j                          t!        j"                  |j$                  ||      S # t&        t(        j*                  f$ r"}	t        j                  d|	d       Y d}	~	yd}	~	wt,        $ r"}
t        j                  d|
d       Y d}
~
yd}
~
ww xY w)a4  Decode base64 encoded data into an Attachment object.

        Args:
            raw_data: Base64 encoded string data
            context: Context string for filename generation.

        Returns:
            Attachment object with decoded data, or None if decoding fails or type is not recognizable
        z*Attachment data is not a string, skipping.NT)validate)zapplication/octet-streamz
text/plainz,Attachment type is not recognized, skipping.)	extensionwbF)modedeletesuffix)data	file_namecontent_typezDFailed to decode attachment data, reason: invalid base64. Reason: %s)exc_infoz,Failed to decode attachment data, reason: %s)
isinstancestrLOGGERwarningbase64	b64decoder	   detect_mime_typedebugget_file_extensioncreate_attachment_filenametempfileNamedTemporaryFilewriteflushcloser   
Attachmentname
ValueErrorbinasciiError	Exception)selfr   r   r   decoded_bytes	mime_typer   r   	temp_fileeexs              /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/api_objects/attachment/decoder_base64.pydecodezBase64AttachmentDecoder.decode   sH    (C(NNGH.	",,XEM (88GI 	-W WKL (::9EI (BB9I
 !33%	I OOM*OOOO ((^^yy  HNN+ 	LLV    	NN>T   	s+   AD 0BD E+ D==E+	E&&E+N)r   )__name__
__module____qualname____doc__r   r   r   r   r   r-   r:        r9   r   r      sN     ;BAA 67A 	A
 
*''	(Ar@   r   )r"   r0   loggingr(   typingr   r   r    r   r   r	   	getLoggerr;   r    AttachmentDecoderr   r?   r@   r9   <module>rF      sC        ) ) 2 2			8	$Gg77 Gr@   