Restrict uploading Video and Audio files in Media Library based on Mime Type/Restrict uploading zip file which contains Video and Audio files in Media Library based on Mime Type:
By default, Sitecore allows the user to upload multiple types of files in the media library. This blog will guide you to restrict the content author not to upload specific file types(Ex. audio, video). Please follow the steps to create the custom pipeline processor to apply the restriction from Sitecore UI.
Create a pipeline processor :
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.Upload;
using Sitecore.Zip;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Web;
namespace SampleWebsite.Web.Foundation.Pipelines.Pipelines
{
public class CheckFileTypePipeline : UploadProcessor
{
private bool _isAllowed;
private string _extensions;
public CheckFileTypePipeline(string blocked)
{
if (string.IsNullOrEmpty(blocked))
return;
this._isAllowed = false;
this._extensions = blocked.Replace(" ", "").ToLower();
}
public void Process(UploadArgs args)
{
Assert.ArgumentNotNull((object)args, nameof(args));
if (string.IsNullOrEmpty(this._extensions))
return;
List<string> stringList = this.PrepareExtensions(this._extensions);
foreach (string file1 in (NameObjectCollectionBase)args.Files)
{
HttpPostedFile file2 = args.Files[file1];
if (!string.IsNullOrEmpty(file2.FileName))
{
if (UploadProcessor.IsUnpack(args,file2))
{
ZipReader zipReader = new ZipReader(file2.InputStream);
try
{
foreach (ZipEntry entry in zipReader.Entries)
{
string mimeTypef = MimeMapping.GetMimeMapping(entry.Name);
bool flag = stringList.Any(x => mimeTypef.Contains(x));
if ((!this._isAllowed || !flag) && (!this._isAllowed && flag || this._isAllowed && !flag))
{
HttpContext.Current.Response.Write("<script type=\"text/JavaScript\">alert(\"Uploading
files with " + mimeTypef + " as type is restricted\");</script>");
Log.Audit(string.Format("Upload restricted: {0}", (object)entry.Name), (object)this);
new Done().Process(args);
args.AbortPipeline();
return;
}
}
}
finally
{
file2.InputStream.Position = 0L;
}
}
string mimeTypef2 = MimeMapping.GetMimeMapping(file2.FileName);
bool flag1 = stringList.Any(x => mimeTypef2.Contains(x));
if ((!this._isAllowed || !flag1) && (!this._isAllowed && flag1 || this._isAllowed && !flag1))
{
HttpContext.Current.Response.Write("<script
type=\"text/JavaScript\">alert(\"Uploading files with " + mimeTypef2 + "
as file type is restricted\");</script>");
Log.Audit(string.Format("Upload restricted: {0}", (object)file2.FileName), (object)this);
new Done().Process(args);
args.AbortPipeline();
break;
}
}
}
}
private List<string> PrepareExtensions(string csvExtensions)
{
string[] strArray = csvExtensions.Split(',');
List<string> stringList = new List<string>();
foreach (string str in strArray)
stringList.Add(str);
return stringList;
}
}
}
Create a patch Config file :
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<processors>
<uiUpload>
<processor mode="on" type=" SmpleSite.Web.Foundation.Pipelines.Pipelines.CheckFileType,
SmpleSite.Web.Foundation.Pipelines" patch:before="*[1]">
<param desc="Blocked filetypes (comma
separated)">video,audio</param>
</processor>
</uiUpload>
</processors>
</sitecore>
</configuration>
Let's deploy these files to the website's root folder and give it a try.
Go to Media library and try to upload a file and then click on “Choose File”.
No comments:
Post a Comment