
    +j=                        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m	Z	m
Z
mZmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#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/m0Z0m1Z1m2Z2 d d
l3m4Z4 	 d3de5de5de,dz  fdZ6	 	 	 	 	 	 	 d4de5de5dz  dee5         dz  de0de5de5de5dz  de5dz  de,fdZ7	 d5de5de5de8de8fdZ9	 	 d6de5de5dz  de5de,fdZ:de5de8fdZ;de5de+dz  fdZd Z<	 	 	 	 	 	 	 	 	 	 d7de5d$ee5         dz  d%ee5         dz  d&e5dz  d'e=dz  d(e>d)e>d*e5d+e5d,e5dz  de/fd-Z?d.ede@fd/ZAd*e5d+e5de5dz  fd0ZB	 d3de5de5de.dz  fd1ZC	 d3de5de5de.fd2ZDdS )8    N)timezone)Sequence)CursorPayloadInvalidCursorErrordecode_cursordecode_cursor_intdecode_cursor_timeencode_cursorencode_cursor_from_time)Asset)asset_exists_by_hashreference_exists_for_asset_iddelete_reference_by_idfetch_reference_and_assetsoft_delete_reference_by_idfetch_reference_asset_and_tagsget_asset_by_hashget_reference_by_idget_reference_with_owner_checklist_references_pagelist_all_file_paths_by_asset_idlist_references_by_asset_idset_reference_metadataset_reference_previewset_reference_tagsupdate_asset_hash_and_mimeupdate_reference_access_timeupdate_reference_nameupdate_reference_updated_at)select_best_live_path)compute_relative_filename)	AssetDataAssetDetailResultAssetSummaryDataDownloadResolutionResultListAssetsResultUserMetadataextract_asset_dataextract_reference_data)create_session reference_idowner_idreturnc                     t                      5 }t          || |          }|s	 d d d            d S |\  }}}t          t          |          t	          |          |          cd d d            S # 1 swxY w Y   d S )Nr,   r-   refassettags)r*   r   r#   r)   r(   )r,   r-   sessionresultr2   r3   r4   s          D/home/wildlama/comfy/ComfyUI/app/assets/services/asset_management.pyget_asset_detailr8   7   s     
		 
W/%
 
 

  	
 
 
 
 
 
 
 
 "UD &s++$U++
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   A01A00A47A4manualnamer4   user_metadata
tag_origin	mime_type
preview_idc                 P   t                      5 }t          || |          }	d}
|||	j        k    rt          || |           d}
|	j        rt          |	j                  nd }d }|t          |          }n3|r1|	j        pi }|                    d          |k    rt          |          }||r||d<   t          || |           d}
|t          || ||           d}
|t          ||	j        |          }|rd}
|t          || |           d}
|
r|t          || 	           t          || |
          }|st!          d          |\  }	}}t#          t%          |	          t'          |          |          }|                                 |cd d d            S # 1 swxY w Y   d S )NF)r,   r:   Tfilename)r,   r;   )r,   r4   origin)asset_idr=   r,   preview_reference_idr,   r0   zState changed during updater1   )r*   r   r:   r   	file_pathr!   dictr;   getr   r   r   rB   r   r   r   RuntimeErrorr#   r)   r(   commit)r,   r:   r4   r;   r<   r-   r=   r>   r5   r2   touchedcomputed_filenamenew_metacurrent_metaupdatedr6   r3   tag_listdetails                      r7   update_asset_metadatarR   L   s    
		 EW,WlHMM 0 0!'4PPPPGHK_5cmDDD[_ $$M**HH 	.,2L
++/@@@--  9'8$"l(    G)!	    G 0#,)  G  !!)%/   
 G 	L},'lKKKK/%
 
 

  	><===%UH"&s++$U++
 
 

 	KE E E E E E E E E E E E E E E E E Es   E?FF"FTdelete_content_if_orphanc                    t                      5 }|s4t          || |          }|                                 |cddd           S t          ||           }|r|j        nd}|r|j        nd}t          || |          }|s"|                                 	 ddd           dS |s"|                                 	 ddd           dS t          ||          }|r"|                                 	 ddd           dS t          ||          }	|r|		                    |           |
                    t          |          }
|
|                    |
           |                                 |	D ]h}t          j        t                    5  |r3t           j                            |          rt!          j        |           ddd           n# 1 swxY w Y   i	 ddd           n# 1 swxY w Y   dS )a  Delete an asset reference.

    With ``delete_content_if_orphan=False`` (a soft delete), the reference is
    hidden and the underlying content is preserved. With ``True``, the content
    is also removed once it becomes orphaned.

    Note: the public DELETE /api/assets/{id} endpoint always soft-deletes
    (passes ``False``); the orphan-reclamation path is intentionally
    internal-only, retained for a future GC/admin caller.
    r0   NrE   FTrB   )r*   r   rJ   r   rB   rF   r   r   r   appendrH   r   delete
