Advertisement
Guest User

Untitled

a guest
Jan 7th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. using Sitecore;
  2. using Sitecore.Diagnostics;
  3. using Sitecore.Exceptions;
  4. using Sitecore.Pipelines.Attach;
  5. using Sitecore.Pipelines.Upload;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Web;
  10.  
  11. namespace Custom.Business.Pipelines.Upload
  12. {
  13.     public class ImageCheckSize : UploadProcessor
  14.     {      
  15.         public List<string> RestrictedExtensions { get; set; }
  16.  
  17.         public ImageCheckSize()
  18.         {
  19.             RestrictedExtensions = new List<string>();
  20.         }
  21.  
  22.         public void Process(UploadArgs args)
  23.         {
  24.             Assert.ArgumentNotNull((object) args, "args");
  25.             if (args.Destination == UploadDestination.File)
  26.                 return;
  27.  
  28.             foreach (string index in args.Files)
  29.             {
  30.                 HttpPostedFile file = args.Files[index];
  31.                 if (!string.IsNullOrEmpty(file.FileName) && IsRestrictedExtension(file.FileName))
  32.                 {
  33.                     if ((long) file.ContentLength > MaxImageSizeInDatabase)
  34.                     {
  35.                         // This doesn't work, not sure why since Sitecore calls the same in it's own CheckSize processor and it shows an alert :(
  36.                         //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>");
  37.  
  38.                         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));
  39.                         Log.Warn(args.ErrorText, this);
  40.                         args.AbortPipeline();
  41.                         break;
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.  
  47.         private bool IsRestrictedExtension(string filename)
  48.         {
  49.             return RestrictedExtensions.Exists(restrictedExtension => string.Equals(restrictedExtension, Path.GetExtension(filename), StringComparison.CurrentCultureIgnoreCase));
  50.         }
  51.  
  52.         public static long MaxImageSizeInDatabase
  53.         {
  54.             get
  55.             {
  56.                 return Sitecore.Configuration.Settings.GetLongSetting("Media.MaxImageSizeInDatabase", 524288000L);
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. namespace Custom.Business.Pipelines.Attach
  63. {
  64.     public class ImageCheckSize
  65.     {
  66.         public List<string> RestrictedExtensions { get; set; }
  67.  
  68.         public ImageCheckSize()
  69.         {
  70.             RestrictedExtensions = new List<string>();
  71.         }
  72.  
  73.         public void Process(AttachArgs args)
  74.         {
  75.             if (args.MediaItem.FileBased || (IsRestrictedExtension(args.File.FileName) && args.File.InputStream.Length <= MaxImageSizeInDatabase))
  76.                 return;
  77.             // 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 :(
  78.             // But not sure how else to abort the pipeline AND show some sort of message
  79.             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)));
  80.         }
  81.  
  82.         private bool IsRestrictedExtension(string filename)
  83.         {
  84.             return RestrictedExtensions.Exists(restrictedExtension => string.Equals(restrictedExtension, Path.GetExtension(filename), StringComparison.CurrentCultureIgnoreCase));
  85.         }
  86.  
  87.         public static long MaxImageSizeInDatabase
  88.         {
  89.             get
  90.             {
  91.                 return Sitecore.Configuration.Settings.GetLongSetting("Media.MaxImageSizeInDatabase", 524288000L);
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement