Copied to clipboard

Flag this post as spam?

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


  • Mikkel Johansen 116 posts 291 karma points
    Jan 05, 2016 @ 12:36
    Mikkel Johansen
    0

    File Upload - choose a mediatype depending on extension

    If I upload (drag'n'drop) a jpg/png/gif file the media-node created is of type "Image". All others are of type "File".

    Is it possible to add file-extension so fx ".pdf" is going to be of media-type "PDF".

    -Mikkel

  • Mikkel Johansen 116 posts 291 karma points
    Jan 05, 2016 @ 13:38
    Mikkel Johansen
    0

    I have just looked in the Umbraco source code in the MediaController.

    Looks like that the core is default setting the mediatype to "File" or if the file-extension is jpg/png/ext. it is a "Image".

    So no option of doing any thing like file-extion => mediatype in any config-files.

    • Mikkel
  • Dave Woestenborghs 3342 posts 11278 karma points MVP 6x admin c-trib
    Jan 05, 2016 @ 13:39
    Dave Woestenborghs
    2

    Hi Mikkel,

    First of all you need to create a new media type called PDF. You can inherit from the file media type. No extra properties are needed.

    After that you need to add a eventhandler that converts your uploaded fileds with a pdf extension from the File media type to the PDF media type.

    This should to do the trick.

    namespace YourNameSpace
    {
            /// <summary>
        /// Overriden ApplicationEventHandler class
        /// </summary>
        public class RegisterMediaEvents : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
                MediaService.Saved += this.MediaService_Saved;
    
            }        
    
            private void MediaService_Saved(IMediaService sender, SaveEventArgs<IMedia> e)
            {        
                foreach (var item in e.SavedEntities)
                {
                    // only do this for file uploads
                    if (item.ContentType.Alias.Equals(Models.ContentModels.File.ModelTypeAlias))
                    {
                        var filePropertyValue = item.GetValue("umbracoFile");
                        if (filePropertyValue != null && !string.IsNullOrEmpty(filePropertyValue.ToString()))
                        {
                            var filename = filePropertyValue.ToString();
    
                            // check if the uploaded file is a pdf
                            var isPdf = filename.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)
    
                            if (isPdf)
                            {
                                var contentTypeService = Umbraco.Core.ApplicationContext.Current.Services.ContentTypeService;
    
                                var pdfMediaType = contentTypeService.GetMediaType("pdfMediaTypeAlias");
    
                                if (pdfMediaType != null)
                                {
                                    item.ChangeContentType(pdfMediaType, true);
    
                                    Umbraco.Core.ApplicationContext.Current.Services.MediaService.Save(item, 0, false);
                                }
                            }
                        }
                    }
                }
            }
    
    
        }
    }
    
  • Sören Deger 728 posts 2837 karma points c-trib
    Jan 05, 2016 @ 14:13
    Sören Deger
    1

    Hi Dave,

    very great! I've been looking so long for such a solution. #h5yr

    Cheers,

    Sören

  • Mikkel Johansen 116 posts 291 karma points
    Jan 05, 2016 @ 14:48
    Mikkel Johansen
    0

    Thanks Dave :-)

    I was looking for a non-event soloution. Looks like I must be the event-way.

    By the way shouldn't be the MediaService your are using?

    var contentTypeService = Umbraco.Core.ApplicationContext.Current.Services.ContentTypeService;

  • Sören Deger 728 posts 2837 karma points c-trib
    Jan 05, 2016 @ 14:53
    Sören Deger
    0

    Hi Mikkel,

    it's a little bit strange, but really you can't get the mediaType with MediaService. You can only get the mediaType with contentTypeService. In umbraco database contentTypes and mediaTypes are in the same table.

    Cheers,

    Sören

  • Dave Woestenborghs 3342 posts 11278 karma points MVP 6x admin c-trib
    Jan 05, 2016 @ 15:04
    Dave Woestenborghs
    0

    That's used to get the media type. A media type is also a content type but a special one.

    Dave

  • Dave Woestenborghs 3342 posts 11278 karma points MVP 6x admin c-trib
    Jan 05, 2016 @ 15:05
    Dave Woestenborghs
    0

    Also if it works, can you mark this post as a solution, so others can find it as well.

    dave

Please Sign in or register to post replies

Write your reply to:

Draft