contextlibsuppress	Exceptionospathisfileremove)r,   r-   rS   r5   deletedref_rowrB   rF   still_exists
file_paths	asset_rowps               r7   delete_asset_referencere      s    
		 .!W' 	1lX  G NN.! .! .! .! .! .! .! .! &gLIII'.87##D)0:G%%d	(,
 
 
  	NN%.! .! .! .! .! .! .! .!(  	NN-.! .! .! .! .! .! .! .!0 5WxPPP 	NN7.! .! .! .! .! .! .! .!> 5WxPPP
 	)i(((KKx00	 NN9%%%  	! 	!A$Y// ! ! !** !IaLLL! ! ! ! ! ! ! ! ! ! ! ! ! ! !	!W.! .! .! .! .! .! .! .! .! .! .! .! .! .! .!` 4sO   *G"AG"#G"(G"<BG"6G	=G"	GG"GG""G&)G&rD   c                 n   t                      5 }t          || |           t          || |           t          || |          }|st	          d          |\  }}}t          t          |          t          |          |          }|                                 |cd d d            S # 1 swxY w Y   d S )NrC   r0   z#State changed during preview updater1   )	r*   r   r   r   rI   r#   r)   r(   rJ   )	r,   rD   r-   r5   r6   r2   r3   r4   rQ   s	            r7   set_asset_previewrg      s(   
 
		 W&whGGG%!5	
 	
 	
 	
 0,
 
 
  	FDEEE!UD"&s++$U++
 
 

 	/                 s   BB**B.1B.
asset_hashc                 t    t                      5 }t          ||           cd d d            S # 1 swxY w Y   d S N)rh   )r*   r   )rh   r5   s     r7   asset_existsrk      s    			 DW#G
CCCD D D D D D D D D D D D D D D D D Ds   -11c                     t                      5 }t          ||           }t          |          cd d d            S # 1 swxY w Y   d S rj   )r*   queries_get_asset_by_hashr(   )rh   r5   r3   s      r7   r   r     s    			 )W)'jIII!%(() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )s    <A A )
created_at
updated_atr:   size   rn   descinclude_tagsexclude_tagsname_containsmetadata_filterlimitoffsetsortorderafterc
                    d}
d}|t           v }|	m|t           vrt          d|          t          |	t           |          }|j        |k    rt          d|j        d|          t	          |          |j        }}
|r|dz   n|}t                      5 }t          || |||||||||
|          \  }}}d}|r7t          |          |k    r$t          ||dz
           ||          }|d|         }g }|D ]_}|
                    t          t          |          t          |j                  |                    |j        g                                `t!          |||	          cddd           S # 1 swxY w Y   dS )
a  List assets with optional cursor pagination.

    When ``after`` is supplied it overrides ``offset``. The cursor's sort field
    must match ``sort`` and be in the cursor-supported allowlist; mismatches
    raise InvalidCursorError so the handler can map to 400 INVALID_CURSOR.
    Nz,cursor pagination is not supported for sort=)expected_orderzcursor sort field z does not match request sort    )r-   rs   rt   ru   rv   rw   rx   ry   rz   after_cursor_valueafter_cursor_idr1   )itemstotalnext_cursor)_CURSOR_SORT_FIELDSr   r   
sort_field_resolve_cursor_valueidr*   r   len_encode_next_cursorrV   r$   r)   r(   r3   rH   r&   )r-   rs   rt   ru   rv   rw   rx   ry   rz   r{   cursor_value	cursor_idmint_cursorpayloadfetch_limitr5   refstag_mapr   r   r   r2   s                         r7   list_assets_pager     sH   $ #'L I
 --K***$GtGG    ':5QQQ%%$`W%7``X\``   #8"@"@'*i
  +5%!))K			 "SW3%%'++% 
  
  
gu #' 	 3t99u,, .d519otUKKK<D(* 	 	CLL .s33,SY77 SVR00       e5kRRRE"S "S "S "S "S "S "S "S "S "S "S "S "S "S "S "S "S "Ss   CE11E58E5r   c                     | j         dv r#t          |                               d          S | j         dk    rt          |           S | j        S )z<Map a decoded cursor payload to a column-typed Python value.)rn   ro   Ntzinforp   )r   r	   replacer   value)r   s    r7   r   r   _  sV    999 "'**22$2???V## )))=    c                 x   |dk    rt          d| j        | j        |          S |dk    rD| j        | j        j        dS t          dt          | j        j                  | j        |          S |dk    r| j        n| j        }|dS t          ||	                    t          j                  | j        |          S )un  Mint a cursor pointing at *ref* for the given sort dimension.

    Returns None when the boundary row carries a NULL sort value (e.g. an asset
    record whose size_bytes hasn't been backfilled). Continuing pagination
    across a NULL boundary is undefined under keyset ordering — better to
    truncate cleanly here than to mint a cursor that mis-positions.
    r:   )rz   rp   Nrn   r   )r
   r:   r   r3   
