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.

Double code removal - GetAllowedFileTypes

KrzysztofPajakb67040bdeb90
diff --git a/src/Web/Grand.Web.Admin/Controllers/PictureController.cs b/src/Web/Grand.Web.Admin/Controllers/PictureController.cs
index 1dd3a3dab..b2dfc36e2 100644
--- a/src/Web/Grand.Web.Admin/Controllers/PictureController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/PictureController.cs
@@ -4,11 +4,11 @@ using Grand.Business.Storage.Extensions;
using Grand.Business.Storage.Interfaces;
using Grand.Domain.Common;
using Grand.Domain.Media;
+using Grand.Web.Admin.Extensions;
using Grand.Web.Common.Security.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using System;
-using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -36,15 +36,6 @@ namespace Grand.Web.Admin.Controllers
_mediaSettings = mediaSettings;
}
- [NonAction]
- protected virtual IList<string> GetAllowedFileTypes()
- {
- if (string.IsNullOrEmpty(_mediaSettings.AllowedFileTypes))
- return new List<string> { ".gif", ".jpg", ".jpeg", ".png", ".bmp", ".webp" };
- else
- return _mediaSettings.AllowedFileTypes.Split(',').Select(x => x.Trim().ToLowerInvariant()).ToList();
- }
-
[HttpPost]
[IgnoreAntiforgeryToken]
public virtual async Task<IActionResult> AsyncUpload(Reference reference = Reference.None, string objectId = "")
@@ -81,7 +72,7 @@ namespace Grand.Web.Admin.Controllers
if (!string.IsNullOrEmpty(fileExtension))
fileExtension = fileExtension.ToLowerInvariant();
- if (!GetAllowedFileTypes().Contains(fileExtension))
+ if (!FileExtensions.GetAllowedMediaFileTypes(_mediaSettings.AllowedFileTypes).Contains(fileExtension))
{
return Json(new
{
@@ -139,7 +130,7 @@ namespace Grand.Web.Admin.Controllers
if (!string.IsNullOrEmpty(fileExtension))
fileExtension = fileExtension.ToLowerInvariant();
- if (!GetAllowedFileTypes().Contains(fileExtension))
+ if (!FileExtensions.GetAllowedMediaFileTypes(_mediaSettings.AllowedFileTypes).Contains(fileExtension))
{
return Json(new
{
diff --git a/src/Web/Grand.Web.Admin/Controllers/ProductController.cs b/src/Web/Grand.Web.Admin/Controllers/ProductController.cs
index 7fd75afce..3614e8210 100644
--- a/src/Web/Grand.Web.Admin/Controllers/ProductController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/ProductController.cs
@@ -1248,7 +1248,7 @@ namespace Grand.Web.Admin.Controllers
_ = new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);
}
- if (GetAllowedFileTypes(mediaSettings).Contains(fileExtension))
+ if (FileExtensions.GetAllowedMediaFileTypes(mediaSettings.AllowedFileTypes).Contains(fileExtension))
{
var fileBinary = file.GetDownloadBits();
//insert picture
@@ -1267,14 +1267,6 @@ namespace Grand.Web.Admin.Controllers
return Json(new { success = values.Any(), data = values });
}
- protected virtual IList<string> GetAllowedFileTypes(MediaSettings mediaSettings)
- {
- if (string.IsNullOrEmpty(mediaSettings.AllowedFileTypes))
- return new List<string> { ".gif", ".jpg", ".jpeg", ".png", ".bmp", ".webp" };
- else
- return mediaSettings.AllowedFileTypes.Split(',').Select(x => x.Trim().ToLowerInvariant()).ToList();
- }
-
[PermissionAuthorizeAction(PermissionActionName.Preview)]
[HttpPost]
public async Task<IActionResult> ProductPictureList(DataSourceRequest command, string productId)
diff --git a/src/Web/Grand.Web.Admin/Extensions/AllowedFileExtensions.cs b/src/Web/Grand.Web.Admin/Extensions/AllowedFileExtensions.cs
new file mode 100644
index 000000000..9ef4ee384
--- /dev/null
+++ b/src/Web/Grand.Web.Admin/Extensions/AllowedFileExtensions.cs
@@ -0,0 +1,16 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Grand.Web.Admin.Extensions
+{
+ public static class FileExtensions
+ {
+ public static IList<string> GetAllowedMediaFileTypes(string allowedFileTypes)
+ {
+ if (string.IsNullOrEmpty(allowedFileTypes))
+ return new List<string> { ".gif", ".jpg", ".jpeg", ".png", ".bmp", ".webp" };
+ else
+ return allowedFileTypes.Split(',').Select(x => x.Trim().ToLowerInvariant()).ToList();
+ }
+ }
+}