Last active
June 12, 2016 12:45
-
-
Save casper-rasmussen/8ba5a1135fadc79a87b9d53cc409fc5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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