Sitecore Sample Pipelines

How to restrict certain types of files from being uploaded into Sitecore


Create a Custom Class like below 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.IO;
using Sitecore.Pipelines.Upload;

namespace mySitecoreTestNew.Pipelines
{
    public class CustomSave
    {
        public void Process(UploadArgs args)
        {
            foreach (string key in args.Files)
            {
                HttpPostedFile file = args.Files[key];
                if (file.FileName.Length > 0 && file.ContentLength > 0)
                {
                    string filename = FileUtil.MakePath(args.Folder, Path.GetFileName(file.FileName), '\\');
                    if (Path.GetExtension(filename).Equals(".png"))
                    {
                        string customMessage = "<script>alert(\"You are prohibited from uploading *.png files!\")</script>";
                        HttpContext.Current.Response.Write(customMessage);
                        Log.Warn("Uploading *.png files are prohibited!", this);

                        //Custom Log
                        var logger = LoggerFactory.GetLogger("MyLog");
                        logger.Info("This is an info message for my custom log");

                        //Finish uploading dialog
                        Done done = new Done();
                        done.Process(args);
                        //Prevent other processors from execution
                        args.AbortPipeline();
                        //Break uploading process
                        return;
                    }
                }
            }
        }
    }
}


Build the Solution and Publish it.

Create a patch Configuration  file.
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <processors>
      <uiUpload>
        <processor mode="on" type="mySitecoreTestNew.Pipelines.CustomSave, mySitecoreTestNew" patch:before="processor[@type='Sitecore.Pipelines.Upload.CheckSize, Sitecore.Kernel']">
             </processor>
      </uiUpload>
    </processors>
  </sitecore>
</configuration>


The Structure of the Project Should be look like below

Comments