Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Implementation of ImageProcessor's IImageService interface for EpiServer - Retrieves EpiServer blobs for processing by ImageProcessor
/// <summary>
/// EpiServer implementation of an ImageProcessor IImageService - provides the episerver image blobs to ImageProcessor for resizing etc.
/// </summary>
public class EpiServerImageService : IImageService
{
/// <summary>
/// Gets or sets the prefix for the given implementation.
/// <remarks>
/// This value is used as a prefix for any image requests that should use this service.
/// </remarks>
/// </summary>
public string Prefix { get; set; } = string.Empty;
/// <summary>
/// Gets a value indicating whether the image service requests files from
/// the locally based file system.
/// </summary>
public bool IsFileLocalService => false;
/// <summary>
/// Gets or sets any additional settings required by the service.
/// </summary>
public Dictionary<string, string> Settings { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Gets or sets the white list of <see cref="System.Uri"/>.
/// </summary>
public Uri[] WhiteList { get; set; }
/// <summary>
/// Gets a value indicating whether the current request passes sanitizing rules.
/// </summary>
/// <param name="path">
/// The image path.
/// </param>
/// <returns>
/// <c>True</c> if the request is valid; otherwise, <c>False</c>.
/// </returns>
public bool IsValidRequest(string path)
{
return ImageHelpers.IsValidImageExtension(path);
}
/// <summary>
/// Gets the image using the given identifier.
/// </summary>
/// <param name="id">
/// The value identifying the image to fetch.
/// </param>
/// <returns>
/// The <see cref="System.Byte"/> array containing the image data.
/// </returns>
public Task<byte[]> GetImage(object id)
{
var helper = ServiceLocator.Current.GetInstance<IContentRouteHelper>();
var content = helper?.Content;
if (content != null && content.QueryDistinctAccess(AccessLevel.Read))
{
var binary = content as IBinaryStorable;
if (binary != null)
return Task.FromResult(binary.BinaryData.ReadAllBytes());
}
return Task.FromResult<byte[]>(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment