99using Microsoft . AspNetCore . Hosting ;
1010using Microsoft . Extensions . FileProviders ;
1111using Microsoft . Extensions . Options ;
12- using SixLabors . ImageSharp . Web . Middleware ;
1312using SixLabors . ImageSharp . Web . Resolvers ;
1413
1514namespace SixLabors . ImageSharp . Web . Caching
@@ -34,16 +33,6 @@ public class PhysicalFileSystemCache : IImageCache
3433 /// </summary>
3534 private readonly IFileProvider fileProvider ;
3635
37- /// <summary>
38- /// The cache configuration options.
39- /// </summary>
40- private readonly PhysicalFileSystemCacheOptions cacheOptions ;
41-
42- /// <summary>
43- /// The middleware configuration options.
44- /// </summary>
45- private readonly ImageSharpMiddlewareOptions options ;
46-
4736 /// <summary>
4837 /// Contains various format helper methods based on the current configuration.
4938 /// </summary>
@@ -52,35 +41,31 @@ public class PhysicalFileSystemCache : IImageCache
5241 /// <summary>
5342 /// Initializes a new instance of the <see cref="PhysicalFileSystemCache"/> class.
5443 /// </summary>
55- /// <param name="cacheOptions ">The cache configuration options.</param>
44+ /// <param name="options ">The cache configuration options.</param>
5645 /// <param name="environment">The hosting environment the application is running in.</param>
57- /// <param name="options">The middleware configuration options.</param>
5846 /// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param>
5947 public PhysicalFileSystemCache (
60- IOptions < PhysicalFileSystemCacheOptions > cacheOptions ,
48+ IOptions < PhysicalFileSystemCacheOptions > options ,
6149#if NETCOREAPP2_1
6250 IHostingEnvironment environment ,
6351#else
6452 IWebHostEnvironment environment ,
6553#endif
66- IOptions < ImageSharpMiddlewareOptions > options ,
6754 FormatUtilities formatUtilities )
6855 {
6956 Guard . NotNull ( environment , nameof ( environment ) ) ;
7057 Guard . NotNull ( options , nameof ( options ) ) ;
7158 Guard . NotNullOrWhiteSpace ( environment . WebRootPath , nameof ( environment . WebRootPath ) ) ;
7259
7360 // Allow configuration of the cache without having to register everything.
74- this . cacheOptions = cacheOptions != null ? cacheOptions . Value : new PhysicalFileSystemCacheOptions ( ) ;
75- this . cacheRootPath = GetCacheRoot ( this . cacheOptions , environment . WebRootPath , environment . ContentRootPath ) ;
76- if ( ! Directory . Exists ( this . cacheRootPath ) )
77- {
78- Directory . CreateDirectory ( this . cacheRootPath ) ;
79- }
61+ PhysicalFileSystemCacheOptions cacheOptions = options != null ? options . Value : new PhysicalFileSystemCacheOptions ( ) ;
62+ this . cacheRootPath = GetCacheRoot ( cacheOptions , environment . WebRootPath , environment . ContentRootPath ) ;
63+ this . cacheFolderDepth = ( int ) cacheOptions . CacheFolderDepth ;
64+
65+ // Ensure cache directory is created before initializing the file provider
66+ Directory . CreateDirectory ( this . cacheRootPath ) ;
8067
8168 this . fileProvider = new PhysicalFileProvider ( this . cacheRootPath ) ;
82- this . options = options . Value ;
83- this . cacheFolderDepth = ( int ) this . options . CacheHashLength ;
8469 this . formatUtilities = formatUtilities ;
8570 }
8671
@@ -124,10 +109,8 @@ public async Task SetAsync(string key, Stream stream, ImageCacheMetadata metadat
124109 string metaPath = this . ToMetaDataFilePath ( path ) ;
125110 string directory = Path . GetDirectoryName ( path ) ;
126111
127- if ( ! Directory . Exists ( directory ) )
128- {
129- Directory . CreateDirectory ( directory ) ;
130- }
112+ // Ensure cache directory is created before creating files
113+ Directory . CreateDirectory ( directory ) ;
131114
132115 using ( FileStream fileStream = File . Create ( imagePath ) )
133116 {
@@ -167,6 +150,11 @@ private string ToImageFilePath(string path, in ImageCacheMetadata metaData)
167150 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
168151 internal static unsafe string ToFilePath ( string key , int cacheFolderDepth )
169152 {
153+ if ( cacheFolderDepth > key . Length )
154+ {
155+ cacheFolderDepth = key . Length ;
156+ }
157+
170158 // Each key substring char + separator + key
171159 int length = ( cacheFolderDepth * 2 ) + key . Length ;
172160 fixed ( char * keyPtr = key )
0 commit comments