a
    ø£Be  ã                   @  sŠ   U d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ d
ZG dd„ deejƒZg Zded< dS )z.Serve files directly from the ContentsManager.é    )ÚannotationsN)Údecodebytes)Ú	Awaitable)Úensure_async)Úweb)Ú
authorized)ÚJupyterHandlerÚcontentsc                      sV   e Zd ZdZeZe‡ fdd„ƒZej	e
dddœdd„ƒƒZej	e
dd
d„ƒƒZ‡  ZS )ÚFilesHandlerzÝserve files via ContentsManager

    Normally used when ContentsManager is not a FileContentsManager.

    FileContentsManager subclasses use AuthenticatedFilesHandler by default,
    a subclass of StaticFileHandler.
    c                   s   t ƒ jd S )zThe content security policy.z; sandbox allow-scripts)ÚsuperÚcontent_security_policy)Úself©Ú	__class__© ú`/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/files/handlers.pyr      s    z$FilesHandler.content_security_policyÚstrzAwaitable[None] | None)ÚpathÚreturnc                 C  s$   | j |dd� |  ¡  | j |dd�S )zThe head response.F)Úinclude_body)ÚgetÚcheck_xsrf_cookie)r   r   r   r   r   Úhead%   s    zFilesHandler.headTc           
      Ã  sŽ  |   ¡  | j}|js>t| |¡ƒI dH r>| j d¡ t d¡‚| 	d¡}d|v rb| 
dd¡\}}n|}t|j|d|d�ƒI dH }|  dd¡r–|  |¡ | ¡  d	¡r²|  d
d¡ n�t |¡\}}|dkrÔ|  d
d¡ |dkrê|  d
d¡ nX|du�r|  d
d¡ n@|du�r|  d
|¡ n(|d dk�r6|  d
d¡ n|  d
d¡ |�rŠ|d dk�rt|d  d¡}	|  t|	ƒ¡ n|  |d ¡ |  ¡  dS )zGet a file by path.Nz,Refusing to serve hidden file, via 404 Errori”  ú/é   Úfile)ÚtypeÚcontentÚdownloadz.ipynbzContent-Typezapplication/x-ipynb+jsonz
text/plainztext/plain; charset=UTF-8Úgzipzapplication/gzipzapplication/octet-streamÚformatÚbase64r   Úascii)r   Zcontents_managerZallow_hiddenr   Z	is_hiddenÚlogÚinfor   Ú	HTTPErrorÚstripÚrsplitr   Zget_argumentZset_attachment_headerÚlowerÚendswithZ
set_headerÚ	mimetypesÚ
guess_typeÚencodeÚwriter   Úflush)
r   r   r   ÚcmÚ_ÚnameÚmodelZcur_mimeÚencodingZ	b64_bytesr   r   r   r   -   s@    




zFilesHandler.get)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚAUTH_RESOURCEZauth_resourceÚpropertyr   r   Úauthenticatedr   r   r   Ú__classcell__r   r   r   r   r
      s   r
   zlist[JupyterHandler]Údefault_handlers)r7   Ú
__future__r   r*   r!   r   Útypingr   Zjupyter_core.utilsr   Útornador   Zjupyter_server.authr   Zjupyter_server.base.handlersr   r8   ZStaticFileHandlerr
   r<   Ú__annotations__r   r   r   r   Ú<module>   s   M