vendor/CMF/1.6.3/CMFCore

view FSImage.py @ 2:4c712d7bd1d7

Added tag 1.6.3 for changeset 1babb9d61518
author Georges Racinet on purity.racinet.fr <georges@racinet.fr>
date Fri, 09 Sep 2011 12:44:00 +0200
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Customizable image objects that come from the filesystem.
15 $Id$
16 """
18 import Globals
19 from DateTime import DateTime
20 from AccessControl import ClassSecurityInfo
21 from OFS.Cache import Cacheable
22 from OFS.Image import Image, getImageInfo
24 from permissions import FTPAccess
25 from permissions import View
26 from permissions import ViewManagementScreens
27 from DirectoryView import registerFileExtension
28 from DirectoryView import registerMetaType
29 from FSObject import FSObject
30 from utils import _dtmldir
31 from utils import _setCacheHeaders, _ViewEmulator
32 from utils import expandpath, _FSCacheHeaders, _checkConditionalGET
35 class FSImage(FSObject):
36 """FSImages act like images but are not directly
37 modifiable from the management interface."""
38 # Note that OFS.Image.Image is not a base class because it is mutable.
40 meta_type = 'Filesystem Image'
42 _data = None
44 manage_options=(
45 {'label':'Customize', 'action':'manage_main'},
46 ) + Cacheable.manage_options
48 security = ClassSecurityInfo()
49 security.declareObjectProtected(View)
51 def __init__(self, id, filepath, fullname=None, properties=None):
52 id = fullname or id # Use the whole filename.
53 FSObject.__init__(self, id, filepath, fullname, properties)
55 security.declareProtected(ViewManagementScreens, 'manage_main')
56 manage_main = Globals.DTMLFile('custimage', _dtmldir)
57 content_type = 'unknown/unknown'
59 def _createZODBClone(self):
60 return Image(self.getId(), '', self._readFile(1))
62 def _readFile(self, reparse):
63 fp = expandpath(self._filepath)
64 file = open(fp, 'rb')
65 try:
66 data = self._data = file.read()
67 finally:
68 file.close()
69 if reparse or self.content_type == 'unknown/unknown':
70 self.ZCacheable_invalidate()
71 ct, width, height = getImageInfo( data )
72 self.content_type = ct
73 self.width = width
74 self.height = height
75 return data
77 #### The following is mainly taken from OFS/Image.py ###
79 __str__ = Image.__str__.im_func
81 _image_tag = Image.tag.im_func
82 security.declareProtected(View, 'tag')
83 def tag(self, *args, **kw):
84 # Hook into an opportunity to reload metadata.
85 self._updateFromFS()
86 return self._image_tag(*args, **kw)
88 security.declareProtected(View, 'index_html')
89 def index_html(self, REQUEST, RESPONSE):
90 """
91 The default view of the contents of a File or Image.
93 Returns the contents of the file or image. Also, sets the
94 Content-Type HTTP header to the objects content type.
95 """
97 self._updateFromFS()
98 view = _ViewEmulator().__of__(self)
100 # If we have a conditional get, set status 304 and return
101 # no content
102 if _checkConditionalGET(view, extra_context={}):
103 return ''
105 RESPONSE.setHeader('Content-Type', self.content_type)
107 # old-style If-Modified-Since header handling.
108 if self._setOldCacheHeaders():
109 # Make sure the CachingPolicyManager gets a go as well
110 _setCacheHeaders(view, extra_context={})
111 return ''
113 data = self._readFile(0)
114 data_len = len(data)
115 RESPONSE.setHeader('Content-Length', data_len)
117 #There are 2 Cache Managers which can be in play....
118 #need to decide which to use to determine where the cache headers
119 #are decided on.
120 if self.ZCacheable_getManager() is not None:
121 self.ZCacheable_set(None)
122 else:
123 _setCacheHeaders(view, extra_context={})
124 return data
126 def _setOldCacheHeaders(self):
127 # return False to disable this simple caching behaviour
128 return _FSCacheHeaders(self)
130 def modified(self):
131 return self.getModTime()
133 security.declareProtected(View, 'getContentType')
134 def getContentType(self):
135 """Get the content type of a file or image.
137 Returns the content type (MIME type) of a file or image.
138 """
139 self._updateFromFS()
140 return self.content_type
142 security.declareProtected(View, 'get_size')
143 def get_size( self ):
144 """
145 Return the size of the image.
146 """
147 self._updateFromFS()
148 return self._data and len( self._data ) or 0
150 security.declareProtected(FTPAccess, 'manage_FTPget')
151 manage_FTPget = index_html
153 Globals.InitializeClass(FSImage)
155 registerFileExtension('gif', FSImage)
156 registerFileExtension('jpg', FSImage)
157 registerFileExtension('jpeg', FSImage)
158 registerFileExtension('png', FSImage)
159 registerFileExtension('bmp', FSImage)
160 registerMetaType('Image', FSImage)