Skip to content

Commit 97b0bdf

Browse files
Issue #69: (#70)
+ Add support for UploadOrder, UploadAllow, UploadDeny
1 parent d08bd18 commit 97b0bdf

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

elFinder.NetCore.Web/Controllers/FileSystemController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ private Connector GetConnector()
6060
Write = false,
6161
Locked = true
6262
}
63-
}
63+
},
64+
// Upload file type constraints
65+
//UploadAllow = new[] { "image", "text" },
66+
//UploadDeny = new[] { "text/csv" },
67+
//UploadOrder = new[] { "allow", "deny" }
6468
};
6569

6670
driver.AddRoot(root);

elFinder.NetCore/Connector.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using elFinder.NetCore.Drawing;
88
using elFinder.NetCore.Drivers;
9+
using elFinder.NetCore.Exceptions;
910
using elFinder.NetCore.Helpers;
1011
using Microsoft.AspNetCore.Http;
1112
using Microsoft.AspNetCore.Mvc;
@@ -41,6 +42,10 @@ public async Task<IActionResult> ProcessAsync(HttpRequest request)
4142
{
4243
return Error.FolderNotFound();
4344
}
45+
catch (FileTypeNotAllowException)
46+
{
47+
return Error.FileTypeNotAllow();
48+
}
4449
}
4550

4651
protected async Task<IActionResult> ProcessCoreAsync(HttpRequest request)

elFinder.NetCore/Drivers/FileSystem/FileSystemDriver.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Net.Mime;
77
using System.Threading.Tasks;
88
using elFinder.NetCore.Drawing;
9+
using elFinder.NetCore.Exceptions;
910
using elFinder.NetCore.Extensions;
1011
using elFinder.NetCore.Helpers;
1112
using elFinder.NetCore.Models;
@@ -767,6 +768,44 @@ public async Task<JsonResult> UploadAsync(FullPath path, IEnumerable<IFormFile>
767768
}
768769
}
769770

771+
if (path.RootVolume.UploadOrder != null)
772+
{
773+
foreach (var file in files)
774+
{
775+
var mimeType = MimeHelper.GetMimeType(Path.GetExtension(file.FileName));
776+
var constraintMap = new Dictionary<string, IEnumerable<string>>
777+
{
778+
["allow"] = path.RootVolume.UploadAllow,
779+
["deny"] = path.RootVolume.UploadDeny,
780+
};
781+
782+
foreach (var constraintType in path.RootVolume.UploadOrder)
783+
{
784+
var constraint = constraintMap[constraintType];
785+
if (constraint == null) continue;
786+
switch (constraintType)
787+
{
788+
case "allow":
789+
{
790+
if (!constraint.Contains("all")
791+
&& !constraint.Contains(mimeType)
792+
&& !constraint.Contains(mimeType.Type))
793+
throw new FileTypeNotAllowException();
794+
break;
795+
}
796+
case "deny":
797+
{
798+
if (constraint.Contains("all")
799+
|| constraint.Contains(mimeType)
800+
|| constraint.Contains(mimeType.Type))
801+
throw new FileTypeNotAllowException();
802+
break;
803+
}
804+
}
805+
}
806+
}
807+
}
808+
770809
foreach (string rename in renames)
771810
{
772811
var fileInfo = new FileInfo(Path.Combine(path.Directory.FullName, rename));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace elFinder.NetCore.Exceptions
7+
{
8+
public class FileTypeNotAllowException : Exception
9+
{
10+
}
11+
}

elFinder.NetCore/Helpers/Error.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public static JsonResult MaxUploadFileSize()
3434
return FormatSimpleError("errFileMaxSize");
3535
}
3636

37+
public static JsonResult FileTypeNotAllow()
38+
{
39+
return FormatSimpleError("errUploadMime");
40+
}
41+
3742
public static JsonResult MissedParameter(string command)
3843
{
3944
return new JsonResult(new { error = new string[] { "errCmdParams", command } });

elFinder.NetCore/RootVolume.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public RootVolume(
4747

4848
DirectorySeparatorChar = directorySeparatorChar == default(char) ? Path.DirectorySeparatorChar : directorySeparatorChar; // Can be changed for other providers
4949
ThumbnailDirectory = $"{rootDirectory}{DirectorySeparatorChar}.tmb";
50+
UploadOrder = new[] { "deny", "allow" };
5051
}
5152

5253
/// <summary>
@@ -161,6 +162,10 @@ public double? MaxUploadSizeInMb
161162
/// </summary>
162163
public string VolumeId { get; set; }
163164

165+
public IEnumerable<string> UploadAllow { get; set; }
166+
public IEnumerable<string> UploadDeny { get; set; }
167+
public IEnumerable<string> UploadOrder { get; set; }
168+
164169
public bool CanCreateThumbnail(IFile input)
165170
{
166171
return ThumbnailUrl != null && PictureEditor.CanProcessFile(input.Extension);

0 commit comments

Comments
 (0)