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.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Minor refactoring - get content type
KrzysztofPajak6a8e3584a4e9
diff --git a/src/Web/Grand.Web.Admin/Controllers/PictureController.cs b/src/Web/Grand.Web.Admin/Controllers/PictureController.csindex 19c707860..e5afb01ab 100644--- a/src/Web/Grand.Web.Admin/Controllers/PictureController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/PictureController.cs@@ -6,6 +6,7 @@ using Grand.Domain.Common;using Grand.Domain.Media;using Grand.Web.Common.Security.Authorization;using Microsoft.AspNetCore.Mvc;+using Microsoft.AspNetCore.StaticFiles;using System;using System.Collections.Generic;using System.IO;@@ -77,7 +78,7 @@ namespace Grand.Web.Admin.Controllersvar contentType = httpPostedFile.ContentType;var fileExtension = Path.GetExtension(fileName);- if (!String.IsNullOrEmpty(fileExtension))+ if (!string.IsNullOrEmpty(fileExtension))fileExtension = fileExtension.ToLowerInvariant();if (!GetAllowedFileTypes().Contains(fileExtension))@@ -89,38 +90,9 @@ namespace Grand.Web.Admin.ControllersimageUrl = ""});}-- //contentType is not always available- //that's why we manually update it here- //http://www.sfsu.edu/training/mimetype.htm- if (String.IsNullOrEmpty(contentType))+ if (string.IsNullOrEmpty(contentType)){- switch (fileExtension)- {- case ".bmp":- contentType = "image/bmp";- break;- case ".gif":- contentType = "image/gif";- break;- case ".jpeg":- case ".jpg":- case ".jpe":- case ".jfif":- case ".pjpeg":- case ".pjp":- contentType = "image/jpeg";- break;- case ".png":- contentType = "image/png";- break;- case ".tiff":- case ".tif":- contentType = "image/tiff";- break;- default:- break;- }+ _ = new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);}var fileBinary = httpPostedFile.GetDownloadBits();@@ -156,7 +128,7 @@ namespace Grand.Web.Admin.Controllersvar qqFileNameParameter = "qqfilename";var fileName = httpPostedFile.FileName;- if (String.IsNullOrEmpty(fileName) && form.ContainsKey(qqFileNameParameter))+ if (string.IsNullOrEmpty(fileName) && form.ContainsKey(qqFileNameParameter))fileName = form[qqFileNameParameter].ToString();fileName = Path.GetFileName(fileName);@@ -164,7 +136,7 @@ namespace Grand.Web.Admin.Controllersvar contentType = httpPostedFile.ContentType;var fileExtension = Path.GetExtension(fileName);- if (!String.IsNullOrEmpty(fileExtension))+ if (!string.IsNullOrEmpty(fileExtension))fileExtension = fileExtension.ToLowerInvariant();if (!GetAllowedFileTypes().Contains(fileExtension))@@ -176,39 +148,11 @@ namespace Grand.Web.Admin.Controllers});}- //contentType is not always available- //that's why we manually update it here- //http://www.sfsu.edu/training/mimetype.htm- if (String.IsNullOrEmpty(contentType))+ if (string.IsNullOrEmpty(contentType)){- switch (fileExtension)- {- case ".bmp":- contentType = "image/bmp";- break;- case ".gif":- contentType = "image/gif";- break;- case ".jpeg":- case ".jpg":- case ".jpe":- case ".jfif":- case ".pjpeg":- case ".pjp":- contentType = "image/jpeg";- break;- case ".png":- contentType = "image/png";- break;- case ".tiff":- case ".tif":- contentType = "image/tiff";- break;- default:- break;- }+ _ = new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);}- if (String.IsNullOrEmpty(contentType))+ if (string.IsNullOrEmpty(contentType)){return Json(new{diff --git a/src/Web/Grand.Web.Admin/Controllers/ProductController.cs b/src/Web/Grand.Web.Admin/Controllers/ProductController.csindex 71809f4ea..ba777d8bf 100644--- a/src/Web/Grand.Web.Admin/Controllers/ProductController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/ProductController.cs@@ -27,6 +27,7 @@ using MediatR;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;using Microsoft.AspNetCore.Mvc.Rendering;+using Microsoft.AspNetCore.StaticFiles;using System;using System.Collections.Generic;using System.IO;@@ -1244,7 +1245,7 @@ namespace Grand.Web.Admin.Controllersif (string.IsNullOrEmpty(contentType)){- contentType = GetContentType(fileExtension);+ _ = new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType);}if (GetAllowedFileTypes(mediaSettings).Contains(fileExtension))@@ -1273,31 +1274,6 @@ namespace Grand.Web.Admin.Controllerselsereturn mediaSettings.AllowedFileTypes.Split(',');}- protected virtual string GetContentType(string fileExtension)- {- switch (fileExtension)- {- case ".bmp":- return "image/bmp";- case ".gif":- return "image/gif";- case ".jpeg":- case ".jpg":- case ".jpe":- case ".jfif":- case ".pjpeg":- case ".pjp":- return "image/jpeg";- case ".png":- return "image/png";- case ".tiff":- case ".tif":- return "image/tiff";- default:- return "";- }- }-[PermissionAuthorizeAction(PermissionActionName.Preview)][HttpPost]