Sitecore Media Library MaxSize

谁说我不能喝 提交于 2019-12-14 02:34:18

问题


In Sitecore, I'd like to set Upload MaxSize only for image files. We can update Media.MaxSizeInDatabase to set the MaxSize, but this setting includes all files in Media Library.

Is there any way to set MaxSize only for image files? Or, can I create any validation for this??

Thank you in advance!!

========= Update ==========

I tried to use all code and setting, but it was not working. I think Code is fine, but I might have to make sure the place of configurations. When I add "xmlns:patch" attribute in on the top, like below

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">

It show error "Unrecongnized attribute xmlns:patch". So, I added the configurations in "/configuration/sitecore" element in a web.config, like below

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    .....
    <sitecore database="SqlServer">
        <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
            <sitecore>
                <processors>
                    <uiUpload>
                        <processor mode="on" type="ImageMaxSize.ImageItemValidator2, Sitecore.Kernel" patch:before="processor[@type='Sitecore.Pipelines.Upload.CheckSize, Sitecore.Kernel']">
                            <restrictedExtensions hint="raw:AddRestrictedExtension">
                                <!-- Be sure to prefix with a dot -->
                                <extension>.jpg</extension>
                                <extension>.jepg</extension>
                                <extension>.png</extension>
                                <extension>.bmp</extension>
                            </restrictedExtensions>
                        </processor>
                    </uiUpload>
                </processors>
            </sitecore>
        </configuration>

This is not working


回答1:


You can patch in your own processor into uiUpload to check the file size for certain types, in a very similar way to this post from Mike Reynolds to restrict certain file types to be upload.

public class ImageCheckSize : UploadProcessor
{       
    public List<string> RestrictedExtensions { get; set; }

    public ImageCheckSize()
    {
        RestrictedExtensions = new List<string>();
    }

    public void Process(UploadArgs args)
    {
        Assert.ArgumentNotNull((object) args, "args");
        if (args.Destination == UploadDestination.File)
            return;

        foreach (string index in args.Files)
        {
            HttpPostedFile file = args.Files[index];
            if (!string.IsNullOrEmpty(file.FileName) && IsRestrictedExtension(file.FileName))
            {
                if ((long) file.ContentLength > MaxImageSizeInDatabase)
                {
                    args.ErrorText = string.Format("The image \"{0}\" is too big to be uploaded. The maximum size for uploading images is {1}.", file.FileName, MainUtil.FormatSize(MaxImageSizeInDatabase));
                    Log.Warn(args.ErrorText, this);
                    args.AbortPipeline();
                    break;
                }
            }
        }
    }

    private bool IsRestrictedExtension(string filename)
    {
        return RestrictedExtensions.Exists(restrictedExtension => string.Equals(restrictedExtension, Path.GetExtension(filename), StringComparison.CurrentCultureIgnoreCase));
    }

    public static long MaxImageSizeInDatabase
    {
        get
        {
            return Sitecore.Configuration.Settings.GetLongSetting("Media.MaxImageSizeInDatabase", 524288000L);
        }
    }
}

And then add in the required config changes. Create a new config file in /App_Config/Includes (e.g. ImageSizeCheck.config)

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">

  <sitecore>
    <processors>
      <uiUpload>
        <processor mode="on" type="Custom.Business.Pipeline.Upload.ImageCheckSize, Custom.Business"
                   patch:before="processor[@type='Sitecore.Pipelines.Upload.CheckSize, Sitecore.Kernel']">
          <restrictedExtensions hint="list">
            <extension>.jpg</extension>
            <extension>.png</extension>
          </restrictedExtensions>
        </processor>
      </uiUpload>
    </processors>

    <settings>
      <setting name="Media.MaxImageSizeInDatabase" value="1MB" />
    </settings>

  </sitecore>
</configuration>

You will also need to add another processor into attachFile processors to handle if the user attaches a new file to an existing media item - use dotPeek to see the implementation in Sitecore.Pipelines.Attach.CheckSize,Sitecore.Kernel.

One caveat to this is that the displayed error message is not too friendly, but the error is logged correctly in the log files :(



来源:https://stackoverflow.com/questions/27782405/sitecore-media-library-maxsize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!