Back to index

plone3  3.1.7
FSImage.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Customizable image objects that come from the filesystem.
00014 
00015 $Id: FSImage.py 81543 2007-11-05 17:32:33Z tseaver $
00016 """
00017 
00018 import Globals
00019 from AccessControl import ClassSecurityInfo
00020 from OFS.Cache import Cacheable
00021 from OFS.Image import Image, getImageInfo
00022 
00023 from DirectoryView import registerFileExtension
00024 from DirectoryView import registerMetaType
00025 from FSObject import FSObject
00026 from permissions import FTPAccess
00027 from permissions import View
00028 from permissions import ViewManagementScreens
00029 from utils import _checkConditionalGET
00030 from utils import _dtmldir
00031 from utils import _FSCacheHeaders
00032 from utils import _setCacheHeaders
00033 from utils import _ViewEmulator
00034 
00035 
00036 class FSImage(FSObject):
00037 
00038     """FSImages act like images but are not directly
00039     modifiable from the management interface."""
00040     # Note that OFS.Image.Image is not a base class because it is mutable.
00041 
00042     meta_type = 'Filesystem Image'
00043     content_type = 'unknown/unknown'
00044     alt = ''
00045     height = ''
00046     width = ''
00047     _data = None
00048 
00049     manage_options = ({'label':'Customize', 'action':'manage_main'},)
00050 
00051     security = ClassSecurityInfo()
00052     security.declareObjectProtected(View)
00053 
00054     security.declareProtected(ViewManagementScreens, 'manage_main')
00055     manage_main = Globals.DTMLFile('custimage', _dtmldir)
00056 
00057     def __init__(self, id, filepath, fullname=None, properties=None):
00058         id = fullname or id # Use the whole filename.
00059         FSObject.__init__(self, id, filepath, fullname, properties)
00060 
00061     def _createZODBClone(self):
00062         return Image(self.getId(), '', self._readFile(1))
00063 
00064     def _readFile(self, reparse):
00065         """Read the data from the filesystem.
00066         """
00067         file = open(self._filepath, 'rb')
00068         try:
00069             data = self._data = file.read()
00070         finally:
00071             file.close()
00072 
00073         if reparse or self.content_type == 'unknown/unknown':
00074             self.ZCacheable_invalidate()
00075             ct, width, height = getImageInfo( data )
00076             self.content_type = ct
00077             self.width = width
00078             self.height = height
00079         return data
00080 
00081     #### The following is mainly taken from OFS/Image.py ###
00082 
00083     __str__ = Image.__str__.im_func
00084 
00085     _image_tag = Image.tag.im_func
00086     security.declareProtected(View, 'tag')
00087     def tag(self, *args, **kw):
00088         # Hook into an opportunity to reload metadata.
00089         self._updateFromFS()
00090         return self._image_tag(*args, **kw)
00091 
00092     security.declareProtected(View, 'index_html')
00093     def index_html(self, REQUEST, RESPONSE):
00094         """
00095         The default view of the contents of a File or Image.
00096 
00097         Returns the contents of the file or image.  Also, sets the
00098         Content-Type HTTP header to the objects content type.
00099         """
00100         self._updateFromFS()
00101         view = _ViewEmulator().__of__(self)
00102 
00103         # If we have a conditional get, set status 304 and return
00104         # no content
00105         if _checkConditionalGET(view, extra_context={}):
00106             return ''
00107 
00108         RESPONSE.setHeader('Content-Type', self.content_type)
00109 
00110         # old-style If-Modified-Since header handling.
00111         if self._setOldCacheHeaders():
00112             # Make sure the CachingPolicyManager gets a go as well
00113             _setCacheHeaders(view, extra_context={})
00114             return ''
00115 
00116         data = self._readFile(0)
00117         data_len = len(data)
00118         RESPONSE.setHeader('Content-Length', data_len)
00119 
00120         #There are 2 Cache Managers which can be in play....
00121         #need to decide which to use to determine where the cache headers
00122         #are decided on.
00123         if self.ZCacheable_getManager() is not None:
00124             self.ZCacheable_set(None)
00125         else:
00126             _setCacheHeaders(view, extra_context={})
00127         return data
00128 
00129     def _setOldCacheHeaders(self):
00130         # return False to disable this simple caching behaviour
00131         return _FSCacheHeaders(self)
00132 
00133     def modified(self):
00134         return self.getModTime()
00135 
00136     security.declareProtected(View, 'getContentType')
00137     def getContentType(self):
00138         """Get the content type of a file or image.
00139 
00140         Returns the content type (MIME type) of a file or image.
00141         """
00142         self._updateFromFS()
00143         return self.content_type
00144 
00145     security.declareProtected(View, 'get_size')
00146     def get_size(self):
00147         """
00148             Return the size of the image.
00149         """
00150         self._updateFromFS()
00151         return self._data and len( self._data ) or 0
00152 
00153     security.declareProtected(FTPAccess, 'manage_FTPget')
00154     manage_FTPget = index_html
00155 
00156 Globals.InitializeClass(FSImage)
00157 
00158 registerFileExtension('gif', FSImage)
00159 registerFileExtension('jpg', FSImage)
00160 registerFileExtension('jpeg', FSImage)
00161 registerFileExtension('png', FSImage)
00162 registerFileExtension('bmp', FSImage)
00163 registerMetaType('Image', FSImage)