
    +j.6                        S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJr  S SK	r	S SK
r
S SKJrJr  S SKJrJrJr  SSKJr  SSKJrJr  SS	KJrJrJrJr  SS
KJrJrJr  SSK J!r!J"r"J#r#   " S S\5      r$ " S S\5      r%SSSSSS.S\&\RN                     S\
RP                  \)\
RP                     -  S\*S\+S-  S\+S-  S\,S\*S-  S\)\+   4S jjr-SSSS.S\&\RN                     S\
RP                  S\+S-  S\+S-  S\*S-  S\+4S jjr.S S!S"S#.S\&\RN                     S$\R^                  S%\+S&\+S\+S\+4S' jjr0\Rb                  Rd                  \Rf                  Rh                  SSS(.S\&\RN                     S)\Rj                  S*\Rb                  S+\Rf                  S,\*S-  S\+S-  S\+4S- jjr6S.S/S0S1.r7S\&\RN                     S2\Rp                  S3\+S\+4S4 jr9  SJS\&\RN                     S5\S6\+S7\+S-  S\+S-  S8\:S-  S\+4S9 jjr;SS:S;S<SSS=.S\&\RN                     S>\+S?\\+-  S@\+S-  SA\*SB\:SC\:S\+S-  S8\:S-  SS4SD jjr<SE\+SF\+SG\*SH\+S\+4
SI jr=g)K    N)BytesIO)urlparse)	BaseModelField)IOInputTypes   )request_logger)is_processing_interruptedsleep_with_interrupt)ApiEndpoint_diagnose_connectivity_display_time_progresssync_op)ApiServerErrorLocalNetworkErrorProcessingInterrupted)audio_ndarray_to_bytesio"audio_tensor_to_contiguous_ndarraytensor_to_bytesioc                   L    \ rS rSr% \" SSS9r\\S'   \" SSS9r\S-  \S'   S	r	g)
UploadRequest   .zFilename to uploaddescription	file_nameNzJMime type of the file. For example: image/png, image/jpeg, video/mp4, etc.content_type )
__name__
__module____qualname____firstlineno__r   r   str__annotations__r   __static_attributes__r       C/home/wildlama/comfy/ComfyUI/comfy_api_nodes/util/upload_helpers.pyr   r      s/    3,@AIsA$` L#* r'   r   c                   F    \ rS rSr% \" SSS9r\\S'   \" SSS9r\\S'   Sr	g	)
UploadResponse'   .zURL to GET uploaded filer   download_urlzURL to PUT file to upload
upload_urlr   N)
r    r!   r"   r#   r   r,   r$   r%   r-   r&   r   r'   r(   r*   r*   '   s&    c/IJL#JC-HIJIr'   r*      	UploadingTi  @ 
max_images	mime_type
wait_labelshow_batch_indextotal_pixelsclsimager1   r2   r3   r4   r5   returnc          	      $  ^^#    / n[        T[        5      (       am  T Hf  m[        TR                  5      S:  nU(       a3  UR	                  U4S j[        TR                  S   5       5       5        MU  UR                  T5        Mh     Ob[        TR                  5      S:  nU(       a2  UR	                  U4S j[        TR                  S   5       5       5        OUR                  T5        / n	[        [        U5      U5      n
[        R                  " 5       n[        U
5       Hf  nX|   n[        XUS9nUnU(       a  U(       a  U
S:  a  U SUS-    SU
 S	3n[        XUR                  X?U5      I S
h  vN nU	R                  U5        Mh     U	$  N7f)z~
Uploads images to ComfyUI API and returns download URLs.
To upload multiple images, stack them in the batch dimension first.
   c              3   .   >#    U  H
  nTU   v   M     g 7fNr   ).0iimgs     r(   	<genexpr>,upload_images_to_comfyapi.<locals>.<genexpr>?   s     C/B!s1v/B   r   c              3   .   >#    U  H
  nTU   v   M     g 7fr<   r   )r=   r>   r7   s     r(   r@   rA   E   s     C-B58-BrB   )r5   r2   r
   z (/)N)
isinstancelistlenshapeextendrangeappendmintime	monotonicr   upload_file_to_comfyapiname)r6   r7   r1   r2   r3   r4   r5   tensorsis_batchdownload_urlsnum_to_uploadbatch_start_tsidxtensorimg_ioeffective_labelurlr?   s    `               @r(   upload_images_to_comfyapir\   ,   sO     #%G%C399~)HCuSYYq\/BCCs#  u{{#a'NNCU5;;q>-BCCNN5!  "MGj1M^^%N]#"6PYZ$*}q/@!+BsQwiqqIO+CibpqqS! $  rs   E.F2F3F)r2   r3   r5   c          
      B   #    [        U USUUSUS9I Sh  vN S   $  N7f)zCUploads a single image to ComfyUI API and returns its download URL.r
   Fr0   Nr   )r\   )r6   r7   r2   r3   r5   s        r(   upload_image_to_comfyapir^   [   s=      (!"%
 	
 
	 
		
s   mp4aacz	audio/mp4)container_format
codec_namer2   audiora   rb   c                   #    US   nUS   n[        U5      n[        XuX#5      n[        X[        R                  " 5        SU 3U5      I Sh  vN $  N7f)z
Uploads a single audio input to ComfyUI API and returns its download URL.
Encodes the raw waveform into the specified format before uploading.
sample_ratewaveform.N)r   r   rP   uuiduuid4)	r6   rc   ra   rb   r2   re   rf   audio_data_npaudio_bytes_ios	            r(   upload_audio_to_comfyapirl   q   s[      ]+K":.H6x@M-mJZgN(aP`Oa>bdmnnnns   A
AAA)	containercodecmax_durationr3   videorm   rn   ro   c                  #    Ub*   UR                  5       nXd:  a  [        SUS SU S35      e SUR                  R                  5        3n[        R                  " 5        S	UR                  R                  5        3n	[        5       n
UR                  XUS
9  U
R                  S5        [        X
XU5      I Sh  vN $ ! [         a4  n[        R                  " S[        U5      5        [        SU 35      UeSnAff = f NE7f)z
Uploads a single video to ComfyUI API and returns its download URL.
Uses the specified container and codec for saving the video before upload.
NzVideo duration (z.2fz s) exceeds the maximum allowed (zs).z Error getting video duration: %sz-Could not verify video duration from source: zvideo/rg   )formatrn   r   )get_duration
ValueError	Exceptionloggingerrorr$   valuelowerrh   ri   r   save_toseekrP   )r6   rp   rm   rn   ro   r3   actual_durationeupload_mime_typefilenamevideo_bytes_ios              r(   upload_video_to_comfyapir      s     	Y#002O- &s&;;[\h[iilm  .  	 5 5 789**,q!6!6!8 9:H YN	MM.%M@(hZdeee  	YMM<c!fELQCPQWXX	Y fs4   D(B? BD:D ;D?
C=	/C88C==Dzmodel/gltf-binaryz	model/objapplication/octet-stream)glbobjfbxmodel_3dfile_formatc           	         #    [        U UR                  5       [        R                  " 5        SU 3[        R                  US5      5      I Sh  vN $  N7f)zDUploads a 3D model file to ComfyUI API and returns its download URL.rg   r   N)rP   get_datarh   ri   _3D_MIME_TYPESget)r6   r   r   s      r(   upload_3d_model_to_comfyapir      sR      )::<.+';(BC	   s   AAAAfile_bytes_ior   r~   progress_origin_tsc           	         #    Uc
  [        US9nO	[        X#S9n[        U [        SSS9U[        SSS9I Sh  vN n[	        U UR
                  UUUUS	9I Sh  vN   UR                  $  N0 N7f)
zBUploads a single file to ComfyUI API and returns its download URL.N)r   )r   r   z/customers/storagePOST)pathmethodF)endpointdataresponse_modelfinal_label_on_successmonitor_progress)r   r3   r   )r   r   r   r*   upload_filer-   r,   )r6   r   r   r~   r3   r   request_objectcreate_resps           r(   rP   rP      s      &:&Y"6vF%# K %-   ###!s!   4A+A'A+A)A+)A+r:         ?g       @)r   max_retriesretry_delayretry_backoffr3   r   r-   filer   r   r   r   c                
  ^ ^^^#    [        U[        5      (       aE  [        R                  " [        5         UR                  S5        SSS5        UR                  5       n	OF[        U[        5      (       a&  [        US5       n
U
R                  5       n	SSS5        O[        S5      e0 n[        5       nU(       a  X;S'   OUR                  S5        SnUnUb  UO[        R                  " 5       m[        R                  " 5       R                   SS n US-  n[#        SXU5      n[$        R&                  " SS	9n[(        R*                  " 5       mU UUU4S
 jn[(        R,                  " U" 5       5      nSn [.        R0                  " USUU=(       d    SSS[3        W	5       S3S9  [$        R4                  " US9nUR7                  XXS9n[(        R,                  " U5      n[(        R8                  " UU1[(        R:                  S9I Sh  vN u  nnUU;   a!  UU;   a  UR=                  5         [?        S5      e UI Sh  vN nU ISh  vN   URB                  S:  Ga  [        R                  " [        5          URE                  5       I Sh  vN nSURB                   3n[.        R0                  " USUURB                  [I        URJ                  5      UUS9  SSS5        URB                  S;   a  X::  a  [M        UT TTST(       a  [N        OSS9I Sh  vN   X-  n SSS5      ISh  vN   TR                  5         U(       a=  UR=                  5         [        R                  " [        5         UI Sh  vN   SSS5        U(       d  GMw  [        R                  " [        5         URQ                  5       I Sh  vN   SSS5        GM  [	        SURB                   S35      e[.        R0                  " USUURB                  [I        URJ                  5      SS9   SSS5      ISh  vN   TR                  5         U(       a=  UR=                  5         [        R                  " [        5         UI Sh  vN   SSS5        U(       a<  [        R                  " [        5         URQ                  5       I Sh  vN   SSS5        gg! , (       d  f       GN|= f! , (       d  f       GN7= f GN GN! [(        R@                   a    [?        S5      Sef = f GN GN! [         a    URG                  5       I Sh  vN  n GNf = f! , (       d  f       GNn= f GN> GN, GN! , (       d  f       GN= f GN! , (       d  f       GMk  = f GN^ GN! , (       d  f       GN= f N! , (       d  f       g= f! , ISh  vN  (       d  f       GO= f! [(        R@                   a    [?        S5      Se[$        RR                  [T        4 Ga  nX::  Ga@  [.        R0                  " USUU=(       d    SS[3        W	5       S3[W        U5      RX                   S[        U5       S3S9  [M        UT TTST(       a  [N        OSS9I Sh  vN    X-  n SnATR                  5         U(       aP  UR=                  5         [        R                  " [        5         UI Sh  vN    SSS5        O! , (       d  f       O= fU(       d  GM  [        R                  " [        5         URQ                  5       I Sh  vN    SSS5        GM!  ! , (       d  f       GM1  = f[[        5       I Sh  vN  nUS   (       d  []        S 5      Ue[_        S!5      UeSnAff = f TR                  5         U(       aP  UR=                  5         [        R                  " [        5         UI Sh  vN    SSS5        O! , (       d  f       O= fU(       aN  [        R                  " [        5         URQ                  5       I Sh  vN    SSS5        O! , (       d  f       O= fO! TR                  5         U(       aP  UR=                  5         [        R                  " [        5         UI Sh  vN    SSS5        O! , (       d  f       O= fU(       aN  [        R                  " [        5         URQ                  5       I Sh  vN    SSS5        f ! , (       d  f       f = ff = fGM  7f)"z
Upload a file to a signed URL (e.g., S3 pre-signed PUT) with retries, Comfy progress display, and interruption.

Raises:
    ProcessingInterrupted, LocalNetworkError, ApiServerError, Exception
r   Nrbz2file must be a BytesIO or a filesystem path stringzContent-Typer.   r
   PUT)totalc                  f  >#     TR                  5       (       dz  [        5       (       a  g T(       a-  [        T T[        [        R
                  " 5       T-
  5      S 5        [        R                  " S5      I S h  vN   TR                  5       (       d  My  g g  N! [        R                   a     g f = f7f)Nr   )	is_setr   r   intrN   rO   asynciosleepCancelledError)r6   start_tsstop_evtr3   s   r(   _monitorupload_file.<locals>._monitor	  s     "//++022!.sJDNNDTW_D_@`bfg!--,,, #//++
 -)) sE   B1$B B1AB 7B8B B1B B.+B1-B..B1z[File data z bytes])operation_idrequest_methodrequest_urlrequest_headersrequest_paramsrequest_data)timeout)r   headersskip_auto_headers)return_whenzUpload cancelledi  zUpload failed with status )r   r   r   response_status_coderesponse_headersresponse_contenterror_message>               )display_callbackzFailed to upload (HTTP z).zFile uploaded successfully.)r   r   r   r   r   r   zTask cancelledz: z (will retry))r   r   r   r   r   r   internet_accessiblezVUnable to connect to the network. Please check your internet connection and try again.z1The API service appears unreachable at this time.)0rF   r   
contextlibsuppressru   r{   readr$   openrt   setaddrN   rO   rh   ri   hex_generate_operation_idaiohttpClientTimeoutr   Eventcreate_taskr   log_request_responserH   ClientSessionputwaitFIRST_COMPLETEDcancelr   r   statusjsontextdictr   r   r   closeClientErrorOSErrortyper    r   r   r   ) r6   r-   r   r   r   r   r   r3   r   r   fr   r   attemptdelayop_uuidr   r   r   monitor_tasksessreqreq_taskdonependingrespbodymsgr}   diagr   r   s    `      `                      @@r(   r   r      s    $ $    +IIaL ,yy{	D#		$668D  MNN G"%%".n-GE%7%C!IYHjjlr"G
1-eZ'R''d3==?		 		 **8:6-1d	'//)$& '4#*3t9+W= ((9D((:'(gC**3/H"),,,/GU\UlUl"mmMD't#G(;!+,>??J%~ t;;#%#,,Y75)-#4D !;4;;-H&;;)5+0(215-1$,,-?-1*- 8 {{&DDI_2!&$ GQ-CW[   . 7 tH LLN##%((3&&& 4t((3**,&& 43] $&=dkk]"$MNN33!-#( *)-%)$,,%7%B K tH LLN##%((3&&& 4((3**,&& 43 S ,+ b n &)) J+,>?TIJ  $5( 5)-#4#4D5 87 % P ' 43 ' 43U P ' 43 ' 43U ttL %% 	D'(89tC##W- 	]%33!-#( *$+Ot#.s4yk!A%)!W%5%5$6bQ"N +?I%;t   & LLN##%((3&&& 433t((3**,&& 4333 0111D-.'l !!TU[\\5	]Q H LLN##%((3&&& 433((3**,&& 433  LLN##%((3&&& 433((3**,&& 433 k s>  0dR=:dSCd3BW S!.W 4S' 9S$:S' >W TW ,V)4T;6T	T
TAT;;V)U	V)W $U%W )Ad*U0U1U5dd!U+5U(6U+:dAV)W  U>!W %Ad&V,V-V1)dV.V/V3
d=
Sd
Sd!W $S' '!TW TT8.T1
/T84T;7T88T;;
U
		V)W U
U% d(U++
U;5	d>W V
VdV
V&"d)W/V20W<W ?`? W :]>>A5]93Y64	]9=`? Ad[[	[	d
[%!d2d\1 \#!\1&d1
];	d]9]']99]>>`? Ad_	_
_	d
_&"(d
`-`!`-$	d-
`;7d?Ac=bb

b	c=
b$ (c=c+c
c+"	c=+
c95c==dr   r[   r   r   c                     [        U5      nUR                  R                  SS5      S   =(       d    UR                  =(       d    SR	                  S5      R                  SS5      nU  SU SU SU 3$ ! [         a    Sn Nf = f)NrD   r
   upload__try)r   r   rsplitnetlocstripreplaceru   )r   r[   r   r   parsedslugs         r(   r   r   }  s    #""3*2.K&--K8RRSVW__`cehi XQtfAgYd7)44  s   A%A4 4BB)r/   N)>r   r   rv   rN   rh   ior   urllib.parser   r   torchpydanticr   r   comfy_api.latestr   r   r	    r   _helpersr   r   clientr   r   r   r   common_exceptionsr   r   r   conversionsr   r   r   r   r*   r   	ComfyNodeTensorrG   r   r$   boolr\   r^   Audiorl   VideoContainerMP4
VideoCodecH264Videor   r   File3Dr   floatrP   r   r   r   r'   r(   <module>r     s         !   % - -  E  X W I JY J  (!*,	bll	,<<$u||,,, 	,
 Tz, d
, , *, 
#Y,f !(*		bll		<<	 Tz		
 d
	 *	 		4 " o	bll	o;;o 	o
 o o 	o. ',&:&:&>&>#..33#( f	bll	 f;; f ##	 f
  f * f d
 f 	 fH %	bll	ll  		& )'+$	bll	$$ $ Dj	$
 d
$ $ 	$J  $!'+^'	bll	^'^' C-^'
 *^' ^' ^' ^' d
^' ^' 
^'B53 5S 53 5 5QT 5r'   