Ejemplo n.º 1
0
    def _isAssetCached(self, contentItem, assetName, lang):
        # Ideally I would like to get the mime type from DataProvider but that would
        # mean changing quite a lot, so I make an assumption.
        #
        # I look at files in the cache directory and if there is a match (without the extension)
        # then I check the mtime, if the file was written after the mtime on
        # the content item then I assume the file is current and use that file.

        import os.path

        filename = web.encodeIdVersion(contentItem.id,contentItem.version)
        filename = '_'.join(filter(None, [filename, assetName, lang]))

        dirList = os.listdir(self.cachedir)

        for file in dirList:
            root, ext = os.path.splitext(file)
            if root == filename:
                path = os.path.join(self.cachedir, file)
                filemtime = datetime.datetime.utcfromtimestamp( os.path.getmtime( path ) )
                if filemtime > contentItem.mtime:
                    mimetype = mimetypes.guess_type( file )[0]
                    
                    if mimetype.startswith('image'):
                        return imaging.ImageResource( path ), path
                    else:
                        return static.File(path), path

        return None, None
Ejemplo n.º 2
0
    def _getCachePath(self, contentItem, assetName, lang, mimetype, data):
        filename = web.encodeIdVersion(contentItem.id,contentItem.version)
        filename = '_'.join(filter(None, [filename, assetName, lang]))
        ext = mimetypes.guess_extension(mimetype)
        if ext is not None:
            filename = filename + ext
        path = os.path.join(self.cachedir, filename)

        # work out if the existing file, if any, is current
        writeFile = True
        if os.path.exists( path ):
            filemtime = datetime.datetime.utcfromtimestamp( os.path.getmtime( path ) )
            if filemtime > contentItem.mtime:
                writeFile = False

        if writeFile:
            image = open( path, "w" )
            image.write(data)
            image.close()

        return path
Ejemplo n.º 3
0
 def getURLForAsset( self, item, includeVersion=True, language=None ):
     rv = self.url
     if language is not None:
         rv = rv.child(language)
     rv = rv.child(web.encodeIdVersion(item.id,item.version, includeVersion))
     return rv