Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Media and media types work as follows:
NOTE: You can create specific classes for handling media of type images, video, and anything generic other than video or image, such as text or PDF files.
Media are 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:
During initialization EPiServer CMS scans assemblies in the bin folder for .NET classes decorated with [ContentType]-attribute, a media type must inherit from MediaData as the following example shows.
[ContentType]
public class CustomFile : MediaData
{
public virtual string Copyright { get; set; }
}
ImageData and VideoData are specialized base classes distinguish images and videos from other generic media, to apply special handling in the user interface. For example, types inheriting ImageData display thumbnails in the user interface. Both ImageData and VideoData inherit from MediaData.
The following example shows how you can set up generic media types, and also images and video:
[ContentType(GUID = "EE3BD195-7CB0-4756-AB5F-E5E223CD9820")]
public class GenericMedia : MediaData
{
public virtual string Description { get; set; }
}
[ContentType(GUID = "0A89E464-56D4-449F-AEA8-2BF774AB8730")]
[MediaDescriptor(ExtensionString = "jpg,jpeg,jpe,ico,gif,bmp,png")]
public class ImageFile : ImageData
{
public virtual string Description { get; set; }
public virtual string Copyright { get; set; }
}
[ContentType(GUID = "85468104-E06F-47E5-A317-FC9B83D3CBA6")]
[MediaDescriptor(ExtensionString = "flv,mp4,webm")]
public class VideoFile : VideoData
{
public virtual string Description { get; set; }
}
The MediaDescriptor attribute defines a list of file extensions and associates specific file types to 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 class.
The ImageDescriptor attribute automatically generates scaled images. The following example shows the ImageData.Thumbnail property that has 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; }
}
}
When you route to a BLOB type property, EPiServer determines whether the BLOB is null and the property has an ImageDescriptor attribute. If both are true, then it automatically generated as scaled image from IBinaryStorable.BinaryData.
The built-in EPiServer.Web.ContentMediaHttpHandler delivers all media by default. However, if you want custom processing before sending the media to the visitor, the following example shows how you can implement your own HTTP Handler. Handlers for media uses the same templating system as other content types in CMS, which means you can use any type of template for media; you are not limited to HTTP handlers.
public partial class CustomFileHandler : IHttpHandler, IRenderTemplate<CustomFile>
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
//Get file content
var routeHelper = ServiceLocator.Current.GetInstance<ContentRouteHelper>();
var content = routeHelper.Content;
if (content == null)
{
throw new HttpException(404, "Not Found.");
}
//Check access
if (!content.QueryDistinctAccess(AccessLevel.Read))
{
var accessDenied = DefaultAccessDeniedHandler.CreateAccessDeniedDelegate();
accessDenied(this);
return;
}
//Cast to custom file
var customFile = content as CustomFile;
if (customFile == null || customFile.BinaryData==null)
{
throw new HttpException(404, "Not Found.");
}
//Set caching policy
context.Response.Cache.SetCacheability(HttpCacheability.Private);
context.Response.Cache.SetMaxAge(TimeSpan.FromDays(1));
//Do custom processing
//TransmitProcessedFile(customFile.BinaryData)
}
}
Last updated: Feb 23, 2015