Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Move Elfinder from Controller to the Services (#186)

Krzysztof Pajakbe58060ab1d5
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/EditorTemplates/Editor.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/EditorTemplates/Editor.cshtml
index 1c0fda4d0..c959f9844 100644
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/EditorTemplates/Editor.cshtml
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/EditorTemplates/Editor.cshtml
@@ -16,6 +16,7 @@
function elfinderDialog@(random)(context) { // <------------------ +context
var fm = $('<div/>').dialogelfinder({
url: '@Url.Action("Connector", "ElFinder")',
+ baseUrl: '/_content/Grand.Web.Admin/administration/elfinder/',
lang: 'en',
width: 840,
height: 450,
diff --git a/src/Web/Grand.Web.Admin/Controllers/ElFinderController.cs b/src/Web/Grand.Web.Admin/Controllers/ElFinderController.cs
index 4693b1868..e43018ca1 100644
--- a/src/Web/Grand.Web.Admin/Controllers/ElFinderController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/ElFinderController.cs
@@ -1,15 +1,8 @@
-using elFinder.Net.AspNetCore.Extensions;
-using elFinder.Net.AspNetCore.Helper;
-using elFinder.Net.Core;
-using Grand.Business.Common.Interfaces.Security;
+using Grand.Business.Common.Interfaces.Security;
using Grand.Business.Common.Services.Security;
-using Grand.Domain.Media;
-using Grand.SharedKernel.Extensions;
+using Grand.Web.Admin.Interfaces;
using Grand.Web.Common.Security.Authorization;
using Microsoft.AspNetCore.Mvc;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
using System.Threading.Tasks;
namespace Grand.Web.Admin.Controllers
@@ -21,13 +14,7 @@ namespace Grand.Web.Admin.Controllers
#region Fields
private readonly IPermissionService _permissionService;
- private readonly IConnector _connector;
- private readonly IDriver _driver;
- private readonly MediaSettings _mediaSettings;
-
- private readonly string _fullPathToUpload;
- private readonly string _fullPathToThumbs;
- private readonly string _pathToUpload;
+ private readonly IElFinderViewModelService _elFinderViewModelService;
#endregion
@@ -35,28 +22,13 @@ namespace Grand.Web.Admin.Controllers
public ElFinderController(
IPermissionService permissionService,
- IConnector connector,
- IDriver driver,
- MediaSettings mediaSettings
+ IElFinderViewModelService elFinderViewModelService
)
{
- _mediaSettings = mediaSettings;
- _driver = driver;
+ _elFinderViewModelService = elFinderViewModelService;
_permissionService = permissionService;
- _connector = connector;
- _connector.Options.EnabledCommands = _mediaSettings.FileManagerEnabledCommands.Split(',').Select(x => x.Trim()).ToList();
- _connector.Options.DisabledUICommands = _mediaSettings.FileManagerDisabledUICommands.Split(',').Select(x => x.Trim()).ToList();
-
- _pathToUpload = Path.Combine("assets", "images", "uploaded");
-
- _fullPathToUpload = Path.Combine(CommonPath.WebRootPath, _pathToUpload);
- if (!Directory.Exists(_fullPathToUpload))
- Directory.CreateDirectory(_fullPathToUpload);
-
- _fullPathToThumbs = Path.Combine(CommonPath.WebRootPath, Path.Combine("assets", "images", "thumbs"));
- if (!Directory.Exists(_fullPathToThumbs))
- Directory.CreateDirectory(_fullPathToThumbs);
+ //var x = Url.Action("Thumb");
}
#endregion
@@ -69,15 +41,7 @@ namespace Grand.Web.Admin.Controllers
if (!await _permissionService.Authorize(StandardPermission.HtmlEditorManagePictures))
return new JsonResult(new { error = "You don't have required permission" });
- await SetupConnectorAsync();
- var cmd = ConnectorHelper.ParseCommand(Request);
- if (cmd.Cmd == "upload" && cmd.Files != null && cmd.Files.Any())
- cmd.Files = cmd.Files.Where(x => !NotAllowedExtensions(Path.GetExtension(x.FileName))).ToList();
-
- var ccTokenSource = ConnectorHelper.RegisterCcTokenSource(HttpContext);
- var conResult = await _connector.ProcessAsync(cmd, ccTokenSource);
- var actionResult = conResult.ToActionResult(HttpContext);
- return actionResult;
+ return await _elFinderViewModelService.Connector();
}
public async Task<IActionResult> Thumb(string id)
@@ -85,58 +49,7 @@ namespace Grand.Web.Admin.Controllers
if (!await _permissionService.Authorize(StandardPermission.HtmlEditorManagePictures))
return new JsonResult(new { error = "You don't have required permission" });
- await SetupConnectorAsync();
- var thumb = await _connector.GetThumbAsync(id, HttpContext.RequestAborted);
- var actionResult = ConnectorHelper.GetThumbResult(thumb);
- return actionResult;
- }
-
- private async Task SetupConnectorAsync()
- {
- var volume = new Volume(_driver,
- _fullPathToUpload,
- _fullPathToThumbs,
- "/assets/images/uploaded/",
- $"{Url.Action("Thumb")}/"
- ) {
- Name = "Volume",
- MaxUploadConnections = 3
- };
- volume.ObjectAttributes = new List<FilteredObjectAttribute>() {
- new FilteredObjectAttribute()
- {
- FileFilter = (file) => {
- return NotAllowedExtensions(file.Extension);
- },
- ObjectFilter = (obj) =>
- {
- var extensions = Path.GetExtension(obj.FullName);
- if(!string.IsNullOrEmpty(extensions))
- return NotAllowedExtensions(extensions);
-
-
- return false;
- },
- ShowOnly = false, Access = false, Visible = false, Write = false, Read = false
- },
- };
- _connector.AddVolume(volume);
- await volume.Driver.SetupVolumeAsync(volume);
-
- }
-
- private bool NotAllowedExtensions(string extensions)
- {
- var allowedFileTypes = new List<string>();
- if (string.IsNullOrEmpty(_mediaSettings.AllowedFileTypes))
- allowedFileTypes = new List<string> { ".gif", ".jpg", ".jpeg", ".png", ".bmp", ".webp" };
- else
- allowedFileTypes = _mediaSettings.AllowedFileTypes.Split(',').Select(x => x.Trim().ToLowerInvariant()).ToList();
-
- if (allowedFileTypes.Contains(extensions))
- return false;
-
- return true;
+ return await _elFinderViewModelService.Thumbs(id);
}
#endregion
diff --git a/src/Web/Grand.Web.Admin/Interfaces/IElFinderViewModelService.cs b/src/Web/Grand.Web.Admin/Interfaces/IElFinderViewModelService.cs
new file mode 100644
index 000000000..104dd6df9
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Interfaces/IElFinderViewModelService.cs
@@ -0,0 +1,12 @@
+using Microsoft.AspNetCore.Mvc;
+using System.Threading.Tasks;
+
+namespace Grand.Web.Admin.Interfaces
+{
+ public interface IElFinderViewModelService
+ {
+ Task SetupConnectorAsync();
+ Task<IActionResult> Connector();
+ Task<IActionResult> Thumbs(string id);
+ }
+}
diff --git a/src/Web/Grand.Web.Admin/Services/ElFinderViewModelService.cs b/src/Web/Grand.Web.Admin/Services/ElFinderViewModelService.cs
new file mode 100644
index 000000000..46b35b192
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Services/ElFinderViewModelService.cs
@@ -0,0 +1,127 @@
+using elFinder.Net.AspNetCore.Extensions;
+using elFinder.Net.AspNetCore.Helper;
+using elFinder.Net.Core;
+using Grand.Domain.Media;
+using Grand.SharedKernel.Extensions;
+using Grand.Web.Admin.Interfaces;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Routing;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Grand.Web.Admin.Services
+{
+ public partial class ElFinderViewModelService : IElFinderViewModelService
+ {
+ private readonly IDriver _driver;
+ private readonly IConnector _connector;
+ private readonly IHttpContextAccessor _httpContextAccessor;
+ private readonly LinkGenerator _linkGenerator;
+ private readonly MediaSettings _mediaSettings;
+
+ private readonly string _fullPathToUpload;
+ private readonly string _fullPathToThumbs;
+ private readonly string _urlThumb;
+ private readonly string _urlpathUploded;
+
+ public ElFinderViewModelService(
+ IDriver driver,
+ IConnector connector,
+ IHttpContextAccessor httpContextAccessor,
+ LinkGenerator linkGenerator,
+ MediaSettings mediaSettings)
+ {
+ _driver = driver;
+ _connector = connector;
+ _httpContextAccessor = httpContextAccessor;
+ _linkGenerator = linkGenerator;
+
+ _mediaSettings = mediaSettings;
+
+ _connector.Options.EnabledCommands = _mediaSettings.FileManagerEnabledCommands.Split(',').Select(x => x.Trim()).ToList();
+ _connector.Options.DisabledUICommands = _mediaSettings.FileManagerDisabledUICommands.Split(',').Select(x => x.Trim()).ToList();
+
+ _urlpathUploded = "/assets/images/uploaded/";
+
+ _fullPathToUpload = Path.Combine(CommonPath.WebRootPath, Path.Combine("assets", "images", "uploaded"));
+ if (!Directory.Exists(_fullPathToUpload))
+ Directory.CreateDirectory(_fullPathToUpload);
+
+ _fullPathToThumbs = Path.Combine(CommonPath.WebRootPath, Path.Combine("assets", "images", "thumbs"));
+ if (!Directory.Exists(_fullPathToThumbs))
+ Directory.CreateDirectory(_fullPathToThumbs);
+
+ _urlThumb = $"{_linkGenerator.GetPathByAction(_httpContextAccessor.HttpContext, "Thumb", "ElFinder", new { area = "Admin" })}/";
+ }
+
+ protected virtual bool NotAllowedExtensions(string extensions)
+ {
+ var allowedFileTypes = new List<string>();
+ if (string.IsNullOrEmpty(_mediaSettings.AllowedFileTypes))
+ allowedFileTypes = new List<string> { ".gif", ".jpg", ".jpeg", ".png", ".bmp", ".webp" };
+ else
+ allowedFileTypes = _mediaSettings.AllowedFileTypes.Split(',').Select(x => x.Trim().ToLowerInvariant()).ToList();
+
+ if (allowedFileTypes.Contains(extensions))
+ return false;
+
+ return true;
+ }
+
+ public virtual async Task SetupConnectorAsync()
+ {
+ var volume = new Volume(_driver,
+ _fullPathToUpload,
+ _fullPathToThumbs,
+ _urlpathUploded,
+ _urlThumb) {
+ Name = "Volume",
+ MaxUploadConnections = 3
+ };
+ volume.ObjectAttributes = new List<FilteredObjectAttribute>() {
+ new FilteredObjectAttribute()
+ {
+ FileFilter = (file) => {
+ return NotAllowedExtensions(file.Extension);
+ },
+ ObjectFilter = (obj) =>
+ {
+ var extensions = Path.GetExtension(obj.FullName);
+ if(!string.IsNullOrEmpty(extensions))
+ return NotAllowedExtensions(extensions);
+
+ return false;
+ },
+ ShowOnly = false, Access = false, Visible = false, Write = false, Read = false
+ },
+ };
+ _connector.AddVolume(volume);
+ await volume.Driver.SetupVolumeAsync(volume);
+ }
+
+ public virtual async Task<IActionResult> Connector()
+ {
+ await SetupConnectorAsync();
+ var cmd = ConnectorHelper.ParseCommand(_httpContextAccessor.HttpContext.Request);
+ if (cmd.Cmd == "upload" && cmd.Files != null && cmd.Files.Any())
+ cmd.Files = cmd.Files.Where(x => !NotAllowedExtensions(Path.GetExtension(x.FileName))).ToList();
+
+ var ccTokenSource = ConnectorHelper.RegisterCcTokenSource(_httpContextAccessor.HttpContext);
+ var conResult = await _connector.ProcessAsync(cmd, ccTokenSource);
+ var actionResult = conResult.ToActionResult(_httpContextAccessor.HttpContext);
+ return actionResult;
+ }
+
+ public virtual async Task<IActionResult> Thumbs(string id)
+ {
+ await SetupConnectorAsync();
+
+ var thumb = await _connector.GetThumbAsync(id, _httpContextAccessor.HttpContext.RequestAborted);
+ var actionResult = ConnectorHelper.GetThumbResult(thumb);
+ return actionResult;
+ }
+ }
+}
diff --git a/src/Web/Grand.Web.Admin/Startup/StartupApplication.cs b/src/Web/Grand.Web.Admin/Startup/StartupApplication.cs
index 867b32391..e9e92d2c4 100644
--- a/src/Web/Grand.Web.Admin/Startup/StartupApplication.cs
+++ b/src/Web/Grand.Web.Admin/Startup/StartupApplication.cs
@@ -58,6 +58,7 @@ namespace Grand.Web.Admin.Startup
services.AddScoped<IBrandViewModelService, BrandViewModelService>();
services.AddScoped<IProductViewModelService, ProductViewModelService>();
services.AddScoped<IPictureViewModelService, PictureViewModelService>();
+ services.AddScoped<IElFinderViewModelService, ElFinderViewModelService>();
}
public void Configure(IApplicationBuilder application, IWebHostEnvironment webHostEnvironment)
{