Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS
Applies to versions: 12 and higher
Other versions:
ARCHIVED This content is retired and no longer maintained. See the version selector for other versions of this topic.

Media types and templates

Recommended reading 
Note: This documentation is for the preview version of the upcoming release of CMS 12/Commerce 14/Search & Navigation 14. Features included here might not be complete, and might be changed before becoming available in the public release. This documentation is provided for evaluation purposes only.

This topic explains the concept of media types in Optimizely CMS, and describes how to define specialized media types when setting up an Optimizely website.

Note: Media types must first be defined in your content model, in order for built-in asset functionality such as file upload or drag-and-drop to work in edit view. It is recommended that you create specific classes for media of type images, video, and anything generic other than video or image, such as text or PDF files.

How it works

Media and media types work as follows:

  • A media type defines a set of properties.
  • A media is an instance of the .NET class that defined the media type.
  • When an editor creates/uploads media, values are assigned to the properties defined by the media type.
  • When an instance of media is requested by a visitor, the default media handler sends the binary data to the visitor.

During initialization, assemblies in the bin folder are scanned for .NET classes decorated with the [ContentType] attribute, and inheriting from MediaData. Meta data for media is defined as properties on the media class, and can be edited from edit view.

Generic media type

You can use the Episerver Visual Studio extension to create media types. The example below shows a generic media type for handling files, inheriting from MediaData, with a description property available under the Content tab in edit view.

using System;
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Framework.DataAnnotations;

namespace MyOptimizelySite.Models.Media
  {
    [ContentType(DisplayName = "GenericMedia", GUID = "89761dbb-bf22-4cee-93c7-9d661d75cad8", Description = "Used for generic file types such as Word or PDF documents.")]
    [MediaDescriptor(ExtensionString = "pdf,doc,docx")]
    public class GenericMedia : MediaData
      {
                [CultureSpecific]
                [Editable(true)]
                [Display(
                    Name = "Description",
                    Description = "Add a description of the content.",
                    GroupName = SystemTabNames.Content,
                    Order = 1)]
                public virtual String Description { get; set; }
      }
  }

MediaDescriptor attribute

The MediaDescriptor attribute defines a list of file extensions and associates specific file types with a given content type, which creates content of the correct content type when a user uploads media via the user interface.

When you create media content from the server side, you also can have this same content type resolve by using the ContentMediaResolver.

Specialized media types

ImageData and VideoData are specialized base classes distinguishing images and videos from other generic media, to apply special handling in edit view. For example, media types inheriting ImageData will display image thumbnails when listing images in the Media folder in edit view. Both ImageData and VideoData inherit from MediaData.

Example: Media type for images, inheriting from ImageData, and with properties for copyright and a description.

namespace MyOptimizelySite.Models.Media
  {
    [ContentType(DisplayName = "ImageFile", GUID = "875b3b51-e0a7-412c-8f56-44f59c184440", Description = "Used for images of different file formats.")]
    [MediaDescriptor(ExtensionString = "jpg,jpeg,jpe,ico,gif,bmp,png")]
    public class ImageFile : ImageData
      {
         //// <summary>
         /// Gets or sets the copyright.
         /// </summary>
         public virtual string Copyright { get; set; }
         public virtual String Description { get; set; }
      }
  }

The UIHint property attribute is used to select either editor/renderer or both by defining a hint string. You can use EPiServer.Web.UIHint for known types in the system, for instance UIHint.Image.

Example: Media type for videos, inheriting from VideoData, and with properties for copyright and link to video preview image.

namespace MyOptimizelySite.Models.Media
  {
    [ContentType(DisplayName = "VideoFile", GUID = "f2285e5a-be15-47b6-8952-e3c61deaefd2", Description = "Used for specific video file formats.")]
    [MediaDescriptor(ExtensionString = "flv,mp4,webm")]
    public class VideoFile : VideoData
      {
        /// <summary>
        /// Gets or sets the copyright.
        /// </summary>
        public virtual string Copyright { get; set; }

        /// <summary>
        /// Gets or sets the URL to the preview image.
        /// </summary>
        [UIHint(UIHint.Image)]
        public virtual ContentReference PreviewImage { get; set; }
      }
  }

ImageDescriptor attribute

The ImageDescriptor attribute automatically generates scaled images. When you route to a BLOB type property, Optimizely determines whether the BLOB is null and the property has an ImageDescriptor attribute. If both are true, a scaled image is automatically generated from IBinaryStorable.BinaryData.

