
    j                     &   d dl Z d dlZd dlmZ d dlZddlmZmZ ddlmZ	 ddlm
Z
mZ ddlmZmZ dd	lmZ dd
lmZ  e j(                  e      ZdZ	 dde	j0                  dej2                  dej4                  deej6                     ddf
dZdeddfdZde	j0                  dej2                  dej4                  deej6                     ddf
dZdej@                  dejB                  dejD                  de	j0                  deej6                     ddfdZ#y)    N)Optional   )upload_clientfile_upload_monitor)upload_options   )format_helperss3_httpx_client)file_parts_strategys3_file_uploader)client)typesi   r   rest_clientupload_httpx_clientmonitorreturnc           	         	 t        | |||       | j                  rt        | j                         | j                  | j	                          y y # t
        $ ro}t        j                  d| j                  | j                  t        j                  | j                        |d       | j                  | j                  |        d }~ww xY w)N)r   r   httpx_clientr   zLFailed to upload attachment: '%s' from file: [%s] with size: [%s]. Error: %sT)exc_info)_do_upload_attachmentdelete_after_upload_delete_attachment_file	file_pathon_upload_success	ExceptionLOGGERerror	file_namer	   format_bytes	file_sizeon_upload_failed)r   r   r   r   es        w/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/file_upload/file_uploader.pyupload_attachmentr$      s    )#,		
 --#N$<$<= ++7,,. 8  Z$$$$''(@(@A 	 	
 **6++A.s   AA 	CA*CCr   c                     	 t        j                  |        y # t        $ r&}t        j	                  d|  d| d       Y d }~y d }~ww xY w)Nz#Failed to delete attachment file: 'z'. Reason: .)osunlinkOSErrorr   info)r   r"   s     r#   r   r   8   sG    V
		) V9)KPQsRSTUUVs    	AAAr   c                 z   t        j                  | j                  | j                  t         j                        }|j                         }t        j                  d| d| j                   d       t        j                  ||      }|j                  | || j                        }t        |j                        dkD  sJ d       |j                         rdt        j                  d	| j                   d
       t!        |||| |       t        j                  d| j                   d| j                   d       y t        j                  d	| j                   d       |j#                  |j                  d   | j                  t$        |       t        j                  d| j                   d| j                   d       y )N)r   r    max_file_part_sizez Initiating multipart upload for z parts of: 'z' file)r   r   )r   num_of_file_partsbase_url_pathr   z,At least one URL must be returned by backendzStarting attachment upload `z` to S3 directly.)upload_rest_client
file_partsupload_metadatar   r   zAttachment 'z' [z bytes] is uploaded to S3.z` to backend.)
upload_urlr   
chunk_sizer   z bytes] is uploaded to backend.)r   FilePartsStrategyr   r    MIN_FILE_PART_SIZE	calculater   debugr   r   RestFileUploadClientstart_uploadencoded_url_overridelenurlsshould_use_s3_uploaderupload_to_s3_directlyupload_file_localUPLOAD_CHUNK_SIZE)r   r   r   r   r0   parts_numberr/   r1   s           r#   r   r   ?   s    %66 ** **.AAJ
 '')L
LL
*<.^E]E]D^^de ';;l )55%&$99 6 O
 ##$q(X*XX(--/*>+C+C*DDUV	
 	1!+)	
 	>334C8P8P7QQkl	
 	*>+C+C*DMR	
 	,,&++A.$..(	 	- 	
 	>334C8P8P7QQpq	
    r/   r0   r1   c                    t        j                  ||j                  t        j                         |      }|j                         }|D cg c]-  }t        j                  |j                  |j                        / }}t        j                  d|j                   d|j                   d|j                   dt        |       d	       | j!                  |||       y c c}w )	N)r0   pre_sign_urlsr   r   )e_tagpart_numberzSent: z / z bytes of file z in z parts.)r   r1   r0   )r   S3FileDataUploaderr<   r
   
get_cacheduploadrest_api_typesMultipartUploadPartrE   rF   r   r7   
bytes_sentr    filer;   s3_upload_completed)	r/   r0   r1   r   r   s3_uploaderuploaded_partspartsent_file_partss	            r#   r>   r>   w   s     #55%**$//1	K !'')N #	 #D 	****$*:*:	
 #	   LL
''(J,@,@+AQ[Q`Q`Paaefijyfze{  |C  	D **%'" + s   
2C)N)$loggingr'   typingr   httpx r   r   r   file_upload_optionsr	   r
   s3_multipart_uploadr   r   rest_apir   rest_api_clientr   rJ   	getLogger__name__r   r@   FileUploadOptionsOpikApiClientFileUploadMonitorr$   strr   r   r8   r4   MultipartUploadMetadatar>    rB   r#   <module>rd      sY    	   0 3 . F 0 .			8	$#  @D	"'99" ((" " );;<	"
 
"JVs Vt V5
'995
 ((5
 ,,5
 );;<	5

 
5
p%::#55 #:: (99	
 );;< 
rB   