November Happy Hour will be moved to Thursday December 5th.

Set cache policy with the new media system

Vote:
 

With the old VPP system you could create and register a StaticFileHandler and override the cache policy, like this:

public class CdnStaticFileHandler : StaticFileHandler
{
    protected override void SetCachePolicy(HttpContext context, DateTime fileChangedDate)
    {
        context.Response.Cache.SetExpires(DateTime.UtcNow.AddYears(1));
        // ...and other cache headers
    } 
}

I'm not able to get this to work with the new media system. Is there a way to achieve the same result with the new media system? Preferably a way with access to the IContent object.

#81073
Feb 09, 2014 22:13
Vote:
 

As far as I know you can register handler by implementing a template for particular content type

public class CustomMediaFileHandler : StaticFileHandler, IRenderTemplate<CustomMediaFile>
{
    protected override void SetCachePolicy(HttpContextBase context, DateTime fileChangedDate)
    {
        var routeHelper = ServiceLocator.Current.GetInstance<ContentRouteHelper>();
        var content = routeHelper.Content;

    

 

Haven't played with following setup - so you should test it out.

#81075
Feb 09, 2014 23:51
Vote:
 

I ended up trying something like

    [TemplateDescriptor(Inherited = true, TemplateTypeCategory = TemplateTypeCategories.HttpHandler)]
    public class ImageHandler : ContentMediaHttpHandler, IRenderTemplate<ImageFile>
    {
        protected override void SetCachePolicy(HttpContextBase context, DateTime fileChangedDate)
        {
            // Stuff
        }
    }

but that just results in "Ambiguous match found." without any sensable stack trace. :/

#81078
Edited, Feb 10, 2014 2:03
Vote:
 

Yes, unfortunately you cannot inherit from ContentMediaHttpHandler and at the same time implement IRenderTemplate for particular media content type. Try to inherit from BlobHttpHandler and implement GetBlob on your own:

public class SamplePdfHandler : BlobHttpHandler, IRenderTemplate<PdfFile>
{
    protected override Blob GetBlob(HttpContextBase httpContext)
    {
        var customRouteData = httpContext.Request.RequestContext.GetCustomRouteData<string>(DownloadMediaRouter.DownloadSegment);
        if (!string.IsNullOrEmpty(customRouteData))
        {
            httpContext.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", customRouteData));
        }

        var binary = ServiceLocator.Current.GetInstance<ContentRouteHelper>().Content as IBinaryStorable;
        return binary == null ? null : binary.BinaryData;
    }

    protected override void SetCachePolicy(HttpContextBase context, DateTime fileChangedDate)
    {
        base.SetCachePolicy(context, fileChangedDate);
    }
}

    

#81084
Feb 10, 2014 9:51
Vote:
 

Worked great, thanks!

#81120
Feb 10, 2014 18:19
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.