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.

Refactor to use IWebHostEnvironment for path handling

KrzysztofPajakbeaa143f8980
diff --git a/src/Business/Grand.Business.Common/Services/Pdf/HtmlToPdfService.cs b/src/Business/Grand.Business.Common/Services/Pdf/HtmlToPdfService.cs
index d15ea74c9..6eec9a2b3 100644
--- a/src/Business/Grand.Business.Common/Services/Pdf/HtmlToPdfService.cs
+++ b/src/Business/Grand.Business.Common/Services/Pdf/HtmlToPdfService.cs
@@ -5,6 +5,8 @@ using Grand.Domain.Media;
using Grand.Domain.Orders;
using Grand.Domain.Shipping;
using Grand.SharedKernel.Extensions;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Configuration;
using Scryber;
using Scryber.Components;
using Path = System.IO.Path;
@@ -21,16 +23,17 @@ public class HtmlToPdfService : IPdfService
private readonly IRepository<Download> _downloadRepository;
private readonly ILanguageService _languageService;
private readonly IStoreFilesContext _storeFilesContext;
-
private readonly IViewRenderService _viewRenderService;
+ private readonly IWebHostEnvironment _webHostEnvironment;
public HtmlToPdfService(IViewRenderService viewRenderService, IRepository<Download> downloadRepository,
- ILanguageService languageService, IStoreFilesContext storeFilesContext)
+ ILanguageService languageService, IStoreFilesContext storeFilesContext, IWebHostEnvironment webHostEnvironment)
{
_viewRenderService = viewRenderService;
_languageService = languageService;
_downloadRepository = downloadRepository;
_storeFilesContext = storeFilesContext;
+ _webHostEnvironment = webHostEnvironment;
}
public async Task PrintOrdersToPdf(Stream stream, IList<Order> orders, string languageId = "",
@@ -52,7 +55,7 @@ public class HtmlToPdfService : IPdfService
var fileName = $"order_{order.OrderGuid}_{CommonHelper.GenerateRandomDigitCode(4)}.pdf";
- var dir = CommonPath.WebMapPath("assets/files/exportimport");
+ var dir = Path.Combine(_webHostEnvironment.WebRootPath, "assets/files/exportimport");
if (dir == null)
throw new ArgumentNullException(nameof(dir));
diff --git a/src/Business/Grand.Business.Storage/Startup/StartupApplication.cs b/src/Business/Grand.Business.Storage/Startup/StartupApplication.cs
index 458544de9..8eaf0115c 100644
--- a/src/Business/Grand.Business.Storage/Startup/StartupApplication.cs
+++ b/src/Business/Grand.Business.Storage/Startup/StartupApplication.cs
@@ -1,4 +1,5 @@
-using Grand.Business.Core.Interfaces.Storage;
+using DotLiquid.Tags;
+using Grand.Business.Core.Interfaces.Storage;
using Grand.Business.Storage.Services;
using Grand.Infrastructure;
using Grand.Infrastructure.Configuration;
@@ -40,7 +41,9 @@ public class StartupApplication : IStartupApplication
services.AddScoped<IMediaFileStore>(serviceProvider =>
{
- var fileStore = new FileSystemStore(CommonPath.WebRootPath);
+ var webHostEnvironment = serviceProvider.GetRequiredService<IWebHostEnvironment>();
+ var param = configuration[CommonPath.Param];
+ var fileStore = new FileSystemStore(Path.Combine(webHostEnvironment.WebRootPath, param ?? ""));
return new DefaultMediaFileStore(fileStore);
});
}
diff --git a/src/Core/Grand.Infrastructure/StartupBase.cs b/src/Core/Grand.Infrastructure/StartupBase.cs
index 7659621fa..b1773f49d 100644
--- a/src/Core/Grand.Infrastructure/StartupBase.cs
+++ b/src/Core/Grand.Infrastructure/StartupBase.cs
@@ -201,7 +201,7 @@ public static class StartupBase
InitDatabase(services, configuration);
//set base application path
- var param = configuration["Directory"];
+ var param = configuration[CommonPath.Param];
if (!string.IsNullOrEmpty(param))
CommonPath.Param = param;
diff --git a/src/Core/Grand.SharedKernel/Extensions/CommonPath.cs b/src/Core/Grand.SharedKernel/Extensions/CommonPath.cs
index 30ae7dde0..d9b5cfd73 100644
--- a/src/Core/Grand.SharedKernel/Extensions/CommonPath.cs
+++ b/src/Core/Grand.SharedKernel/Extensions/CommonPath.cs
@@ -24,11 +24,6 @@ public static class CommonPath
/// </summary>
public static string BaseDirectory { get; set; }
- /// <summary>
- /// Gets or sets web application content files
- /// </summary>
- public static string WebRootPath => Path.Combine(WebHostEnvironment, Param);
-
/// <summary>
/// Gets or sets web application content files
/// </summary>
@@ -45,16 +40,6 @@ public static class CommonPath
return Path.Combine(BaseDirectory, path);
}
- /// <summary>
- /// Maps a virtual path to a physical disk path (for tenants).
- /// </summary>
- /// <param name="path">The path to map. E.g. "~/bin"</param>
- /// <returns>The physical path. E.g. "c:\inetpub\wwwroot\"</returns>
- public static string WebMapPath(string path)
- {
- path = path.Replace("~/", "").TrimStart('/');
- return Path.Combine(WebRootPath, path);
- }
/// <summary>
/// Maps a virtual path to a physical disk path. (not for tenants)
diff --git a/src/Tests/Grand.SharedKernel.Tests/Extensions/CommonPathTests.cs b/src/Tests/Grand.SharedKernel.Tests/Extensions/CommonPathTests.cs
deleted file mode 100644
index 11461a6c7..000000000
--- a/src/Tests/Grand.SharedKernel.Tests/Extensions/CommonPathTests.cs
+++ /dev/null
@@ -1,36 +0,0 @@
-using Grand.SharedKernel.Extensions;
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-
-namespace Grand.SharedKernel.Tests.Extensions;
-
-[TestClass]
-public class CommonPathTests
-{
- public CommonPathTests()
- {
- CommonPath.BaseDirectory = "base";
- CommonPath.WebHostEnvironment = "www";
- }
-
- [TestMethod]
- public void MapPathTest()
- {
- var path = CommonPath.MapPath("path");
- Assert.AreEqual(Path.Combine("base", "path"), path);
- }
-
-
- [TestMethod]
- public void WebMapPathTest()
- {
- var path = CommonPath.WebMapPath("path");
- Assert.AreEqual(Path.Combine("www", "path"), path);
- }
-
- [TestMethod]
- public void WebHostMapPathTest()
- {
- var path = CommonPath.WebHostMapPath("path");
- Assert.AreEqual(Path.Combine("www", "path"), path);
- }
-}
\ No newline at end of file
diff --git a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs
index 5d7af64e6..b89fd0b83 100644
--- a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs
@@ -36,8 +36,10 @@ using Grand.Web.Common.Security.Authorization;
using Grand.Web.Common.Security.Captcha;
using Grand.Web.Common.Themes;
using MediatR;
+using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
+using Microsoft.Extensions.Configuration;
using OperatingSystem = Grand.Infrastructure.OperatingSystem;
namespace Grand.Web.Admin.Controllers;
@@ -743,7 +745,7 @@ public class SettingController(
}
[HttpPost]
- public async Task<IActionResult> PushNotifications(PushNotificationsSettingsModel model)
+ public async Task<IActionResult> PushNotifications(PushNotificationsSettingsModel model, [FromServices] IConfiguration configuration, [FromServices] IWebHostEnvironment webHostEnvironment)
{
var storeScope = await GetActiveStore();
var settings = settingService.LoadSetting<PushNotificationsSettings>(storeScope);
@@ -755,21 +757,20 @@ public class SettingController(
await ClearCache();
//save to file
- SavePushNotificationsToFile(model);
+ SavePushNotificationsToFile(model, configuration, webHostEnvironment);
Success(translationService.GetResource("Admin.Configuration.Updated"));
return await PushNotifications();
}
- private void SavePushNotificationsToFile(PushNotificationsSettingsModel model)
+ private void SavePushNotificationsToFile(PushNotificationsSettingsModel model, IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
{
//edit js file needed by firebase
var filename = "firebase-messaging-sw.js";
- var oryginalFilePath = CommonPath.WebHostMapPath(filename);
- var savedFilePath = CommonPath.WebMapPath(filename);
- if (System.IO.File.Exists(oryginalFilePath))
+ var filePath = Path.Combine(webHostEnvironment.WebRootPath, configuration[CommonPath.Param] ?? "", filename);
+ if (System.IO.File.Exists(filePath))
{
- var lines = System.IO.File.ReadAllLines(oryginalFilePath);
+ var lines = System.IO.File.ReadAllLines(filePath);
var i = 0;
foreach (var line in lines)
{
@@ -783,11 +784,11 @@ public class SettingController(
i++;
}
- System.IO.File.WriteAllLines(savedFilePath, lines);
+ System.IO.File.WriteAllLines(filePath, lines);
}
else
{
- throw new ArgumentNullException($"{oryginalFilePath} not exist");
+ throw new ArgumentNullException($"{filePath} not exist");
}
}