
    i~                         d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZmZ ddlmZ ddlmZmZ ddlmZmZ  G d d	      Zy)
z7Attachment helpers shared by HTTP routes and executors.    N)Path)AnyDictListOptional)
UploadFile)MessageBlockMessageBlockType)AttachmentStoreAttachmentRecordc            
           e Zd ZdZ ed      ddeez  ddfdZdedefd	Zdeddfd
Zdede	fdZ
dededefdZdddedee   dee	   dee   fdZdedeeef   fdZdddededee   fdZy)AttachmentServicez)Handles attachment lifecycle per session.	WareHouse)rootr   returnNc                $   t        j                  t              | _        t	        |      | _        | j
                  j                  dd       t        j                  j                  dd      j                         j                         }|dv | _        y )NTparentsexist_okMAC_AUTO_CLEAN_ATTACHMENTS0>   1yestrue)logging	getLogger__name__loggerr   attachments_rootmkdirosenvirongetstriplowerclean_on_cleanup)selfr   env_flags      O/Users/bowang/.openclaw/workspace/ChatDev/server/services/attachment_service.py__init__zAttachmentService.__init__   sn    ''1 $T
##D4#@::>>">DJJLRRT (,@ @    
session_idc                 (    | j                  |d      S )NTcreate)_session_attachments_path)r'   r,   s     r)   prepare_session_workspacez+AttachmentService.prepare_session_workspace   s    --j-FFr+   c                     | j                  |d      }|sy | j                  r4t        j                  |d       | j                  j                  d|       y | j                  j                  d|       y )NFr.   T)ignore_errorsz+Cleaned attachment directory for session %sz;Attachment cleanup disabled; preserved files for session %s)r0   r&   shutilrmtreer   info)r'   r,   attachment_dirs      r)   cleanup_sessionz!AttachmentService.cleanup_session   s`    77
57Q  MM.=KKJJWKKMzr+   c                 :    | j                  |      }t        |      S N)r1   r   )r'   r,   paths      r)   get_attachment_storez&AttachmentService.get_attachment_store*   s    --j9t$$r+   uploadc           
      <  K   |j                   xs d}t        t        j                  d            }||z  }	 |j	                  d      5 }	 |j                  d       d {   }|sn|j                  |       .	 d d d        | j                  |      }|j                  xs t        j                  |      d   }	|j                  |t        j                  |	      ||	dd|d	
      }
|
|j                         r	 |j                          	 |j#                          S 7 # 1 sw Y   xY w# t         $ r Y +w xY w# t         $ r Y S w xY w# |j                         r!	 |j                          n# t         $ r Y nw xY w	 |j#                          w # t         $ r Y w w xY wxY ww)Nz
upload.binmac_upload_)prefixwbi   r   user_upload
web_upload)sourceoriginr,   )kinddisplay_name	mime_typeextra)filenamer   tempfilemkdtempopenreadwriter<   content_type	mimetypes
guess_typeregister_filer
   from_mime_typeexistsunlinkOSErrorrmdir)r'   r,   r=   rJ   temp_dir	temp_pathbufferchunkstorerH   records              r)   save_upload_filez"AttachmentService.save_upload_file.   s    ??2l((>?x'		%"(++k"::E LL'	  	 & --j9E++Py/C/CH/Ma/PI((%44Y?%#+*", ) 
F !$$& 3 ; &%0     !$$&   s   5FE 	DD D:A-E 'F8D(	D7FDD%!E (	D41F3D44F7	E FEFFE)(F)	E52F4E55F9F
	F
	FFFFF)target_storeattachment_idsr`   c                L   |sg S | j                  |      }|j                  j                         }|r|j                  j                         nd }g }|D ]O  }|j                  |      }	|	s|r||k7  }
|j	                  |	|
      }	|j                  |	j                                Q |S )N)	copy_file)r<   r   resolver#   ingest_recordappendas_message_block)r'   r,   ra   r`   source_storesource_roottarget_rootblocksattachment_idr^   copy_requireds              r)   build_attachment_blocksz)AttachmentService.build_attachment_blocksR   s     I00<"''//15Al''//1t%'+M!%%m4F +{ :%33Fm3TMM&1134 , r+   c                 D    | j                  |      }|j                         S r:   )r<   export_manifest)r'   r,   r]   s      r)   list_attachment_manifestsz+AttachmentService.list_attachment_manifestsi   s!    ))*5$$&&r+   Tr.   r/   c                    |j                  d      r|nd| }| j                  |z  dz  dz  }|r|j                  dd       |S |j                         r|S d S )Nsession_code_workspaceattachmentsTr   )
startswithr   r    rU   )r'   r,   r/   session_dir_namer;   s        r)   r0   z+AttachmentService._session_attachments_pathm   sh    )3)>)>z)J:RZ[eZfPg$$'77:JJ]ZJJtdJ3K{{}t.$.r+   )r   
__module____qualname____doc__r   strr*   r1   r8   r   r<   r   r   r_   r   r   r	   rn   r   r   rq   boolr0    r+   r)   r   r      s    3-1+-> As
 A4 AGC GD G
# 
$ 
%s % %" "j "M] "R 37 S	
 / 
l	.'C 'DcN ' LP /C /D /T\]aTb /r+   r   )rz   r   rQ   r!   r4   rK   pathlibr   typingr   r   r   r   fastapir   entity.messagesr	   r
   utils.attachmentsr   r   r   r}   r+   r)   <module>r      s5    =   	    , ,  : ?b/ b/r+   