m
&Gc        	   @   s  d  Z  d Z  d Z d k l Z d k l Z d k l Z d k l	 Z	 d k
 l Z d k l Z l Z l Z l Z d	 k l Z l Z d
 k l Z d k l Z l Z d k l Z d k l Z d k l Z d k Z d k Z d k Z d k  Z  d k! Z! d k" Z" d k# Z# d k$ Z$ d k% Z% d k& l' Z' l( Z( d k) l* Z* d k+ l, Z, d k- l. Z. d k/ l0 Z0 d k1 l2 Z2 d k3 l4 Z4 d k5 l6 Z6 d k7 l8 Z8 d k5 l9 Z9 d k7 l: Z: d k; Z; d Z< d Z= e; i> e<  Z? y d k@ lA ZA Wn  eB j
 o d kC lA ZA n Xy d kD lE ZE Wn eB j
 o eF ZE n Xe eG    ZH e iI ZJ e iK ZL d ZM d  ZN d! kO Te d" eG    ZP d# d# d# d# d# d d eF d$  ZQ d e e e e	 f d%     YZR e eR  d&   ZS d'   ZT d(   ZU d) ZV d* ZW e iX eV eW  ZY d+   ZZ e# i[ d,  Z\ d-   Z] e^ d.  Z_ d/ k` la Za d0 kb lc Zc eF eF d1  Zd d2   Ze d3 ef f d4     YZg d5 f  d6     YZh d7 f  d8     YZi d S(9   s    ExtFile product module s   ExtFile product module.
    The ExtFile-Product works like the Zope File-product, but stores
    the uploaded file externally in a repository-direcory.s   2.0.2(   s   CatalogAware(   s
   SimpleItem(   s   PropertyManager(   s	   Cacheable(   s   Pdata(   s   HTMLFiles   MessageDialogs   InitializeClasss   package_home(   s   ClassSecurityInfos   getSecurityManager(   s   Permissions(   s
   aq_acquires   aq_base(   s   ResourceLockedError(   s   rfc1123_date(   s   DateTimeN(   s   joins   isfile(   s   TemporaryFile(   s   TM(   s   Redirect(   s   IStreamIterator(   s
   html_quote(   s   WriteLockInterface(   s	   interface(   s   IExtFile(   s   event(   s   ExtFileUpdatedEventt   ExtFilei    (   s   guess_content_type(   s	   IDAVAwares   Change ExtFile/ExtImages   Download ExtFile/ExtImage(   t   *s   www/extFileAddt    c	   
      C   s  | o} t | d d	  d	 j	 od | i } | t t i | d  t i | d  t i | d   d } | p | } t	 |  } n t
 | | | | |  }	 |  i | |	  | d j o |  i |  i | |  n | d	 j	 o |  i |  | d d Sn | S(
   s    Add an ExtFile to a folder. t   filenamet   /s   \t   :i   R   t   update_menui    N(   t   idt   getattrt   filet   NoneR   t   maxt   stringt   rfindt   titlet   normalize_idR    t   descrt   permission_checkt   redirect_default_viewt   tempExtFilet   selft
   _setObjectt   _getObt   manage_file_uploadt   content_typet   REQUESTt   manage_main(
   R   R   R   R   R	   R   R   R   R   R   (    (    t>   /home2/kennisonb/webapps/zope/Zope/Products/ExtFile/ExtFile.pyt   manage_addExtFileh   s     !	c           B   s  t  Z d  Z e e j	 o e i e e  n e i e  e f Z	 h  d d <d d <d d <h  d d <d d <d d <h  d d	 <d d <d d <h  d d
 <d d <d d <h  d d <d d <d d <f Z
 d Z d Z h  d d <d d <h  d d <d d <h  d d <d d <f e i e i d e i Z e   Z d Z e i e d  h  d h  d d <<d h  d d <d d <d d <d  d! <<d" h  d# d$ <d% d& <d' d& <d( d) <d* d+ <d, d- <d d <<d. h  d d <<d/ h  d d <<d d <Z d d d d d0  Z e d1    Z d2   Z d3   Z d4   Z d5   Z e i e d6  d d e e e e d7  Z  e i e d8  d9   Z! e i e d:  e e" e e d d d;  Z# e i e d<  d=   Z$ e i e d>  d?   Z% e i e d@  dA   Z& e i e dB  dC   Z' e i e dD  dE   Z( e i e dF  dG   Z) e i e dH  dI   Z* e i e dJ  dK   Z+ e i e dL  dM   Z, e i e dN  e" dO  Z- e i e dP  d d dQ  Z. e i e dR  dS   Z/ e i e0 dT  dU   Z1 e i e dV  dW   Z2 e i e0 d  e3 dX e4    Z5 e i e6 dY  d d e dZ  Z7 e i e0 d  e3 d[ e4    Z8 e i e6 d\  d d e d]  Z9 e i e6 d^  d d e d_  Z: e i e6 d`  e da  Z; e i e6 db  dc   Z< e i dd de  e i dd df  e i dd dg  dh   Z= e di  Z> e dj  Z? dk   Z@ dl   ZA dm   ZB dn   ZC do   ZD dp   ZE dq   ZF e e d d dr  ZG e e d ds  ZH dt   ZI e du  ZJ d d dv  ZK d d dw  ZL e" dx  ZM e e dy  ZN dz   ZO d{   ZP d|   ZQ e iR d}  d~   ZS d   ZT d   ZU d   ZV d   ZW d   ZX d   ZY RS(   s    The ExtFile-Product works like the Zope File-product, but stores
        the uploaded file externally in a repository-directory. R   R   t   typeR   t   modet   wR   t   textR   t   use_download_permission_checkt   booleanR   i    t   labelt   Editt   actionR   t   ViewR   t   Uploadt   manage_uploadFormi   R    t   Managert   imaget   defaults   default.gift   htmls   html.gift   xmls   xml.gift   pythons   py.gift   applicationt   pdfs   pdf.gift   zips   zip.gift   tart   mswords   doc.gift   excels   xls.gift
   powerpoints   ppt.gift   videot   audioc         C   sL   | |  _  | |  _ | |  _ | |  _ | |  _ t |  _ g  |  _ d |  _	 d S(   s&    Initialize a new instance of ExtFile R   N(
   R   R   R   R   R   R!   R   t   __version__R   R   (   R   R   R   R   R   R   (    (    R   t   __init__   s     							c         C   s   |  i   o d Sn |  i   S(   NR   (   R   t	   is_brokent
   index_html(   R   (    (    R   t   data   s    c         C   s   |  i S(   N(   R   R<   (   R   (    (    R   t   __str__   s    c         C   s   d S(   Ni   (    (   R   (    (    R   t   __len__   s    c         C   s  | i d d  } | d j	 o t i | d  d } y t t |  i    } Wn d } n X| d j	 o |  i o t |  i  } n t d  } | d j o] | | j oP t |  i  } | i i d |  | i i d |  i  | i i d  d Sqq	n d S(	   s    HTTP If-Modified-Since header handling: return True if
            we can handle this request by returning a 304 response.
        s   If-Modified-Sincet   ;i    s   Last-Modifieds   Content-Typei0  i   N(   R   t
   get_headerR
   t   headerR   t   splitt   longt   DateTimet   timeTimet	   mod_sinceR   t   _p_mtimet   last_modt   rfc1123_datet   RESPONSEt	   setHeaderR   t	   setStatus(   R   R   RH   RF   RA   (    (    R   t"   _if_modified_since_request_handler   s$       

c         C   sh   |  i oZ |  i   oI | oA |  i d |  } | |  i   j o | i	 i
 |  d Sq` qd n d S(   s    redirect_default_view property handling: return True if
            we can handle this request by returning a 302 response.
            Patch provided by Oliver Bleutgen.
        t   previewi   N(   R   R   t   static_modet   icont   _static_urlRN   t
   static_urlt   absolute_urlR   RJ   t   redirect(   R   RP   RN   R   RR   (    (    R   t&   _redirect_default_view_request_handler   s     
R;   c         C   s  | d j	 oG |  i |  o |  i d  d Sn |  i | | |  o d SqT n |  i | |  \ }	 }
 } } |  i |	  }	 t d j o! t i d |	 |
 | | f  n |	 o t i |	  d } n2 |  i d t  }	 t i |	  d } d }
 d } | d j o| d j	 o| d j	 o t   } y d k l } | i |	  } | i d	 j o | i d	  } n | i } t  | d
  o | i! } n | i" t# |  t# |  f |  } | i$ | d d d Wn t |	 d  } q,X| i% d d  | i&   } | i% d  d }
 n t |	 d  } | d j	 oc t' |  i(  } | i* i+ d |  | i* i+ d |
  | i* i+ d |  |  i d  t, |  Sn z | i-   SWd | i.   Xd S(   s3    Return the file with it's corresponding MIME-type R   i   s   serving %s, %s, %s, %si   t   brokens	   image/gifi    (   s   Imaget   RGBt	   ANTIALIASt   JPEGt   qualityiU   t   rbi   s
   image/jpegs   Last-Modifieds   Content-Types   Content-LengthN(/   R   R
   R   RM   t   ZCacheable_setRU   RP   RN   t   _get_file_to_serveR   R   t   _get_fsnamet   _debugt   loggert   infot   ost   statt   sizet   _get_icon_filet   Truet   widtht   heightt   TemporaryFileR<   t   PILt   Imaget   opent   imR   t   convertt   BICUBICt   filtert   hasattrRX   t   resizet   intt   savet   seekt   tellRI   RG   RH   RJ   RK   t   stream_iteratort   readt   close(   R   RP   RN   Rg   Rh   R   RJ   Rm   Rd   R   R   R<   Rk   Rp   RH   (    (    R   R;      s^      !'		$
 t   view_image_or_filec         C   s   t  |  i     d S(   s8    The default view of the contents of the File or Image. N(   t   RedirectR   RS   (   R   (    (    R   Rz   ;  s     t   linkc   	      K   s   |  i d | d |  } | d j o t |  i    } n | p t |  } n | d j o t |  d d  } n t |  } |  i | | | | |  S(   s    Returns an HTML link tag for the file or image.
            The 'text' argument is subject to HTML-quoting. To pass a
            structure (e.g. an <img /> tag), set the 'structure' keyword
            argument to True.
        RP   RN   R   R   N(   R   RQ   RP   RN   t   hrefR    R
   t
   html_quotet   title_or_idt	   structureR   R   t   _linkt	   css_classt   args(	   R   R    R   R   R   RP   RN   R   R}   (    (    R   R|   @  s     t   icon_gifc         C   s   t  |  i d d    d S(   s)    Return an icon for the file's MIME-Type RP   i   N(   R{   R   RQ   (   R   (    (    R   R   R  s     t   icon_tagc         C   s   d |  i d d  S(   s(    Generate the HTML IMG tag for the icon s   <img src="%s" border="0" />RP   i   N(   R   RQ   (   R   (    (    R   R   W  s     t	   icon_htmlc         C   s
   |  i   S(   s    Same as icon_tag N(   R   R   (   R   (    (    R   R   \  s     R:   c         C   s   |  i |  i  S(   s@    Check if external file exists and return true (1) or false (0) N(   R   R^   R   (   R   (    (    R   R:   a  s     t   get_sizec         C   s2   |  i |  i  } | o t i |  d Sn d S(   s'    Returns the size of the file or image i   i    N(   R   R^   R   t   fnRb   Rc   (   R   R   (    (    R   R   f  s
     t   rawsizec         C   s
   |  i   S(   s    Same as get_size N(   R   R   (   R   (    (    R   R   n  s     t   getSizec         C   s
   |  i   S(   s    Same as get_size N(   R   R   (   R   (    (    R   R   s  s     Rd   c         C   s   |  i |  i    S(   s:    Returns a formatted stringified version of the file size N(   R   t   _bytetostringR   (   R   (    (    R   Rd   x  s     t   getContentTypec         C   s   |  i S(   s:    Returns the content type (MIME type) of a file or image. N(   R   R   (   R   (    (    R   R   }  s     t   getIconPathc         C   s$  | o t d d d  Sn y t i |  i d  \ } } WnH t j
 o< t	 |  d d
  d
 j	 o d \ } } q d \ } } n X|  i i |  ou |  i | d } xJ |  i | i   D]5 } t i | |  d	 j o |  i | | } Pq q Wt d d | |  Sn t d d |  i d  S(   s    Depending on the MIME Type of the file/image an icon
            can be displayed. This function determines which
            image in the Products/ExtFile/www/icons/...  directory
            should be used as icon for this file/image.
        t   wwwt   iconss
   broken.gifR   t   has_previewR*   R   R+   i    N(   s   imageR   (   R   R   (   RV   t   joinR   RB   R   R   t   catt   subt
   ValueErrorR   R
   t   _typest   has_keyR	   t   keyst   itemt   find(   R   RV   R   R   R   R	   (    (    R   R     s$      	RR   c         C   s   |  i | |  S(   sa    In static mode, returns the static url of the file,
            absolute_url otherwise.
        N(   R   RQ   RP   RN   (   R   RP   RN   (    (    R   RR     s     RO   c         C   s   t  i i d  d j	 S(   s,    Returns true if we are serving static urls t   EXTFILE_STATIC_PATHN(   Rb   t   environt   getR
   (   R   (    (    R   RO     s     t   get_filenamec         C   s   |  i |  i  S(   sg    Returns the filename as filesystem path.
            Used by the ZMI to display the filename.
        N(   R   t   _fsnameR   (   R   (    (    R   R     s     t   PrincipiaSearchSourcec         C   s%   |  i i d  o t |   Sn d S(   s,    Allow file objects to be searched.
        s   text/R   N(   R   R   t
   startswitht   str(   R   (    (    R   R     s     s   www/extFileEditt   manage_editExtFilec         C   sy   |  i | j o | |  _ n |  i | j o | |  _ n |  i   |  i   | d j	 o |  i |  | d d Sn d S(   s    Manage the edited values t   manage_tabs_messages   Saved changes.N(   R   R   R   t   reindex_objectt   ZCacheable_invalidateR   R
   R   (   R   R   R   R   (    (    R   R     s       

s   www/extFileUploadt   manage_uploadc         C   sm   |  i   o t d   n t | t  o' t   } | i |  | i	 d  n | } |  i
 | | |  S(   s7    Upload file from file handle, Pdata, or string buffer s   File is locked via WebDAVi    N(   R   t   wl_isLockedt   ResourceLockedErrort
   isinstanceR	   R   Ri   t	   temp_filet   writeRu   R   R   R   (   R   R	   R   R   R   (    (    R   R     s     	R   c         C   sj  |  i   o t d   n t | t  o t | d  } n! t | t  o t |  } n | o t
 | |  } n |  i | | i d  |  i   |  i	  } | i d  |  i   zZ |  i o |  i |  i	 |  } |  i |  i d | d | } |  i | |  i |   Wd |  i   X| |  _ | |  _	 |  i   | d j	 o |  i |  | d d	 Sn d S(
   s8    Upload file from file handle, Pdata, or local directorys   File is locked via WebDAVR[   id   i    R   t   backupNR   s   Upload complete.(   R   R   R   R   R	   R   Rl   t   Pdatat   pdataR   t
   HTTPUploadt   _get_content_typeRx   t   _get_zodb_idRu   t	   _registerR   t   _changedR   t   _get_ufnt   new_fnt   _update_datat   _temp_fsnamet   _dir__unlockt   _afterUpdateR   R
   R   (   R   R	   R   R   R   R   (    (    R   R     s0     
 		
t   manage_http_uploadc         C   s:  |  i   o t d   n t i | d  } t i |  } t |  } |  i	 | | i
 d  |  i   |  i  } | i d  |  i   zZ |  i o |  i |  i |  } |  i |  i d | d | } |  i | |  i |   Wd |  i   X| |  _ | |  _ |  i   | d j	 o |  i |  | d d	 Sn d S(
   s    Upload file from http-server s   File is locked via WebDAVs   /:id   i    R   R   NR   s   Upload complete.(   R   R   R   t   urllibt   quotet   urlt   urlopenR	   R   R   Rx   R   R   Ru   R   R   R   R   R   R   R   R   R   R   R   R
   R   (   R   R   R   R   R	   R   R   (    (    R   R     s*     
 		
t   PUTc         C   s2  |  i | |  |  i | | d d | d } | i d d	  } | o t	 | |  } n |  i
 | | i d  |  i   |  i  } | i d  |  i   zZ |  i o |  i |  i |  } |  i |  i d | d | } |  i | |  i |   Wd	 |  i   X| |  _ | |  _ |  i   | i d
  | S(   s    Handle HTTP PUT requests t   refreshi   t   BODYFILEs   content-typeid   i    R   R   Ni   (   R   t	   dav__initR   RJ   t   dav__simpleifhandlerR	   R@   R
   R   R   R   Rx   R   Ru   R   R   R   R   R   R   R   R   R   R   RL   (   R   R   RJ   R	   R   R   R   (    (    R   R     s,     

 		
s
   FTP accesst   manage_FTPstatt   manage_FTPlistt   manage_FTPgetc         C   s   |  i d |  i  S(   s    Return body for FTP R   N(   R   R;   R   (   R   (    (    R   R   -  s     c         C   s   | d j o& t |  d d  d j	 o |  i  } n t |  d d  o2 | d j p t   i   i t |   o d Sn d Sd S(   s3    Check if the user is allowed to download the file R   R!   i    i   N(   R   R
   R   R   t   getSecurityManagert   getUsert   has_permissiont   DownloadPermission(   R   R   (    (    R   t   _access_permitted5  s     &=c   	      C   s   t  | d d  } | o | i d  o | d } n2 t  | d d  p | } t | | |  \ } } | i d  } | d j o | |  Sn | S(   s    Determine the mime-type t   headerss   content-typeR   R?   i    N(   R   R	   R
   R   R   R   R   R   t   guess_content_typet   bodyt   encR   t   cutoff(	   R   R	   R   R   R   R   R   R   R   (    (    R   R   B  s     c         C   s   t  | t  o |  i |  } n t  | t  o |  i |  } n y |  i | |  WnU t |  o> y t i	 |  Wq t
 j
 o t i d d t q Xn   n X|  i   d S(   s    Store infile to outfile R   t   exc_infoN(   R   t   infilet   listR   R   t   outfilet   _copyt   isfileRb   t   removet   OSErrorR`   t   errorRf   t   http__refreshEtag(   R   R   R   (    (    R   R   O  s     c   
      C   s>  t  | t  o t | d  } d }	 n | } d }	 t  | t  oG t i t	  } z t | d  } |  i   Wd t i |  Xd } n | } d } d d >} | i |  } | i |  x4 t |  | j o  | i |  } | i |  q Wy | i d  Wn n X|	 o | i   n | o | i   n d S(   s    Read binary data from infile and write it to outfile
            infile and outfile may be strings, in which case a file with that
            name is opened, or filehandles, in which case they are accessed
            directly.
        R[   i   i    t   wbNi   i   (   R   R   R   Rl   t   instreamt   close_inR   Rb   t   umaskt   REPOSITORY_UMASKt	   outstreamR   R   t	   close_outt	   blocksizeRx   t   blockR   t   lenRu   Ry   (
   R   R   R   R   R   R   R   R   R   R   (    (    R   R   a  s>     
 

     c         C   s  |  i |  } |  i |  } t |  o y t i | | d  Wn& t	 j
 o t
 i d d t qXt |  o> y t i |  Wq t	 j
 o t
 i d d t q XqnS t |  oE y t i | | d  Wqt	 j
 o t
 i d d t qXn d S(   s    Rename the file to .undo s   .undot   _deleteR   N(   R   R   R   t   tmp_fnR   t   old_fnR   Rb   t   renameR   R`   R   Rf   R   (   R   R   R   R   (    (    R   R     s$      c         C   s   |  i |  } t |  oi t | d  oX |  i   y! t i | d |  i |   Wq t	 j
 o t
 i d d t q Xn d S(   s-    Restore filename after delete or copy-paste s   .undot   _undoR   N(   R   R   R   R   R   R   Rb   R   R   R   R`   R   Rf   (   R   R   R   (    (    R   R     s     
!c         C   s_   t  g } | i t  t | t  o | i |  n | d j o | i |  n t |   S(   sp    Generates the full filesystem name, incuding directories from
            REPOSITORY_PATH and filename
        R   N(	   t   INSTANCE_HOMEt   patht   extendt   REPOSITORY_PATHR   R   R   t   appendR   (   R   R   R   (    (    R   R     s     	c         C   s   d |  i |  S(   s:    Generates the full filesystem name of the temporary file s   %s.tmpN(   R   R   R   (   R   R   (    (    R   R     s     c         C   sn   |  i |  } t |  o | Sn |  i |  } t |  o | Sn |  i |  t |  o | Sn d S(   s    Returns the full filesystem name, preferring tmp over main.
            Also attempts to undo. Returns None if the file is broken.
        N(   R   R   R   R   R   R   R   R   (   R   R   R   R   (    (    R   R^     s     c         C   s   t  t j p | p | g  j o" |  i d | d | d |  } n | } | o6 t  t j p | o |  i
 |  q |  i |  n | S(   sq    If no unique filename has been generated, generate one.
            Otherwise, return the existing one.
        R   R   t   lockN(   t   UNDO_POLICYt   ALWAYS_BACKUPR   R   R   t   _get_new_ufnR   R   R   R   R   R   (   R   R   R   R   R   R   R   (    (    R   R     s     !"c         C   s  |  i   } t o t |  } n t i | d  } | d o | |  } | | }
 n | } d }
 | p |  i
 } n t t t f j on t |  } | d j	 oQ | t i j o |
 p6 t t j o |
 | j o | |
 } q n | }
 q q n | d j	 o
 | } n |  i   } g  }	 t t j o
 | }	 n[t t t t f j ot t j of d i t |  | g  } t i  t! i" |  i#    d  } | i% d d  } | i% d d  } n= t t j o) t |  } | i&   d i |  } n | } x t' t(  D]Q } t* |  t+ t( | j  o |	 i, t+ d  q|	 i, | t+   | t+ } qWn0 t t- j o" t. |  t/  } | | |  }	 n t1 t2 j o+ g  } |	 D] } | | i5   q~ }	 n |  i6 |	  } t8 i i9 |  p5 t8 i: t;  } z t8 i< |  Wd t8 i: |  Xn t= } t i? | d	  d
 j o1 t i% | d	 d  } t@ tA   iB d   } n d
 } t i? | d  d j o tD d t=   n t i? | d  d
 j os tE |  d d  d j	 oA |  iF iG d  o. tH   iI   iJ   } t i% | d |  } q1t i% | d d  } n t i? | d  d
 j o+ t iL | d  } t i% | d |  } n t i? | d  d
 j p t i? | d  d
 j o. t i% | d |  } t i% | d |
  } n | o |  iN |  n | o& t | t i% | d d |   } n t | t i% | d d   } x` tP |  p" tP | d  p tP | d  o0 | d } t | t i% | d d |   } q;W| o t i% | d d |  } n t i% | d d  } |	 i, |  |	 S(   s    Create a new unique filename t   .i   R   iR   t   _t   +Ns   %ti    s   %cs
   %m%d%H%M%Ss   Invalid file format '%s's   %uR   t   AUTHENTICATED_USERs   %ps   %ns   %es   .%ss   .undos   .tmp(Q   R   R   R   t   COPY_OF_PROTECTIONt   copy_of_protectR   R   t   post   id_namet   id_extR   t   REPOSITORY_EXTENSIONSt   MIMETYPE_APPENDt   MIMETYPE_REPLACEt   guess_extensiont   mime_extR
   t   configt   unknown_typesR   t   rel_url_listt   _get_zodb_patht   dirst
   REPOSITORYt	   SYNC_ZODBt   SLICEDt   SLICED_REVERSEt   SLICED_HASHR   R   t   hashedt   base64t   encodestringt   shat   newt   digestt   tempt   replacet   reverset   ranget   SLICE_DEPTHt   iR   t   SLICE_WIDTHR   t   CUSTOMt
   aq_acquiret   CUSTOM_METHODt   methodt   NORMALIZE_CASEt	   NORMALIZEt   _[1]t   dt   lowerR   t   dirpathRb   t   isdirR   R   t   makedirst   FILE_FORMATt
   fileformatR   Rs   RD   t   strftimet   counterR   R   R   R   R   R   t   getUserNamet   usert
   joinfieldsR   t
   _dir__lockR   R   (   R   R   R   R   R!  R   R   R   R  R  R   R  R  R   R   R  R  R  R'  R%  R  R   R)  (    (    R   R     s     


"
 + ,2 & /
'c         C   s   |  i S(   s    Returns the id of this object N(   R   R   (   R   (    (    R   R   H  s     c         C   sb   | d j o |  i } n t t j o | i d  i d  } n t	 | i
    } t d |  S(   s,    Returns the ZODB path of the parent object i   R   N(   t   parentR
   R   t	   aq_parentt	   ZODB_PATHt   VIRTUALRS   RB   R   R   t   getPhysicalPathRp   (   R   R,  R   (    (    R   R  L  s     c         C   s#  t  i i d  } | d	 j	 o |  i | |  \ } } } } | o d |  i   Sqt  i i d  } |  i i } | d	 j	 o- | d  d j o d | } q d | } n | t i |  d } | t i d i |   Sn= | o d |  i   Sn$ | o d |  i   Sn |  i   Sd	 S(
   s#    Return the static url of the file R   s	   %s?icon=1t   EXTFILE_STATIC_HOSTi   s   https://s   http://R   s   %s?preview=1N(   Rb   R   R   t   static_pathR
   R   R]   RP   RN   R   R   RS   t   static_hostR   t
   SERVER_URLt   hostR   R   R   (   R   RP   RN   R3  R   R5  R   R2  (    (    R   RQ   W  s&     c         C   s   |  i   p
 d } n | o t |  d d  o
 d } n | o |  i   } d } n0 | o |  i } |  i	 } n |  i } |  i } | | | | f S(   s/    Find out about the file we are going to serve i   R   i    s	   image/gifN(
   R   R   RN   R   RP   Re   R   R   t   prev_filenamet   prev_content_type(   R   RP   RN   R   R   (    (    R   R]   s  s     


			c         C   s   t  t |  i |   S(   s    Returns the filesystem path of the icon corresponding
            to self.content_type. If broken is True, returns the
            filesystem path of the "broken" icon.
        N(   R   t   hereR   R   RV   (   R   RV   (    (    R   Re     s     c   	      K   s   d | f } | d j	 o d | | f } n | d j	 o d | | f } n xA | i   D]3 } | i |  } | o d | | | f } q\ q\ Wd | | f S(   s]    Constructs an HTML link tag.
            Assumes all arguments are properly quoted.
        s   <a href="%s"s   %s title="%s"s   %s class="%s"s
   %s %s="%s"s	   %s>%s</a>N(   R}   t   resultR   R
   R   R   R   t   keyR   t   valueR    (	   R   R}   R    R   R   R   R;  R9  R:  (    (    R   R     s      c         C   s   t  |  } | d j o5 | d } | d j o | d } d } qT d } n d } d | } | d  } | d d	 j o | d  } n | | } | S(   sq    Convert an int-value (file-size in bytes) to an String
            with the file-size in Byte, KB or MB
        i  i   s    MBs    KBs    Bytess   %4.2fi   i   R   N(   t   floatR;  t   bytest   typt   strg(   R   R;  R?  R=  R>  (    (    R   R     s     





 
c         C   s   t  t t f j o
 | | j S(   s;    Return true if the content_type has changed (and we care) N(   R   R   R   R   t   new_content_type(   R   R   R@  (    (    R   R     s     c         C   s!   |  i   t i t |    d S(   sc    Called whenever the file data has been updated.
            Fires an ExtFileUpdatedEvent.
        N(   R   R   t   eventt   notifyt   ExtFileUpdatedEvent(   R   (    (    R   R     s     
t
   get_fsnamec         C   s   |  i |  i  S(   s   Returns the current file system path of the file or image.
            This path can be used to access the file even while a
            transaction is in progress (aka Zagy's revenge :-).
            Returns None if the file does not exist in the repository.
        N(   R   R^   R   (   R   (    (    R   RD    s     c         C   s3   t  |  d  o t d   n t |  |  _ d S(   s    Lock a directory t   _v_dir__locks   Double lock in threadN(   Rq   R   t   DirLockErrort   DirLockt   dirRE  (   R   RH  (    (    R   R+    s     c         C   s2   t  |  d  o |  i i   t |  d  n d S(   s&    Unlock a previously locked directory RE  N(   Rq   R   RE  t   releaset   delattr(   R   (    (    R   R     s     c         C   s[   t  o t i d t i |    n t i |   t  o t i d t i |    n d  S(   Ns   registering %ss   registered %s(   R_   R`   Ra   t   TMt   containsR   t   register(   R   (    (    R   R     s
      c         C   s/   d |  _ t o t i d |  i    n d  S(   Ni   s   beginning %s(   R   t   _v_begin_calledR_   R`   Ra   R   (   R   (    (    R   t   _begin  s    	 c         C   s  d |  _ t i |   |  i o |  i |  i  } t o t i	 d |  n t
 |  o t o t i	 d |  n |  i |  i  } t
 |  o> y t i |  Wq t j
 o t i d d t q Xn y t i | |  Wqt j
 o t i d d t qXqn d S(   s    Commits the temporary file i   s   finishing %ss	   isfile %st   _finishR   N(   R   t   _v_finish_calledRK  R   R   R   R   R_   R`   Ra   R   R   R   Rb   R   R   Rf   R   (   R   R   R   (    (    R   RP    s(     	
  c         C   s   d |  _ t i |   |  i o |  i |  i  } t o t i	 d |  n t
 |  oZ t o t i	 d |  n y t i |  Wq t j
 o t i d d t q Xq n d S(   s    Deletes the temporary file i   s   aborting %ss	   isfile %st   _abortR   N(   R   t   _v_abort_calledRK  R   R   R   R   R_   R`   Ra   R   Rb   R   R   Rf   (   R   R   (    (    R   RR    s     	
  (   s   Manager(Z   t   __name__t
   __module__t   __doc__t	   IDAVAwareR
   t	   interfacet
   implementst   IExtFilet   WriteLockInterfacet   __implements__t   _propertiesR!   R   t   PropertyManagert   manage_optionst
   SimpleItemt	   Cacheablet   ClassSecurityInfot   securityt	   meta_typet   setPermissionDefaultR   R   R9   t   propertyR<   R=   R>   RM   RU   t   declareProtectedt   ViewPermissionR;   Rz   t   FalseR|   R   R   R   R:   R   R   R   Rd   R   R   RR   RO   t   AccessPermissionR   R   t   HTMLFilet   globalsR   t   ChangePermissionR   R(   R   R   R   R   R   R   R   R   R   R   R   R   R   R^   R   R   R   R  RQ   R]   Re   R   R   R   R   t   declarePrivateRD  R+  R   R   RO  RP  RR  (    (    (    R   R    |   s    	^						<																	"		
			y							
			c         C   sk   |  i |  i  } | oN |  i   z2 |  i   } |  i | |  i |   | |  _ Wd |  i	   Xn d S(   s    When a copy of the object is created (zope copy-paste-operation),
        this function is called by CopySupport.py. A copy of the external
        file is created and self.filename is changed.

        Subscriber for (IExtFile, IObjectClonedEvent)
    N(
   R   R^   R   R   R   R   R   R   R   R   (   R   RA  R   R   (    (    R   t
   afterClone  s     
 c         C   s   d k  l } d k  l } | i |  o d Sn |  i |  i  | i |  o d Sn |  i |  i  } | o^ |  i
   zB |  i   } |  i | |  i |   |  i |  i  | |  _ Wd |  i   Xn d S(   s   This method is called, whenever _setObject in ObjectManager gets
        called. This is the case after a normal add and if the object is a
        result of cut-paste- or rename-operation. In the first case, the
        external files doesn't exist yet, otherwise it was renamed to .undo
        by beforeDelete before and must be restored by _undo().

        Subscriber for (IExtFile, IObjectMovedEvent)
    (   s   IObjectAddedEvent(   s   IObjectRemovedEventN(   t   zope.app.container.interfacest   IObjectAddedEventt   IObjectRemovedEventt
   providedByRA  R   R   R   R^   R   R   R   R   R   R   R   R   (   R   RA  Rq  Rr  R   R   (    (    R   t   afterAdd  s$     
 c         C   s9   d k  l } | i |  o d Sn |  i |  i  d S(   s   This method is called, when the object is deleted. To support
        undo-functionality and because this happens too, when the object
        is moved (cut-paste) or renamed, the external file is not deleted.
        It is just renamed to filename.undo and remains in the
        repository, until it is deleted manually.

        Subscriber for (IExtFile, IObjectWillBeMovedEvent)
    (   s   IObjectWillBeAddedEventN(   t   OFS.interfacest   IObjectWillBeAddedEventRs  RA  R   R   R   (   R   RA  Rv  (    (    R   t   beforeDeleteC  s
     sK    ,;:'"()[]{}ݟtK   ____________AAAAAAaaaaaaCcEEEEEeeeeeIIIIiiiiNnOOOOOOooooooSssUUUUuuuuYYyyZzc         C   s   y t  |  d  } WnD t j
 o8 } y t  |  d  } WqZ t j
 o } |  SqZ Xn X| i d d  }  t i |  t  }  |  S(   Ns   utf-8s   iso-8859-15t   ignore(	   t   unicodeR   t   uidt   UnicodeErrort	   TypeErrort   encodeR   t	   translatet   TRANSMAP(   R   R}  R{  (    (    R   R   [  s    s   (^(copy[0-9]*_of_)+)c         C   s=   t  i |   } | d  j	 o |  t | i d   }  n |  S(   Ni   (   t
   copy_of_ret   matchR   R
   R   t   group(   R   R  (    (    R   R   m  s    c         C   sC   |  i   }  t i i |   } | d  j	 o | Sn t i |  |  S(   N(
   R   R   R  t   mimetypes_override_mapR   t	   extensionR
   t	   mimetypesR   t   strict(   R   R  R  (    (    R   R   u  s
    (   s   FieldStorage(   s
   FileUploadc         C   s   t  |  t  o | o | |  i d <q nu h  d d <} | o | | d <n9 t |  d  o( |  i i d  o |  i d | d <n t t |  d |  }  | o | |  _	 n |  S(   sD    Create a FileUpload instance from a file handle (and content_type) s   content-typet   REQUEST_METHODt   POSTt   CONTENT_TYPER   R   N(
   R   t   fpt
   FileUploadR   R   R   Rq   R   t   FieldStorageR   (   R  R   R   R   (    (    R   R     s     #c         C   so   t    } |  i |  i } } | i |  x2 | d  j	 o$ | i | i } } | i |  q, W| i d  | S(   Ni    (   Ri   R   R	   R<   t   nextR   R
   Ru   (   R	   R  R   R<   (    (    R   R     s    	 RF  c           B   s   t  Z RS(   N(   RT  RU  (    (    (    R   RF    s   RG  c           B   sA   t  Z d  Z d Z d   Z d   Z d   Z d   Z d   Z RS(   s%    Manage the lockfile for a directory s   @@@lockc         C   s   t  i i | |  i  |  _ x} t t i	  D]E } |  i   o t i d |  i  n |  i   St i t i  q+ Wt i d |  i  t d |  i   d  S(   Ns   Waiting for lock %ss   Failed to get lock %s(   Rb   R   R   RH  R   t	   lock_namet   _lockR  R  t   dirlock_sleep_countR  t   islockedR`   t   debugt   _mklockt   timet   sleept   dirlock_sleep_secondsR   RF  (   R   RH  R  (    (    R   R9     s     
c         C   s   |  i   d  S(   N(   R   t   _rmlock(   R   (    (    R   RI    s    c         C   s   t  i i |  i  d  S(   N(   Rb   R   t   existsR   R  (   R   (    (    R   R    s    c         C   s-   t  |  i d  } | i d  | i   d  S(   Nt   wtsN   ExtFile directory lock. This file should only exist for short periods of time.(   Rl   R   R  t   fR   Ry   (   R   R  (    (    R   R    s    c         C   s   t  i |  i  d  S(   N(   Rb   R   R   R  (   R   (    (    R   R    s    (	   RT  RU  RV  R  R9   RI  R  R  R  (    (    (    R   RG    s    				Rw   c           B   sE   t  Z e f Z d d >d  Z d   Z d   Z d   Z d   Z RS(   Ni   i   c         C   s   | |  _ | |  _ d  S(   N(   t   streamR   t   _streamR   t
   _blocksize(   R   R  R   (    (    R   R9     s    	c         C   s@   |  i i |  i  } | p  |  i i   d  |  _ t  n | S(   N(   R   R  Rx   R  R<   Ry   R
   t   StopIteration(   R   R<   (    (    R   R    s    	
c         C   sH   |  i i   } |  i i d d  |  i i   } |  i i | d  | S(   Ni    i   (   R   R  Rv   t   cur_posRu   Rd   (   R   R  Rd   (    (    R   R>     s
    c         C   s   |  i   o d p d S(   Ni   i    (   R   R>   (   R   (    (    R   t   __nonzero__  s    c         C   s%   |  i d  j	 o |  i i   n d  S(   N(   R   R  R
   Ry   (   R   (    (    R   t   __del__  s    (	   RT  RU  t   IStreamIteratorR\  R9   R  R>   R  R  (    (    (    R   Rw     s   				(j   RV  R8   t&   Products.ZCatalog.CatalogPathAwarenesst   CatalogAwaret   OFS.SimpleItemR`  t   OFS.PropertyManagerR^  t	   OFS.CacheRa  t	   OFS.ImageR   t   GlobalsRk  t   MessageDialogt   InitializeClasst   package_homet   AccessControlRb  R   t   Permissionst   AcquisitionR  t   aq_baset   webdav.LockableR   t   webdav.commonRI   RD   R   Rb   R   R  R  R  t   reR  t	   threadingt   os.pathR   R   t   tempfileRi   t   Products.ExtFileRK  t   zExceptionsR{   t   ZPublisher.IteratorsR  t   DocumentTemplate.html_quoteR~   t   webdav.WriteLockInterfaceR[  t   zopeRX  t
   interfacesRZ  RA  RC  t   loggingt   _SUBSYSR_   t	   getLoggerR`   t   zope.contenttypeR   t   ImportErrort   zope.app.content_typest    Products.GenericSetup.interfacesRW  R
   Rl  R8  t   viewRh  t   view_management_screensRj  Rm  R   t   configurationt   manage_addExtFileFormR   R    Ro  Rt  Rw  t	   bad_charst
   good_charst	   maketransR  R   t   compileR  R   Rf   R   t   cgiR  t   ZPublisher.HTTPRequestR  R   R   R   RF  RG  Rw   (G   R{   R   Ra  Rh  R  R  R  RD   R  R  RW  Rm  R[  R   RA  R  Rw   R_   R  RF  R  R  R  R   R  RK  Rw  R  R`   R8   Rk  RV  R   R  R   R  RC  R   R   R   R    Rb  R   R8  R  R`  R  Rj  RX  R  Rt  RI   R  Ri   Ro  R   R   R  R   R  R   R  R   R  R^  RZ  RG  Rb   R~   R  R   (    (    R   t   ?   s   *-$			!   
		$					!