Skip to content

Commit

Permalink
VCST-2120: Fix icon URL in module list (#2863)
Browse files Browse the repository at this point in the history
fix: Fix icon URL in module list (#2863)
Co-authored-by: Artem Dudarev <artem@virtoworks.com>
  • Loading branch information
Ljutyj and artem-dudarev authored Dec 2, 2024
1 parent acb3b0e commit 7ca66d0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using VirtoCommerce.Platform.Core;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class ModulesController : Controller
private readonly IPlatformRestarter _platformRestarter;
private static readonly object _lockObject = new object();
private static readonly FormOptions _defaultFormOptions = new FormOptions();
private readonly ILocalModuleCatalog _localModuleCatalog;

public ModulesController(
IExternalModuleCatalog externalModuleCatalog,
Expand All @@ -52,7 +54,8 @@ public ModulesController(
IOptions<PlatformOptions> platformOptions,
IOptions<ExternalModuleCatalogOptions> externalModuleCatalogOptions,
IOptions<LocalStorageModuleCatalogOptions> localStorageModuleCatalogOptions,
IPlatformRestarter platformRestarter)
IPlatformRestarter platformRestarter,
ILocalModuleCatalog localModuleCatalog)
{
_externalModuleCatalog = externalModuleCatalog;
_moduleInstaller = moduleInstaller;
Expand All @@ -63,6 +66,7 @@ public ModulesController(
_externalModuleCatalogOptions = externalModuleCatalogOptions.Value;
_localStorageModuleCatalogOptions = localStorageModuleCatalogOptions.Value;
_platformRestarter = platformRestarter;
_localModuleCatalog = localModuleCatalog;
}

/// <summary>
Expand Down Expand Up @@ -90,11 +94,48 @@ public ActionResult<ModuleDescriptor[]> GetModules()
{
EnsureModulesCatalogInitialized();

var retVal = _externalModuleCatalog.Modules.OfType<ManifestModuleInfo>().OrderBy(x => x.Id).ThenBy(x => x.Version)
.Select(x => new ModuleDescriptor(x))
.ToArray();
var allModules = _externalModuleCatalog.Modules
.OfType<ManifestModuleInfo>()
.OrderBy(x => x.Id)
.ThenBy(x => x.Version)
.Select(x => new ModuleDescriptor(x))
.ToList();

return Ok(retVal);
_localModuleCatalog.Initialize();
var localModules = _localModuleCatalog.Modules.OfType<ManifestModuleInfo>().ToDictionary(x => x.Id);

foreach (var module in allModules.Where(x => !string.IsNullOrEmpty(x.IconUrl)))
{
module.IconUrl = localModules.TryGetValue(module.Id, out var localModule) && IconFileExists(localModule)
? localModule.IconUrl
: null;
}

return Ok(allModules);
}

private static bool IconFileExists(ManifestModuleInfo module)
{
// PathString should start from "/"
var moduleIconUrl = module.IconUrl;
if (!moduleIconUrl.StartsWith('/'))
{
moduleIconUrl = "/" + moduleIconUrl;
}

var basePath = new PathString($"/modules/$({module.Id})");
var iconUrlPath = new PathString(moduleIconUrl);

if (!iconUrlPath.StartsWithSegments(basePath, out var subPath) ||
string.IsNullOrEmpty(subPath.Value) ||
!Directory.Exists(module.FullPhysicalPath))
{
return false;
}

using var fileProvider = new PhysicalFileProvider(module.FullPhysicalPath);

return fileProvider.GetFileInfo(subPath.Value).Exists;
}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions src/VirtoCommerce.Platform.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,13 @@ public void ConfigureServices(IServiceCollection services)
var healthBuilder = services.AddHealthChecks()
.AddCheck<ModulesHealthChecker>("Modules health",
failureStatus: HealthStatus.Unhealthy,
tags: new[] { "Modules" })
tags: ["Modules"])
.AddCheck<CacheHealthChecker>("Cache health",
failureStatus: HealthStatus.Degraded,
tags: new[] { "Cache" })
tags: ["Cache"])
.AddCheck<RedisHealthCheck>("Redis health",
failureStatus: HealthStatus.Unhealthy,
tags: new[] { "Cache" });
tags: ["Cache"]);

var connectionString = Configuration.GetConnectionString("VirtoCommerce");
switch (databaseProvider)
Expand All @@ -522,19 +522,19 @@ public void ConfigureServices(IServiceCollection services)
healthBuilder.AddMySql(connectionString,
name: "MySql health",
failureStatus: HealthStatus.Unhealthy,
tags: new[] { "Database" });
tags: ["Database"]);
break;
case "PostgreSql":
healthBuilder.AddNpgSql(connectionString,
name: "PostgreSql health",
failureStatus: HealthStatus.Unhealthy,
tags: new[] { "Database" });
tags: ["Database"]);
break;
default:
healthBuilder.AddSqlServer(connectionString,
name: "SQL Server health",
failureStatus: HealthStatus.Unhealthy,
tags: new[] { "Database" });
tags: ["Database"]);
break;
}

Expand Down

0 comments on commit 7ca66d0

Please sign in to comment.