
    ^(jK                        U 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
 h dZee   ed<   i aeeeee   ee   f   f   ed<   e
j&                  r&e j(                  j+                  e
j&                        Zn4e j(                  j/                  e j(                  j1                  e            Ze j(                  j5                  ed      Ze j(                  j5                  ed	      geft        d	<   e j(                  j5                  ed
      gdgft        d
<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      gdgft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      g e       ft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed      geft        d<   e j(                  j5                  ed       gd!hft        d <   e j(                  j5                  ed"      geft        d"<   e j(                  j5                  ed#      geft        d#<   e j(                  j5                  ed$      geft        d$<   e j(                  j5                  ed%      geft        d%<   e j(                  j5                  ed&      geft        d&<   e j(                  j5                  ed'      geft        d'<   e j(                  j5                  ed(      geft        d(<   e j(                  j5                  ed)      ae j(                  j5                  ed*      ae j(                  j5                  ed+      ae j(                  j5                  ed,      ai a eeeee   eee!f   e!f   f   ed-<    G d. d/      Z" e"       Z#d0d1d2a$d3ed4efd5Z%e j(                  jM                  t<              s	  e jN                  t<               d7ed4dfd8Z)d9ed4dfd:Z*d;ed4dfd<Z+d4efd=Z,d4efd>Z-d4efd?Z.d4efd@Z/dAed4dfdBZ0dCZ1dddDed4efdEZ2dFed4edz  fdGZ3dHed4edz  fdIZ4dJee   dKeedL      d4ee   fdMZ5dDed4eeedz  f   fdNZ6dedDedOedz  d4efdPZ7d4e8fdQZ9dfd3edRedSe8d4dfdTZ:d3ed4ee   fdUZ;dedVedWee   dz  d4eee   eee!f   f   fdXZ<dJee   dYee   d4ee   fdZZ=d3ed[ed4edz  fd\Z>d3ed[ed4efd]Z?d3ed4eee   eee!f   e!f   fd^Z@d3ed4eee   eee!f   e!f   dz  fd_ZAd3ed4ee   fd`ZBdgdaed7ed4eeeeCeef   fdbZDd4ee   fdcZEy#   ejP                  d6       Y axY w)h    N)LiteralList)
Collection)args>   .pt.bin.pkl.pt2.pth.sft.ckpt.safetensorssupported_pt_extensionsfolder_names_and_pathsmodelscheckpointsconfigsz.yamllorasvaetext_encodersclipunetdiffusion_modelsclip_visionstyle_models
embeddings	diffusersfolder
vae_approx
controlnett2i_adaptergligenupscale_modelslatent_upscale_modelscustom_nodeshypernetworks
photomakerclassifiers model_patchesaudio_encodersbackground_removalframe_interpolationgeometry_estimationoptical_flow	detectionoutputtempinputuserfilename_list_cachec            	           e Zd ZdZd Zddedeee   eee	f   e	f   fdZ
dedeee   eee	f   e	f   ddfdZd	 Zd
 Zd Zy)CacheHelperz9
    Helper class for managing file list cache data.
    c                      i | _         d| _        y NF)cacheactiveselfs    6/Users/danicosta/Desktop/Flux2/ComfyUI/folder_paths.py__init__zCacheHelper.__init__J   s    KM
    Nkeyreturnc                 V    | j                   s|S | j                  j                  ||      S N)r;   r:   get)r=   rA   defaults      r>   rE   zCacheHelper.getN   s#    {{Nzz~~c7++r@   valuec                 <    | j                   r|| j                  |<   y y rD   )r;   r:   )r=   rA   rG   s      r>   setzCacheHelper.setS   s    ;;#DJJsO r@   c                 8    | j                   j                          y rD   )r:   clearr<   s    r>   rK   zCacheHelper.clearW   s    

r@   c                     d| _         | S )NT)r;   r<   s    r>   	__enter__zCacheHelper.__enter__Z   s    r@   c                 2    d| _         | j                          y r9   )r;   rK   )r=   exc_type	exc_value	tracebacks       r>   __exit__zCacheHelper.__exit__^   s    

r@   rD   )__name__
__module____qualname____doc__r?   strtuplelistdictfloatrE   rI   rK   rM   rR    r@   r>   r7   r7   F   s|    ,s ,U49d3:>NPU3U-V ,
$s $5cDe4De)K#L $QU $r@   r7   imagemodel)webpfbxfolder_namerB   c                 0    ddd}|j                  | |       S )Nr   r   )r   r   )rE   )ra   legacys     r>   
map_legacyrd   i   s     (%'F::k;//r@   z Failed to create input directory
output_dirc                     | a y rD   output_directory)re   s    r>   set_output_directoryri   t   s    !r@   temp_dirc                     | a y rD   temp_directory)rj   s    r>   set_temp_directoryrn   x       Nr@   	input_dirc                     | a y rD   input_directory)rp   s    r>   set_input_directoryrt   |   s    Or@   c                      t         S rD   rg   r\   r@   r>   get_output_directoryrv      s    r@   c                      t         S rD   rl   r\   r@   r>   get_temp_directoryrx      s    r@   c                      t         S rD   rr   r\   r@   r>   get_input_directoryrz      s    r@   c                      t         S rD   user_directoryr\   r@   r>   get_user_directoryr~      s    r@   user_dirc                     | a y rD   r|   )r   s    r>   set_user_directoryr      ro   r@   __namec                 2   | rt        | t              st        d      | j                  dd      j	                         st        d|  d      | j                  d      rt        d      t        j                  j                  t               t         |        S )a  
    Get the path to a System User directory.

    System User directories (prefixed with '__') are only accessible via internal API,
    not through HTTP endpoints. Use this for storing system-internal data that
    should not be exposed to users.

    Args:
        name: System user name (e.g., "system", "cache"). Must be alphanumeric
              with underscores allowed, but cannot start with underscore.

    Returns:
        Absolute path to the system user directory.

    Raises:
        ValueError: If name is empty, invalid, or starts with underscore.

    Example:
        >>> get_system_user_directory("cache")
        '/path/to/user/__cache'
    z System user name cannot be empty_r)   zInvalid system user name: ''z1System user name should not start with underscore)
isinstancerW   
ValueErrorreplaceisalnum
startswithospathjoinr~   SYSTEM_USER_PREFIX)r   s    r>   get_system_user_directoryr      s    , z$,;<<<<R ((*6tfA>??sLMM77<<*,1C0DTF.KLLr@   user_idc                     | rt        | t              sy| j                  t              ryt        j
                  j                  t               |       S )ar  
    Get the path to a Public User directory for HTTP endpoint access.

    This function provides structural security by returning None for any
    System User (prefixed with '__'). All HTTP endpoints should use this
    function instead of directly constructing user paths.

    Args:
        user_id: User identifier from HTTP request.

    Returns:
        Absolute path to the user directory, or None if user_id is invalid
        or refers to a System User.

    Example:
        >>> get_public_user_directory("default")
        '/path/to/user/default'
        >>> get_public_user_directory("__system")
        None
    N)r   rW   r   r   r   r   r   r~   )r   s    r>   get_public_user_directoryr      s>    * *Wc2,-77<<*,g66r@   	type_namec                 ^    | dk(  r
t               S | dk(  r
t               S | dk(  r
t               S y )Nr1   r2   r3   )rv   rx   rz   )r   s    r>   get_directory_by_typer      s8    H#%%F!##G"$$r@   filescontent_types)r]   videoaudior^   c                    g }| D ]x  }|j                  d      d   }|t        vr;t        j                  |d      \  }}|s<|j                  d      d   }|t        |<   n	t        |   }||v sh|j	                  |       z |S )a  
    Example:
        files = os.listdir(folder_paths.get_input_directory())
        videos = filter_files_content_types(files, ["video"])

    Note:
        - 'model' in MIME context refers to 3D models, not files containing trained weights and parameters
    .F)strict/r   )splitextension_mimetypes_cache	mimetypes
guess_typeappend)r   r   resultfile	extension	mime_typer   content_types           r>   filter_files_content_typesr      s     FJJsOB'	55$//UCLIq$??3/2L3?%i04Y?L=(MM$  Mr@   c                     | j                  d      rt               }| d d } | |fS | j                  d      rt               }| d d } | |fS | j                  d      rt               }| d d } | |fS | d fS )Nz[output]iz[input]iz[temp]i)endswithrv   rz   rx   )r   base_dirs     r>   annotated_filepathr      s    }}Z ')CRy > 
y	!&(CRy > 
x	 %'CRy > Tzr@   default_dirc                     t        |       \  } }|||}n
t               }t        j                  j	                  ||       S rD   )r   rz   r   r   r   )r   r   r   s      r>   get_annotated_filepathr     s>    '-ND(""H*,H77<<$''r@   c                     t        |       \  } }|
t               }t        j                  j	                  ||       }t        j                  j                  |      S rD   )r   rz   r   r   r   exists)r   r   filepaths      r>   exists_annotated_filepathr     sE    '-ND(&(ww||Hd+H77>>(##r@   full_folder_path
is_defaultc                     t        |       } | t        v rgt        |    \  }}||v r.|rT|d   |k7  rK|j                  |       |j                  d|       y |r|j                  d|       y |j	                  |       y y y |gt               ft        | <   y Nr   )rd   r   removeinsertr   rI   )ra   r   r   paths_extss        r>   add_model_folder_pathr   !  s    [)K,,-k:uu$eAh*::-.Q 01Q 01-. ;z 1A/A35.I{+r@   c                 6    t        |       } t        |    d   d d  S r   )rd   r   )ra   s    r>   get_folder_pathsr   3  s!    [)K!+.q1!44r@   	directoryexcluded_dir_namesc                    t         j                  j                  |       sg i fS |g }g }i }	 t         j                  j                  |       || <   t        j                  dj                  |              t        j                  | dd      D ]  \  }}}|D cg c]	  }||vs| c}|d d  |D ]R  }	 t         j                  j                  t         j                  j                  ||      |       }	|j                  |	       T |D ]E  }t         j                  j                  ||      }
	 t         j                  j                  |
      ||
<   G  t        j                  dj                  t        |                   ||fS # t        $ r t        j                  d|  d       Y Tw xY wc c}w #  t        j                  d| d       Y xY w# t        $ r t        j                  d|
 d       Y w xY w)NzWarning: Unable to access z. Skipping this path.z#recursive file list on directory {}T)followlinkstopdownz. Skipping this file.zfound {} files)r   r   isdirgetmtimeFileNotFoundErrorloggingwarningdebugformatwalkrelpathr   r   len)r   r   r   dirsdirpathsubdirs	filenamesd	file_namerelative_pathr   s              r>   recursive_searchr   7  s   77==#2v!FDW''**95Y MM7>>yIJ
 (*wwydTX'Y#)!(HAA5G,GaH
"I "Wi0PR[ \m, # AWa0DWW--d3T
  (Z" MM"))#f+674<5  W4YK?TUVW I
"<YKG\ ]^ % "<TFBW XYs<   "E> 	F' F'0AF,'"G>"F$#F$,G
"G21G2
extensionsc                 F    t        t        t        fd|                   S )Nc                     t         j                  j                  |       d   j                         v xs t	              dk(  S )Nr   r   )r   r   splitextlowerr   )ar   s    r>   <lambda>z)filter_files_extensions.<locals>.<lambda>a  s<    (8(8(;B(?(E(E(G:(U(mY\]gYhlmYm(mr@   )sortedrY   filter)r   r   s    `r>   filter_files_extensionsr   `  s    $vmotuvwwr@   filenamec                    t        |       } | t        vryt        |    }t        j                  j	                  t        j                  j                  d|      d      }|d   D ]  }t        j                  j                  ||      }t        j                  j                  |      r|c S t        j                  j                  |      sft        j                  dj                  |              y)C
    Get the full path of a file in a folder, has to be a file
    Nr   r   z;WARNING path {} exists but doesn't link anywhere, skipping.)rd   r   r   r   r   r   isfileislinkr   r   r   )ra   r   foldersx	full_paths        r>   get_full_pathr   e  s    
 [)K00$[1Gwwrww||C:C@HQZGGLLH-	77>>)$WW^^I&OOY``ajkl  r@   c                 F    t        | |      }|t        d|  d| d      |S )r   zModel in folder 'z' with filename 'z' not found.)r   r   )ra   r   r   s      r>   get_full_path_or_raiser   y  s;     k84I"3K=@QRZQ[[g hiir@   c                    t        |       } t               }t        |    }i }|d   D ]7  }t        |dg      \  }}|j	                  t        ||d                i ||}9 t        t        |            |t        j                         fS )Nr   z.git)r      )
