
    +jK+                        d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
m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mZ d dlmZ d	d
lmZ d	dlmZmZmZmZmZ d	dlm Z  d	dl!m"Z"m#Z#m$Z$ d	dl%m&Z& h dZ'ddddddde(deee)         z  e(z  ez  dz  de*dz  de+de*de*de,ej-                 ddfdZ.dddde(de*de,ej-                 dej/        fdZ0ddddd e(de*de+de,ej-                 dej1        f
d!Z2dddde(de*de,ej-                 defd"Z3d#e(de(d$e+de(fd%Z4ddddd&de(d'e(d(e(dz  de*dz  de+de,ej-                 dej5        fd)Z6dS )*    N)BytesIO)Path)IO)urljoinurlparse)ClientErrorContentTypeError)	InputImplTypes)get_output_directory   )request_logger)default_base_urlget_auth_headeris_processing_interruptedsleep_with_interruptto_aiohttp_url)_diagnose_connectivity)ApiServerErrorLocalNetworkErrorProcessingInterrupted)bytesio_to_image_tensor>                        ?g       @)timeoutmax_retriesretry_delayretry_backoffclsurldestr!   r"   r#   r$   r%   returnc                &  K   t          |t          t          f          st          |d          st	          d          d}|}i }	t          |           }
|
j        sl|
j        se|t	          d          t          t                      
                    d          dz   |                     d                    } t          |          }		 |dz  }t          d	| |          }t          j        |
          }t          |t          t          f          }d}d}dd}d}	 t!          j        t$                    5  t'          j        |d	|            ddd           n# 1 swxY w Y   t          j        |          }t-          j                    fd}t-          j         |                      }t-          j        |                    t5          |           |	                    }t-          j        ||ht,          j                   d{V \  }}||v r`||v r\|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   t=          d          	 | d{V }n## t,          j        $ r t=          d          dw xY w|4 d{V  |j         dk    rt!          j        t$                    5  	 |!                                 d{V }nY# tD          t          f$ rE |#                                 d{V }tI          |          dk    r|ndtI          |           d}Y nw xY wt'          j        |d	| |j         tK          |j&                  |d|j                     ddd           n# 1 swxY w Y   |j         tN          v r||k    rtQ          ||ddd           d{V  ||z  }	 ddd          d{V  )                                 |rM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |ra|*                                sM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |rKt!          j        t$                    5  |+                                 d{V  ddd           n# 1 swxY w Y   |rYt!          j        t$                    5  |,                                 |+                                 ddd           n# 1 swxY w Y   t%          d|j          d          |r|t          t          |                    }t!          j        t$                    5  |j-        .                    dd           ddd           n# 1 swxY w Y   t_          |d          }|}n|}d}	 	 t-          j0        |j1        2                    d          d           d{V }n5# t,          j3        $ r d}Y n"t,          j        $ r t=          d          dw xY wti                      rt=          d          |s|j1        5                                rn)|6                    |           |tI          |          z  }t          |tn                    rFt!          j        t$                    5  |8                    d           ddd           n# 1 swxY w Y   t'          j        |d	| |j         tK          |j&                  d| d            	 ddd          d{V  )                                 |rM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |ra|*                                sM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |rKt!          j        t$                    5  |+                                 d{V  ddd           n# 1 swxY w Y   |r\t!          j        t$                    5  |,                                 |+                                 ddd           dS # 1 swxY w Y   dS dS # 1 d{V swxY w Y   nL# t,          j        $ r t=          d          dtr          tt          f$ r}||k    rt'          j        |d	| tw          |          j<         d!t          |           d"#           tQ          ||ddd           d{V  ||z  }Y d}~)                                 |rM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |ra|*                                sM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |rKt!          j        t$                    5  |+                                 d{V  ddd           n# 1 swxY w Y   |rYt!          j        t$                    5  |,                                 |+                                 ddd           n# 1 swxY w Y   
