Copied to clipboard

Flag this post as spam?

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


  • Jonathan Roberts 409 posts 1063 karma points
    Oct 26, 2016 @ 11:08
    Jonathan Roberts
    0

    Media upload validation

    Hi, I would like to restrict CMS users from adding large files to the Media library. Is there an extension i could write to validate files uploaded?

    Jon

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 26, 2016 @ 20:45
    Dennis Aaen
    0

    Hi Jon,

    If you want exclude specific file extensions to be uploaded to the media library then you can add the file extension(s) to the Disallow section in the umbracoSettings.config.

     <!-- These file types will not be allowed to be uploaded via the upload control for media and content -->
        <disallowedUploadFiles>ashx,aspx,ascx,config,cshtml,vbhtml,asmx,air,axd,swf,xml,html,htm,svg,php,htaccess</disallowedUploadFiles>
    

    Hope this helps,

    /Dennis

  • Casper 70 posts 308 karma points
    Oct 27, 2016 @ 19:45
    Casper
    0

    In order to cancel out large files, you could go about doing a class that derive from ApplicationEventHandler and hook up to MediaService.Saving event:

    using System;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Services;
    
    namespace App.EventHandlers
    {
    
        public class AppEventHandler : ApplicationEventHandler
        {
            const int MAX_SIZE = 100000;
    
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                MediaService.Saving += (sender, e) =>
                {
                    foreach (var entity in e.SavedEntities)
                    {
                        var property = entity?.Properties?.First(p => p.Alias.InvariantEquals("umbracobytes"));
                        if (property != null)
                        {
                            var size = Convert.ToInt32(property.Value);
                            if (size > MAX_SIZE)
                                if (e.CanCancel)
                                    e.Cancel = true; // todo: explain cancel in notification
                        }
                    }
                };
            }
        }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies