o
    ¹i  ã                   @   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                       ó*   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r| j||d d|vr$t dd¡‚|d d	kr5t dd
 |d ¡¡‚d|vrE|d dkrEt dd¡‚|  |¡}|dkrX| j d|› ¡ n| j d|› d|› ¡ z |dkrwt	ƒ  
||d |  d¡¡ n|  ||d |  d¡¡ W n+ tjy   ‚  ty¯ } z| jjd||dd t dd|› d|› ¡|‚d}~ww | j |dd}|dkrÂ| j||d | jd|dœd |S t	ƒ  ||¡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ÚstripÚrun_pre_save_hookr   Ú	HTTPErrorr   Ú_get_os_pathÚlogÚdebugÚsuperÚ
_save_fileÚ_save_large_fileÚ	ExceptionÚerrorÚrun_post_save_hooksÚemitr   ©Úselfr   r   r
   r   Úe©Ú	__class__© úl/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyter_server/services/contents/largefilemanager.pyr      sH   
þ
€€þzLargeFileManager.savec              
   C   s  |dvr
t  dd¡‚z|dkr| d¡}n
| d¡}t |¡}W n ty9 } zt  dd|› d|› ¡|‚d	}~ww |  |¡? tj 	|¡rTtj 
tj |¡t |¡¡}t|d
ƒ}| |¡ W d	  ƒ n1 siw   Y  W d	  ƒ d	S W d	  ƒ d	S 1 sw   Y  d	S ©zSave content of a generic file.>   ÚtextÚbase64r   z:Must specify format of file contents as 'text' or 'base64'r3   Úutf8ÚasciizEncoding error saving z: NÚab)r   r    Úencoder4   Ú	b64decoder'   Úperm_to_403Úosr   ÚislinkÚjoinÚdirnameÚreadlinkÚopenÚwrite©r,   r   r   r   ÚbcontentÚ	b64_bytesr-   Úfr0   r0   r1   r&   >   s0   þ

€€ÿÿý"ýz!LargeFileManager._save_large_file©r   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   Ú__classcell__r0   r0   r.   r1   r      s    .r   c                       r   )ÚAsyncLargeFileManagerz'Handle large file upload asynchronouslyr   c              
   ƒ   sÂ  |  dd¡}|dur×| d¡}|dkr| j||d d|vr%t dd¡‚|d d	kr6t dd
 |d ¡¡‚d|vrF|d dkrFt dd¡‚|  |¡}|dkrY| j d|› ¡ n| j d|› d|› ¡ z&|dkr{t	ƒ  
||d |  d¡¡I dH  n|  ||d |  d¡¡I dH  W n+ tjy”   ‚  ty¶ } z| jjd||dd t dd|› d|› ¡|‚d}~ww | j |ddI dH }|dkrÌ| j||d | jd|dœd |S t	ƒ  ||¡I dH S r	   r   r+   r.   r0   r1   r   X   sJ   €
þ
"€€þ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|› ¡|‚d	}~ww |  |¡C tj 	|¡rUtj 
tj |¡t |¡¡}t|d
ƒ}t|j|ƒI d	H  W d	  ƒ n1 snw   Y  W d	  ƒ d	S W d	  ƒ d	S 1 s†w   Y  d	S r2   )r   r    r8   r4   r9   r'   r:   r;   r   r<   r=   r>   r?   r@   r   rA   rB   r0   r0   r1   r&   ‡   s2   €þ

€€ÿÿý"ýz&AsyncLargeFileManager._save_large_filerF   rG   r0   r0   r.   r1   rM   U   s    /rM   )r4   r;   Úanyio.to_threadr   Útornador   Ú,jupyter_server.services.contents.filemanagerr   r   r   rM   r0   r0   r0   r1   Ú<module>   s    H