AI OnAI Off
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.
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. :/
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);
}
}
With the old VPP system you could create and register a StaticFileHandler and override the cache policy, like this:
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.