Skip to content

Commit

Permalink
Cleanup usings and use file scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt committed Jan 27, 2024
1 parent f7a9e9e commit 1b69951
Show file tree
Hide file tree
Showing 57 changed files with 3,359 additions and 3,550 deletions.
13 changes: 5 additions & 8 deletions elFinder.NetCore.Web/Controllers/FileManagerController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using Microsoft.AspNetCore.Mvc;
namespace elFinder.NetCore.Web.Controllers;

namespace elFinder.NetCore.Web.Controllers
[Route("file-manager")]
public class FileManagerController : Controller
{
[Route("file-manager")]
public class FileManagerController : Controller
public IActionResult Index()
{
public IActionResult Index()
{
return View();
}
return View();
}
}
131 changes: 63 additions & 68 deletions elFinder.NetCore.Web/Controllers/FileSystemController.cs
Original file line number Diff line number Diff line change
@@ -1,86 +1,81 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using elFinder.NetCore.Drivers.FileSystem;
using elFinder.NetCore.Drivers.FileSystem;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace elFinder.NetCore.Web.Controllers
namespace elFinder.NetCore.Web.Controllers;

[Route("el-finder/file-system")]
public class FileSystemController : Controller
{
[Route("el-finder/file-system")]
public class FileSystemController : Controller
[Route("connector")]
public async Task<IActionResult> Connector()
{
[Route("connector")]
public async Task<IActionResult> Connector()
try
{
try
{
var connector = GetConnector();
return await connector.ProcessAsync(Request);
}
catch (Exception ex)
{
return Json(new { error = "Unable to process your request: " + ex.Message });
}
var connector = GetConnector();
return await connector.ProcessAsync(Request);
}

[Route("thumb/{hash}")]
public async Task<IActionResult> Thumbs(string hash)
catch (Exception ex)
{
var connector = GetConnector();
return await connector.GetThumbnailAsync(HttpContext.Request, HttpContext.Response, hash);
return Json(new { error = "Unable to process your request: " + ex.Message });
}
}

private Connector GetConnector()
{
var driver = new FileSystemDriver();
[Route("thumb/{hash}")]
public async Task<IActionResult> Thumbs(string hash)
{
var connector = GetConnector();
return await connector.GetThumbnailAsync(HttpContext.Request, HttpContext.Response, hash);
}

string absoluteUrl = UriHelper.BuildAbsolute(Request.Scheme, Request.Host);
var uri = new Uri(absoluteUrl);
private Connector GetConnector()
{
var driver = new FileSystemDriver();

string absoluteUrl = UriHelper.BuildAbsolute(Request.Scheme, Request.Host);
var uri = new Uri(absoluteUrl);

var root = new RootVolume(
PathHelper.MapPath("~/Files"),
$"{uri.Scheme}://{uri.Authority}/Files/",
$"{uri.Scheme}://{uri.Authority}/el-finder/file-system/thumb/")
var root = new RootVolume(
PathHelper.MapPath("~/Files"),
$"{uri.Scheme}://{uri.Authority}/Files/",
$"{uri.Scheme}://{uri.Authority}/el-finder/file-system/thumb/")
{
//IsReadOnly = !User.IsInRole("Administrators")
IsReadOnly = false, // Can be readonly according to user's membership permission
IsLocked = false, // If locked, files and directories cannot be deleted, renamed or moved
Alias = "Files", // Beautiful name given to the root/home folder
//MaxUploadSizeInKb = 2048, // Limit imposed to user uploaded file <= 2048 KB
AccessControlAttributes = new HashSet<NamedAccessControlAttributeSet>()
{
//IsReadOnly = !User.IsInRole("Administrators")
IsReadOnly = false, // Can be readonly according to user's membership permission
IsLocked = false, // If locked, files and directories cannot be deleted, renamed or moved
Alias = "Files", // Beautiful name given to the root/home folder
//MaxUploadSizeInKb = 2048, // Limit imposed to user uploaded file <= 2048 KB
AccessControlAttributes = new HashSet<NamedAccessControlAttributeSet>()
new NamedAccessControlAttributeSet(PathHelper.MapPath("~/Files/readonly.txt"))
{
Write = false,
Locked = true
},
new NamedAccessControlAttributeSet(PathHelper.MapPath("~/Files/Prohibited"))
{
new NamedAccessControlAttributeSet(PathHelper.MapPath("~/Files/readonly.txt"))
{
Write = false,
Locked = true
},
new NamedAccessControlAttributeSet(PathHelper.MapPath("~/Files/Prohibited"))
{
Read = false,
Write = false,
Locked = true
},
new NamedAccessControlAttributeSet(PathHelper.MapPath("~/Files/Parent/Children"))
{
Read = true,
Write = false,
Locked = true
}
Read = false,
Write = false,
Locked = true
},
// Upload file type constraints
//UploadAllow = new[] { "image", "text" },
//UploadDeny = new[] { "text/csv" },
//UploadOrder = new[] { "allow", "deny" }
};
new NamedAccessControlAttributeSet(PathHelper.MapPath("~/Files/Parent/Children"))
{
Read = true,
Write = false,
Locked = true
}
},
// Upload file type constraints
//UploadAllow = new[] { "image", "text" },
//UploadDeny = new[] { "text/csv" },
//UploadOrder = new[] { "allow", "deny" }
};

driver.AddRoot(root);
driver.AddRoot(root);

return new Connector(driver)
{
// This allows support for the "onlyMimes" option on the client.
MimeDetect = MimeDetectOption.Internal
};
}
return new Connector(driver)
{
// This allows support for the "onlyMimes" option on the client.
MimeDetect = MimeDetectOption.Internal
};
}
}
23 changes: 10 additions & 13 deletions elFinder.NetCore.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using Microsoft.AspNetCore.Mvc;
namespace elFinder.NetCore.Web.Controllers;