rd   rI   r   r   updater   r   rY   timeperf_counter)ra   output_listr   output_foldersr   r   folders_alls          r>   get_filename_list_r     s    [)K%K$[1GNQZ-aVHM{25'!*EF:N:k: 
 ${#$nd6G6G6IIIr@   c                 Z   t         j                  |       }||S t        |       } | t        vry t        |    }|d   D ]0  }|d   |   }|}t        j
                  j                  |      |k7  s0 y  t        |    }|d   D ]+  }t        j
                  j                  |      s#||d   vs+ y  |S )Nr   r   )	cache_helperrE   rd   r5   r   r   r   r   r   )ra   strong_cacheoutr   time_modifiedr   r   s          r>   cached_filename_list_r     s    ##K0L [)K--
k
*CVAq	77F#}4	  %[1GQZ77==A 
 Jr@   c                     t        |       } t        |       }|t        |       }|t        | <   t        j                  | |       t        |d         S r   )rd   r   r   r5   r   rI   rY   )ra   r   s     r>   get_filename_listr     sN    [)K

,C
{ -+.K([#&A<r@   filename_prefixc                 L   
 dt         dt        t        t         f   f fd}dt         dt        dt        dt         fd}d v r
 | ||       t        j                  j                  t        j                  j                               }t        j                  j                  t        j                  j                               
t        j                  j                  ||      }t        j                  j                  |t        j                  j                  |      f      |k7  rd	t        j                  j                  |      z   d
z   |z   dz   t        j                  j                  |t        j                  j                  |      f      z   }t        j                  |       t        |      	 t        t        
fdt!        |t        j"                  |                        d   dz   }	|
|	| fS # t$        $ r d}	Y t&        $ r t        j(                  |d       d}	Y 7w xY w)Nr   rB   c                     t        t        j                  j                              }| d |dz    }	 | |dz   d  }|j	                  d      d   }t        |j	                  d      d         }||fS #  d}Y ||fS xY w)Nr   r   r   r   )r   r   r   basenamer   int)r   
prefix_lenprefix	remainderbase_remainderdigitsr   s         r>   map_filenamez)get_save_image_path.<locals>.map_filename  s    ))/:;
/:>*	 a1I&__S1!4N--c2156F v~	Fv~s   9A0 0A9r3   image_widthimage_heightc                    | j                  dt        |            } | j                  dt        |            } t        j                         }| j                  dt        |j                              } | j                  dt        |j
                        j                  d            } | j                  dt        |j                        j                  d            } | j                  dt        |j                        j                  d            } | j                  dt        |j                        j                  d            } | j                  d	t        |j                        j                  d            } | S )
Nz%width%z%height%z%year%z%month%   z%day%z%hour%z%minute%z%second%)r   rW   r   	localtimetm_yeartm_monzfilltm_mdaytm_hourtm_mintm_sec)r3   r	  r
  nows       r>   compute_varsz)get_save_image_path.<locals>.compute_vars  s   i[)9:j#l*;<nnhCKK(89iSZZ)>)>q)ABgs3;;'7'='=a'@AhCKK(8(>(>q(ABj#cjj/*?*?*BCj#cjj/*?*?*BCr@   %zX**** ERROR: Saving image outside the output folder is not allowed.
 full_output_folder: z
         output_dir: z
         commonpath: c                     t         j                  j                  | d   d d       t         j                  j                        k(  xr | d   d   dk(  S )Nr   r   r   )r   r   normcase)r   r   s    r>   r   z%get_save_image_path.<locals>.<lambda>  sN    rww'7'7!Sb	'BbggFVFVW_F`'`'tefgheijlemqtet'tr@   r   r   T)exist_ok)rW   rX   r  r   r   dirnamenormpathr  r   
commonpathabspathr   error	Exceptionmaxr   maplistdirr   r   makedirs)r   re   r	  r
  r  r  	subfolderfull_output_foldererrcounterr   s   `         @r>   get_save_image_pathr)    s   	s 	uS#X 	
