Skip to content

Commit

Permalink
Bumped version to 1.3.6 and did a little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gordon-matt committed Nov 1, 2021
1 parent a2ae433 commit 671e0ad
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 79 deletions.
2 changes: 1 addition & 1 deletion elFinder.NetCore.Web/elFinder.NetCore.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Description>Demo Project for elFinder ASP.NET Core backend</Description>
<PackageProjectUrl>https://github.com/gordon-matt/elFinder.NetCore</PackageProjectUrl>
<PackageLicenseUrl></PackageLicenseUrl>
<Version>1.3.5</Version>
<Version>1.3.6</Version>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\elFinder.NetCore\elFinder.NetCore.csproj" />
Expand Down
24 changes: 12 additions & 12 deletions elFinder.NetCore/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public async Task<IActionResult> ProcessAsync(HttpRequest request)
{
return Error.FolderNotFound();
}
catch (FileTypeNotAllowException)
catch (FileTypeNotAllowedException)
{
return Error.FileTypeNotAllow();
return Error.FileTypeNotAllowed();
}
}

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

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

default:
return Error.CommandNotFound();
return Error.UnknownCommand();
}
}
case "rm":
Expand Down Expand Up @@ -252,23 +252,23 @@ protected async Task<IActionResult> ProcessCoreAsync(HttpRequest request)
}
case "zipdl":
{
var targetsStr = parameters.GetValueOrDefault("targets[]");
var targets = parameters.GetValueOrDefault("targets[]");
var download = parameters.GetValueOrDefault("download");

if (download != "1")
{
var targets = await GetFullPathArrayAsync(targetsStr);
return await driver.ZipDownloadAsync(targets);
var targetPaths = await GetFullPathArrayAsync(targets);
return await driver.ZipDownloadAsync(targetPaths);
}

var cwdPath = await driver.ParsePathAsync(targetsStr[0]);
var archiveFileKey = targetsStr[1];
var downloadFileName = targetsStr[2];
var mimeType = targetsStr[3];
var cwdPath = await driver.ParsePathAsync(targets[0]);
string archiveFileKey = targets[1];
string downloadFileName = targets[2];
string mimeType = targets[3];

return await driver.ZipDownloadAsync(cwdPath, archiveFileKey, downloadFileName, mimeType);
}
default: return Error.CommandNotFound();
default: return Error.UnknownCommand();
}
}

Expand Down
69 changes: 37 additions & 32 deletions elFinder.NetCore/Drivers/FileSystem/FileSystemDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public async Task<JsonResult> UploadAsync(FullPath path, IEnumerable<IFormFile>
{
if (file.Length > path.RootVolume.MaxUploadSize.Value)
{
return Error.MaxUploadFileSize();
return Error.UploadFileTooLarge();
}
}
}
Expand All @@ -779,26 +779,34 @@ public async Task<JsonResult> UploadAsync(FullPath path, IEnumerable<IFormFile>
["deny"] = path.RootVolume.UploadDeny,
};

