Logo Search packages:      
Sourcecode: zope-extfile version File versions

def zope-extfile-1.4.2::ExtFile::ExtFile::index_html (   self,
  icon = 0,
  preview = 0,
  width = None,
  height = None,
  REQUEST = None 
)

Return the file with it's corresponding MIME-type 

Definition at line 195 of file ExtFile.py.

                                 :
        """ Return the file with it's corresponding MIME-type """

        # If the redirect property is set we attempt to redirect to the static URL.
        # Patch provided by Oliver Bleutgen.
        if self.redirect_default_view:
            if self.static_mode() and REQUEST is not None and not icon:
                static_url = self.static_url(preview=preview)
                if static_url != self.absolute_url():
                    return REQUEST.RESPONSE.redirect(static_url)

        # HTTP If-Modified-Since header handling. (Copied from OFS/Image.py)
        # XXX: This seems handicapped. How does it handle icon and preview?
        if REQUEST is None and hasattr(self,'REQUEST'): 
            REQUEST = self.REQUEST
        if REQUEST is not None:
            header = REQUEST.get_header('If-Modified-Since', None)
            if header is not None:
                header = string.split(header, ';')[0]
                try:    mod_since = long(DateTime(header).timeTime())
                except: mod_since = None
                if mod_since is not None:
                    if self._p_mtime:
                        last_mod = long(self._p_mtime)
                    else:
                        last_mod = long(0)
                    if last_mod > 0 and last_mod < mod_since:
                        # Set headers for Apache caching
                        REQUEST.RESPONSE.setHeader('Last-Modified', last_mod)
                        REQUEST.RESPONSE.setHeader('Content-Type', self.content_type)
                        # RFC violation. See http://collector.zope.org/Zope/544
                        #REQUEST.RESPONSE.setHeader('Content-Length', self.get_size())
                        REQUEST.RESPONSE.setStatus(304)
                        return ''
        
        filename, content_type, icon, preview = self._get_file_to_serve(icon, preview)
        filename = self._get_fsname(filename)

        if _debug > 1: LOG(_SUBSYS, INFO, 'serving %s, %s, %s, %s' %(filename, content_type, icon, preview))

        cant_read_exc = "Can't read: "
        if filename:
            try: size = os.stat(filename)[6]
            except: raise cant_read_exc, ("%s (%s)" %(self.id, filename))
        else:
            filename = join(Globals.package_home(globals()), 'icons', 'broken.gif')
            try: size = os.stat(filename)[6]
            except: raise cant_read_exc, ("%s (%s)" %(self.id, filename))
            content_type = 'image/gif'
            icon = 1

        if icon==0 and width is not None and height is not None:
            data = TemporaryFile() # hold resized image
            try:
                from PIL import Image
                im = Image.open(filename) 
                if im.mode!='RGB' and im.mode!='CMYK': 
                    im = im.convert('RGB')
                filter = Image.BICUBIC
                if hasattr(Image, 'ANTIALIAS'): # PIL 1.1.3
                    filter = Image.ANTIALIAS
                im = im.resize((int(width),int(height)), filter)
                im.save(data, 'JPEG', quality=85)
            except:
                data = open(filename, 'rb')
            else:
                data.seek(0,2)
                size = data.tell()
                data.seek(0)
                content_type = 'image/jpeg'
        else:
            data = open(filename, 'rb')
        try:
            if REQUEST is not None:
                last_mod = rfc1123_date(self._p_mtime)
                REQUEST.RESPONSE.setHeader('Last-Modified', last_mod)
                REQUEST.RESPONSE.setHeader('Content-Type', content_type)
                REQUEST.RESPONSE.setHeader('Content-Length', size)
                blocksize = 2<<16
                while 1:
                    buffer = data.read(blocksize)
                    REQUEST.RESPONSE.write(buffer)
                    if len(buffer) < blocksize:
                        break
                return ''
            else:
                return data.read()
        finally:
            data.close()
    
    security.declareProtected(ViewPermission, 'view_image_or_file')
    def view_image_or_file(self):


Generated by  Doxygen 1.6.0   Back to index