size_bytesstrrn   ro   r   r   r   utc)r2   ry   rz   r   s       r7   r   r   j  s     v~~VSXsvUCCCCv~~9	 4 <4VS)=%>%>eTTTT"l22CNNE}t"4hl)K)KSV[`aaaar   c                    t                      5 }t          ||           }|s	 ddd           dS t          ||j                  }fd|D             }t	          |          }|s	 ddd           dS t
          j                            |          }|D ]}|j        |k    r|j	        r	|j	        } n|j
        pt          j        |          d         pd}	ddd           n# 1 swxY w Y   t          ||	|          S )a$  Resolve a blake3 hash to an on-disk file path.

    Only references visible to *owner_id* are considered (owner-less
    references are always visible).

    Returns a DownloadResolutionResult with abs_path, content_type, and
    download_name, or None if no asset or live path is found.
    NrU   c                 @    g | ]}|j         d k    s|j         k    |S r+   )r-   ).0rr-   s     r7   
<listcomp>z(resolve_hash_to_path.<locals>.<listcomp>  s<     
 
 
zR1:#9#9 #9#9#9r   r   application/octet-streamabs_pathcontent_typedownload_name)r*   rm   r   r   r    r[   r\   basenamerF   r:   r=   	mimetypes
guess_typer%   )
rh   r-   r5   r3   r   visibler   display_namer2   ctypes
    `        r7   resolve_hash_to_pathr     s    
		 
W)':>> 	
 
 
 
 
 
 
 
 +7UXFFF
 
 
 

 
 
 )11 	
 
 
 
 
 
 
 
 w''11 	 	C}((SX("xO *#L11!4*) 	#
 
 
 
 
 
 
 
 
 
 
 
 
 
 
, $"   s   C"6C"4A"C""C&)C&c           
         t                      5 }t          || |          }|st          d|  d          |\  }}|j        r,t          j                            |j                  r|j        }nJt          ||j                  }t          |          }|s#t          d|  d|j         d|j         d          |j        }|j        }	t          || 	           |                                 |	pt          j        |p|          d
         pd}
|pt          j                            |          }t%          ||
|          cd d d            S # 1 swxY w Y   d S )Nr0   zAssetReference z
 not foundrU   z No live path for AssetReference z (asset id=z, name=)rE   r   r   r   )r*   r   
ValueErrorrF   r[   r\   r]   r   r   r    FileNotFoundErrorr:   r=   r   rJ   r   r   r   r%   )r,   r-   r5   pairr2   r3   r   r   ref_name
asset_mimer   r   s               r7   resolve_asset_for_downloadr     s    
		 '
W(,
 
 
  	IG|GGGHHH
U = 
	RW^^CM:: 
	}HH /wJJJD,T22H '>| > >!&> >25(> > >   8_
$W<HHHH  *#H$899!<*) 	
 !>BG$4$4X$>$>''
 
 
G'
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
s   D/EEEr   )NNNr9   r+   NN)T)Nr+   )
r+   NNNNrq   r   rn   rr   N)ErX   r   r[   datetimer   typingr   app.assets.services.cursorr   r   r   r   r	   r
   r   app.assets.database.modelsr   app.assets.database.queriesr   r   r   r   r   r   r   rm   r   r   r   r   r   r   r   r   r   r   r   r   app.assets.helpersr    app.assets.services.path_utilsr!   app.assets.services.schemasr"   r#   r$   r%   r&   r'   r(   r)   app.database.dbr*   r   r8   rR   boolre   rg   rk   r   rG   intr   objectr   r   r   r    r   r7   <module>r      s           				                              - , , , , ,                                         * 5 4 4 4 4 4 D D D D D D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 + * * * * *
 
 


 
 
 
 
. !%"& !O OO
*O 3-$
O  	O
 O O TzO d
O O O O Oj &*? ??? #? 
	? ? ? ?H (, *  	   >DS DT D D D D
)# ))d*: ) ) ) ) C  )-)- $#'MS MSMS3-$&MS 3-$&MS :	MS
 D[MS MS MS MS MS :MS MS MS MS MS`= V    b3 bs bsTz b b b b. & &&& $& & & &V +
 +
+
+
 +
 +
 +
 +
 +
 +
r   