Sitecore maximum size for uploading files based on file type and UserRoles using UploadProcessor


    public class CheckSize : UploadProcessor
    {
        // Methods
        public void Process(UploadArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            if (args.Destination != UploadDestination.File)
            {
                foreach (string str in args.Files)
                {
                    HttpPostedFile file = args.Files[str];

                    if (!string.IsNullOrEmpty(file.FileName))
                    {
                        if (UploadProcessor.IsUnpack(args, file))
                        {
                            ZipReader reader = new ZipReader(file.InputStream);

                            try
                            {
                                foreach (ZipEntry entry in reader.Entries)
                                {
                                    if (!entry.IsDirectory && (entry.Size > Settings.Media.MaxSizeInDatabase))
                                    {
                                        string text = file.FileName + "/" + entry.Name;
                                        HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.frames[0].scForm.postRequest(\"\", \"\", \"\", 'ShowFileTooBig(" + StringUtil.EscapeJavascriptString(text) + ")')</script></head><body>Done</body></html>");
                                        args.ErrorText = string.Format("The file \"{0}\" is too big to be uploaded. The maximum size for uploading files is {1}.", text, MainUtil.FormatSize(Settings.Media.MaxSizeInDatabase));
                                        args.AbortPipeline();
                                        break;
                                    }
                                }
                                continue;
                            }
                            finally
                            {
                                file.InputStream.Position = 0L;
                            }
                        }

                        if (file.ContentLength > Settings.Media.MaxSizeInDatabase)
                        {
                            string fileName = file.FileName;
                            HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.frames[0].scForm.postRequest(\"\", \"\", \"\", 'ShowFileTooBig(" + StringUtil.EscapeJavascriptString(fileName) + ")')</script></head><body>Done</body></html>");
                            args.ErrorText = string.Format("The file \"{0}\" is too big to be uploaded. The maximum size for uploading files is {1}.", fileName, MainUtil.FormatSize(Settings.Media.MaxSizeInDatabase));
                            args.AbortPipeline();
                            break;
                        }

                        if (!Context.IsAdministrator)
                        {
                            Item uploadRestrictionsFolder = Context.ContentDatabase.GetItem("path to your UploadRestrictionsFolder");

                            if (uploadRestrictionsFolder.HasChildren)
                            {
                                User user = Context.User;
                                string extensionNameOfUploadedFile = System.IO.Path.GetExtension(file.FileName.ToLower());
                                UserRoles roles = user.Roles;
                                IEnumerable<Item> uploadRestrictionsItems = uploadRestrictionsFolder.GetChildren();

                                foreach (Role userRole in roles)
                                {
                                    foreach (Item uploadRestrictionsItem in uploadRestrictionsItems)
                                    {
                                        string uploadRestrictionsItemName = uploadRestrictionsItem.Name.ToLower();
                                        string userRoleName = userRole.Name.Replace((userRole.Domain + "\\"), "").ToLower();

                                        if (uploadRestrictionsItemName == userRoleName)
                                        {
                                            if (!String.IsNullOrEmpty(uploadRestrictionsItem["TypeAndSize"]))
                                            {
                                                NameValueCollection typeAndSize = WebUtil.ParseUrlParameters(uploadRestrictionsItem.Fields["TypeAndSize"].Value, '&');

                                                foreach (string extensionTypeName in typeAndSize.Keys)
                                                {
                                                    string valuesOfTheSize = typeAndSize[extensionTypeName];

                                                    if (("." + extensionTypeName) == extensionNameOfUploadedFile)
                                                    {
                                                        long fileSize = 0L;

                                                        if (valuesOfTheSize.IndexOf("B", StringComparison.OrdinalIgnoreCase) >= 0)
                                                        {
                                                            Regex regex = new Regex(@"(\.|,)\d");
                                                            fileSize = StringUtil.ParseSizeString(regex.Replace(valuesOfTheSize, string.Empty));
                                                        }
                                                        else
                                                        {
                                                            try
                                                            {
                                                                fileSize = Int64.Parse(valuesOfTheSize);
                                                            }
                                                            catch (Exception exception)
                                                            {
                                                                Log.Error(" could not parse Int64", exception, typeof(CheckSize));
                                                            }

                                                        }

                                                        if (file.ContentLength > fileSize)
                                                        {
                                                            string fileName = file.FileName;
                                                            HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.frames[0].scForm.postRequest(\"\", \"\", \"\", 'ShowFileTooBig(" + StringUtil.EscapeJavascriptString(fileName) + ")')</script></head><body>Done</body></html>");
                                                            args.ErrorText = string.Format("The file \"{0}\" is too big to be uploaded. The maximum size for uploading files is {1}.", fileName, MainUtil.FormatSize(fileSize));
                                                            args.AbortPipeline();
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                            }
                        }

                    }
                }
            }
        }
    }



Web.config


      <uiUpload>
        <processor mode="on" type="path.to.your.namespace.CheckSize, "your.dll" />
        <processor mode="on" type="Sitecore.Pipelines.Upload.ResolveFolder, Sitecore.Kernel" />
        <processor mode="on" type="Sitecore.Pipelines.Upload.Save, Sitecore.Kernel" />
        <processor mode="on" type="Sitecore.Pipelines.Upload.Done, Sitecore.Kernel" />
      </uiUpload>

your UploadRestriction item

uploader



Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s