Skip to content

Litium add-on for organizing and mapping media to other entities

License

Notifications You must be signed in to change notification settings

distancify/Distancify.LitiumAddOns.MediaMapper

Repository files navigation

Distancify.LitiumAddOns.MediaMapper

Build status

Litium Add-on to programmatically organize media in the media archive and link media to other entities.

This add-on:

  • Automatically puts uploaded files in the correct folder
  • Automatically link files to any product fields, including your custom ones
  • Allows you to map files based on more than just filename, including file content and EXIF data
  • Allows you to specify a sort order for a product's display images
  • Allows you to update a file's fields/meta data as it is mapped

Requires Litium 7.

Getting Started

Prerequisites

This library aims at extending the e-comemrce platform Litium. In order to use and develop the project, you need to fulfill their development system requirements.

Install

Install-Package Distancify.LitiumAddOns.MediaMapper

To enable the mapper, add the following app setting in web.config. Note that if you have a multi-node cluster, only enable the mapper on one of your machines to avoid concurrency issues.

<configuration>
  <appSettings>
    <add key="MediaMapperEnabled" value="true"/>
  </appSettings>
</configuration>

Logging

This add-on uses the Serilog framework for logging. Make sure you forward Serilog messages to Litium's logging framework if you're still using that.

Configure

You need to create the class that is responsible for classifying files by implementing the IMediaProfiler interface.

using Distancify.LitiumAddOns.MediaMapper;
using System.Text.RegularExpressions;

public class MyMediaProfiler : IMediaProfiler
{
    private Regex _imagePattern = new Regex(@"^(?<articleNumber>.+?)\.jpe?g$");

    public MediaProfile GetMediaProfile(MediaProfileBuilder builder)
    {
        var match = _imagePattern.Match(builder.File.Name);
        if (match.Success)
        {
            return CreateMediaProfile(builder, match.Groups);
        }

        return null;
    }

    private MediaProfile CreateMediaProfile(MediaProfileBuilder builder, GroupCollection groups)
    {
        var articleNumber = groups["articleNumber"].Value;
        builder.MapToBaseProductImages(articleNumber);
        builder.ArchiveTo(GetArchivePath(articleNumber));
        builder.SetField("MediaType", "Product");
        return builder.Create();
    }

    private string GetArchivePath(string articleNumber)
    {
        return $"{articleNumber.Substring(0, 2)}/{articleNumber}";
    }
}

Next, you need to register at least one media mapper in the dependency injection container, using your newly created profiler. Each Mapper instance monitors a folder in the Media Archive module:

public class MediaMapperConfig : IComponentInstaller
{
    public void Install(IIoCContainer container, Assembly[] assemblies)
    {
        container.RegisterMediaMapper(new MyMediaProfiler(), "/upload");
    }
}

Mapping to Custom Fields

This add-on comes with the ability to map to Litium's built-in MediaPointerFile and MediaPointerImageArray field types. You can easily add support for your custom fields by implemeting the IFieldSetter interface.

Customized Image Sorting

The MediaPointerImageArrayFieldSetter comes with a static configuration that can be used to implement custom sorting logic for when setting the media order to a field. Here's an example:

MediaPointerImageArrayFieldSetter.Sort = (entity, field, images) =>
{
    images.Sort((a, b) => string.Compare(a.Name, b.Name));
};

Publishing

The project is built on AppVeyor and set up to automatically push any release branch to NuGet.org. To create a new release, create a new branch using the following convention: release/v<Major>.<Minor>. AppVeyor will automatically append the build number.

Running the tests

The tests are built using xUnit and does not require any setup in order to run inside Visual Studio's standard test runner.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning.

Authors

See the list of contributors who participated in this project.

License

This project is licensed under the LGPL v3 License - see the LICENSE file for details

About

Litium add-on for organizing and mapping media to other entities

Resources

License

Stars

Watchers

Forks

Packages

No packages published