Skip to content

Instantly share code, notes, and snippets.

@AThraen
Created March 2, 2023 13:58
Show Gist options
  • Select an option

  • Save AThraen/bbb50702567481be60794982032b996d to your computer and use it in GitHub Desktop.

Select an option

Save AThraen/bbb50702567481be60794982032b996d to your computer and use it in GitHub Desktop.
Trick to make PDF files automatically download using the Digizuite integration to Optimizely (Episerver) CMS
//Register the PDF's as a specific content type in Optimizely. Ensure Guid is unique.
//Connect to Digizuite asset-types using the DigizuiteType attribute.
[ContentType(DisplayName = "Downloadable Digizuite File", GUID = "a4ca5288-35ce-4ee5-b094-72f76ca8e07b", Description = "", AvailableInEditMode = false)]
[DigizuiteType(AssetTypeIds = new[] { Digizuite.Optimizely.Constants.GlobalConstants.AssetTypes.PDF })]
public class DownloadableDocument : DigizuiteFile
{
}
//Create a custom HTTP Handler for the blobs of these types - but just inherit from the DigizuiteBlobHttpHandler.
//Just make sure you override the "IsDownloadingRequest". The default behavior here would be for it to check if the url ends with "/download".
[ServiceConfiguration]
public class DownloadableDocumentBlobHttpHandler : Digizuite.Optimizely.BlobHandling.DigizuiteBlobHttpHandler
{
public DownloadableDocumentBlobHttpHandler(HttpClient httpClient, IOptions<DAMIntegrationConfiguration> options) : base(httpClient, options)
{
}
public override bool IsDownloadingRequest(HttpContext httpContext)
{
return true;
}
}
//Make a custom Media Endpoint that connects the new content type with the new BlobHttpHandler.
[TemplateDescriptor(Inherited = false, AvailableWithoutTag = true, TemplateTypeCategory = TemplateTypeCategories.HttpHandler)]
public class DownloadableFileMediaEndpoint : Endpoint, IRenderTemplate<DownloadableDocument>, IRenderTemplate
{
private static ContentActionDescriptor _mediaContentActionDescriptor = new ContentActionDescriptor()
{
Inherited = false,
ModelType = typeof(DownloadableDocument)
};
private static AuthorizeAttribute _authorizeAttribute = new AuthorizeAttribute("episerver:read");
private static AuthorizeAttribute _previewAttribute = new AuthorizeAttribute("episerver:preview");
public DownloadableFileMediaEndpoint(DownloadableDocumentBlobHttpHandler dblobhandler) : base(new RequestDelegate(dblobhandler.Invoke), new EndpointMetadataCollection(new object[3] {
(object) DownloadableFileMediaEndpoint._mediaContentActionDescriptor,
(object) DownloadableFileMediaEndpoint._authorizeAttribute,
(object) DownloadableFileMediaEndpoint._previewAttribute
}), nameof(DownloadableFileMediaEndpoint))
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment