Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • marcelh 171 posts 471 karma points
    Jun 03, 2014 @ 20:32
    marcelh
    0

    Alternative for IMediaFactory and UmbracoMediaFactory

    In version 4.something there was the UmbracoMediaFactory that you could use to customize the behavior of Media items.

    The case is this: I have a user that uploads documents and downloadable files into the Media section, both mediatypes (or contenttypes currently) have additional properties. In version 4.x I used the UmbracoMediaFactory behavior to create the appropriate mediatype (a download or document) when the user uploaded a file in a certain directory and implicitly set additional properties of the media item. This worked perfectly when using the Desktop Media Uploader or when dragging and dropping files into the Media section in later versions.

    When use uploads a file into the Media section in v7, a media item of contenttype "File" is always created. Is there a way to intervene and create a media item of a custom contenttype? I noticed that the said MediaFactory functionality has become obsolete.

  • marcelh 171 posts 471 karma points
    Jun 03, 2014 @ 21:49
    marcelh
    0

    Got it! Love it, much easier now!

    public class ProductEventHandler : ApplicationEventHandler
    {
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
    base.ApplicationStarted(umbracoApplication, applicationContext);

    MediaService.Saving += OnMediaServiceSaving;
    }

    static void OnMediaServiceSaving(IMediaService sender, SaveEventArgs<IMedia> e)
    {
    foreach (var item in e.SavedEntities)
    {
    if (item.HasIdentity)
    continue;

    var parent = item.Parent();
    if (parent == null || parent.ContentType.Alias.Equals("ProductFolder") == false)
    continue;

    var download = ApplicationContext.Current.Services.ContentTypeService.GetMediaType("Product");
    if (download == null)
    continue;

    item.ChangeContentType(download);
    }
    }
    }

     

Please Sign in or register to post replies

Write your reply to:

Draft