Custom image resizer

With the new Image Resizer Extension point (https://docs.litium.com/support/bugs/bug_details?id=40925) you can on a better way manager how the image should be resized.

In this example we will use a field on the Litium.Media.File to define what we should do with the image when the image is requested by the browser.

The logic will be in two different classes, one that is the one that generate the url for the image and the other one is the actual resizer.

We start adding a decorator for the MediaLocationService that is the part that generate the url for the image. In this example we will fetch the field from the Litium.Media.File-object and assign a field-value to the args.CustomData that will be serialized into the image url.

using JetBrains.Annotations;
using Litium.Blobs;
using Litium.Media;
using Litium.Runtime.DependencyInjection;
using Litium.Web.Media;
using System.Threading.Tasks;

namespace Litium.Accelerator.Mvc
{
    [ServiceDecorator(typeof(MediaLocationService))]
    internal class SpecialMediaLocationService : MediaLocationService
    {
        private readonly MediaLocationService _parent;
        private readonly FileService _fileService;

        public SpecialMediaLocationService(MediaLocationService parent, FileService fileService)
        {
            _parent = parent;
            _fileService = fileService;
        }

        public override string GetLocation<T>([NotNull] MediaLocationServiceArgs args)
        {
            if (args.CustomData == null)
            {
                var file = _fileService.Get(args.SystemId);
                if (file != null)
                {
                    // Add custom data to use when fetching the image, all data that is added is included in the URL for the image.
                    // All data that is needed for the rendering is needed to be added, resolving different parameters in the resizer
                    // should be avoided because the URL will be website neutral.
                    args.CustomData = file.Fields.GetValue<string>("MyImageTypeAttribute");
                }
            }
            return _parent.GetLocation<T>(args);
        }
    }

Now when we have modified how the image url will be generated we need to decorate the BlobImageResizer to update and use another resize algoritm if the specified CustomData with the specific value exists in the url.

using JetBrains.Annotations;
using Litium.Blobs;
using Litium.Media;
using Litium.Runtime.DependencyInjection;
using Litium.Web.Media;
using System.Threading.Tasks;

namespace Litium.Accelerator.Mvc
{
    [ServiceDecorator(typeof(BlobImageResizer))]
    internal class SpecialBlobImageResizer : BlobImageResizer
    {
        private readonly BlobImageResizer _parent;

        public SpecialBlobImageResizer(BlobImageResizer parent)
        {
            _parent = parent;
        }

        public override Task ResizeAsync([NotNull] Blob source, [NotNull] Blob destination, [NotNull] FormatPath formatPath)
        {
            // CustomData contains all the data that was added when the page is rendering.
            // This resizer should not use HttpContext to lookup domain name etc because that will not work if CDN is used
            // and the same image url can be used for multiple websites.
            if (formatPath.CustomData == "Article")
            {
                // Do some special processing for the image, image should be stored in the destination blob
                return _parent.ResizeAsync(source, destination, formatPath);
            }
            return _parent.ResizeAsync(source, destination, formatPath);
        }
    }
}