namespace elFinder.NetCore.Web.Controllers
public class HomeController : Controller
{
public class HomeController : Controller
[Route("")]
public IActionResult Index()
{
[Route("")]
public IActionResult Index()
{
return View();
}
return View();
}

[Route("error")]
public IActionResult Error()
{
return View();
}
[Route("error")]
public IActionResult Error()
{
return View();
}
}
23 changes: 10 additions & 13 deletions elFinder.NetCore.Web/Controllers/TinyMCEController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using Microsoft.AspNetCore.Mvc;
namespace elFinder.NetCore.Web.Controllers;

namespace elFinder.NetCore.Web.Controllers
[Route("tiny-mce")]
public class TinyMCEController : Controller
{
[Route("tiny-mce")]
public class TinyMCEController : Controller
public IActionResult Index()
{
public IActionResult Index()
{
return View();
}
return View();
}

[Route("browse")]
public IActionResult Browse()
{
return View();
}
[Route("browse")]
public IActionResult Browse()
{
return View();
}
}
35 changes: 15 additions & 20 deletions elFinder.NetCore.Web/PathHelper.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
using Microsoft.AspNetCore.Hosting;
using System;
using System.IO;
namespace elFinder.NetCore.Web;

namespace elFinder.NetCore.Web
public static class PathHelper
{
public static class PathHelper
public static string WebRootPath { get; set; }

public static string GetFullPathNormalized(string path)
{
public static string WebRootPath { get; set; }
return Path.TrimEndingDirectorySeparator(Path.GetFullPath(path));
}

public static string GetFullPathNormalized(string path)
{
return Path.TrimEndingDirectorySeparator(Path.GetFullPath(path));
}
public static string MapPath(string path, string basePath = null)
{
basePath = string.IsNullOrEmpty(basePath) ? WebRootPath : basePath;

public static string MapPath(string path, string basePath = null)
if (string.IsNullOrEmpty(basePath))
{
basePath = string.IsNullOrEmpty(basePath) ? WebRootPath : basePath;

if (string.IsNullOrEmpty(basePath))
{
throw new ArgumentException("elFinder PathHelper don't have WebRootPath or basePath configured.");
}

path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
return GetFullPathNormalized(Path.Combine(basePath, path));
throw new ArgumentException("PathHelper does not have WebRootPath or basePath configured.");
}

path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
return GetFullPathNormalized(Path.Combine(basePath, path));
}
}
5 changes: 2 additions & 3 deletions elFinder.NetCore.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Builder;
using elFinder.NetCore.Web;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -26,7 +26,6 @@
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");


elFinder.NetCore.Web.PathHelper.WebRootPath = app.Environment.WebRootPath;
PathHelper.WebRootPath = app.Environment.WebRootPath;

app.Run();
1 change: 1 addition & 0 deletions elFinder.NetCore.Web/ProjectUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.AspNetCore.Mvc;
51 changes: 24 additions & 27 deletions elFinder.NetCore/AccessControlAttributeSet.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
using System;
namespace elFinder.NetCore;

namespace elFinder.NetCore
public class AccessControlAttributeSet
{
public class AccessControlAttributeSet
public bool Read { get; set; } = true;

public bool Write { get; set; } = true;

public bool Locked { get; set; }
}

public class NamedAccessControlAttributeSet : AccessControlAttributeSet
{
public NamedAccessControlAttributeSet() : base()
{
public bool Read { get; set; } = true;
}

public bool Write { get; set; } = true;
public NamedAccessControlAttributeSet(string fullName) : base()
{
FullName = fullName ?? throw new ArgumentNullException(nameof(fullName));
}

public bool Locked { get; set; }
public string FullName { get; }

public override bool Equals(object obj)
{
return (obj as NamedAccessControlAttributeSet)?.FullName == FullName;
}

public class NamedAccessControlAttributeSet : AccessControlAttributeSet
public override int GetHashCode()
{
public NamedAccessControlAttributeSet() : base()
{
}

public NamedAccessControlAttributeSet(string fullName) : base()
{
FullName = fullName ?? throw new ArgumentNullException(nameof(fullName));
}

public string FullName { get; }

public override bool Equals(object obj)
{
return (obj as NamedAccessControlAttributeSet)?.FullName == FullName;
}

public override int GetHashCode()
{
return FullName.GetHashCode();
}
return FullName.GetHashCode();
}
}
Loading

0 comments on commit 1b69951

Please sign in to comment.