Sitecore 媒体库 MaxSize

Sitecore Media Library MaxSize

在 Sitecore 中,我想为图像文件设置 Upload MaxSize。 我们可以更新 Media.MaxSizeInDatabase 来设置 MaxSize,但此设置包括媒体库中的所有文件。

有没有办法只为图片文件设置MaxSize?或者,我可以为此创建任何验证吗??

提前致谢!!

=========更新==========

我尝试使用所有代码和设置,但没有用。我认为代码很好,但我可能必须确定配置的位置。当我在顶部添加 "xmlns:patch" 属性时,如下所示

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

它显示错误 "Unrecongnized attribute xmlns:patch"。因此,我在 web.config 中的“/configuration/sitecore”元素中添加了配置,如下所示

<?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>

这不起作用

您可以将自己的处理器修补到 uiUpload 以检查某些类型的文件大小,其方式与 Mike Reynolds 到 restrict certain file types to be upload 的 post 非常相似。

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);
        }
    }
}

然后添加所需的配置更改。在 /App_Config/Includes 中创建一个新的配置文件(例如 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>

您还需要将另一个处理器添加到 attachFile 个处理器中,以处理用户将新文件附加到现有媒体项的情况 - 使用 dotPeek 查看 [=15= 中的实现].

需要注意的是,显示的错误消息不太友好,但错误已正确记录在日志文件中:(