[Zope-Checkins] CVS: Zope/lib/python/OFS - Image.py:1.145.2.5.2.1
Chris McDonough
chrism at plope.com
Sun Mar 21 15:40:43 EST 2004
Update of /cvs-repository/Zope/lib/python/OFS
In directory cvs.zope.org:/tmp/cvs-serv22603
Modified Files:
Tag: chrism-publishfile-branch
Image.py
Log Message:
- Break apart huge index_html method.
- If we have a cache manager that returns a producer, allow Files/Images
to play with this cache manager.
=== Zope/lib/python/OFS/Image.py 1.145.2.5 => 1.145.2.5.2.1 ===
--- Zope/lib/python/OFS/Image.py:1.145.2.5 Mon Jan 19 11:51:46 2004
+++ Zope/lib/python/OFS/Image.py Sun Mar 21 15:40:42 2004
@@ -33,6 +33,9 @@
from ZPublisher.HTTPRequest import FileUpload
from zExceptions import Redirect
from cgi import escape
+from ZServer.medusa.producers import file_producer
+import stat
+import os
StringType=type('')
manage_addFileForm=DTMLFile('dtml/imageAdd', globals(),Kind='File',kind='file')
@@ -127,13 +130,7 @@
def id(self):
return self.__name__
- def index_html(self, REQUEST, RESPONSE):
- """
- The default view of the contents of a File or Image.
-
- Returns the contents of the file or image. Also, sets the
- Content-Type HTTP header to the objects content type.
- """
+ def _if_modified_since_request_handler(self, REQUEST, RESPONSE):
# HTTP If-Modified-Since header handling.
header=REQUEST.get_header('If-Modified-Since', None)
if header is not None:
@@ -161,19 +158,10 @@
RESPONSE.setHeader('Content-Length', self.size)
RESPONSE.setHeader('Accept-Ranges', 'bytes')
RESPONSE.setStatus(304)
- self.ZCacheable_set(None)
- return ''
-
- if self.precondition and hasattr(self, str(self.precondition)):
- # Grab whatever precondition was defined and then
- # execute it. The precondition will raise an exception
- # if something violates its terms.
- c=getattr(self, str(self.precondition))
- if hasattr(c,'isDocTemp') and c.isDocTemp:
- c(REQUEST['PARENTS'][1],REQUEST)
- else:
- c()
+ self.ZCacheable_set(fcplaceholder)
+ return True
+ def _range_request_handler(self, REQUEST, RESPONSE):
# HTTP Range header handling
range = REQUEST.get_header('Range', None)
request_range = REQUEST.get_header('Request-Range', None)
@@ -228,7 +216,7 @@
RESPONSE.setHeader('Content-Type', self.content_type)
RESPONSE.setHeader('Content-Length', self.size)
RESPONSE.setStatus(416)
- return ''
+ return True
ranges = HTTPRangeSupport.expandRanges(ranges, self.size)
@@ -248,7 +236,7 @@
data = self.data
if type(data) is StringType:
- return data[start:end]
+ RESPONSE.write(data[start:end])
# Linked Pdata objects. Urgh.
pos = 0
@@ -274,7 +262,7 @@
data = data.next
- return ''
+ return True
else:
boundary = choose_boundary()
@@ -364,16 +352,54 @@
del pdata_map
RESPONSE.write('\r\n--%s--\r\n' % boundary)
- return ''
+ return True
+
+ def index_html(self, REQUEST, RESPONSE):
+ """
+ The default view of the contents of a File or Image.
+
+ Returns the contents of the file or image. Also, sets the
+ Content-Type HTTP header to the objects content type.
+ """
+
+ # testing crap
+ ## fname = '/home/chrism/bin/python'
+ ## f = open(fname, 'rb')
+ ## size = os.stat(fname)[stat.ST_SIZE]
+ ## RESPONSE.setHeader('Content-Length', size)
+ ## RESPONSE.setHeader('Content-Type', self.content_type)
+ ## return file_producer(f)
+
+ if self._if_modified_since_request_handler(REQUEST, RESPONSE):
+ return ''
+
+ if self.precondition and hasattr(self, str(self.precondition)):
+ # Grab whatever precondition was defined and then
+ # execute it. The precondition will raise an exception
+ # if something violates its terms.
+ c=getattr(self, str(self.precondition))
+ if hasattr(c,'isDocTemp') and c.isDocTemp:
+ c(REQUEST['PARENTS'][1],REQUEST)
+ else:
+ c()
+
+ if self._range_request_handler(REQUEST, RESPONSE):
+ return ''
RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime))
RESPONSE.setHeader('Content-Type', self.content_type)
RESPONSE.setHeader('Content-Length', self.size)
RESPONSE.setHeader('Accept-Ranges', 'bytes')
- # Don't cache the data itself, but provide an opportunity
- # for a cache manager to set response headers.
- self.ZCacheable_set(None)
+ if self.ZCacheable_isCachingEnabled():
+ marker = []
+ result = self.ZCacheable_get(default=marker)
+ if result is not marker and result is not fcplaceholder:
+ # RAMCacheManager will return an fcplaceholder while
+ # FileCacheManager will return a file producer
+ return result
+
+ self.ZCacheable_set(fcplaceholder)
data=self.data
if type(data) is type(''):
@@ -399,7 +425,7 @@
if size is None: size=len(data)
self.size=size
self.data=data
- self.ZCacheable_invalidate()
+ self.ZCacheable_set(fcplaceholder)
self.http__refreshEtag()
def manage_edit(self, title, content_type, precondition='',
@@ -825,3 +851,7 @@
next=self.next
return ''.join(r)
+
+class FileCachePlaceholder:
+ pass
+fcplaceholder = FileCachePlaceholder()
More information about the Zope-Checkins
mailing list