Skip to content

Instantly share code, notes, and snippets.

@casper-rasmussen
Last active June 12, 2016 12:45
Show Gist options
  • Save casper-rasmussen/8ba5a1135fadc79a87b9d53cc409fc5a to your computer and use it in GitHub Desktop.
Save casper-rasmussen/8ba5a1135fadc79a87b9d53cc409fc5a to your computer and use it in GitHub Desktop.
[TemplateDescriptor(Inherited = true, TemplateTypeCategory = TemplateTypeCategories.HttpHandler)]
public class MediaDownloadHttpHandler : BlobHttpHandler, IRenderTemplate<DocumentFileMediaType>
{
public new bool IsReusable
{
get { return false; }
}
protected override Blob GetBlob(HttpContextBase context)
{
var routeHelper = ServiceLocator.Current.GetInstance<ContentRouteHelper>();
//Get file content
var content = routeHelper.Content;
if (content == null)
throw new HttpException(404, "Not Found.");
//ACCESS CHECK OR DOWNLOAD TRACKING IMPLEMENTED HERE
//Set caching policy
context.Response.Cache.SetCacheability(HttpCacheability.Private);
context.Response.Cache.SetMaxAge(TimeSpan.FromDays(1));
//Get name of file
var downLoadFileName = context.Request.RequestContext.GetCustomRouteData<string>(DownloadMediaRouter.DownloadSegment);
if (!string.IsNullOrEmpty(downLoadFileName))
{
//Ensure that the file is being downloaded
context.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", downLoadFileName));
}
var binaryStorable = content as IBinaryStorable;
return binaryStorable != null ? binaryStorable.BinaryData : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment