Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Sitecore;
- using Sitecore.Diagnostics;
- using Sitecore.Exceptions;
- using Sitecore.Pipelines.Attach;
- using Sitecore.Pipelines.Upload;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Web;
- namespace Custom.Business.Pipelines.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)
- {
- // This doesn't work, not sure why since Sitecore calls the same in it's own CheckSize processor and it shows an alert :(
- //HttpContext.Current.Response.Write("<html><head><script type=\"text/JavaScript\" language=\"javascript\">window.top.scForm.getTopModalDialog().frames[0].scForm.postRequest(\"\", \"\", \"\", 'ShowFileTooBig(" +StringUtil.EscapeJavascriptString(file.FileName) + ")')</script></head><body>Done</body></html>");
- 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);
- }
- }
- }
- }
- namespace Custom.Business.Pipelines.Attach
- {
- public class ImageCheckSize
- {
- public List<string> RestrictedExtensions { get; set; }
- public ImageCheckSize()
- {
- RestrictedExtensions = new List<string>();
- }
- public void Process(AttachArgs args)
- {
- if (args.MediaItem.FileBased || (IsRestrictedExtension(args.File.FileName) && args.File.InputStream.Length <= MaxImageSizeInDatabase))
- return;
- // This doesn't show the message passed in, not sure why since Sitecore calls the same in it's own CheckSize processor and it shows an alert :(
- // But not sure how else to abort the pipeline AND show some sort of message
- throw new ClientAlertException(String.Format("The file is too big to be attached. The maximum size of a file that can be uploaded is {0}.", MainUtil.FormatSize(MaxImageSizeInDatabase)));
- }
- 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);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement