0

I have a custom pipeline processor that is patched in before the ResizeProcessor in the getMediaStream pipeline. When I request an image from sitecore that has no query parameters, such as site/-/media/.../my-image.jpg, the media cache entry for that file will have the [key] stored as the list of query parameters with all values set to 0 or false (depending on type). An example entry looks like this

[key]
?as=False&bc=0&h=0&iar=False&mh=0&mw=0&sc=0&thn=False&w=0

[extension]
jpg

[headers]
Content-Type: image/jpeg

[dataFile]
9ec21b9f3d964272a32da412422fec97.jpg

;-----

This is what I would expect, but a problem occurs when using custom parameters and requesting an image with parameters before requesting the original. Say I request the same image, but with my custom cropping parameters added, as such, site/-/media/images/test/rally-scoob?w=1200&h=300&cha=l&c=1&hash=682947F3A745596DF0B8991F41BEC2709CED7755 which generates the following media cache entry:

[key]
?as=False&bc=0&h=300&iar=False&mh=0&mw=0&sc=0&thn=False&w=1200&cha=l

[extension]
jpg

[headers]
Content-Type: image/jpeg

[dataFile]
b0ae589092754a6b9376f0c6d76351cd.jpg

;-----

This is fine, as long as the image with the query parameters is requested after the original. If the order is switched, then the returned image for the parameterless image is not correct. The only workaround that I can think of is to append my custom parameters to the [key] of the media entry in the same way that the other parameters are defaulted. The problem is that I'm not sure what needs to be patched in order to add custom parameters to the Media Cache keys by default.

Does anyone have any experience with altering the way that keys are stored in the media cache? Thanks in advance.

  • When you say "the returned image is not correct", what does that mean? – Chris Auer May 8 '17 at 3:35
  • The image returned is the cropped image from the first request, but with dimensions that match the original. It treats the cropped image as if it were the original. It should return the original image, not the cropped image. Imagine the first image I request is a 1 dollar bill with 10% of the image cropped off of the left side. The next image I request is an image without any cropping parameters. You'd expect to get a whole dollar bill back, but you actually get a dollar bill with 10% of the left side missing. Hope this helps. – Ryan Shohoney May 8 '17 at 18:55
  • Sounds like you have the caching issue. Can you add a dmc=1 to your query and see if the image is right of both urls? – Chris Auer May 8 '17 at 20:27
0

It seems that you have something wrong with caching of images(not necessary on Sitecore MediaLibrary Cache level). You have 2 different keys:

?as=False&bc=0&h=0&iar=False&mh=0&mw=0&sc=0&thn=False&w=0

and

?as=False&bc=0&h=300&iar=False&mh=0&mw=0&sc=0&thn=False&w=1200&cha=l

Based on this keys MediaLibrary cache should return different cached images. If I were you I would look why cache return wrong image for request without cropping parameters.

And now answer on your question about altering keys that are stored in the media cache: Media cache key is built based on Sitecore.Resources.Media.MediaRequest parameters. Sitecore uses Sitecore.Resources.Media.MediaUrlOptions properties and CustomOptions of MediaRequest to build cache key. It is hard to extend MediaUrlOptions, but easy to add anything to CustomOptions. Here is example how I am adding custom option inside MediaRequestHandler:

public class MediaRequestHandler : Sitecore.Resources.Media.MediaRequestHandler
{
    protected override bool DoProcessRequest(HttpContext context, MediaRequest request, Media media)
    {
        if (context?.Request.AcceptTypes != null && (context.Request.AcceptTypes).Contains("image/webp"))
        {
            request.Options.CustomOptions["extension"] = "webp";
        }

        return base.DoProcessRequest(context, request, media);
    }

    private static bool AcceptWebP(HttpContext context)
    {
        return context?.Request.AcceptTypes != null && (context.Request.AcceptTypes).Contains("image/webp");
    }

}

It works fine for me. I get additional &extension=webp to key for requests, when it is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.