a
    ø£Be  ã                   @   s\   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 G dd„ deƒZ	G dd„ deƒZ
dS )	é    N)Úrun_sync)Úweb)ÚAsyncFileContentsManagerÚFileContentsManagerc                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )ÚLargeFileManagerzHandle large file upload.Ú c              
      sÄ  |  dd¡}|du�r²| d¡}|dkr6| j||d� d|vrJt dd¡‚|d d	krlt dd
 |d ¡¡‚d|vrŒ|d dkrŒt dd¡‚|  |¡}|dkr²| j d|› �¡ n| j d|› d|› �¡ z@|dkrðt	ƒ  
||d |  d¡¡ n|  ||d |  d¡¡ W nj tj�y"   ‚ Y nT t�yt } z:| jjd||dd� t dd|› d|› �¡|‚W Y d}~n
d}~0 0 | j |dd�}|dk�rœ| j||d� | jd|dœd� |S t	ƒ  ||¡S dS ©z9Save the file model and return the model with no content.ÚchunkNú/é   )ÚmodelÚpathÚtypeé�  zNo file type providedÚfilez7File type "{}" is not supported for large file transferÚcontentÚ	directoryzNo file content providedéÿÿÿÿzSaving last chunk of file zSaving chunk z	 of file ÚformatzError while saving file: %s %sT)Úexc_infoiô  z$Unexpected error while saving file: ú F)r   )r   Úos_pathÚsave)Úactionr   )Údata©ÚgetÚstripZrun_pre_save_hookr   Ú	HTTPErrorr   Z_get_os_pathÚlogÚdebugÚsuperZ
_save_fileÚ_save_large_fileÚ	ExceptionÚerrorZrun_post_save_hooksÚemitr   ©Úselfr   r   r	   r   Úe©Ú	__class__© út/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/services/contents/largefilemanager.pyr      sB    

þ
0
zLargeFileManager.savec              
   C   s
  |dvrt  dd¡‚z,|dkr*| d¡}n| d¡}t |¡}W n> ty~ } z&t  dd|› d|› �¡|‚W Y d	}~n
d	}~0 0 |  |¡�l tj 	|¡r´tj 
tj |¡t |¡¡}t|d
ƒ�}| |¡ W d	  ƒ n1 sÞ0    Y  W d	  ƒ n1 sü0    Y  d	S ©zSave content of a generic file.>   Úbase64Útextr   z:Must specify format of file contents as 'text' or 'base64'r/   Úutf8ÚasciizEncoding error saving z: NÚab)r   r   Úencoder.   Ú	b64decoder#   Úperm_to_403Úosr   ÚislinkÚjoinÚdirnameÚreadlinkÚopenÚwrite©r'   r   r   r   ZbcontentZ	b64_bytesr(   Úfr+   r+   r,   r"   >   s"    þ
0z!LargeFileManager._save_large_file)r   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   Ú__classcell__r+   r+   r)   r,   r      s   .r   c                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )ÚAsyncLargeFileManagerz'Handle large file upload asynchronouslyr   c              
   ƒ   sÜ  |  dd¡}|du�rÄ| d¡}|dkr6| j||d� d|vrJt dd¡‚|d d	krlt dd
 |d ¡¡‚d|vrŒ|d dkrŒt dd¡‚|  |¡}|dkr²| j d|› �¡ n| j d|› d|› �¡ zL|dkröt	ƒ  
||d |  d¡¡I dH  n|  ||d |  d¡¡I dH  W nj tj�y.   ‚ Y nT t�y€ } z:| jjd||dd� t dd|› d|› �¡|‚W Y d}~n
d}~0 0 | j |dd�I dH }|dk�r®| j||d� | jd|dœd� |S t	ƒ  ||¡I dH S dS r   r   r&   r)   r+   r,   r   X   sB    

þ
""0
zAsyncLargeFileManager.savec              
   Ã   s  |dvrt  dd¡‚z,|dkr*| d¡}n| d¡}t |¡}W n> ty~ } z&t  dd|› d|› �¡|‚W Y d	}~n
d	}~0 0 |  |¡�t tj 	|¡r´tj 
tj |¡t |¡¡}t|d
ƒ�"}t|j|ƒI d	H  W d	  ƒ n1 sæ0    Y  W d	  ƒ n1 �s0    Y  d	S r-   )r   r   r3   r.   r4   r#   r5   r6   r   r7   r8   r9   r:   r;   r   r<   r=   r+   r+   r,   r"   ‡   s"    þ
0z&AsyncLargeFileManager._save_large_file)r   r?   r+   r+   r)   r,   rE   U   s   /rE   )r.   r6   Zanyio.to_threadr   Útornador   Z,jupyter_server.services.contents.filemanagerr   r   r   rE   r+   r+   r+   r,   Ú<module>   s   H