ó
ù2¢\c           @   sz   d  d l  m Z d  d l m Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d e f d „  ƒ  YZ
 d S(   iÿÿÿÿ(   t   FileContentsManager(   t   contextmanager(   t   webNt   LargeFileManagerc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   s   Handle large file upload.t    c         C   s  | j  d d ƒ } | d k	 rç| j d ƒ } d | k rN t j d d ƒ ‚ n  | d d k r€ t j d d j | d ƒ ƒ ‚ n  d | k r± | d d	 k r± t j d d
 ƒ ‚ n  |  j | ƒ } y… | d k r$|  j j d | ƒ |  j	 d | d | ƒ t
 t |  ƒ j | | d | j  d ƒ ƒ n  |  j | | d | j  d ƒ ƒ Wna t j k
 r^‚  nK t k
 r¨} |  j j d | | d t ƒt j d d | | f ƒ ‚ n X|  j  | d t ƒ} | d k rã|  j d | d | ƒ n  | St
 t |  ƒ j | | ƒ Sd S(   s9   Save the file model and return the model with no content.t   chunkt   /t   typei  u   No file type providedt   fileu7   File type "{}" is not supported for large file transfert   contentt	   directoryu   No file content providedi   s	   Saving %st   modelt   patht   formatu   Error while saving file: %s %st   exc_infoiô  u)   Unexpected error while saving file: %s %siÿÿÿÿt   os_pathN(   t   gett   Nonet   stripR   t	   HTTPErrorR   t   _get_os_patht   logt   debugt   run_pre_save_hookt   superR   t
   _save_filet   _save_large_filet	   Exceptiont   errort   Truet   Falset   run_post_save_hookt   save(   t   selfR   R   R   R   t   e(    (    sJ   lib/python2.7/site-packages/notebook/services/contents/largefilemanager.pyR       s4    ",$ c      
   C   s  | d d h k r' t  j d d ƒ ‚ n  y@ | d k rH | j d ƒ } n | j d ƒ } t j | ƒ } Wn/ t k
 r˜ } t  j d d | | f ƒ ‚ n X|  j | ƒ l t j j	 | ƒ rè t j j
 t j j | ƒ t j | ƒ ƒ } n  t j | d ƒ  } | j | ƒ Wd	 QXWd	 QXd	 S(
   s   Save content of a generic file.t   textt   base64i  s:   Must specify format of file contents as 'text' or 'base64't   utf8t   asciiu   Encoding error saving %s: %st   abN(   R   R   t   encodeR$   t	   b64decodeR   t   perm_to_403t   osR   t   islinkt   joint   dirnamet   readlinkt   iot   opent   write(   R!   R   R	   R   t   bcontentt	   b64_bytesR"   t   f(    (    sJ   lib/python2.7/site-packages/notebook/services/contents/largefilemanager.pyR   0   s"    -(   t   __name__t
   __module__t   __doc__R    R   (    (    (    sJ   lib/python2.7/site-packages/notebook/services/contents/largefilemanager.pyR      s   %(   t&   notebook.services.contents.filemanagerR    t
   contextlibR   t   tornadoR   t   nbformatR$   R+   R0   R   (    (    (    sJ   lib/python2.7/site-packages/notebook/services/contents/largefilemanager.pyt   <module>   s   