
    j                     2   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZ ddl	m
Z
 ddlmZ ddlmZ  ej                  e      Z	 ddej$                  d	ed
   dededededej*                  fdZdedefdZdedee   fdZdej$                  dee   fdZy)    N)LiteralOptional   )	mime_type)messages   )
attachmentattachment_dataentity_type)tracespan	entity_idproject_nameurl_overridedelete_after_uploadreturnc           
         | j                   t        d      t        |       }t        j                  |j                  d            j                  d      }t        | j                   t              rt        | j                         }d| _
        d}nlt        j                  j                  | j                         r| j                   }n6	 t        j                  | j                   d      }	t        |	      }d| _
        d}| j"                  }|}|t        j                  j%                  |      }| j                  rt'        |      }||}d}nd}t)        j*                  ||||||||      S # t        j                   $ r}
t        d      |
d }
~
ww xY w)NzAttachment data cannot be Nonezutf-8FT)validatezWAttachment data must be bytes, an existing file path, or a valid base64-encoded string.)	file_path	file_namer   r   r   r   encoded_url_overrider   )data
ValueErrorguess_attachment_typebase64	b64encodeencodedecode
isinstancebytes_write_file_like_to_temp_filecreate_temp_copyospathexists	b64decodebinasciiErrorr   basename_try_create_temp_copyr   CreateAttachmentMessage)r
   r   r   r   r   r   mimetypebase_url_pathr   decoded_byteser   should_delete_after_uploadtmp_file_paths                 /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/api_objects/attachment/converters.pyattachment_to_messager3      s}    #9::$_5H$$\%8%8%ABII'RM/&&.1/2F2FG	+0("	,,	-#((		",,_-A-ADQM5mDI/4O,"&  ))I!4GG$$Y/	 ''-i8$%I)-&).&++!*6	 	) ~~ 	i	s   5E' 'F:FF	file_likec                 l   t        j                  dd      }	 |j                  |        |j                          t        j                  d|j                         	 |j                          |j                  S # t        $ r}t        j                  d|d        d}~ww xY w# |j                          w xY w)	a  
    Writes a bytes-like file object to a temporary file on the filesystem.

    This function accepts a file-like object in the form of bytes and writes its
    contents to a temporary file. The temporary file is not deleted automatically
    on program termination. The path to the created temporary file is returned.

    Args:
        file_like: The bytes-like object containing the file content
            to be written to a temporary file.

    Returns:
        The full path to the created temporary file.
    wbF)modedeletez:Created temporary copy of file-like attachment to file: %sz5Failed to write file-like attachment to temp file: %sTexc_infoN)
tempfileNamedTemporaryFilewriteflushLOGGERdebugname	Exceptionerrorclose)r4   	temp_filer/   s      r2   r!   r!   K   s     ++eDI	"HNN	
 	>>  C 	 	

 	 	s$   AA7 7	B BBB! !B3r   c                    t         j                  j                  |       \  }}t        j                  dd|      }	 t        | d      5 }t        j                  ||       ddd       |j                          |j                          t        j                  d| |j                         |j                  S # 1 sw Y   VxY w# t        $ r+ |j                          t        j                  d| d	       Y yw xY w)
ap  
    Create a temporary copy of a file.

    This ensures the file remains available for upload even if the user
    deletes the original file. The temp file is created with delete=False
    so it persists until the upload manager processes and deletes it.

    Args:
        file_path: Path to the original file.

    Returns:
        Path to the temporary copy.
    r6   F)r7   r8   suffixrbNz.Created temporary copy of attachment: %s -> %szZFailed to create temporary copy of attachment: %s. Opik will try to use the original file.Tr9   )r#   r$   splitextr;   r<   openshutilcopyfileobjr>   rD   r?   r@   rA   rB   rC   )r   _	extensionrE   original_files        r2   r*   r*   o   s     77##I.LAy++eIVI)T"m}i8 #<NN	

 ~~ #"  h 	 	

 s*   C  B4AC  4B=9C   1C43C4c                 f   | j                   | j                   S d }| j                   t        j                  | j                        }||S t	        | j
                  t              r"t        j                  | j
                        }|S t	        | j
                  t              rt        j                  }|S )N)file)	content_typer   r   guess_mime_typer   r   strr    BINARY_MIME_TYPE)r
   r,   s     r2   r   r      s    ##/+++H  ,,,/2K2KL/&&,,,/2F2FG O 
O((%	0--O    )F)r   r'   loggingr#   rK   r;   typingr   r   file_uploadr   message_processingr    r	   	getLogger__name__r?   
AttachmentrT   boolr+   r3   r    r!   r*   r    rV   r2   <module>ra      s       	   $ $ * 			8	$ !&8**8)8 8 	8
 8 8 %%8v!U !s !H"S "Xc] "J:+@+@ Xc] rV   