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.

Allow saving a logo picture to the database (picture entity). #165

KrzysztofPajakc7e1c1738a28
diff --git a/src/Business/Grand.Business.System/Services/Installation/InstallDataBrands.cs b/src/Business/Grand.Business.System/Services/Installation/InstallDataBrands.cs
index 7158dcbc8..b7449dcae 100644
--- a/src/Business/Grand.Business.System/Services/Installation/InstallDataBrands.cs
+++ b/src/Business/Grand.Business.System/Services/Installation/InstallDataBrands.cs
@@ -2,7 +2,6 @@
using Grand.Business.Storage.Interfaces;
using Grand.Business.System.Interfaces.Installation;
using Grand.Domain.Catalog;
-using Grand.Domain.Directory;
using Grand.Domain.Seo;
using Microsoft.Extensions.DependencyInjection;
using System;
@@ -28,8 +27,7 @@ namespace Grand.Business.System.Services.Installation
throw new Exception("Brand layout cannot be loaded");
var allbrands = new List<Brand>();
- var brandXiaomi = new Brand
- {
+ var brandXiaomi = new Brand {
Name = "Xiaomi",
BrandLayoutId = brandLayoutInGridAndLines.Id,
PageSize = 6,
@@ -45,8 +43,7 @@ namespace Grand.Business.System.Services.Installation
allbrands.Add(brandXiaomi);
- var brandDell = new Brand
- {
+ var brandDell = new Brand {
Name = "Dell",
BrandLayoutId = brandLayoutInGridAndLines.Id,
PageSize = 6,
@@ -63,8 +60,7 @@ namespace Grand.Business.System.Services.Installation
allbrands.Add(brandDell);
- var brandAdidas = new Brand
- {
+ var brandAdidas = new Brand {
Name = "Adidas",
BrandLayoutId = brandLayoutInGridAndLines.Id,
PageSize = 6,
@@ -83,8 +79,7 @@ namespace Grand.Business.System.Services.Installation
foreach (var brand in allbrands)
{
brand.SeName = SeoExtensions.GenerateSlug(brand.Name, false, false, true);
- await _entityUrlRepository.InsertAsync(new EntityUrl
- {
+ await _entityUrlRepository.InsertAsync(new EntityUrl {
EntityId = brand.Id,
EntityName = "Brand",
LanguageId = "",
diff --git a/src/Business/Grand.Business.System/Services/Installation/InstallDataSettings.cs b/src/Business/Grand.Business.System/Services/Installation/InstallDataSettings.cs
index 01ef8d608..7f1b1d131 100644
--- a/src/Business/Grand.Business.System/Services/Installation/InstallDataSettings.cs
+++ b/src/Business/Grand.Business.System/Services/Installation/InstallDataSettings.cs
@@ -1,5 +1,6 @@
using Grand.Business.Common.Interfaces.Configuration;
using Grand.Business.Common.Interfaces.Directory;
+using Grand.Business.Storage.Interfaces;
using Grand.Business.System.Interfaces.Installation;
using Grand.Domain.AdminSearch;
using Grand.Domain.Blogs;
@@ -25,6 +26,7 @@ using Grand.Domain.Vendors;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -35,6 +37,11 @@ namespace Grand.Business.System.Services.Installation
protected virtual async Task InstallSettings(bool installSampleData)
{
var _settingService = _serviceProvider.GetRequiredService<ISettingService>();
+ var _pictureService = _serviceProvider.GetRequiredService<IPictureService>();
+
+ var path = Path.Combine(_hostingEnvironment.WebRootPath, "logo.png");
+
+ var storePictureId = (await _pictureService.InsertPicture(File.ReadAllBytes(path), "image/png", "Logo")).Id;
await _settingService.SaveSetting(new MenuItemSettings {
DisplayHomePageMenu = !installSampleData,
@@ -319,11 +326,11 @@ namespace Grand.Business.System.Services.Installation
});
await _settingService.SaveSetting(new StoreInformationSettings {
+ LogoPictureId = storePictureId,
StoreClosed = false,
DefaultStoreTheme = "Default",
AllowCustomerToSelectTheme = false,
DisplayCookieInformation = false,
- LogoPicture = "logo.png",
FacebookLink = "https://www.facebook.com/grandnodecom",
TwitterLink = "https://twitter.com/grandnode",
YoutubeLink = "http://www.youtube.com/user/grandnode",
diff --git a/src/Business/Grand.Business.System/Services/Migrations/1.1/MigrationUpdateLogoSettings.cs b/src/Business/Grand.Business.System/Services/Migrations/1.1/MigrationUpdateLogoSettings.cs
new file mode 100644
index 000000000..3c423c3e4
--- /dev/null
+++ b/src/Business/Grand.Business.System/Services/Migrations/1.1/MigrationUpdateLogoSettings.cs
@@ -0,0 +1,76 @@
+using Grand.Business.Common.Interfaces.Configuration;
+using Grand.Business.Common.Interfaces.Logging;
+using Grand.Business.Storage.Interfaces;
+using Grand.Domain.Data;
+using Grand.Domain.Stores;
+using Grand.Infrastructure.Migrations;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.StaticFiles;
+using Microsoft.Extensions.DependencyInjection;
+using System;
+using System.IO;
+using System.Linq;
+using System.Text.Json;
+
+namespace Grand.Business.System.Services.Migrations._1._1
+{
+ public class MigrationUpdateLogoSettings : IMigration
+ {
+
+ public int Priority => 0;
+
+ public DbVersion Version => new(1, 1);
+
+ public Guid Identity => new("82A5E26A-7861-4C72-9D5E-AF95477CE1F7");
+
+ public string Name => "Update logo settings - StoreInformationSettings";
+
+ /// <summary>
+ /// Upgrade process
+ /// </summary>
+ /// <param name="database"></param>
+ /// <param name="serviceProvider"></param>
+ /// <returns></returns>
+ public bool UpgradeProcess(IDatabaseContext database, IServiceProvider serviceProvider)
+ {
+ var repository = serviceProvider.GetRequiredService<IRepository<Domain.Configuration.Setting>>();
+ var storeSettings = repository.Table.Where(x => x.Name == "storeinformationsettings");
+ var settingService = serviceProvider.GetRequiredService<ISettingService>();
+ var logService = serviceProvider.GetRequiredService<ILogger>();
+ var hostingEnvironment = serviceProvider.GetRequiredService<IWebHostEnvironment>();
+
+ //update logo settings
+ try
+ {
+ foreach (var storeSetting in storeSettings)
+ {
+ var metadata = storeSetting.Metadata;
+ var settingsLogo = JsonSerializer.Deserialize<SettingsLogo>(metadata);
+ if (settingsLogo != null && !string.IsNullOrEmpty(settingsLogo.LogoPicture))
+ {
+ var pictureService = serviceProvider.GetRequiredService<IPictureService>();
+ new FileExtensionContentTypeProvider().TryGetContentType(settingsLogo.LogoPicture, out var mimeType);
+
+ var path = Path.Combine(hostingEnvironment.WebRootPath, settingsLogo.LogoPicture);
+
+ var pictureId = pictureService.InsertPicture(File.ReadAllBytes(path), mimeType, "").GetAwaiter().GetResult();
+ var storeInformationSettings = settingService.LoadSetting<StoreInformationSettings>(storeSetting.StoreId);
+ storeInformationSettings.LogoPictureId = pictureId?.Id;
+ settingService.SaveSetting<StoreInformationSettings>(storeInformationSettings, storeSetting.StoreId).GetAwaiter().GetResult();
+ }
+ }
+ }
+ catch(Exception ex)
+ {
+ logService.InsertLog(Domain.Logging.LogLevel.Error, "UpgradeProcess - MigrationUpdateSettings", ex.Message).GetAwaiter().GetResult();
+ }
+ return true;
+ }
+
+ private class SettingsLogo
+ {
+ public string LogoPicture { get; set; }
+ }
+
+ }
+}
diff --git a/src/Core/Grand.Domain/Stores/StoreInformationSettings.cs b/src/Core/Grand.Domain/Stores/StoreInformationSettings.cs
index 4ade2ece1..c216483e7 100644
--- a/src/Core/Grand.Domain/Stores/StoreInformationSettings.cs
+++ b/src/Core/Grand.Domain/Stores/StoreInformationSettings.cs
@@ -27,7 +27,8 @@ namespace Grand.Domain.Stores
/// <summary>
/// Gets or sets a logo
/// </summary>
- public string LogoPicture { get; set; }
+ public string LogoPictureId { get; set; }
+
/// <summary>
/// Gets or sets a value indicating whether we should display info about the cookie
/// </summary>
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/GeneralCommon.TabStoreInformationSettings.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/GeneralCommon.TabStoreInformationSettings.cshtml
index d1c4bf0e2..cc88617b2 100644
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/GeneralCommon.TabStoreInformationSettings.cshtml
+++ b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Setting/GeneralCommon.TabStoreInformationSettings.cshtml
@@ -119,11 +119,11 @@
</div>
<div class="form-group">
<div class="col-8 col-md-4 col-sm-4 text-right">
- <admin-label asp-for="StoreInformationSettings.LogoPicture" class="control-label" />
+ <admin-label asp-for="StoreInformationSettings.LogoPictureId" class="control-label" />
</div>
<div class="col-4 col-md-8 col-sm-8">
- <admin-input asp-for="StoreInformationSettings.LogoPicture" />
- <span asp-validation-for="StoreInformationSettings.LogoPicture"></span>
+ <admin-input asp-for="StoreInformationSettings.LogoPictureId" />
+ <span asp-validation-for="StoreInformationSettings.LogoPictureId"></span>
</div>
</div>
<div class="form-group">
diff --git a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/EditorTemplates/Logo.cshtml b/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/EditorTemplates/Logo.cshtml
deleted file mode 100644
index 8da720829..000000000
--- a/src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/EditorTemplates/Logo.cshtml
+++ /dev/null
@@ -1,77 +0,0 @@
-@model string
-@inject Grand.Business.Storage.Interfaces.IMediaFileStore mediaFileStore
-@{
- var random = CommonHelper.GenerateRandomInteger();
- var clientId = "picture" + random;
-
- var defaultFile = $"/assets/images/no-image.png";
- var param = string.IsNullOrEmpty(CommonPath.Param) ? "" : $"{CommonPath.Param}/";
- string logoFile = !string.IsNullOrEmpty(Model) ? (await mediaFileStore.GetFileInfo(string.IsNullOrEmpty(CommonPath.Param) ? Model : Model.Replace(CommonPath.Param, ""))) != null ? $"{Model}" : defaultFile : defaultFile;
-
-}
-<link rel="stylesheet" type="text/css" asp-src="/_content/Grand.Web.Admin/administration/fineuploader/fineuploader-4.2.2.min.css" />
-<script asp-location="Footer" asp-src="/_content/Grand.Web.Admin/administration/fineuploader/jquery.fineuploader-4.2.2.min.js"></script>
-<div id="@(clientId + "value")">
- <input asp-for="@Model" type="text" class="form-control mb-3" />
-</div>
-<div id="@(clientId + "image")">
- <img src="/@logoFile" style="width:100px" />
-</div>
-<br />
-<div id="@clientId"></div>
-<script type="text/template" id="@(clientId)-qq-template">
- <div class="qq-uploader-selector qq-uploader">
- <div class="qq-upload-drop-area-selector qq-upload-drop-area" qq-hide-dropzone>
- <span>@Loc["Common.FileUploader.DropFiles"]</span>
- </div>
- <div class="qq-upload-button-selector qq-upload-button btn yellow" style="margin: 10px 0px 10px 0px;">
- <div>@Loc["Common.FileUploader.Upload"]</div>
- </div>
- <span class="qq-drop-processing-selector qq-drop-processing">
- <span>@Loc["Common.FileUploader.Processing"]</span>
- <span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
- </span>
- <ul class="qq-upload-list-selector qq-upload-list">
- <li>
- <div class="qq-progress-bar-container-selector">
- <div class="qq-progress-bar-selector qq-progress-bar"></div>
- </div>
- <span class="qq-upload-spinner-selector qq-upload-spinner"></span>
- <span class="qq-edit-filename-icon-selector qq-edit-filename-icon"></span>
- <span class="qq-upload-file-selector qq-upload-file"></span>
- <input class="qq-edit-filename-selector qq-edit-filename" tabindex="0" type="text">
- <span class="qq-upload-size-selector qq-upload-size"></span>
- <a class="qq-upload-cancel-selector qq-upload-cancel" href="#">@Loc["Common.FileUploader.Cancel"]</a>
- <a class="qq-upload-retry-selector qq-upload-retry" href="#">@Loc["Common.FileUploader.Retry"]</a>
- <a class="qq-upload-delete-selector qq-upload-delete" href="#">@Loc["Common.FileUploader.Delete"]</a>
- <span class="qq-upload-status-text-selector qq-upload-status-text"></span>
- </li>
- </ul>
- </div>
-</script>
-
-<script>
- $(document).ready(function() {
-
- $("#@(clientId)").fineUploader({
- request: {
- endpoint: '@(Url.Action("AsyncLogoUpload", "Picture", new { area = Constants.AreaAdmin }))'
- },
- template: "@(clientId)-qq-template",
- multiple: false,
- validation: {
- itemLimit: 1,
- acceptFiles: 'image/*',
- }
- }).on("complete", function(event, id, name, responseJSON, xhr) {
- if (responseJSON.success) {
- $("#@(clientId + "image")").html("<img src='/@param"+ responseJSON.imageUrl + "'/>");
- $("#@(clientId + "value") input").val('@param'+responseJSON.imageUrl);
- $("#@Html.IdFor(model => model)").val('@param'+responseJSON.imageUrl);
- }
- else {
- alert(responseJSON.message);
- }
- });
- });
-</script>
diff --git a/src/Web/Grand.Web.Admin/Models/Settings/GeneralCommonSettingsModel.cs b/src/Web/Grand.Web.Admin/Models/Settings/GeneralCommonSettingsModel.cs
index b9634f9d1..7159b1911 100644
--- a/src/Web/Grand.Web.Admin/Models/Settings/GeneralCommonSettingsModel.cs
+++ b/src/Web/Grand.Web.Admin/Models/Settings/GeneralCommonSettingsModel.cs
@@ -54,9 +54,9 @@ namespace Grand.Web.Admin.Models.Settings
[GrandResourceDisplayName("Admin.Settings.GeneralCommon.AllowToSelectAdminTheme")]
public bool AllowToSelectAdminTheme { get; set; }
- [UIHint("Logo")]
+ [UIHint("Picture")]
[GrandResourceDisplayName("Admin.Settings.GeneralCommon.Logo")]
- public string LogoPicture { get; set; }
+ public string LogoPictureId { get; set; }
[GrandResourceDisplayName("Admin.Settings.GeneralCommon.DisplayCookieInformation")]
public bool DisplayCookieInformation { get; set; }
diff --git a/src/Web/Grand.Web/Views/Shared/_Logo.cshtml b/src/Web/Grand.Web/Views/Shared/_Logo.cshtml
index 323d6fe51..5f50be601 100644
--- a/src/Web/Grand.Web/Views/Shared/_Logo.cshtml
+++ b/src/Web/Grand.Web/Views/Shared/_Logo.cshtml
@@ -1,6 +1,10 @@
@inject StoreInformationSettings storeInformation
@inject IWorkContext workContext
+@inject Grand.Business.Storage.Interfaces.IPictureService pictureService
+@{
+ var urlLogo = await pictureService.GetPictureUrl(storeInformation.LogoPictureId);
+}
<a class="navbar-brand store-logo mx-lg-0 mx-auto" target="_self" href="@Url.RouteUrl("HomePage")">
- <img width="236" height="53" src="/@storeInformation.LogoPicture" alt="@workContext.CurrentStore.Name"/>
- <span class="sr-only">@Loc["admin.configuration.shipping.providers.fields.logo"]</span>
+ <img width="236" height="53" src="@urlLogo" alt="@workContext.CurrentStore.Name"/>
+ <span class="sr-only">Logo</span>
</a>
\ No newline at end of file