示例#1
0
def watermark(url):
    if not settings.SITE_ID == 1:
        return url
    image_url = url.split("?")[0]
    filename = "{0}.{1}".format(image_url, settings.DEFAULT_WATERMARK_SLUG)
    if not filename.startswith(settings.S3_BUCKET_URL):
        return filename
    cache_key = "watermark-%s" % filename
    cache_key = cache_key.strip()
    cached_val = cache.get(cache_key)
    if cached_val:
        return cached_val
    storage = get_upload_storage(delayed=True).remote
    new_url = storage.url(filename.replace(settings.S3_BUCKET_URL, ""))
    cache.set(cache_key, new_url, settings.WATERMARK_URL_CACHE_TIME)
    return new_url
示例#2
0
    def generate_url(self, thumb_name, ssl_mode=False):
        # This is tacked on to the end of the cache key to make sure SSL
        # URLs are stored separate from plain http.
        ssl_postfix = '_ssl' if ssl_mode else ''

        # Try to see if we can hit the cache instead of asking the storage
        # backend for the URL. This is particularly important for S3 backends.
        cache_key = "Thumbcache_%s_%s%s" % (self.url,
                                               thumb_name,
                                               ssl_postfix)
        cache_key = cache_key.strip()

        cached_val = cache.get(cache_key)
        if cached_val:
            return cached_val

        # Determine what the filename would be for a thumb with these
        # dimensions, regardless of whether it actually exists.
        new_filename = self._calc_thumb_filename(thumb_name)

        # Split URL from GET attribs.
        url_get_split = self.url.rsplit('?', 1)
        
        # Just the URL string (no GET attribs).
        url_str = url_get_split[0]
        # Get the URL string without the original's filename at the end.
        url_minus_filename = url_str.rsplit('/', 1)[0]

        # Slap the new thumbnail filename on the end of the old URL, in place
        # of the orignal image's filename.
        new_url = "%s/%s" % (url_minus_filename,
                             os.path.basename(new_filename))

        if new_url.startswith(settings.S3_BUCKET_URL):
            storage = get_upload_storage(delayed=True).remote
            new_url = storage.url(new_url.replace(settings.S3_BUCKET_URL, ""))

        # Cache this so we don't have to hit the storage backend for a while.
        cache.set(cache_key, new_url, THUMBNAIL_URL_CACHE_TIME)
        return new_url