Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 872 karma points
    Sep 27, 2018 @ 12:45
    jake williamson
    1

    is it possible to restrict file section for a new media type based on extension?

    hey out there,

    i was trying to figure out if there's a way to restrict the type of a file a new 'media type' can upload? i know you can globally enable or disable file extensions but i was wondering if there's a way to say:

    my new media type with the alias of 'MP4File' can only upload files with an 'mp4' file extension.
    

    be interested to know!

    cheers,

    jake

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Sep 27, 2018 @ 12:53
    Dan Diplo
    101

    Never tried it, but you might be able to hook into Media Service saving event to add some custom logic:

    https://our.umbraco.com/Documentation/Reference/Events/MediaService-Events

    Most events allow you to cancel the action based on your own logic.

  • jake williamson 207 posts 872 karma points
    Sep 27, 2018 @ 15:01
    jake williamson
    1

    i think you're onto something there dan...

    just ran this up and it seems pretty happy:

    void MediaServiceSaved(IMediaService sender, SaveEventArgs<IMedia> e)
    {
        foreach (var mediaItem in e.SavedEntities)
        {
            if (mediaItem.ContentType.Alias == "MP4File" && mediaItem.GetValue<string>("umbracoExtension") != "mp4")
            {
                e.CancelOperation(new EventMessage("Media", "Please upload a MP4 file", EventMessageType.Warning));
            }
        }
    }
    

    the file still gets uploaded and in this example it's huge mp4 files which is a bit of a pain....

    ...but as a simple file extension check, this works a treat ;)

    i guess there's only so many times the user will sit and wait for a 1gb video file to upload and then be told it's in the wrong format?!

    thank you for the suggestion,

    cheers,

    jake

  • Gogo Dev 30 posts 187 karma points
    Sep 24, 2020 @ 07:39
    Gogo Dev
    1

    Don't you mean: ... && mediaItem.GetValue<string>("umbracoExtension") != "mp4") ?

  • jake williamson 207 posts 872 karma points
    Oct 25, 2020 @ 03:42
    jake williamson
    1

    good spot gogo, updated ;)

Please Sign in or register to post replies

Write your reply to:

Draft