Sitecore媒体库MaxSize

时间:2015-01-05 15:15:48

标签: image sitecore media validation

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

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

提前谢谢!!

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

我尝试使用所有代码和设置,但它无法正常工作。我认为Code很好,但我可能需要确保配置的位置。当我添加" xmlns:patch"属性在顶部,如下所示

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

它显示错误&#34;未记录的属性xmlns:patch&#34;。所以,我在&#34; / configuration / sitecore&#34;中添加了配置。 web.config中的元素,如下所示

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

这不起作用

1 个答案:

答案 0 :(得分:1)

您可以在自己的处理器中修补uiUpload来检查某些类型的文件大小,与Mike Reynolds与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);
        }
    }
}

然后添加所需的配置更改。在/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查看Sitecore.Pipelines.Attach.CheckSize,Sitecore.Kernel中的实施情况。

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

enter image description here