t{                       d{V }|d$         st}          d%          |t          d&          |d}~ww xY w	 )                                 |rM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |ra|*                                sM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |rKt!          j        t$                    5  |+                                 d{V  ddd           n# 1 swxY w Y   |rYt!          j        t$                    5  |,                                 |+                                 ddd           n# 1 swxY w Y   nv# )                                 |rM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |ra|*                                sM|                                 t!          j        t$                    5  | d{V  ddd           n# 1 swxY w Y   |rKt!          j        t$                    5  |+                                 d{V  ddd           n# 1 swxY w Y   |rZt!          j        t$                    5  |,                                 |+                                 ddd           w # 1 swxY w Y   w w xY wK)'a  Stream-download a URL to `dest`.

    `dest` must be one of:
      - a BytesIO (rewound to 0 after write),
      - a file-like object opened in binary write mode (must implement .write()),
      - a filesystem path (str | pathlib.Path), which will be opened with 'wb'.

    If `url` starts with `/proxy/`, `cls` must be provided so the URL can be expanded
    to an absolute URL and authentication headers can be applied.

    Raises:
        ProcessingInterrupted, LocalNetworkError, ApiServerError, Exception (HTTP and other errors)
    writezNdest must be a path (str|Path) or a binary-writable object providing .write().r   Nz<For relative 'cloud' paths, the `cls` parameter is required./Tr   GET)total)operation_idrequest_methodrequest_url)r!   c                     K   	                                   s@t                      rd S t          j        d           d {V                                    >d S d S # t          j        $ r Y d S w xY w)Nr    )is_setr   asynciosleepCancelledError)stop_evts   E/home/wildlama/comfy/ComfyUI/comfy_api_nodes/util/download_helpers.py_monitorz)download_url_to_bytesio.<locals>._monitorX   s      &oo// 1466 #"F%mC000000000 'oo// 1 1 1 1 1 -   FFs   "A .A A.-A.)headers)return_whenzTask cancelledi  i   z[text z bytes]zHTTP )r.   r/   r0   response_status_coderesponse_headersresponse_contenterror_messagezFailed to download (HTTP z).)parentsexist_okwbi   r        z
[streamed z bytes to dest])r.   r/   r0   r;   r<   r=   z: z (will retry))r.   r/   r0   r>   internet_accessiblezVUnable to connect to the network. Please check your internet connection and try again.z4The remote service appears unreachable at this time.)@
isinstancestrr   hasattr
ValueErrorr   schemenetlocr   r   rstriplstripr   _generate_operation_idaiohttpClientTimeout
contextlibsuppress	Exceptionr   log_request_responseClientSessionr3   Eventcreate_taskgetr   waitFIRST_COMPLETEDcancelr   r5   statusjsonr	   textlendictr9   _RETRY_STATUSr   setdonecloseflushparentmkdiropenwait_forcontentreadTimeoutErrorr   at_eofr*   r   seekr   OSErrortype__name__r   r   r   )r&   r'   r!   r"   r#   r$   r%   attemptdelayr9   
parsed_urlop_idtimeout_cfgis_path_sinkfhandlesessionmonitor_taskreq_taskr8   ra   pendingrespbodyr\   psinkwrittenchunkediagr6   s                                 @r7   download_url_to_bytesior       s7     . dS$K(( kw1G1G kijjjGE G#J 'Z%6 ';[\\\&((//44s:CJJsOOLL!#&&S$1&uc7;;+':::!$d4404)-,0(,G	$$Y// o o3W\jmnnnno o o o o o o o o o o o o o o +K@@@G}H     #.xxzz::L*7;;~c7J7JT[;+\+\]]H"),,/GU\Ul"m"m"mmmmmmmMD't##G(;(;!!!(33 # #"NNNNNNN# # # # # # # # # # # # # # #+,<===H%~~~~~~) H H H+,<==4GH  A A A A A A A A;#%%#,Y77  ^)-#4#4#4#4#4#4DD 0*= ^ ^ ^)-#4#4#4#4#4#4D+.t99+<+<44B]3t99B]B]B]DDD^ ';).+0(+15-1$,-?-?-1*?$+*?*?                   {m33;8N8N25#tT4PPPPPPPPP. +A A A A A A A A A A A An # '##%%%(33 ' '&&&&&&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' # #!!!(33 # #"NNNNNNN# # # # # # # # # # # # # # # *(33 * *!--//)))))))* * * * * * * * * * * * * * * $(33 $ $MMOOOMMOOO$ $ $ $ $ $ $ $ $ $ $ $ $ $ $_ $$O$O$O$OPPP  SYYA#,Y77 D DtdCCCD D D D D D D D D D D D D D D"1dmmG"DDD*P&-&6t|7H7H7U7U_b&c&c&c c c c c c c"/ $ $ $ #"1 P P P34DEE4OP 122 F34DEEE  !<..00 "! JJu%%%s5zz)G#*& dG,, %#,Y77 % %		!% % % % % % % % % % % % % % % 3!&#( #)-%)$,%7%7%J'%J%J%J    CA A A A A A A A A A A An # '##%%%(33 ' '&&&&&&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' # #!!!(33 # #"NNNNNNN# # # # # # # # # # # # # # # *(33 * *!--//)))))))* * * * * * * * * * * * * * * $(33 $ $MMOOOMMOOO$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$ $IA A A A A A A A A A A A A A AD % 	D 	D 	D'(899tCW% 	` 	` 	`+%%3!&#( #%)!WW%5"N"NQ"N"N"N	    +5#tT4HHHHHHHHH& # '##%%%(33 ' '&&&&&&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' # #!!!(33 # #"NNNNNNN# # # # # # # # # # # # # # # *(33 * *!--//)))))))* * * * * * * * * * * * * * * $(33 $ $MMOOOMMOOO$ $ $ $ $ $ $ $ $ $ $ $ $ $ $+ 011111111D-. 'l  !!WXX^__#	`IAn # '##%%%(33 ' '&&&&&&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' # #!!!(33 # #"NNNNNNN# # # # # # # # # # # # # # # *(33 * *!--//)))))))* * * * * * * * * * * * * * * $(33 $ $MMOOOMMOOO$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ # '##%%%(33 ' '&&&&&&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' # #!!!(33 # #"NNNNNNN# # # # # # # # # # # # # # # *(33 * *!--//)))))))* * * * * * * * * * * * * * * $(33 $ $MMOOOMMOOO$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$aS$s1  c 3Ec Ec EC"c 	I
c Ic Ic 1I: 9c : Jc %&c	NK('N(AL>;N=L>>=N;c	N	c	N	8c	c 	P22P69P6 	RRR;S""S&)S&)T==UU
Ac	W6c	W	c		W	
c	&4Xc	Y,c	.YBc	 \6c	\	c		\	
9c	c 	^..^25^2<	```7aa"%a")b::b>b>	
cc cc r 0l%A"l .r 7	ggg	h//h36h3i<<j j ")kkk$<l  l%%r .	nn
n	o&&o*-o*p33p7:p7)rrrAx!	s6*x6s::x=s:>Ax	uxux u!x?v&x&v**x-v*.x)x5xxxx	xr!   r%   c                r   K   t                      }t          | |||           d{V  t          |          S )z@Downloads an image from a URL and returns a [B, H, W, C] tensor.r   N)r   r   r   r&   r!   r%   results       r7   download_url_to_image_tensorr      sH       YYF
!#vwC
H
H
HHHHHHHH"6***rB   r!   r"   r%   	video_urlc                ~   K   t                      }t          | ||||           d{V  t          j        |          S )z:Downloads a video from a URL and returns a `VIDEO` output.r   N)r   r   r
   VideoFromFile)r   r!   r"   r%   r   s        r7   download_url_to_video_outputr      sO       YYF
!)VWR]cf
g
g
gggggggg"6***rB   c                X   K   t                      }t          | |||           d{V  |S )zFDownloads content from a URL and returns a new BytesIO (rewound to 0).r   N)r   r   r   s       r7   download_url_as_bytesior      s?       YYF
!#vwC
H
H
HHHHHHHHMrB   methodrp   c           	      @   	 t          |          }|j                            dd          d         p|j        pd                    d                              dd          }n# t          $ r d}Y nw xY w|  d| d| dt          j                    j	        d d          S )Nr+   r   download__try   )
r   pathrsplitrI   stripreplacerQ   uuiduuid4hex)r   r&   rp   parsedslugs        r7   rL   rL      s    #""3**2.M&-M:TTUXYYaabegjkk   AAtAAAA4:<<+;BQB+?AAAs   A A# #A21A2)task_idr!   r"   r%   file_formatr   c                  K   |                     d                                          }t                      }t          | ||||           d{V  |at	          t                                }|| d| z  }|                    |                                           |                    d           t          j
        ||          S )zDownloads a 3D model file from a URL into memory as BytesIO.

    If task_id is provided, also writes the file to disk in the output directory
    for backward compatibility with the old save-to-disk behavior.
    .r   Nr   )sourcer   )rK   lowerr   r   r   r   write_bytesgetvaluerl   r   File3D)	r&   r   r   r!   r"   r%   data
output_diroutput_paths	            r7   download_url_to_file_3dr   	  s       $$S))//11K99D
!           .0011
 g#=#=#=#==000		!<t====rB   )7r3   rO   r   ior   pathlibr   typingr   urllib.parser   r   rM   torchaiohttp.client_exceptionsr   r	   comfy_api.latestCOMFY_IOr
   r   folder_pathsr    r   _helpersr   r   r   r   r   clientr   common_exceptionsr   r   r   conversionsr   r_   rE   bytesfloatintrn   	ComfyNoder   Tensorr   r   r   r   rL   r   r    rB   r7   <module>r      s                           * * * * * * * *   C C C C C C C C + + + + + + - - - - - - - - - - - - - -                    + * * * * * W W W W W W W W W W 0 0 0 0 0 0... !$(x$ x$ x$	x$
BuI

#d
*T
1x$ T\	x$
 x$ x$ x$ 
h 	!x$ 
x$ x$ x$ x$| $(		+ 	+ 	+		+ 	+ 
h 	!		+
 \	+ 	+ 	+ 	+ $(
+ 
+ 
+
+ 
+ 	
+
 
h 	!
+ 
+ 
+ 
+ 
+  $(		 	 			 	 
h 	!		
 	 	 	 	B3 BS B3 B3 B B B B  $( >  >  >	 > > 4Z	 >
 T\ >  > 
h 	! > \ >  >  >  >  >  >rB   