Index: lib/python/App/ImageFile.py =================================================================== --- lib/python/App/ImageFile.py (revision 72248) +++ lib/python/App/ImageFile.py (working copy) @@ -15,7 +15,9 @@ __version__='$Revision: 1.20 $'[11:-2] import os +import stat import time +import os.path from App.config import getConfiguration from zope.app.content_types import guess_content_type @@ -25,6 +27,8 @@ import Acquisition import Globals +from ZPublisher.Iterators import filestream_iterator + class ImageFile(Acquisition.Explicit): """Image objects stored in external files.""" @@ -43,17 +47,25 @@ max_age = 3600 # One hour self.cch = 'public,max-age=%d' % max_age - data = open(path, 'rb').read() + img = open(path, 'rb') + data = img.read(1<<16) # 65k should be enough. + img.close() + content_type, enc=guess_content_type(path, data) if content_type: self.content_type=content_type else: - self.content_type='image/%s' % path[path.rfind('.')+1:] - self.__name__=path[path.rfind('/')+1:] - self.lmt=float(os.stat(path)[8]) or time.time() - self.lmh=rfc1123_date(self.lmt) + ext = os.path.splitext(path)[-1].replace('.', '') + self.content_type='image/%s' % ext + self.__name__ = os.path.split(path)[-1] + stat_info = os.stat(path) + self.size = stat_info[stat.ST_SIZE] + self.lmt = float(stat_info[stat.ST_MTIME]) or time.time() + self.lmh = rfc1123_date(self.lmt) + print path, self.content_type, self.size + def index_html(self, REQUEST, RESPONSE): """Default document""" # HTTP If-Modified-Since header handling. This is duplicated @@ -62,7 +74,8 @@ RESPONSE.setHeader('Content-Type', self.content_type) RESPONSE.setHeader('Last-Modified', self.lmh) RESPONSE.setHeader('Cache-Control', self.cch) - header=REQUEST.get_header('If-Modified-Since', None) + RESPONSE.setHeader('Content-Length', str(self.size).replace('L', '')) + header = REQUEST.get_header('If-Modified-Since', None) if header is not None: header=header.split(';')[0] # Some proxies seem to send invalid date strings for this @@ -82,7 +95,7 @@ RESPONSE.setStatus(304) return '' - return open(self.path,'rb').read() + return filestream_iterator(self.path, mode='rb') HEAD__roles__=None def HEAD(self, REQUEST, RESPONSE):