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 path handling

KrzysztofPajak0177c1dcc442
diff --git a/src/Business/Grand.Business.Storage/Services/PictureService.cs b/src/Business/Grand.Business.Storage/Services/PictureService.cs
index 0253ed5e6..bb239a4fe 100644
--- a/src/Business/Grand.Business.Storage/Services/PictureService.cs
+++ b/src/Business/Grand.Business.Storage/Services/PictureService.cs
@@ -159,7 +159,7 @@ public class PictureService : IPictureService
protected virtual string GetThumbUrl(string thumbFileName, string storeLocation = null)
{
storeLocation = !string.IsNullOrEmpty(storeLocation) ? storeLocation : "";
- return _mediaFileStore.Combine(storeLocation, CommonPath.Param, ImageThumbPath, thumbFileName);
+ return _mediaFileStore.Combine(storeLocation, ImageThumbPath, thumbFileName);
}
/// <summary>
diff --git a/src/Business/Grand.Business.Storage/Startup/StartupApplication.cs b/src/Business/Grand.Business.Storage/Startup/StartupApplication.cs
index 8eaf0115c..ea9e503db 100644
--- a/src/Business/Grand.Business.Storage/Startup/StartupApplication.cs
+++ b/src/Business/Grand.Business.Storage/Startup/StartupApplication.cs
@@ -42,7 +42,7 @@ public class StartupApplication : IStartupApplication
services.AddScoped<IMediaFileStore>(serviceProvider =>
{
var webHostEnvironment = serviceProvider.GetRequiredService<IWebHostEnvironment>();
- var param = configuration[CommonPath.Param];
+ var param = configuration[CommonPath.DirectoryParam];
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 636af49a7..b227e64c8 100644
--- a/src/Core/Grand.Infrastructure/StartupBase.cs
+++ b/src/Core/Grand.Infrastructure/StartupBase.cs
@@ -199,15 +199,7 @@ public static class StartupBase
RegisterConfigurations(services, configuration);
InitDatabase(services, configuration);
-
- //set base application path
- var param = configuration[CommonPath.Param];
- if (!string.IsNullOrEmpty(param))
- CommonPath.Param = param;
-
- CommonPath.WebHostEnvironment = hostingEnvironment.WebRootPath;
- CommonPath.BaseDirectory = hostingEnvironment.ContentRootPath;
-
+
var settingsPath = Path.Combine(hostingEnvironment.ContentRootPath, CommonPath.AppData, configuration["Directory"] ?? "", CommonPath.SettingsFile);
DataSettingsManager.Initialize(settingsPath);
diff --git a/src/Core/Grand.SharedKernel/Extensions/CommonPath.cs b/src/Core/Grand.SharedKernel/Extensions/CommonPath.cs
index 90e6ac912..f50cfb3a2 100644
--- a/src/Core/Grand.SharedKernel/Extensions/CommonPath.cs
+++ b/src/Core/Grand.SharedKernel/Extensions/CommonPath.cs
@@ -13,20 +13,4 @@ public static class CommonPath
public static string SettingsFile = "Settings.cfg";
public static string InstalledPluginsFile = "InstalledPlugins.cfg";
-
- /// <summary>
- /// Extra parameter to path
- /// </summary>
- public static string Param { get; set; } = "";
-
- /// <summary>
- /// Gets or sets application base path
- /// </summary>
- public static string BaseDirectory { get; set; }
-
- /// <summary>
- /// Gets or sets web application content files
- /// </summary>
- public static string WebHostEnvironment { get; set; }
-
}
\ No newline at end of file
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Handlers/UpdateProductReviewTotalsCommandHandlerTest.cs b/src/Tests/Grand.Business.Catalog.Tests/Handlers/UpdateProductReviewTotalsCommandHandlerTest.cs
index d70f33fe7..8262b7b17 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Handlers/UpdateProductReviewTotalsCommandHandlerTest.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Handlers/UpdateProductReviewTotalsCommandHandlerTest.cs
@@ -26,8 +26,6 @@ public class UpdateProductReviewTotalsCommandHandlerTest
[TestInitialize]
public void Init()
{
- CommonPath.BaseDirectory = "";
-
var reviews = new List<ProductReview> {
new() { Id = "1", ReplyText = "text1" },
new() { Id = "2", ReplyText = "text2" }
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Brands/BrandServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Brands/BrandServiceTests.cs
index 24c7605a8..ba6f4c9a9 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Brands/BrandServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Brands/BrandServiceTests.cs
@@ -27,8 +27,6 @@ public class BrandServiceTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_repository = new MongoDBRepositoryTest<Brand>();
_workContextMock = new Mock<IWorkContext>();
_workContextMock.Setup(c => c.CurrentStore).Returns(() => new Store { Id = "" });
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/CategoryServiceDbTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/CategoryServiceDbTests.cs
index 4893a324b..28e9bd408 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/CategoryServiceDbTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/CategoryServiceDbTests.cs
@@ -31,8 +31,6 @@ public class CategoryServiceDbTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_categoryRepository = new MongoDBRepositoryTest<Category>();
_workContextMock = new Mock<IWorkContext>();
_workContextMock.Setup(c => c.CurrentStore).Returns(() => new Store { Id = "" });
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/ProductCategoryServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/ProductCategoryServiceTests.cs
index f363180f3..934fe004c 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/ProductCategoryServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/ProductCategoryServiceTests.cs
@@ -27,8 +27,6 @@ public class ProductCategoryServiceTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_repository = new MongoDBRepositoryTest<Product>();
_workContextMock = new Mock<IWorkContext>();
_workContextMock.Setup(c => c.CurrentStore).Returns(() => new Store { Id = "" });
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Collections/ProductCollectionServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Collections/ProductCollectionServiceTests.cs
index f6a1dbc51..cf35316b7 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Collections/ProductCollectionServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Collections/ProductCollectionServiceTests.cs
@@ -27,8 +27,6 @@ public class ProductCollectionServiceTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_repository = new MongoDBRepositoryTest<Product>();
_workContextMock = new Mock<IWorkContext>();
_workContextMock.Setup(c => c.CurrentStore).Returns(() => new Store { Id = "" });
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs
index 162c1e2d1..808b0c1a9 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs
@@ -44,8 +44,6 @@ public class DiscountServiceTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_repository = new MongoDBRepositoryTest<Discount>();
_discountCouponRepository = new MongoDBRepositoryTest<DiscountCoupon>();
_discountUsageHistoryRepository = new MongoDBRepositoryTest<DiscountUsageHistory>();
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/AuctionServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/AuctionServiceTests.cs
index cfe78acad..447056826 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/AuctionServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/AuctionServiceTests.cs
@@ -29,8 +29,6 @@ public class AuctionServiceTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_repository = new MongoDBRepositoryTest<Bid>();
_productrepository = new MongoDBRepositoryTest<Product>();
_workContextMock = new Mock<IWorkContext>();
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs
index 204f05c34..46d9970c9 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/InventoryManageServiceTests.cs
@@ -33,10 +33,6 @@ public class InventoryManageServiceTests
[TestInitialize]
public void Init()
{
- CommonPath.BaseDirectory = "";
-
- CommonPath.BaseDirectory = "";
-
_repository = new MongoDBRepositoryTest<Product>();
_repositoryInventoryJournal = new MongoDBRepositoryTest<InventoryJournal>();
_workContextMock = new Mock<IWorkContext>();
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/OutOfStockSubscriptionServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/OutOfStockSubscriptionServiceTests.cs
index 47826741d..fe8386c11 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/OutOfStockSubscriptionServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/OutOfStockSubscriptionServiceTests.cs
@@ -21,8 +21,6 @@ public class OutOfStockSubscriptionServiceTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_repository = new MongoDBRepositoryTest<OutOfStockSubscription>();
_mediatorMock = new Mock<IMediator>();
_mediatorMock.Setup(x => x.Send(It.IsAny<SendNotificationsToSubscribersCommand>(), default))
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeFormatterTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeFormatterTests.cs
index 85d0d05eb..5cb2dd7a4 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeFormatterTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeFormatterTests.cs
@@ -35,8 +35,6 @@ public class ProductAttributeFormatterTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_workContextMock = new Mock<IWorkContext>();
_workContextMock.Setup(c => c.CurrentStore).Returns(() => new Store { Id = "" });
_workContextMock.Setup(c => c.CurrentCustomer).Returns(() => new Customer());
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeServiceTests.cs
index be0312181..00c279ec6 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductAttributeServiceTests.cs
@@ -24,8 +24,6 @@ public class ProductAttributeServiceTests
[TestInitialize]
public void Init()
{
- CommonPath.BaseDirectory = "";
-
_repository = new MongoDBRepositoryTest<Product>();
_repositoryproductAttribute = new MongoDBRepositoryTest<ProductAttribute>();
_mediatorMock = new Mock<IMediator>();
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductServiceTests.cs
index 8b36e3d6f..74462ff25 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductServiceTests.cs
@@ -32,8 +32,6 @@ public class ProductServiceTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_productRepository = new MongoDBRepositoryTest<Product>();
_workContextMock = new Mock<IWorkContext>();
_workContextMock.Setup(c => c.CurrentStore).Returns(() => new Store { Id = "" });
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductTagServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductTagServiceTests.cs
index 56fecd1e1..0546820c5 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductTagServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Products/ProductTagServiceTests.cs
@@ -24,8 +24,6 @@ public class ProductTagServiceTests
[TestInitialize]
public void InitializeTests()
{
- CommonPath.BaseDirectory = "";
-
_repositoryProductTag = new MongoDBRepositoryTest<ProductTag>();
_productRepository = new MongoDBRepositoryTest<Product>();
_mediatorMock = new Mock<IMediator>();
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Tax/TaxServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Tax/TaxServiceTests.cs
index ae74b2fbf..8f4e9f9f5 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Tax/TaxServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Tax/TaxServiceTests.cs
@@ -34,8 +34,6 @@ public class TaxServiceTests
[TestInitialize]
public void TestInitialize()
{
- CommonPath.BaseDirectory = "";
-
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(x => x.GetService(typeof(FixedRateTestTaxProvider)))
.Returns(new FixedRateTestTaxProvider());
diff --git a/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs b/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs
index dec9a7729..84ba18828 100644
--- a/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs
+++ b/src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs
@@ -10,7 +10,7 @@ public class LiteDbRepositoryTests
[TestInitialize]
public void Init()
{
- CommonPath.BaseDirectory = "";
+
}
[TestMethod]
diff --git a/src/Tests/Grand.Data.Tests/MongoDb/MongoRepositoryTests.cs b/src/Tests/Grand.Data.Tests/MongoDb/MongoRepositoryTests.cs
index f04896502..52960bb86 100644
--- a/src/Tests/Grand.Data.Tests/MongoDb/MongoRepositoryTests.cs
+++ b/src/Tests/Grand.Data.Tests/MongoDb/MongoRepositoryTests.cs
@@ -13,8 +13,6 @@ public class MongoRepositoryTests
public void Init()
{
_myRepository = new MongoDBRepositoryTest<SampleCollection>();
-
- CommonPath.BaseDirectory = "";
}
[TestMethod]
diff --git a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs
index b89fd0b83..ed4435870 100644
--- a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs
@@ -767,7 +767,7 @@ public class SettingController(
{
//edit js file needed by firebase
var filename = "firebase-messaging-sw.js";
- var filePath = Path.Combine(webHostEnvironment.WebRootPath, configuration[CommonPath.Param] ?? "", filename);
+ var filePath = Path.Combine(webHostEnvironment.WebRootPath, configuration[CommonPath.DirectoryParam] ?? "", filename);
if (System.IO.File.Exists(filePath))
{
var lines = System.IO.File.ReadAllLines(filePath);
diff --git a/src/Web/Grand.Web.Admin/Services/ElFinderViewModelService.cs b/src/Web/Grand.Web.Admin/Services/ElFinderViewModelService.cs
index de0520367..3dacb332f 100644
--- a/src/Web/Grand.Web.Admin/Services/ElFinderViewModelService.cs
+++ b/src/Web/Grand.Web.Admin/Services/ElFinderViewModelService.cs
@@ -8,6 +8,8 @@ using Grand.Web.Admin.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
namespace Grand.Web.Admin.Services;
@@ -59,8 +61,9 @@ public class ElFinderViewModelService : IElFinderViewModelService
}
_fullPathToUpload = uploaded.PhysicalPath;
+ var configuration = httpContextAccessor.HttpContext.RequestServices.GetRequiredService<IConfiguration>();
- _urlpathUploded = (string.IsNullOrEmpty(CommonPath.Param) ? "/" : $"/{CommonPath.Param}/")
+ _urlpathUploded = (string.IsNullOrEmpty(configuration[CommonPath.DirectoryParam]) ? "/" : $"/{configuration[CommonPath.DirectoryParam]}/")
+ uploaded.Path.Replace("\\", "/") + "/";
var thumbs = _mediaFileStore.GetDirectoryInfo(ImageThumbPath);