Example: The ImageData.Thumbnail property with an ImageDescriptor attribute.

/// <summary>
/// Base class for content types which should be handled as images by the system.
/// </summary>
public class ImageData : MediaData
  {
    /// <summary>
    /// Gets or sets the generated thumbnail for this media.
    /// </summary>
    [ImageDescriptor(Width = 48, Height = 48)]
    public override Blob Thumbnail
      {
        get { return base.Thumbnail; }
        set { base.Thumbnail = value; }
      }
  }

Media structure

Media is structured using folders. A folder in the media structure can have other folders or media as children, but a media instance cannot have any children. You can set access rights on folders to control availability for editors. 

The media structure is defined by the following criteria:

  • The global media root folder is set as GlobalAssetsRoot, defining media available for content on all sites in a mult-site scenario.
  • A site-specific media root folder is set as SiteAssetsRoot, defining media only available for a specific site in a multi-site scenario. In a single site scenario, the GlobalAssetsRoot and SiteAssetRoot will typically point to the same folder.
  • A folder is an instance of ContentFolder, and is used to structure content. A content folder does not have any associated rendering, and will not appear on the site.

See  SiteDefinition  in the Optimizely CMS class library.

Changing the maximum upload file size

The maximum size of uploaded files are set on UploadOptions.FileSizeLimit, and the default value is 4 MB. The value can be set (in bytes), for example, from appSettings.json as:

"EPiServer": {
    "CmsUI": {
        "Upload": {
            "FileSizeLimit": 104857600
        }
    }
}

Customizing the media handler

There is a built-in endpoint template registered for all media request. However, media uses the same templating system as other content types in Optimizely CMS,  which means you can use any type of template for media; you are not limited to an endpoint that delivers media as binary data.

If you need to extend the default media handling, for example, to set response headers, register an implementation of IStaticFilePreProcessor.

Example: Below is an example on a custom pre processor that adds a header to the response for PDF requests.

  public class PdfStaticFilePreProcessor : IStaticFilePreProcessor
    {
        //The built-in pre processor that handles MediaOptions has order 0, run after that
        public int Order => 10;

        public void PrepareResponse(StaticFileResponseContext staticFileResponseContext)
        {
            if (staticFileResponseContext.Context.Response.ContentType == "application/pdf")
            {
                staticFileResponseContext.Context.Response.Headers.Add("Content-Disposition", "attachment");
            }
        }
    }​

The static file pre processor is registered using MediaOptions, like:

services.Configure<MediaOptions>(o => o.AddPreProcessor<PdfStaticFilePreProcessor>());​

It is also possible to add a custom media template. If the template is based on an endpoint, then it is nessecary to add a ContentActionDescriptor to the endpoint metadata. It is also recommended to add the policies for CmsPolicyNames.Read and CmsPolicyNames.Preview to ensure access rights are checked.

Example: Below is an example on a custom endpoint template for PDF files that adds a header to the response.

    [TemplateDescriptor(Inherited = true, TemplateTypeCategory = TemplateTypeCategories.HttpHandler)]
    public class PdfEndpoint : Endpoint, IRenderTemplate<PdfFile>
    {
        private readonly static ContentActionDescriptor _mediaContentActionDescriptor = new ContentActionDescriptor { Inherited = true, ModelType = typeof(PdfFile) };
        private static readonly AuthorizeAttribute _authorizeAttribute = new AuthorizeAttribute(CmsPolicyNames.Read);
        private static readonly AuthorizeAttribute _previewAttribute = new AuthorizeAttribute(CmsPolicyNames.Preview);

        public PdfEndpoint(IBlobHttpHandler blobHttpHandler)
            : base(context => ProcessRequest(blobHttpHandler, context),
                  new EndpointMetadataCollection(_mediaContentActionDescriptor, _authorizeAttribute, _previewAttribute), nameof(PdfEndpoint))
        {}

        private static Task ProcessRequest(IBlobHttpHandler blobHttpHandler, HttpContext context)
        {
            context.Response.Headers.Add("Content-Disposition", "attachment");
            return blobHttpHandler.Invoke(context);
        }
    }

Related topics

Do you find this information helpful? Please log in to provide feedback.

Last updated: Jul 02, 2021

Recommended reading