Skip to content

Commit 1208bf8

Browse files
committed
Bumped version to 1.3.6 and did a little refactoring
1 parent 97b0bdf commit 1208bf8

File tree

9 files changed

+155
-79
lines changed

9 files changed

+155
-79
lines changed

elFinder.NetCore.Web/elFinder.NetCore.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Description>Demo Project for elFinder ASP.NET Core backend</Description>
99
<PackageProjectUrl>https://github.com/gordon-matt/elFinder.NetCore</PackageProjectUrl>
1010
<PackageLicenseUrl></PackageLicenseUrl>
11-
<Version>1.3.5</Version>
11+
<Version>1.3.6</Version>
1212
</PropertyGroup>
1313
<ItemGroup>
1414
<ProjectReference Include="..\elFinder.NetCore\elFinder.NetCore.csproj" />

elFinder.NetCore/Connector.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public async Task<IActionResult> ProcessAsync(HttpRequest request)
4242
{
4343
return Error.FolderNotFound();
4444
}
45-
catch (FileTypeNotAllowException)
45+
catch (FileTypeNotAllowedException)
4646
{
47-
return Error.FileTypeNotAllow();
47+
return Error.FileTypeNotAllowed();
4848
}
4949
}
5050

@@ -57,7 +57,7 @@ protected async Task<IActionResult> ProcessCoreAsync(HttpRequest request)
5757
string cmd = parameters.GetValueOrDefault("cmd");
5858
if (string.IsNullOrEmpty(cmd))
5959
{
60-
return Error.CommandNotFound();
60+
return Error.UnknownCommand();
6161
}
6262

6363
switch (cmd)
@@ -206,7 +206,7 @@ protected async Task<IActionResult> ProcessCoreAsync(HttpRequest request)
206206
return await driver.RotateAsync(path, int.Parse(parameters.GetValueOrDefault("degree")));
207207

208208
default:
209-
return Error.CommandNotFound();
209+
return Error.UnknownCommand();
210210
}
211211
}
212212
case "rm":
@@ -252,23 +252,23 @@ protected async Task<IActionResult> ProcessCoreAsync(HttpRequest request)
252252
}
253253
case "zipdl":
254254
{
255-
var targetsStr = parameters.GetValueOrDefault("targets[]");
255+
var targets = parameters.GetValueOrDefault("targets[]");
256256
var download = parameters.GetValueOrDefault("download");
257257

258258
if (download != "1")
259259
{
260-
var targets = await GetFullPathArrayAsync(targetsStr);
261-
return await driver.ZipDownloadAsync(targets);
260+
var targetPaths = await GetFullPathArrayAsync(targets);
261+
return await driver.ZipDownloadAsync(targetPaths);
262262
}
263263

264-
var cwdPath = await driver.ParsePathAsync(targetsStr[0]);
265-
var archiveFileKey = targetsStr[1];
266-
var downloadFileName = targetsStr[2];
267-
var mimeType = targetsStr[3];
264+
var cwdPath = await driver.ParsePathAsync(targets[0]);
265+
string archiveFileKey = targets[1];
266+
string downloadFileName = targets[2];
267+
string mimeType = targets[3];
268268

269269
return await driver.ZipDownloadAsync(cwdPath, archiveFileKey, downloadFileName, mimeType);
270270
}
271-
default: return Error.CommandNotFound();
271+
default: return Error.UnknownCommand();
272272
}
273273
}
274274

elFinder.NetCore/Drivers/FileSystem/FileSystemDriver.cs

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ public async Task<JsonResult> UploadAsync(FullPath path, IEnumerable<IFormFile>
763763
{
764764
if (file.Length > path.RootVolume.MaxUploadSize.Value)
765765
{
766-
return Error.MaxUploadFileSize();
766+
return Error.UploadFileTooLarge();
767767
}
768768
}
769769
}
@@ -779,26 +779,34 @@ public async Task<JsonResult> UploadAsync(FullPath path, IEnumerable<IFormFile>
779779
["deny"] = path.RootVolume.UploadDeny,
780780
};
781781

