o
    ·Ïi}J  ã                   @  s€  U d dl m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 h d£Zded< i ad	ed
< ejrDej ej¡Zn
ej ej e¡¡Zej ed¡Zej ed¡geftd< ej ed¡gdgftd< ej ed¡geftd< ej ed¡geftd< ej ed¡ej ed¡geftd< ej ed¡ej ed¡geftd< ej ed¡geftd< ej ed¡geftd< ej ed¡geftd< ej ed¡gdgftd< ej ed¡geftd< ej ed¡ej ed¡geftd< ej ed¡geftd< ej ed¡geftd< ej ed¡geftd< ej ed ¡geƒ ftd < ej ed!¡geftd!< ej ed"¡geftd"< ej ed#¡gd$hftd#< ej ed%¡geftd%< ej ed&¡geftd&< ej ed'¡aej ed(¡aej ed)¡aej ed*¡ai ad+ed,< G d-d.„ d.ƒZeƒ Z d/d0d1œa!dŒd5d6„Z"ej #t¡sµze $t¡ W n	   e %d7¡ Y dd:d;„Z&dŽd=d>„Z'dd@dA„Z(ddBdC„Z)ddDdE„Z*ddFdG„Z+ddHdI„Z,d‘dKdL„Z-dMZ.d’d“dPdQ„Z/d”dTdU„Z0d•dWdX„Z1d–d]d^„Z2d—d`da„Z3d˜d™dcdd„Z4dšdfdg„Z5d›dœdkdl„Z6ddmdn„Z7d˜dždsdt„Z8dŸdwdx„Z9d dzd{„Z:d¡d|d}„Z;d¢dd€„Z<d£d‚dƒ„Z=dd„d…„Z>d¤d¥dˆd‰„Z?d¦dŠd‹„Z@dS )§é    )ÚannotationsN)ÚLiteralÚList)Ú
Collection)Úargs>   ú.ptú.binú.pklú.pt2ú.pthú.sftú.ckptú.safetensorszset[str]Úsupported_pt_extensionsz%dict[str, tuple[list[str], set[str]]]Ú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ÚoutputÚtempÚinputÚuserz4dict[str, tuple[list[str], dict[str, float], float]]Úfilename_list_cachec                   @  sF   e Zd ZdZdd„ Zddd	d
„Zddd„Zdd„ Zdd„ Zdd„ Z	dS )ÚCacheHelperz9
    Helper class for managing file list cache data.
    c                 C  s   i | _ d| _d S ©NF)ÚcacheÚactive©Úself© r7   ú*/mnt/c/Users/fbmor/ComfyUI/folder_paths.pyÚ__init__B   s   
zCacheHelper.__init__NÚkeyÚstrÚreturnú)tuple[list[str], dict[str, float], float]c                 C  s   | j s|S | j ||¡S ©N)r4   r3   Úget)r6   r:   Údefaultr7   r7   r8   r?   F   s   zCacheHelper.getÚvalueÚNonec                 C  s   | j r
|| j|< d S d S r>   )r4   r3   )r6   r:   rA   r7   r7   r8   ÚsetK   s   ÿzCacheHelper.setc                 C  s   | j  ¡  d S r>   )r3   Úclearr5   r7   r7   r8   rD   O   s   zCacheHelper.clearc                 C  s
   d| _ | S )NT)r4   r5   r7   r7   r8   Ú	__enter__R   s   zCacheHelper.__enter__c                 C  s   d| _ |  ¡  d S r2   )r4   rD   )r6   Úexc_typeÚ	exc_valueÚ	tracebackr7   r7   r8   Ú__exit__V   s   zCacheHelper.__exit__r>   )r:   r;   r<   r=   )r:   r;   rA   r=   r<   rB   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r9   r?   rC   rD   rE   rI   r7   r7   r7   r8   r1   >   s    
r1   ÚimageÚmodel)ÚwebpÚfbxÚfolder_namer;   r<   c                 C  s   dddœ}|  | | ¡S )Nr   r   )r   r   )r?   )rR   Úlegacyr7   r7   r8   Ú
map_legacya   s   ÿrT   z Failed to create input directoryÚ
output_dirrB   c                 C  ó   | a d S r>   ©Úoutput_directory)rU   r7   r7   r8   Úset_output_directoryl   ó   rY   Útemp_dirc                 C  rV   r>   ©Útemp_directory)r[   r7   r7   r8   Úset_temp_directoryp   rZ   r^   Ú	input_dirc                 C  rV   r>   ©Úinput_directory)r_   r7   r7   r8   Úset_input_directoryt   rZ   rb   c                   C  ó   t S r>   rW   r7   r7   r7   r8   Úget_output_directoryx   ó   rd   c                   C  rc   r>   r\   r7   r7   r7   r8   Úget_temp_directory|   re   rf   c                   C  rc   r>   r`   r7   r7   r7   r8   Úget_input_directory€   re   rg   c                   C  rc   r>   ©Úuser_directoryr7   r7   r7   r8   Úget_user_directory„   s   rj   Úuser_dirc                 C  rV   r>   rh   )rk   r7   r7   r8   Úset_user_directory‡   rZ   rl   Ú__ÚsystemÚnamec                 C  s`   | rt | tƒstdƒ‚|  dd¡ ¡ std| › dƒ‚|  d¡r$tdƒ‚t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)Ú
isinstancer;   Ú
ValueErrorÚreplaceÚisalnumÚ
startswithÚosÚpathÚjoinrj   ÚSYSTEM_USER_PREFIX)ro   r7   r7   r8   Úget_system_user_directory’   s   
r{   Úuser_idú
str | Nonec                 C  s0   | rt | tƒs	dS |  t¡rdS t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)rr   r;   rv   rz   rw   rx   ry   rj   )r|   r7   r7   r8   Úget_public_user_directory±   s
   
r~   Ú	type_namec                 C  s.   | dkrt ƒ S | dkrtƒ S | dkrtƒ S d S )Nr,   r-   r.   )rd   rf   rg   )r   r7   r7   r8   Úget_directory_by_typeÎ   s   r€   Úfilesú	list[str]Úcontent_typesú1List[Literal['image', 'video', 'audio', 'model']]c                 C  sr   g }| D ]2}|  d¡d }|tvr)tj|dd\}}|sq|  d¡d }|t|< nt| }||v r6| |¡ q|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_typerp   Úcontent_typer7   r7   r8   Úfilter_files_content_types×   s   


€r“   útuple[str, str | None]c                 C  st   |   d¡rtƒ }| d d… } | |fS |   d¡r$tƒ }| d d… } | |fS |   d¡r6tƒ }| d d… } | |fS | d fS )Nz[output]i÷ÿÿÿz[input]iøÿÿÿz[temp]iùÿÿÿ)Úendswithrd   rg   rf   )ro   Úbase_dirr7   r7   r8   Úannotated_filepathó   s   


÷
úþr—   Údefault_dirc                 C  s6   t | ƒ\} }|d u r|d ur|}ntƒ }tj || ¡S r>   )r—   rg   rw   rx   ry   )ro   r˜   r–   r7   r7   r8   Úget_annotated_filepath  s   r™   Úboolc                 C  s4   t | ƒ\} }|d u rtƒ }tj || ¡}tj |¡S r>   )r—   rg   rw   rx   ry   Úexists)ro   r–   Úfilepathr7   r7   r8   Úexists_annotated_filepath  s
   r   FÚfull_folder_pathÚ
