def buildCacheKey(self, width, height):
        """
        Build cache key for result image data.

        This varies by width and height if we know them.
        If we don't know, then we user agent string itself as a part of the key,
        so that different mobiles don't get wrong image from the cache.
        """

        # We know the user agent so we know the resulting width and height in this stage
        if self.ua:
            key = str(width) + "-" + str(height) + "-"
        else:
            key = get_user_agent_hash(self.request)

        def add_param(key, value):
            key += "-"
            key += str(value)
            return key

        key = add_param(key, self.cache_key)
        key = add_param(key, self.conserve_aspect_ration)
        key = add_param(key, self.padding_width)

        return key
    def getImageDownloadURL(self, url, properties={}):
        """
        Return download URL for image which is put through image resizer.

        @param url: Source image URI, relative to context, or absolite URL

        @param properties: Extra options needed to be given to the resizer, e.g. padding, max width, etc.

        @return: String, URL where to resized image can be downloaded. This URL varies
            by the user agent.
        """
        self.init()

        url = self.mapURL(url)

        # Prepare arguments for the image resizer view
        new_props = {"conserve_aspect_ration" : "true"}
        new_props.update(properties)
        new_props["url"] = url

        if self.isUserAgentSpecific(url, new_props):
            # Check if the result may vary by user agnt
            new_props["user_agent_md5"] = get_user_agent_hash(self.request)

        new_props = self.finalizeViewArguments(new_props)

        return self.site.absolute_url() + "/" + VIEW_NAME + "?" + urllib.urlencode(new_props)
    def getImageDownloadURL(self, url, properties={}):
        """
        Return download URL for image which is put through image resizer.

        @param url: Source image URI, relative to context, or absolite URL

        @param properties: Extra options needed to be given to the
        resizer, e.g. padding, max width, etc.

        @return: String, URL where to resized image can be downloaded.
            This URL varies by the user agent.
        """
        self.init()

        url = self.mapURL(url)

        path = self.cache.makePathKey(url)
        file = self.cache.get(path)
        if file is None:
            helper = ResizeViewHelper(self.context, self.request)
            helper.init()
            helper.parseParameters({'conserve_aspect_ration': True, 'url': url})
            width, height = helper.resolveDimensions()
            tool = getUtility(IImageInfoUtility)

            logger.debug(
                "Resizing image to mobile dimensions %d %d" % (
                    width, height)
                )
            data, format = tool.getURLResizedImage(url, width, height,
                                                   conserve_aspect_ration=True)

            # Mercifully cache broken images from remote HTTP downloads
            if data is None:
                value = ""
            else:
                value = data.getvalue()

            self.cache.set(path, value)

        # Prepare arguments for the image resizer view
        new_props = {"conserve_aspect_ration" : "true"}
        new_props.update(properties)

        new_props["key"] = path

        if self.isUserAgentSpecific(url, new_props):
            # Check if the result may vary by user agnt
            new_props["user_agent_md5"] = get_user_agent_hash(self.request)

        new_props = self.finalizeViewArguments(new_props)

        return (self.site.absolute_url() + "/" + VIEW_NAME + "?" +
                urllib.urlencode(new_props))
def getUserAgentBasedResizedImageURL(context, request, *args, **kwargs):
    """ Get device-sensitive image URL.

    Add device user agent hash to image URL so that caches won't mix different user agent images.
    """

    context = aq_inner(context)
    # Will raise ComponentLookUpError
    name = "mobile_image_resizer"
    view = getMultiAdapter((context, request), name=name)

    view = view.__of__(context)

    # Make user that each user agent gets its own logo version and not cached version for some other user agent
    user_agent_md5 = get_user_agent_hash(request)

    kwargs["user_agent_md5"] = user_agent_md5

    return getResizedImageURL(*args, **kwargs)