C 
c 
 
 
 o&\R 0 0 ABIww 0 0 ABHj)<	ww:rww7I'JKLPZZ(*,''//:L*MN'(*45 (( +-''*<*<j"''//ZlJm=n*op 	cnftvy  {G  IK  IS  IS  Tf  Ig  wh  i  j  kl  m  pq  q x)_LL   
&6s   0;G2 2H#?!H#"H#c                  |   t               } g }	 t        j                  j                  |       sg S t        j                  |       D ][  \  }}}t        j                  j                  ||       }|dk7  s-|j                  |j                  t        j                  d             ] t        |      S # t        $ r g cY S w xY w)zReturns a list of all subfolder paths in the input directory, recursively.

    Returns:
        List of folder paths relative to the input directory, excluding the root directory
    r   r   )rz   r   r   r   r   r   r   r   sepr   r   )rp   r   rootr   r   rel_paths         r>   get_input_subfoldersr.    s     $%IGww~~i(IWWY/MD$wwtY7H3x//<=	 0 g 	s    B- AB- 1;B- -B;:B;)systemrD   )F)r   r   )Fr   r   r   r   typingr   r   collections.abcr   comfy.cli_argsr   r   rI   rW   __annotations__r   rZ   rX   rY   base_directoryr   r  	base_pathr  realpath__file__r   
models_dirrh   rm   rs   r}   r5   r[   r7   r   r   rd   r   r$  r  ri   rn   rt   rv   rx   rz   r~   r   r   r   r   r   r   r   r   boolr   r   r   r   r   r   r   r   r   r   r  r)  r.  r\   r@   r>   <module>r:     s   	      & $l S l@B S%S	3s8(;"<<= B  3 34I 0 0 :;IWW\\)X.
*,'',,z=*Q)RTk(l } %&(ggll:y&I%JWI$V y !$&GGLLW$E#FH_"` w "$'',,z5"A!BD[ \ u ,.GGLL_,UWYW^W^WcWcdnpvWw+x  {R  +S  '/1ww||J/OQSQXQXQ]Q]^hj|Q}.~  AX  .Y ) **,'',,z=*Q)RTk(l } %+-77<<
N+S*TVm)n ~ &)+j,)O(PRi'j | $(*Z(M'NQYPZ&[ { #)+j,)O(PRi'j | $)+j,)OQSQXQXQ]Q]^hjwQx(y  |S  (T | $%'WW\\*h%G$HJa#b x  -/WW\\*FV-W,XZq+r ' (46GGLLMd4e3fh  3A . /+-77<<	>+R*SUXUZ)[ ~ &,.GGLL_,U+VXo*p  ')+j,)O(PRi'j | $*,'',,z=*Q)RUWTX(Y } %,.GGLL_,U+VXo*p  '-/WW\\*FV-W,XZq+r ' (13jJ^1_0`by/z + ,24'',,zK`2a1bd{0| , -24'',,zK`2a1bd{0| , -+-77<<
N+S*TVm)n ~ &(*Z(M'NPg&h { #77<<	84 i0'',,y'2i0LN T#uT#YS%Z0@%%GHHI N 8 }  
0C 0C 0
 
ww~~o&:O$"S "T "   3  4  c C S C     MC Ms M>7s 7sTz 7:S S4Z d3i WMoEp@q vz{~v 8S U3d
?%;  	( 	(3: 	(s 	($t $Js Jc Jt J`d J$5# 5$s) 5' 'cT9I 'SXY]^aYbdhilnsisdtYtSu 'Rx:c? x
3 xTXY\T] x
s c cDj ( s s JC JE$s)T#u*=Mu2T,U Js uT#YS%Z@PRW5W/X[_/_ 43 49 /M /M# /Mafgjloqtvy{~g~a /Mbd3i i:89s   \( (\?