Commit 2b8b699e authored by 's avatar

Added caching headers and handling of If-Modified-Since headers to ImageFile objects.

parent 5a36954a
......@@ -84,29 +84,49 @@
##############################################################################
"""Image object that is stored in a file"""
__version__='$Revision: 1.8 $'[11:-2]
__version__='$Revision: 1.9 $'[11:-2]
from string import rfind
from Globals import package_home
from Common import rfc1123_date
from DateTime import DateTime
from string import rfind
from time import time
from os import stat
import Acquisition
class ImageFile(Acquisition.Explicit):
"""Image object stored in an external file"""
"""Image objects stored in external files."""
def __init__(self,path,_prefix=None):
if _prefix is None: _prefix=SOFTWARE_HOME
elif type(_prefix) is not type(''): _prefix=package_home(_prefix)
elif type(_prefix) is not type(''):
_prefix=package_home(_prefix)
path='%s/%s' % (_prefix, path)
self.path=path
self.content_type='image/%s' % path[rfind(path,'.')+1:]
self.__name__=path[rfind(path,'/')+1:]
def index_html(self, RESPONSE):
# Determine a reasonable last-modification time
# to support aggressive image caching.
self.lmt=float(stat(path)[8]) or time()
self.lmh=rfc1123_date(self.lmt)
def _init_headers(self, request, response):
# attempt aggressive caching!
ms=request.get_header('If-Modified-Since', None)
if ms is not None:
mst=DateTime(ms).timeTime()
if mst >= self.lmt:
response.setStatus(304)
return response
response.setHeader('Content-Type', self.content_type)
response.setHeader('Last-Modified', self.lmh)
response.setHeader('Expires', rfc1123_date(time()+86400.0))
def index_html(self, REQUEST, RESPONSE):
"""Default document"""
RESPONSE['content-type']=self.content_type
self._init_headers(REQUEST, RESPONSE)
f=open(self.path,'rb')
data=f.read()
f.close()
......@@ -115,7 +135,7 @@ class ImageFile(Acquisition.Explicit):
HEAD__roles__=None
def HEAD(self, REQUEST, RESPONSE):
""" """
RESPONSE['content-type'] =self.content_type
self._init_headers(self, REQUEST, RESPONSE)
return ''
def __len__(self):
......@@ -125,9 +145,3 @@ class ImageFile(Acquisition.Explicit):
def __str__(self):
return '<IMG SRC="%s" ALT="%s">' % (self.__name__, self.title_or_id())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment