Back to index

plone3  3.1.7
fileresource.py
Go to the documentation of this file.
00001 '''\
00002 this is a fixture of zope app's fileresource
00003 
00004 It correctly handles cache expiration headers and rereads
00005 files when needed only.
00006 '''
00007 
00008 from zope.interface import implements
00009 try:
00010     from zope.contenttype import guess_content_type
00011 except ImportError: # BBB: Zope < 2.10
00012     try:
00013         # XXX ??? What zope version needs this?
00014         from zope.app.contenttypes import guess_content_type
00015     except ImportError:
00016         from zope.app.content_types import guess_content_type
00017 
00018 import os
00019 from interfaces import IContextFile
00020 
00021 class File(object):
00022     implements(IContextFile)
00023 
00024     def __init__(self, path, name):
00025         self.path = path
00026         self.__name__ = name
00027 
00028     def getLastMod(self):
00029         return os.path.getmtime(self.path)
00030 
00031     def getContents(self):
00032         ##print "***** READ", self.path
00033         f = open(self.path, 'rb')
00034         data = f.read()
00035         f.close()
00036         content_type, enc = guess_content_type(self.path, data)
00037         return dict(data = data, content_type = content_type)
00038 
00039 class Image(File):
00040     """Image objects stored in external files."""
00041 
00042     def getContents(self):
00043         d = super(Image, self).getContens()
00044         if d ['content_type'] in (None, 'application/octet-stream'):
00045             ext = os.path.splitext(self.path)[1]
00046             if ext:
00047                 d['content_type'] = 'image/%s' % ext[1:]
00048         return d