foreach (var constraintType in path.RootVolume.UploadOrder)
foreach (string constraintType in path.RootVolume.UploadOrder)
{
var constraint = constraintMap[constraintType];
if (constraint == null) continue;
var mimeTypes = constraintMap[constraintType];
if (mimeTypes == null)
{
continue;
}

switch (constraintType)
{
case "allow":
{
if (!constraint.Contains("all")
&& !constraint.Contains(mimeType)
&& !constraint.Contains(mimeType.Type))
throw new FileTypeNotAllowException();
if (!mimeTypes.Contains("all") &&
!mimeTypes.Contains(mimeType) &&
!mimeTypes.Contains(mimeType.Type))
{
throw new FileTypeNotAllowedException();
}
break;
}
case "deny":
{
if (constraint.Contains("all")
|| constraint.Contains(mimeType)
|| constraint.Contains(mimeType.Type))
throw new FileTypeNotAllowException();
if (mimeTypes.Contains("all") ||
mimeTypes.Contains(mimeType) ||
mimeTypes.Contains(mimeType.Type))
{
throw new FileTypeNotAllowedException();
}
break;
}
}
Expand Down Expand Up @@ -867,50 +875,47 @@ public async Task<JsonResult> UploadAsync(FullPath path, IEnumerable<IFormFile>

public async Task<JsonResult> ZipDownloadAsync(IEnumerable<FullPath> paths)
{
var tempFile = Path.GetTempFileName();
var tempFileName = Path.GetFileName(tempFile);

using (var newFile = ZipFile.Open(tempFile, ZipArchiveMode.Update))
string tempFilePath = Path.GetTempFileName();
using (var zipArchive = ZipFile.Open(tempFilePath, ZipArchiveMode.Update))
{
foreach (var path in paths)
{
if (path.IsDirectory)
{
await AddDirectoryToArchiveAsync(newFile, path.Directory, string.Empty);
await AddDirectoryToArchiveAsync(zipArchive, path.Directory, string.Empty);
}
else
{
newFile.CreateEntryFromFile(path.File.FullName, path.File.Name);
zipArchive.CreateEntryFromFile(path.File.FullName, path.File.Name);
}
}
}

var zipDownloadData = new ZipDownloadResponseModel.ZipDownloadData();

zipDownloadData.Mime = MediaTypeNames.Application.Zip;
zipDownloadData.File = tempFileName;

return await Json(new ZipDownloadResponseModel
{
ZipDownload = zipDownloadData
ZipDownload = new ZipDownloadResponseModel.ZipDownloadData
{
Mime = MediaTypeNames.Application.Zip,
File = Path.GetFileName(tempFilePath)
}
});
}

public async Task<FileStreamResult> ZipDownloadAsync(FullPath cwdPath, string archivedFileKey, string downloadFileName, string mimeType)
{
var tempDirPath = Path.GetTempPath();
var tempFileInfo = new FileInfo(Path.Combine(tempDirPath, archivedFileKey));
var memStream = new MemoryStream();
string tempPath = Path.GetTempPath();
var tempFile = new FileInfo(Path.Combine(tempPath, archivedFileKey));

using (var fileStream = tempFileInfo.OpenRead())
var memoryStream = new MemoryStream();
using (var fileStream = tempFile.OpenRead())
{
await fileStream.CopyToAsync(memStream);
await fileStream.CopyToAsync(memoryStream);
}

tempFileInfo.Delete();
memStream.Position = 0;
tempFile.Delete();
memoryStream.Position = 0;

return new FileStreamResult(memStream, mimeType)
return new FileStreamResult(memoryStream, mimeType)
{
FileDownloadName = downloadFileName
};
Expand Down
11 changes: 0 additions & 11 deletions elFinder.NetCore/Exceptions/FileTypeNotAllowException.cs

This file was deleted.

27 changes: 27 additions & 0 deletions elFinder.NetCore/Exceptions/FileTypeNotAllowedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Runtime.Serialization;

namespace elFinder.NetCore.Exceptions
{
public class FileTypeNotAllowedException : ApplicationException
{
public FileTypeNotAllowedException()
{
}

public FileTypeNotAllowedException(string message)
: base(message)
{
}

public FileTypeNotAllowedException(string message, Exception innerException)
: base(message, innerException)
{
}

protected FileTypeNotAllowedException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}
79 changes: 60 additions & 19 deletions elFinder.NetCore/Helpers/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,95 @@

namespace elFinder.NetCore.Helpers
{
// For a list of all possible error messages: https://github.com/Studio-42/elFinder/blob/master/js/i18n/elfinder.en.js

public static class Error
{
/// <summary>
/// Access denied.
/// </summary>
/// <returns></returns>
public static JsonResult AccessDenied()
{
return FormatSimpleError("errAccess");
}

public static JsonResult CannotUploadFile()
/// <summary>
/// File not found.
/// </summary>
/// <returns></returns>
public static JsonResult FileNotFound()
{
return FormatSimpleError("errUploadFile");
return FormatSimpleError("errFileNotFound");
}

public static JsonResult CommandNotFound()
/// <summary>
/// File type not allowed.
/// </summary>
/// <returns></returns>
public static JsonResult FileTypeNotAllowed()
{
return FormatSimpleError("errUnknownCmd");
return FormatSimpleError("errUploadMime");
}

public static JsonResult FileNotFound()
/// <summary>
/// Folder not found.
/// </summary>
/// <returns></returns>
public static JsonResult FolderNotFound()
{
return FormatSimpleError("errFileNotFound");
return FormatSimpleError("errFolderNotFound");
}

public static JsonResult FolderNotFound()
/// <summary>
/// Invalid parameters for command "$1".
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
public static JsonResult InvalidCommandParams(string command)
{
return FormatSimpleError("errFolderNotFound");
return new JsonResult(new { error = new string[] { "errCmdParams", command } });
}

public static JsonResult MaxUploadFileSize()
// NOTE: This is a custom error (not out-of-the-box) defined on client side.
/// <summary>
/// Unable to create new file with name "$1"
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static JsonResult NewNameSelectionException(string name)
{
return FormatSimpleError("errFileMaxSize");
return new JsonResult(new
{
error = new[] { "errNewNameSelection", name }
});
}

public static JsonResult FileTypeNotAllow()
/// <summary>
/// Unable to upload "$1".
/// </summary>
/// <returns></returns>
public static JsonResult UnableToUpload()
{
return FormatSimpleError("errUploadMime");
return FormatSimpleError("errUploadFile");
}

public static JsonResult MissedParameter(string command)
/// <summary>
/// Unknown command.
/// </summary>
/// <returns></returns>
public static JsonResult UnknownCommand()
{
return new JsonResult(new { error = new string[] { "errCmdParams", command } });
return FormatSimpleError("errUnknownCmd");
}

public static JsonResult NewNameSelectionException(string name)
/// <summary>
/// File exceeds maximum allowed size.
/// </summary>
/// <returns></returns>
public static JsonResult UploadFileTooLarge()
{
return new JsonResult(new
{
error = new[] { "errNewNameSelection", name }
});
return FormatSimpleError("errUploadFileSize"); // old name - errFileMaxSize
}

private static JsonResult FormatSimpleError(string message)
Expand Down
2 changes: 2 additions & 0 deletions elFinder.NetCore/Models/Commands/ZipDownloadResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public class ZipDownloadData
{
[JsonPropertyName("file")]
public string File { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("mime")]
public string Mime { get; set; }
}
Expand Down
Loading

0 comments on commit 671e0ad

Please sign in to comment.