is_defaultc                 C  sŒ   t | ƒ} | tv r<t|  \}}||v r+|r'|d |kr)| |¡ | d|¡ d S d S d S |r5| d|¡ d S | |¡ d S |gtƒ ft| < d S ©Nr   )rT   r   ÚremoveÚinsertr   rC   )rR   rž   rŸ   ÚpathsÚ_extsr7   r7   r8   Úadd_model_folder_path  s   
ýr¥   c                 C  s   t | ƒ} t|  d d d … S r    )rT   r   )rR   r7   r7   r8   Úget_folder_paths+  s   r¦   Ú	directoryÚexcluded_dir_namesúlist[str] | Noneú"tuple[list[str], dict[str, float]]c              
     s`  t j | ¡s
g i fS ˆ d u rg ‰ g }i }z
t j | ¡|| < W n ty0   t d| › d¡ Y nw t d | ¡¡ t j	| dddD ]`\}}}‡ fdd„|D ƒ|d d …< |D ]#}zt j 
t j ||¡| ¡}| |¡ W qU   t d|› d¡ Y qU|D ]%}	t j ||	¡}
z
t j |
¡||
< W q{ ty    t d|
› d¡ Y q{w qAt d	 t|ƒ¡¡ ||fS )
NzWarning: Unable to access z. Skipping this path.z#recursive file list on directory {}T)ÚfollowlinksÚtopdownc                   s   g | ]}|ˆ vr|‘qS r7   r7   )Ú.0Úd©r¨   r7   r8   Ú
<listcomp>E  s    z$recursive_search.<locals>.<listcomp>z. Skipping this file.zfound {} files)rw   rx   ÚisdirÚgetmtimeÚFileNotFoundErrorÚloggingÚwarningÚdebugÚformatÚwalkÚrelpathry   r   Úlen)r§   r¨   rŽ   ÚdirsÚdirpathÚsubdirsÚ	filenamesÚ	file_nameÚrelative_pathr®   rx   r7   r¯   r8   Úrecursive_search/  s@   ÿþürÁ   úCollection[str]Ú
extensionsc                   s   t tt‡ fdd„| ƒƒƒS )Nc                   s$   t j | ¡d  ¡ ˆ v ptˆ ƒdkS )Nr†   r   )rw   rx   ÚsplitextÚlowerrº   ©Úa©rÃ   r7   r8   Ú<lambda>Y  s   $ z)filter_files_extensions.<locals>.<lambda>)ÚsortedÚlistÚfilter)r   rÃ   r7   rÈ   r8   Úfilter_files_extensionsX  s   rÍ   Úfilenamec                 C  s„   t | ƒ} | tvr
dS t|  }tj tj d|¡d¡}|d D ]!}tj ||¡}tj |¡r1|  S tj |¡r?t 	d 
|¡¡ qdS )ú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.)rT   r   rw   rx   r¹   ry   ÚisfileÚislinkr´   rµ   r·   )rR   rÎ   ÚfoldersÚxÚ	full_pathr7   r7   r8   Úget_full_path]  s   €rÕ   c                 C  s,   t | |ƒ}|du rtd| › d|› dƒ‚|S )rÏ   NzModel in folder 'z' with filename 'z' not found.)rÕ   r³   )rR   rÎ   rÔ   r7   r7   r8   Úget_full_path_or_raiseq  s   
rÖ   r=   c                 C  sp   t | ƒ} tƒ }t|  }i }|d D ]}t|dgd\}}| t||d ƒ¡ i |¥|¥}qtt|ƒƒ|t 	¡ fS )Nr   z.gitr¯   é   )
rT   rC   r   rÁ   ÚupdaterÍ   rÊ   rË   ÚtimeÚperf_counter)rR   Úoutput_listrÒ   Úoutput_foldersrÓ   r   Úfolders_allr7   r7   r8   Úget_filename_list_{  s   rÞ   ú0tuple[list[str], dict[str, float], float] | Nonec                 C  sž   t  | ¡}|d ur|S t| ƒ} | tvrd S t|  }|d D ]}|d | }|}tj |¡|kr2 d S qt|  }|d D ]}tj |¡rL||d vrL d S q;|S )Nr×   r   )	Úcache_helperr?   rT   r0   rw   rx   r²   r   r±   )rR   Ústrong_cacheÚoutrÓ   Útime_modifiedr   rÒ   r7   r7   r8   Úcached_filename_list_ˆ  s(   
ÿ€rä   c                 C  s@   t | ƒ} t| ƒ}|d u rt| ƒ}|t| < t | |¡ t|d ƒS r    )rT   rä   rÞ   r0   rà   rC   rË   )rR   râ   r7   r7   r8   Úget_filename_list¢  s   rå   Úfilename_prefixútuple[str, str, int, str, str]c           
        s:  d‡fdd„}ddd„}dˆv r|ˆ||ƒ‰t j t j ˆ¡¡}t j t j ˆ¡¡‰ t j ||¡}t j |t j |¡f¡|krbdt j |¡ d | d t j |t j |¡f¡ }t 	|¡ t
