Copied to clipboard

Flag this post as spam?

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


  • Jesper Weber 54 posts 170 karma points c-trib
    Feb 23, 2021 @ 14:39
    Jesper Weber
    0

    Set ContentType in MediaService.Saving event

    Hi,

    I have created a custom media type and hooked in to the MediaService.Saving event.

    When a file is uploaded I want to set the content type based on the file extension. How ever at that point Umbraco has already given media a content type.

        public void Initialize()
        {
            MediaService.Saving += MediaService_Saving;
        }
    
        private void MediaService_Saving(IMediaService sender, SaveEventArgs<IMedia> e)
        {
            foreach (var mediaItem in e.SavedEntities)
            {
                var contentTypeAlias = mediaItem.ContentType.Alias;
            }
        }
    

    How do I change the content type or hook in earlier to set the content type my self?

  • Ben McKean 272 posts 549 karma points
    Nov 10, 2021 @ 12:05
    Ben McKean
    0

    Hi Jesper,

    Did you find a solution to this?

    Thanks Ben

  • Jesper Weber 54 posts 170 karma points c-trib
    Nov 18, 2021 @ 12:26
    Jesper Weber
    0

    No unfortunately I did not find a solution.

  • mmaty 109 posts 281 karma points
    Nov 18, 2021 @ 12:03
    mmaty
    0

    I stumbled upon this problem and am looking for a solution as well.

    If a user uploads a pdf file to the media section, I want to assign a specific media type which provides additional properties.

    How can we accomplish this task?

    Thanks Mirko

  • mmaty 109 posts 281 karma points
    Nov 18, 2021 @ 13:10
    mmaty
    0

    I found a solution for my question. Maybe it helps somebody.

    Create a media type Pdf. Do not inherit from File, as you have to redefine the umbracoFile Property. Define the properties Type and Size as in the media type File.

    The umbracoFile property must have a new DataType (the name might be UploadPdf) which is an instance of Umbraco.UploadField. In this DataType you can edit the prevalue "Accepted File extensions". Add the file extension "pdf".

    Assign UploadPdf as DataType to the property umbracoFile of the media type Pdf.

    Add additional properties according to your needs.

    If you now add a pdf file in the media section, it gets the media type Pdf automatically.

    Umbraco uses this code to assign the media type:

    var ext = safeFileName.Substring(safeFileName.LastIndexOf('.') + 1).ToLower();
    var mediaTypes = Services.MediaTypeService.GetAll();
    foreach (var mediaTypeItem in mediaTypes)
    {
        var fileProperty = mediaTypeItem.CompositionPropertyTypes.FirstOrDefault(x => x.Alias == "umbracoFile");
        if (fileProperty != null) {
            var dataTypeKey = fileProperty.DataTypeKey;
            var dataType = Services.DataTypeService.GetDataType(dataTypeKey);
    
            if (dataType != null && dataType.Configuration is IFileExtensionsConfig fileExtensionsConfig) {
                var fileExtensions = fileExtensionsConfig.FileExtensions;
                if (fileExtensions != null)
                {
                    if (fileExtensions.Where(x => x.Value == ext).Count() != 0)
                    {
                        mediaType = mediaTypeItem.Alias;
                        break;
                    }
                }
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft