m
&Gc           @   sP  d  Z  d Z  d Z d k l Z l Z l Z d k l Z d k l	 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 l 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! d k l" 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) d k# l* Z* d k# l+ Z+ d k, l- Z- d k, l. Z. d k, l/ Z/ d k, l0 Z0 d k, l1 Z1 d k2 Z2 d  Z3 d! Z4 e2 i5 e3  Z6 e d" e7    Z8 d# d# d# d# d# d# e- d# d# d! d! d! e9 d$  Z: d  e$ f d%     YZ; e e;  d&   Z< d'   Z= d(   Z> d S()   s   ExtImage product module.s   ExtImage product module.
    The ExtImage-Product works like the Zope Image-product, but stores the
    uploaded image externally in a repository-direcory. It creates a preview
    of the image (requires PIL).s   2.0.2(   s   HTMLFiles   MessageDialogs   InitializeClass(   s   ClassSecurityInfo(   s   ResourceLockedErrorN(   s   joins   isfile(   s   TemporaryFile(   s   Redirect(   s   getImageInfo(   s   Pdata(   s   aq_base(   s
   html_quote(   s   WriteLockInterface(   s	   interface(   s	   IExtImage(   s   event(   s   ExtImageUpdatedEvent(   s   ExtFile(   s   normalize_id(   s   pdata(   s
   HTTPUpload(   s   ViewPermission(   s   AccessPermission(   s   ChangePermission(   s	   IDAVAware(   s
   NO_PREVIEW(   s   GENERATE(   s   UPLOAD_NORESIZE(   s   UPLOAD_RESIZE(   s   REPOSITORY_UMASKt   ExtImagei    s   www/extImageAddt    c         C   sd  | 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 | | d | | |	 |
  n | d j oF | t j p | t j o, |  i |  i | d d | | |	 |
  n | d	 j	 o |  i |  | d d Sn | S(
   s    Add an ExtImage to a folder. t   filenamet   /s   \t   :i   R   i    t   update_menuN(   t   idt   getattrt   filet   NoneR   t   maxt   stringt   rfindt   titlet   normalize_idR    t   descrt   permission_checkt   redirect_default_viewt   tempExtImaget   selft
   _setObjectt   _getObt   manage_file_uploadt   content_typet   create_prevt   maxxt   maxyt   ratiot   previewt   UPLOAD_NORESIZEt   UPLOAD_RESIZEt   REQUESTt   manage_main(   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    t?   /home2/kennisonb/webapps/zope/Zope/Products/ExtFile/ExtImage.pyt   manage_addExtImage[   s"     !	,',c           B   s  t  Z d  Z e e j	 o e i e e  n e i e  e f Z	 e
   Z d Z h  d d <d d <d d <Z e d Z e d Z d d d	 d	 d
  Z d   Z e i e d  d	 d	 e e e d	 d	 d	 d e e e d  Z e i e d  d   Z e i e d  e e e d	 d	 d	 e e d  Z e i e d  d   Z e i e d  e e e e d  Z e i e d  d d  Z e i e d  d   Z e i e d  d   Z e i e d  d   Z e i e d   d!   Z e i e d"  d#   Z e i e d$  d%   Z  e i e d&  d'   Z! e i e d(  d)   Z" e i e d*  d+   Z# e i e d,  d-   Z$ e i e% d.  d/   Z& e i e% d0  e' d1 e(    Z) e i e* d2  e d3  Z+ e i e* d4  d	 d	 d	 e d5  Z, e i e% d6  e' d7 e(    Z- e i e* d8  d d d	 e. d d d	 e d9  Z/ e i e* d:  d d d	 e. d d d	 e d;  Z0 e i e* d<  d	 e d=  Z1 e i e* d>  d?   Z2 d@   Z3 dA   Z4 dB   Z5 dC   Z6 e dD  Z7 dE   Z8 dF   Z9 dG   Z: RS(H   s    The ExtImage-Product works like the Zope Image-product, but stores the
        uploaded image externally in a repository-directory. It can create a
        preview of the image (requires PIL).R    t   defaulti   t   mini   R
   i  R   i    c         C   sJ   t  t |   i | | | | |  g  |  _	 d |  _
 d |  _ d |  _ d S(   s'    Initialize a new instance of ExtImage R   i   i    N(   t   superR    R   t   __init__R   R   R   R   R   t   prev_filenamet   prev_content_typet
   prev_ratiot   has_preview(   R   R   R   R   R   R   (    (    R!   R&      s     "			c         C   s
   |  i   S(   N(   R   t   tag(   R   (    (    R!   t   __str__   s    R+   t   0c         K   s  |  i   p
 d } n |  i   p
 d } n | o |  i o
 d } n | o" |  i d d  } d \ } } nZ | o. |  i d d  } |  i
 |  i  \ } } n% |  i   } |  i
 |  i  \ } } | p | } | p | } | p | } | p | } | o- | d j o  t t t |  |   } n | o- | d j o  t t t |  |   } n d | f } | d j o t |  d d  } n d | t |  f } | d j o t |  d	 d  } n d
 | t |  f } | o d | | f } n | o d | | f } n | d j	 o d | | f } n xA | i   D]3 } | i  |  } | o d | | | f } qKqKWd | S(   s=   Generate an HTML IMG tag for this image, with customization.
            Arguments to self.tag() can be any valid attributes of an IMG tag.
            'src' will always be an absolute pathname, to prevent redundant
            downloading of images. Defaults are applied intelligently for
            'height', 'width', and 'alt'. If specified, the 'scale', 'xscale',
            and 'yscale' keyword arguments will be used to automatically adjust
            the output height and width values of the image tag.
            Adopted and adapted from OFS/Image.py
        i   t   iconi    R   s   <img src="%s"t   altR   s   %s alt="%s"R   s   %s title="%s"s   %s height="%s"s   %s width="%s"s   %s class="%s"s
   %s %s="%s"s   %s />N(   i    i    ("   R   t   is_webviewableR   t   _access_permittedR*   R.   t   _static_urlt   urlt	   img_widtht
   img_heightt   _getImageSizeR'   R   t   heightt   widtht   xscalet   scalet   xdeltat   yscalet   ydeltaR	   t   strt   intt   resultR/   R   t
   html_quoteR   t	   css_classt   argst   keyst   keyt   gett   value(   R   R   R.   R7   R8   R/   R:   R9   R<   t   borderR   R   RB   RC   R4   R5   R@   R;   RE   R=   R3   RG   (    (    R!   R+      sT     


   R   c         C   s   t  |  i d d    d S(   s    Return a preview of the image R   i   N(   t   RedirectR   R2   (   R   (    (    R!   R      s     t   preview_tagc	   
      K   sC   |  i d d d | d | d | d | d | d | d	 | d
 | |	  	S(   s1    Generates an HTML IMG tag for the preview image R   i   R7   R8   R/   R:   R9   R<   R   RB   N(   R   R+   R7   R8   R/   R:   R9   R<   R   RB   RC   (
   R   R7   R8   R/   R:   R9   R<   R   RB   RC   (    (    R!   RJ      s     t   preview_htmlc         C   s
   |  i   S(   s    Same as preview_tag N(   R   RJ   (   R   (    (    R!   RK      s     t   preview_linkc         K   s+   |  i d d d | d | d | d | |  S(   s9    Returns an HTML link tag for the preview image.
        R   i   t   textt	   structureR   RB   N(   R   t   linkRM   RN   R   RB   RC   (   R   RM   RN   R   RB   RC   (    (    R!   RL      s     t   thumbnail_tagi   c         C   sd  t  d |  } |  i o |  i |  i  os |  i |  i  \ } } | | j p | | j o- |  i	 d d d t
 |  t  | |   Sq |  i	 d d  Sq`d |  i   f Sn |  i   og |  i |  i  \ } } | | j p | | j o' |  i	 d t
 |  t  | |   Sq`|  i	   Sn9 |  i |  i  o |  i	 d d  Sn d |  i   f Sd S(	   se    Returns the HTML image tag for a thumbnail view of this image.
            Used by the ZMI.
        i    R   i   R:   sA   <img src="%s?preview=1" alt="" title="" height="32" width="32" />R.   s7   <img src="%s" alt="" title="" height="32" width="32" />N(   R
   t   sizeR   R*   t   _get_fsnameR'   R6   R8   R7   R+   t   floatt   absolute_urlR0   R   (   R   RQ   R7   R8   (    (    R!   RP      s"     
-'t	   is_brokenc         C   sO   |  i o2 |  i |  i j o |  i |  i  p d Sq< n t t |   i   S(   s@    Check if external file exists and return true (1) or false (0) i   N(   R   R*   R   R'   RR   R%   R    RU   (   R   (    (    R!   RU     s
     R0   c         C   sC   |  i   } | d j p | d j p | d j o d Sn d Sd S(   s<    Return 1 for GIF, JPEG, and PNG images, otherwise return 0 t   JPEGt   GIFt   PNGi   i    N(   R   t   format(   R   RY   (    (    R!   R0     s
     't   get_prev_sizec         C   s2   |  i |  i  } | o t i |  d Sn d S(   s&    Returns the size of the preview file i   i    N(   R   RR   R'   t   fnt   ost   stat(   R   R[   (    (    R!   RZ   '  s
     t   prev_rawsizec         C   s
   |  i   S(   s    Same as get_prev_size N(   R   RZ   (   R   (    (    R!   R^   /  s     t	   prev_sizec         C   s   |  i |  i    S(   s=    Returns a formatted stringified version of the preview size N(   R   t   _bytetostringRZ   (   R   (    (    R!   R_   4  s     R8   c         C   s   |  i |  i  d S(   s    Pixel width of the image i    N(   R   R6   R   (   R   (    (    R!   R8   9  s     R7   c         C   s   |  i |  i  d S(   s    Pixel height of the image i   N(   R   R6   R   (   R   (    (    R!   R7   >  s     t
   prev_widthc         C   s   |  i |  i  d S(   s    Pixel width of the preview i    N(   R   R6   R'   (   R   (    (    R!   Ra   C  s     t   prev_heightc         C   s   |  i |  i  d S(   s    Pixel height of the preview i   N(   R   R6   R'   (   R   (    (    R!   Rb   H  s     RY   c         C   sK   |  i |  i  } y' d k l } | i |  } | i SWn d Sn Xd S(   s&    Get the PIL file format of the image (   s   Imaget   unknownN(   R   RR   R   t   PILt   Imaget   opent   imRY   (   R   Rg   Re   R   (    (    R!   RY   M  s     t   get_prev_filenamec         C   s   |  i |  i  S(   s+    Returns the preview file name for display N(   R   t   _fsnameR'   (   R   (    (    R!   Rh   X  s     R    s   www/extImageEditt   manage_del_prevc         C   sx   |  i o' |  i |  i j o |  i |  i  n d |  _ d |  _ |  i   | d j	 o |  i	 |  | d d Sn d S(   s    Delete the Preview Image R   i    t   manage_tabs_messages   Preview deleted.N(
   R   R*   R   R'   t   _deleteR(   t   ZCacheable_invalidateR   R	   R    (   R   R   (    (    R!   Rj   e  s     		
t   manage_create_prevc         C   s  |  i | |  \ } } | d j oa | d j oT |  i   z8 |  i |  i d d } |  i |  i	 | | | |  Wd |  i   Xn | d j o |  i Snu |  i o |  i |  | d d SnQ | d j o* | d j o t d d	 d
 d d d  Sn t d d d
 d d d  Sd S(   s    Create a preview Image i    R   s
   image/jpegNRk   s   Preview created.R-   R   t	   Attentiont   messages   You must enter a value > 0t   actions   ./manage_maint   Warnings/   An error occurred while generating the preview.(   R   t   _formatDimensionsR   R   t	   _registert   _get_ufnR'   t   new_fnt   _createPreviewR   R   t   _dir__unlockR   R	   R*   R    t   MessageDialog(   R   R   R   R   R   Rv   (    (    R!   Rn   r  s(     
  
		t   manage_uploadForms   www/extImageUploadt   manage_uploadc	   
   	   C   s|   |  i   o t d   n t | t  o' t   }	 |	 i |  |	 i	 d  n | }	 |  i
 |	 | | | | | | |  S(   s8    Upload image from file handle, Pdata, or string buffer s   File is locked via WebDAVi    N(   R   t   wl_isLockedt   ResourceLockedErrort
   isinstanceR   R>   t   TemporaryFilet	   temp_filet   writet   seekR   R   t
   is_previewR   R   R   R   R   (
   R   R   R   R   R   R   R   R   R   R   (    (    R!   R{     s     	R   c	         C   s  |  i   o t d   n | ot | t  o t | d  } n! t | t  o t	 |  } n |  i
 | |  \ } } | t j o | d j ow | d j oj |  i   z5 |  i |  i d d }	 |  i | |  i |	   Wd |  i   X|  i |	 |	 | | |  q| 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!   n t" t# |   i$ | |  | t% j o |  i
 | |  \ } } | d j oa | d j oT |  i   z8 |  i |  i d d }	 |  i |  i& |	 | | |  Wd |  i   Xqn | d j	 o |  i) |  | d	 d
 Sn d S(   s:    Upload image from file handle, Pdata, or local directory s   File is locked via WebDAVt   rbi    R   s
   image/jpegNid   t   backupRk   s   Upload complete.(*   R   R|   R}   R   R~   R   R>   Rf   t   Pdatat   pdataRs   R   R   R   R   Rt   Ru   R'   Rv   t   _update_datat   _temp_fsnameRx   Rw   R   R   t
   HTTPUploadt   _get_content_typet   readt   _get_zodb_idR(   R   t   _changedR   t   _initPreviewR%   R    R   t   GENERATER   R   R	   R    (   R   R   R   R   R   R   R   R   R   Rv   R   (    (    R!   R     sX     '
 
 		
  t   manage_http_uploadc         C   s[  |  i   o t d   n | o 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   n t t |   i |  | d j	 o |  i |  | d d	 Sn d S(
   s    Upload image from http-server s   File is locked via WebDAVs   /:id   i    R   R   NRk   s   Upload complete.(    R   R|   R}   R   t   urllibt   quoteR3   t   urlopenR   R   R   R   R   R(   R   R   Rt   R'   R   R   Ru   Rv   R   R   Rx   R   R%   R    R   R   R	   R    (   R   R3   R   R   R   R   Rv   R   (    (    R!   R     s.     
 		t   PUTc         C   s   t  t |   i | |  } |  i ou |  i |  i |  i	  \ } } |  i   z; |  i |  i d d } |  i |  i | | | |  i  Wd |  i   Xn | S(   s    Handle HTTP PUT requests R   s
   image/jpegN(   R%   R    R   R   R   t   RESPONSER*   Rs   t	   prev_maxxt	   prev_maxyR   R   Rt   Ru   R'   Rv   Rw   R   R)   Rx   (   R   R   R   R   R   Rv   (    (    R!   R     s     

# c         C   sY   |  i |  } y8 d k l } | i |  } | i d | i d f SWn d Sn Xd S(   s&    Return width, height tuple using PIL (   s   Imagei    i   N(   i    i    (   R   RR   R   Rd   Re   Rf   Rg   RQ   (   R   R   Rg   Re   (    (    R!   R6     s     c         C   s  y d k  l } Wn t j
 o nX|  i |  } | ol| i |  }
 |
 i	 d j o |
 i
 d  }
 n | i } t | d  o | i } n | o |
 i | | f |  n |
 i | | f |  }
 t i t  } |  i |  }	 y |
 i |	 d d d Wnb t i |  t |	  o> y t i |	  Wqdt j
 o t i d d t  qdXn   n Xt i |  d	 |  _! | |  _" | |  _# | |  _$ | |  _% n |  i&   d
 S(   s    Generate a preview using PIL (   s   Imaget   RGBt	   ANTIALIASRV   t   qualityiU   Rw   t   exc_infos
   image/jpegN('   Rd   Re   t   ImportErrorR   RR   t   from_filenamet   imfileRf   Rg   t   modet   convertt   BICUBICt   filtert   hasattrR   R   t	   thumbnailR   R   t   resizeR\   t   umaskt   REPOSITORY_UMASKR   t   to_filenamet   outfilet   savet   isfilet   removet   OSErrort   loggert   errort   TrueR(   R'   R   R   R)   R   (   R   R   R   R   R   R   R   R   R   R   Rg   Re   (    (    R!   Rw     sF     					c         C   sV   |  i   |  i |  i  \ } } | d j p | d j o d |  _ n
 d |  _ d S(   s    Verify the preview i    i   N(   R   Rm   R6   R'   Ra   Rb   R*   (   R   Rb   Ra   (    (    R!   R   5  s     
c         C   sJ  t  | t  o7 y t |  } WqG t j
 o |  i d } qG Xn t  | t  o7 y t |  } Wq t j
 o |  i d } q Xn | d j o | d j o | |  i d j  o |  i d } n& | |  i d j o |  i d } n | |  i d j  o |  i d } q@| |  i d j o |  i d } q@n | | f S(   s+    Make sure, the dimensions are valid int's R#   i    R$   R
   N(   R~   R   R>   R?   t
   ValueErrorR   t   _image_sizeR   (   R   R   R   (    (    R!   Rs   ?  s*             c         C   sK   t  |  \ } } } | o
 | } n" t t |   i	 | | | |  } | S(   s    Determine the mime-type N(   t   getImageInfot   bodyt   ctt   wt   hR   R%   R    R   R   R   R   (   R   R   R   R   R   R   R   R   (    (    R!   R   N  s     
!c         C   s!   |  i   t i t |    d S(   se    Called whenever the image data has been updated.
            Fires an ExtImageUpdatedEvent.
        N(   R   Rm   t   eventt   notifyt   ExtImageUpdatedEvent(   R   (    (    R!   t   _afterUpdateX  s     
c         C   s-  |  i o|  i |  i j 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 t t |   i   d S(   s    Commits the temporary file s   finishing %ss	   isfile %st   _finishR   N(   R   R'   R   R   t   tmp_fnt   _debugR   t   infoR   Ri   R[   R\   R   R   R   R   t   renameR%   R    R   (   R   R   R[   (    (    R!   R   c  s&        c         C   s   |  i o |  i |  i j 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 t t |   i   d S(   s    Deletes the temporary file s   aborting %ss	   isfile %st   _abortR   N(   R   R'   R   R   R   R   R   R   R   R\   R   R   R   R   R%   R    R   (   R   R   (    (    R!   R   v  s        (;   t   __name__t
   __module__t   __doc__t	   IDAVAwareR	   t	   interfacet
   implementst	   IExtImaget   WriteLockInterfacet   __implements__t   ClassSecurityInfot   securityt	   meta_typeR   R   R   R&   R,   t   declareProtectedt   ViewPermissionR+   R   RJ   RK   t   FalseRL   RP   RU   R0   RZ   R^   R_   R8   R7   Ra   Rb   RY   t   AccessPermissionRh   t   HTMLFilet   globalsR    t   ChangePermissionRj   Rn   Rz   t
   NO_PREVIEWR{   R   R   R   R6   Rw   R   Rs   R   R   R   R   (    (    (    R!   R    r   s    		!

	-:	!	 										
		!!3		
	(	
	
		c         C   s   |  i o |  i |  i j ot |  i |  i  } | oW |  i   z; |  i d |  i  } |  i
 | |  i |   | |  _ Wd |  i   Xq n |  i p g  |  _ n 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 (IExtImage, IObjectClonedEvent)
    R   N(   R   R*   R   R'   RR   R[   Rt   t   _get_new_ufnR(   Rv   R   R   Rx   (   R   R   R[   Rv   (    (    R!   t
   afterClone  s     
 
c         C   s7  d k  l } d k  l } | i |  o d Sn |  i o' |  i |  i j o |  i	 |  i  n | i |  o d Sn |  i o |  i |  i j o |  i
 |  i  } | og |  i   zK |  i d |  i  } |  i | |  i |   |  i |  i  | |  _ Wd |  i   Xq3n |  i p g  |  _ n 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 (IExtImage, IObjectMovedEvent)
    (   s   IObjectAddedEvent(   s   IObjectRemovedEventNR   (   t   zope.app.container.interfacest   IObjectAddedEventt   IObjectRemovedEventt
   providedByR   R   R*   R   R'   t   _undoRR   R[   Rt   R   R(   Rv   R   R   Rl   Rx   (   R   R   R   R   Rv   R[   (    (    R!   t   afterAdd  s,     
 
c         C   sZ   d k  l } | i |  o d Sn |  i o' |  i |  i j o |  i |  i  n 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 (IExtImage, IObjectWillBeMovedEvent)
    (   s   IObjectWillBeAddedEventN(	   t   OFS.interfacest   IObjectWillBeAddedEventR   R   R   R*   R   R'   Rl   (   R   R   R   (    (    R!   t   beforeDelete  s     (?   R   t   __version__t   GlobalsR   Ry   t   InitializeClasst   AccessControlR   t   webdav.LockableR}   R   R\   R   t   os.patht   joinR   t   tempfileR   t   zExceptionsRI   t	   OFS.ImageR   R   t   Acquisitiont   aq_baset   DocumentTemplate.html_quoteRA   t   webdav.WriteLockInterfaceR   t   zopeR   t
   interfacesR   R   R   t   Products.ExtFile.ExtFilet   ExtFileR   R   R   R   R   R   R   t   configurationR   R   R   R   R   t   loggingt   _SUBSYSR   t	   getLoggerR   R   t   manage_addExtImageFormR	   R"   R    R   R   R   (.   RI   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   R   R   R   R   R   Ry   R   R   R   R"   R   R   R   R   R   R    R   R\   RA   (    (    R!   t   ?   sX   )	0  
		(