Skip to content

Commit 36de016

Browse files
Align provider root path resolving with ASP.NET Core
1 parent 1766b4c commit 36de016

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/ImageSharp.Web/Caching/PhysicalFileSystemCache.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ public async Task SetAsync(string key, Stream stream, ImageCacheMetadata metadat
9494
string directory = Path.GetDirectoryName(path);
9595

9696
// Ensure cache directory is created before creating files
97-
Directory.CreateDirectory(directory);
97+
if (!Directory.Exists(directory))
98+
{
99+
Directory.CreateDirectory(directory);
100+
}
98101

99102
using (FileStream fileStream = File.Create(imagePath))
100103
{

src/ImageSharp.Web/Providers/PhysicalFileSystemProvider.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public PhysicalFileSystemProvider(
4747

4848
this.providerRootPath = GetProviderRoot(options.Value, environment.WebRootPath, environment.ContentRootPath);
4949
this.formatUtilities = formatUtilities;
50+
51+
// Disable provider by default when no root path is configured or webroot doesn't exist
52+
if (this.providerRootPath == null)
53+
{
54+
this.Match = _ => false;
55+
}
5056
}
5157

5258
/// <inheritdoc/>
@@ -87,12 +93,32 @@ public Task<IImageResolver> GetAsync(HttpContext context)
8793
/// <returns><see cref="string"/> representing the fully qualified provider root path.</returns>
8894
internal static string GetProviderRoot(PhysicalFileSystemProviderOptions providerOptions, string webRootPath, string contentRootPath)
8995
{
90-
string providerRoot = providerOptions.ProviderRootPath ?? webRootPath ?? "wwwroot";
91-
string fullPath = Path.IsPathFullyQualified(providerRoot)
92-
? providerRoot
93-
: Path.GetFullPath(providerRoot, contentRootPath);
96+
string providerRootPath = providerOptions.ProviderRootPath ?? webRootPath;
97+
if (providerRootPath == null)
98+
{
99+
// Default to /wwwroot if it exists
100+
string wwwroot = Path.Combine(contentRootPath, "wwwroot");
101+
if (Directory.Exists(wwwroot))
102+
{
103+
providerRootPath = wwwroot;
104+
}
105+
}
106+
107+
if (!string.IsNullOrEmpty(providerRootPath))
108+
{
109+
string fullPath = Path.IsPathFullyQualified(providerRootPath)
110+
? providerRootPath
111+
: Path.GetFullPath(providerRootPath, contentRootPath);
112+
113+
if (!Directory.Exists(fullPath))
114+
{
115+
Directory.CreateDirectory(fullPath);
116+
}
117+
118+
return EnsureTrailingSlash(fullPath);
119+
}
94120

95-
return EnsureTrailingSlash(fullPath);
121+
return null;
96122
}
97123

98124
internal static string EnsureTrailingSlash(string path)

0 commit comments

Comments
 (0)