782-
foreach (var constraintType in path.RootVolume.UploadOrder)
782+
foreach (string constraintType in path.RootVolume.UploadOrder)
783783
{
784-
var constraint = constraintMap[constraintType];
785-
if (constraint == null) continue;
784+
var mimeTypes = constraintMap[constraintType];
785+
if (mimeTypes == null)
786+
{
787+
continue;
788+
}
789+
786790
switch (constraintType)
787791
{
788792
case "allow":
789793
{
790-
if (!constraint.Contains("all")
791-
&& !constraint.Contains(mimeType)
792-
&& !constraint.Contains(mimeType.Type))
793-
throw new FileTypeNotAllowException();
794+
if (!mimeTypes.Contains("all") &&
795+
!mimeTypes.Contains(mimeType) &&
796+
!mimeTypes.Contains(mimeType.Type))
797+
{
798+
throw new FileTypeNotAllowedException();
799+
}
794800
break;
795801
}
796802
case "deny":
797803
{
798-
if (constraint.Contains("all")
799-
|| constraint.Contains(mimeType)
800-
|| constraint.Contains(mimeType.Type))
801-
throw new FileTypeNotAllowException();
804+
if (mimeTypes.Contains("all") ||
805+
mimeTypes.Contains(mimeType) ||
806+
mimeTypes.Contains(mimeType.Type))
807+
{
808+
throw new FileTypeNotAllowedException();
809+
}
802810
break;
803811
}
804812
}
@@ -867,50 +875,47 @@ public async Task<JsonResult> UploadAsync(FullPath path, IEnumerable<IFormFile>
867875

868876
public async Task<JsonResult> ZipDownloadAsync(IEnumerable<FullPath> paths)
869877
{
870-
var tempFile = Path.GetTempFileName();
871-
var tempFileName = Path.GetFileName(tempFile);
872-
873-
using (var newFile = ZipFile.Open(tempFile, ZipArchiveMode.Update))
878+
string tempFilePath = Path.GetTempFileName();
879+
using (var zipArchive = ZipFile.Open(tempFilePath, ZipArchiveMode.Update))
874880
{
875881
foreach (var path in paths)
876882
{
877883
if (path.IsDirectory)
878884
{
879-
await AddDirectoryToArchiveAsync(newFile, path.Directory, string.Empty);
885+
await AddDirectoryToArchiveAsync(zipArchive, path.Directory, string.Empty);
880886
}
881887
else
882888
{
883-
newFile.CreateEntryFromFile(path.File.FullName, path.File.Name);
889+
zipArchive.CreateEntryFromFile(path.File.FullName, path.File.Name);
884890
}
885891
}
886892
}
887893

888-
var zipDownloadData = new ZipDownloadResponseModel.ZipDownloadData();
889-
890-
zipDownloadData.Mime = MediaTypeNames.Application.Zip;
891-
zipDownloadData.File = tempFileName;
892-
893894
return await Json(new ZipDownloadResponseModel
894895
{
895-
ZipDownload = zipDownloadData
896+
ZipDownload = new ZipDownloadResponseModel.ZipDownloadData
897+
{
898+
Mime = MediaTypeNames.Application.Zip,
899+
File = Path.GetFileName(tempFilePath)
900+
}
896901
});
897902
}
898903

899904
public async Task<FileStreamResult> ZipDownloadAsync(FullPath cwdPath, string archivedFileKey, string downloadFileName, string mimeType)
900905
{
901-
var tempDirPath = Path.GetTempPath();
902-
var tempFileInfo = new FileInfo(Path.Combine(tempDirPath, archivedFileKey));
903-
var memStream = new MemoryStream();
906+
string tempPath = Path.GetTempPath();
907+
var tempFile = new FileInfo(Path.Combine(tempPath, archivedFileKey));
904908

905-
using (var fileStream = tempFileInfo.OpenRead())
909+
var memoryStream = new MemoryStream();
910+
using (var fileStream = tempFile.OpenRead())
906911
{
907-
await fileStream.CopyToAsync(memStream);
912+
await fileStream.CopyToAsync(memoryStream);
908913
}
909914

910-
tempFileInfo.Delete();
911-
memStream.Position = 0;
915+
tempFile.Delete();
916+
memoryStream.Position = 0;
912917

913-
return new FileStreamResult(memStream, mimeType)
918+
return new FileStreamResult(memoryStream, mimeType)
914919
{
915920
FileDownloadName = downloadFileName
916921
};

elFinder.NetCore/Exceptions/FileTypeNotAllowException.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace elFinder.NetCore.Exceptions
5+
{
6+
public class FileTypeNotAllowedException : ApplicationException
7+
{
8+
public FileTypeNotAllowedException()
9+
{
10+
}
11+
12+
public FileTypeNotAllowedException(string message)
13+
: base(message)
14+
{
15+
}
16+
17+
public FileTypeNotAllowedException(string message, Exception innerException)
18+
: base(message, innerException)
19+
{
20+
}
21+
22+
protected FileTypeNotAllowedException(SerializationInfo info, StreamingContext context)
23+
: base(info, context)
24+
{
25+
}
26+
}
27+
}

elFinder.NetCore/Helpers/Error.cs

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,95 @@
22

33
namespace elFinder.NetCore.Helpers
44
{
5+
// For a list of all possible error messages: https://github.com/Studio-42/elFinder/blob/master/js/i18n/elfinder.en.js
6+
57
public static class Error
68
{
9+
/// <summary>
10+
/// Access denied.
11+
/// </summary>
12+
/// <returns></returns>
713
public static JsonResult AccessDenied()
814
{
915
return FormatSimpleError("errAccess");
1016
}
1117

12-
public static JsonResult CannotUploadFile()
18+
/// <summary>
19+
/// File not found.
20+
/// </summary>
21+
/// <returns></returns>
22+
public static JsonResult FileNotFound()
1323
{
14-
return FormatSimpleError("errUploadFile");
24+
return FormatSimpleError("errFileNotFound");
1525
}
1626

17-
public static JsonResult CommandNotFound()
27+
/// <summary>
28+
/// File type not allowed.
29+
/// </summary>
30+
/// <returns></returns>
31+
public static JsonResult FileTypeNotAllowed()
1832
{
19-
return FormatSimpleError("errUnknownCmd");
33+
return FormatSimpleError("errUploadMime");
2034
}
2135

22-
public static JsonResult FileNotFound()
36+
/// <summary>
37+
/// Folder not found.
38+
/// </summary>
39+
/// <returns></returns>
40+
public static JsonResult FolderNotFound()
2341
{
24-
return FormatSimpleError("errFileNotFound");
42+
return FormatSimpleError("errFolderNotFound");
2543
}
2644

27-
public static JsonResult FolderNotFound()
45+
/// <summary>
46+
/// Invalid parameters for command "$1".
47+
/// </summary>
48+
/// <param name="command"></param>
49+
/// <returns></returns>
50+
public static JsonResult InvalidCommandParams(string command)
2851
{
29-
return FormatSimpleError("errFolderNotFound");
52+
return new JsonResult(new { error = new string[] { "errCmdParams", command } });
3053
}
3154

32-
public static JsonResult MaxUploadFileSize()
55+
// NOTE: This is a custom error (not out-of-the-box) defined on client side.
56+
/// <summary>
57+
/// Unable to create new file with name "$1"
58+
/// </summary>
59+
/// <param name="name"></param>
60+
/// <returns></returns>
61+
public static JsonResult NewNameSelectionException(string name)
3362
{
34-
return FormatSimpleError("errFileMaxSize");
63+
return new JsonResult(new
64+
{
65+
error = new[] { "errNewNameSelection", name }
66+
});
3567
}
3668

37-
public static JsonResult FileTypeNotAllow()
69+
/// <summary>
70+
/// Unable to upload "$1".
71+
/// </summary>
72+
/// <returns></returns>
73+
public static JsonResult UnableToUpload()
3874
{
39-
return FormatSimpleError("errUploadMime");
75+
return FormatSimpleError("errUploadFile");
4076
}
4177

42-
public static JsonResult MissedParameter(string command)
78+
/// <summary>
79+
/// Unknown command.
80+
/// </summary>
81+
/// <returns></returns>
82+
public static JsonResult UnknownCommand()
4383
{
44-
return new JsonResult(new { error = new string[] { "errCmdParams", command } });
84+
return FormatSimpleError("errUnknownCmd");
4585
}
4686

47-
public static JsonResult NewNameSelectionException(string name)
87+
/// <summary>
88+
/// File exceeds maximum allowed size.
89+
/// </summary>
90+
/// <returns></returns>
91+
public static JsonResult UploadFileTooLarge()
4892
{
49-
return new JsonResult(new
50-
{
51-
error = new[] { "errNewNameSelection", name }
52-
});
93+
return FormatSimpleError("errUploadFileSize"); // old name - errFileMaxSize
5394
}
5495

5596
private static JsonResult FormatSimpleError(string message)

elFinder.NetCore/Models/Commands/ZipDownloadResponseModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ public class ZipDownloadData
1111
{
1212
[JsonPropertyName("file")]
1313
public string File { get; set; }
14+
1415
[JsonPropertyName("name")]
1516
public string Name { get; set; }
17+
1618
[JsonPropertyName("mime")]
1719
public string Mime { get; set; }
1820
}

0 commit comments

Comments
 (0)