Skip to content

Commit

Permalink
Prevent exception when Themes directory not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
support committed Sep 14, 2022
1 parent 627248e commit bbde23d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public static void UsePageNotFound(this IApplicationBuilder application)
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger>();
//get current customer
var workContext = context.HttpContext.RequestServices.GetRequiredService<IWorkContext>();
_ = logger.InsertLog(Domain.Logging.LogLevel.Error,
_ = logger.InsertLog(Domain.Logging.LogLevel.Error,
$"Error 404. The requested page ({context.HttpContext.Request?.GetDisplayUrl()}) was not found",
customer: workContext.CurrentCustomer,
customer: workContext.CurrentCustomer,
ipAddress: context.HttpContext?.Connection?.RemoteIpAddress?.ToString(),
pageurl: context.HttpContext?.Request?.GetDisplayUrl(),
referrerUrl: context.HttpContext?.Request?.GetTypedHeaders().Referer?.ToString());
Expand Down Expand Up @@ -211,32 +211,35 @@ public static void UseGrandStaticFiles(this IApplicationBuilder application, App

OnPrepareResponse = ctx =>
{
if (!String.IsNullOrEmpty(appConfig.StaticFilesCacheControl))
if (!string.IsNullOrEmpty(appConfig.StaticFilesCacheControl))
ctx.Context.Response.Headers.Append(HeaderNames.CacheControl, appConfig.StaticFilesCacheControl);
}

});

//themes
application.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(CommonPath.ThemePath),
RequestPath = new PathString("/Themes"),
OnPrepareResponse = ctx =>
{
if (!String.IsNullOrEmpty(appConfig.StaticFilesCacheControl))
ctx.Context.Response.Headers.Append(HeaderNames.CacheControl, appConfig.StaticFilesCacheControl);
}
});
if (Directory.Exists(CommonPath.ThemePath))
application.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(CommonPath.ThemePath),
RequestPath = new PathString("/Themes"),
OnPrepareResponse = ctx =>
{
if (!string.IsNullOrEmpty(appConfig.StaticFilesCacheControl))
ctx.Context.Response.Headers.Append(HeaderNames.CacheControl, appConfig.StaticFilesCacheControl);
}
});

//plugins
application.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(CommonPath.PluginsPath),
RequestPath = new PathString("/Plugins"),
OnPrepareResponse = ctx =>
{
if (!string.IsNullOrEmpty(appConfig.StaticFilesCacheControl))
ctx.Context.Response.Headers.Append(HeaderNames.CacheControl, appConfig.StaticFilesCacheControl);
}
});
if (Directory.Exists(CommonPath.PluginsPath))
application.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(CommonPath.PluginsPath),
RequestPath = new PathString("/Plugins"),
OnPrepareResponse = ctx =>
{
if (!string.IsNullOrEmpty(appConfig.StaticFilesCacheControl))
ctx.Context.Response.Headers.Append(HeaderNames.CacheControl, appConfig.StaticFilesCacheControl);
}
});

}

Expand Down Expand Up @@ -320,7 +323,7 @@ public static void UseDefaultSecurityHeaders(this IApplicationBuilder applicatio
builder.AddStyleSrc().From("*").UnsafeEval().UnsafeInline();
})
.AddPermissionsPolicy(builder =>
{
{
builder.AddAutoplay().Self();
builder.AddCamera().Self();
builder.AddEncryptedMedia().Self();
Expand Down
13 changes: 7 additions & 6 deletions src/Web/Grand.Web.Common/Themes/ThemeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ public partial class ThemeList : IThemeList
public ThemeList()
{
ThemeConfigurations = new List<ThemeConfiguration>();
foreach (var themeName in Directory.GetDirectories(CommonPath.ThemePath))
{
var configuration = CreateThemeConfiguration(themeName);
if (configuration != null)
if (Directory.Exists(CommonPath.ThemePath))
foreach (var themeName in Directory.GetDirectories(CommonPath.ThemePath))
{
ThemeConfigurations.Add(configuration);
var configuration = CreateThemeConfiguration(themeName);
if (configuration != null)
{
ThemeConfigurations.Add(configuration);
}
}
}
}

public IList<ThemeConfiguration> ThemeConfigurations { get; }
Expand Down

0 comments on commit bbde23d

Please sign in to comment.