|ƒ‚ztt‡ fdd„t|t  |¡ƒƒƒd d }	W n ty„   d}	Y n ty•   t j|dd d}	Y nw |ˆ |	|ˆfS )NrÎ   r;   r<   útuple[int, str]c                   s^   t tj ˆ ¡ƒ}| d |d … }zt| |d d …  d¡d ƒ}W ||fS    d}Y ||fS )Nr×   rp   r   )rº   rw   rx   ÚbasenameÚintr‰   )rÎ   Ú
prefix_lenÚprefixÚdigits)ræ   r7   r8   Úmap_filename­  s    þz)get_save_image_path.<locals>.map_filenamer.   Úimage_widthrê   Úimage_heightc                 S  s¶   |   dt|ƒ¡} |   dt|ƒ¡} t ¡ }|   dt|jƒ¡} |   dt|jƒ d¡¡} |   dt|jƒ d¡¡} |   dt|jƒ d¡¡} |   dt|j	ƒ d¡¡} |   d	t|j
ƒ d¡¡} | S )
Nz%width%z%height%z%year%z%month%é   z%day%z%hour%z%minute%z%second%)rt   r;   rÙ   Ú	localtimeÚtm_yearÚtm_monÚzfillÚtm_mdayÚtm_hourÚtm_minÚtm_sec)r.   rï   rð   Únowr7   r7   r8   Úcompute_vars¶  s   z)get_save_image_path.<locals>.compute_varsú%zX**** ERROR: Saving image outside the output folder is not allowed.
 full_output_folder: z
         output_dir: z
         commonpath: c                   s4   t j | d d d… ¡t j ˆ ¡ko| d d dkS )Nr×   r†   rp   )rw   rx   ÚnormcaserÆ   )rÎ   r7   r8   rÉ   Ó  s   4 z%get_save_image_path.<locals>.<lambda>r   r×   T)Úexist_ok)rÎ   r;   r<   rè   )r.   r;   rï   rê   rð   rê   r<   r;   )rw   rx   ÚdirnameÚnormpathré   ry   Ú
commonpathÚabspathr´   ÚerrorÚ	ExceptionÚmaxrÌ   ÚmapÚlistdirrs   r³   Úmakedirs)
ræ   rU   rï   rð   rî   rû   Ú	subfolderÚfull_output_folderÚerrÚcounterr7   )rÎ   ræ   r8   Úget_save_image_path¬  s<   
	
ÿþþýý
.þr  c                  C  s~   t ƒ } g }z.tj | ¡sg W S t | ¡D ]\}}}tj || ¡}|dkr.| | tjd¡¡ qt	|ƒW S  t
y>   g  Y S w )z¸Returns 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ˆ   )rg   rw   rx   r›   r¸   r¹   r   rt   ÚseprÊ   r³   )r_   rÒ   Úrootr»   rp   Úrel_pathr7   r7   r8   Úget_input_subfoldersÛ  s   €
ÿr  )rR   r;   r<   r;   )rU   r;   r<   rB   )r[   r;   r<   rB   )r_   r;   r<   rB   )r<   r;   )rk   r;   r<   rB   )rn   )ro   r;   r<   r;   )r|   r;   r<   r}   )r   r;   r<   r}   )r   r‚   rƒ   r„   r<   r‚   )ro   r;   r<   r”   r>   )ro   r;   r˜   r}   r<   r;   )r<   rš   )F)rR   r;   rž   r;   rŸ   rš   r<   rB   )rR   r;   r<   r‚   )r§   r;   r¨   r©   r<   rª   )r   rÂ   rÃ   rÂ   r<   r‚   )rR   r;   rÎ   r;   r<   r}   )rR   r;   rÎ   r;   r<   r;   )rR   r;   r<   r=   )rR   r;   r<   rß   )r   r   )ræ   r;   rU   r;   r<   rç   )r<   r‚   )AÚ
__future__r   rw   rÙ   r‹   r´   Útypingr   r   Úcollections.abcr   Úcomfy.cli_argsr   r   Ú__annotations__r   Úbase_directoryrx   r  Ú	base_pathrÿ   ÚrealpathÚ__file__ry   Ú
models_dirrC   rX   r]   ra   ri   r0   r1   rà   rŠ   rT   r›   r  r  rY   r^   rb   rd   rf   rg   rj   rl   rz   r{   r~   r€   r“   r—   r™   r   r¥   r¦   rÁ   rÍ   rÕ   rÖ   rÞ   rä   rå   r  r  r7   r7   r7   r8   Ú<module>   sœ    $$$þ











	




)






/