限制umbraco媒体纠察程序的上传大小和文件类型

问题描述 投票:0回答:1

试图像这样通过dialogService选择媒体

  dialogService.mediaPicker({
            startNodeId: undefined,
            multiPicker: false,
            cropSize: undefined,
            showDetails: true,
            callback: function (data) {

             }

有什么方法可以自定义此媒体选择器配置,并且只允许某些媒体类型并指定大小

umbraco umbraco7
1个回答
0
投票

我认为您最好的选择是为MediaService的Saving事件创建一个事件处理程序,并在保存文件之前检查条件。

此处有更多信息:https://our.umbraco.com/Documentation/Getting-Started/Code/Subscribing-To-Events/

public class RestrictMediaTypeAndSize : ApplicationEventHandler
    {

        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            MediaService.Saving += MediaService_Saving;
        }

        private void MediaService_Saving(IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
        {
            foreach (var media in e.SavedEntities)
            {
                //you can check for other types of content
                if (media.ContentType.Alias == Image.ModelTypeAlias)
                {
                    var fileExtension = GetFileExtension(media);
                    if (!HasAllowedExtension(fileExtension))
                    {
                        //we cancel the saving process
                        e.Cancel = true;

                        //we send a message to the user.
                        var errorMessage = new EventMessage("Error", $"Files of type {fileExtension} are not allowed.");
                        e.Messages.Add(errorMessage);

                        //we found an error so we exit the foreach loop to finish the execution.
                        break;
                    }

                    if (!HasValidDimensions(media))
                    {
                        //we cancel the saving process
                        e.Cancel = true;

                        //we send a message to the user.
                        var errorMessage = new EventMessage("Error", "Files with a size of (whatever your restrictions) are not allowed.");
                        e.Messages.Add(errorMessage);

                        //we found an error so we exit the foreach loop to finish the execution.
                        break;
                    }
                }
            }
        }

        private bool HasAllowedExtension(string fileExtension)
        {
            string[] allowedExtensions = new string[] { ".jpg", ".png" };
            return allowedExtensions.Contains(fileExtension);
        }

        private bool HasValidDimensions(IMedia media)
        {
            //take the image dimensions for the properties populated when you upload the file.
            var height = (int)media.Properties["umbracoHeight"].Value;
            var width = (int)media.Properties["umbracoWidth"].Value;

            //check for whatever conditions you want.
            return height < 1000 && width < 2000;
        }

        private string GetFileExtension(IMedia media)
        {
            //The umbracoFile Propery is a Json object and we need the src property from it which is the file path.
            var filePath = JObject.Parse(media.Properties["umbracoFile"].Value.ToString()).SelectToken("$.src").Value<string>();
            return System.IO.Path.GetExtension(filePath);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.