Skip to content

Commit 5dcae69

Browse files
Fix null key exception
1 parent bb8eaad commit 5dcae69

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/ImageSharp.Web/Caching/PhysicalFileSystemCacheResolver.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using System.IO;
56
using System.Threading.Tasks;
67
using Microsoft.Extensions.FileProviders;
@@ -13,7 +14,7 @@ namespace SixLabors.ImageSharp.Web.Resolvers
1314
/// </summary>
1415
public class PhysicalFileSystemCacheResolver : IImageCacheResolver
1516
{
16-
private readonly IFileInfo fileInfo;
17+
private readonly IFileInfo metaFileInfo;
1718
private readonly FormatUtilities formatUtilities;
1819
private ImageCacheMetadata metadata;
1920

@@ -26,14 +27,14 @@ public class PhysicalFileSystemCacheResolver : IImageCacheResolver
2627
/// </param>
2728
public PhysicalFileSystemCacheResolver(IFileInfo metaFileInfo, FormatUtilities formatUtilities)
2829
{
29-
this.fileInfo = metaFileInfo;
30+
this.metaFileInfo = metaFileInfo;
3031
this.formatUtilities = formatUtilities;
3132
}
3233

3334
/// <inheritdoc/>
3435
public async Task<ImageCacheMetadata> GetMetaDataAsync()
3536
{
36-
using Stream stream = this.fileInfo.CreateReadStream();
37+
using Stream stream = this.metaFileInfo.CreateReadStream();
3738
this.metadata = await ImageCacheMetadata.ReadAsync(stream);
3839
return this.metadata;
3940
}
@@ -42,7 +43,7 @@ public async Task<ImageCacheMetadata> GetMetaDataAsync()
4243
public Task<Stream> OpenReadAsync()
4344
{
4445
string path = Path.ChangeExtension(
45-
this.fileInfo.PhysicalPath,
46+
this.metaFileInfo.PhysicalPath,
4647
this.formatUtilities.GetExtensionFromContentType(this.metadata.ContentType));
4748

4849
return Task.FromResult<Stream>(File.OpenRead(path));

src/ImageSharp.Web/ImageCacheMetadata.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static ImageCacheMetadata FromDictionary(IDictionary<string, string> dict
123123
return new ImageCacheMetadata(
124124
sourceLastWriteTimeUtc,
125125
cacheLastWriteTimeUtc,
126-
contentType ?? string.Empty,
126+
contentType,
127127
cacheControlMaxAge,
128128
contentLength);
129129
}
@@ -160,7 +160,8 @@ public bool Equals(ImageCacheMetadata other)
160160
return this.SourceLastWriteTimeUtc == other.SourceLastWriteTimeUtc
161161
&& this.CacheLastWriteTimeUtc == other.CacheLastWriteTimeUtc
162162
&& this.ContentType == other.ContentType
163-
&& this.CacheControlMaxAge == other.CacheControlMaxAge;
163+
&& this.CacheControlMaxAge == other.CacheControlMaxAge
164+
&& this.ContentLength == other.ContentLength;
164165
}
165166

166167
/// <inheritdoc/>

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,10 @@ private async Task ProcessRequestAsync(
323323
// Save the image to the cache and send the response to the caller.
324324
await this.cache.SetAsync(key, outStream, cachedImageMetadata);
325325

326-
// Remove the resolver from the cache so we always resolve next request.
326+
// Remove the resolver from the cache so we always resolve next request
327+
// for the same key.
327328
CacheResolverLru.TryRemove(key);
328329

329-
// Replace cache metadata item value.
330-
CacheMetadataLru.TryRemove(key);
331-
CacheMetadataLru.GetOrAdd(key, _ => cachedImageMetadata);
332-
333330
await this.SendResponseAsync(imageContext, key, cachedImageMetadata, outStream, null);
334331
}
335332
catch (Exception ex)
@@ -373,9 +370,10 @@ private async Task<ValueTuple<bool, ImageMetadata>> IsNewOrUpdatedAsync(
373370
ImageContext imageContext,
374371
string key)
375372
{
376-
ImageMetadata sourceImageMetadata = default;
377373
using (await AsyncLock.ReaderLockAsync(key))
378374
{
375+
ImageMetadata sourceImageMetadata = default;
376+
379377
// Check to see if the cache contains this image.
380378
IImageCacheResolver cachedImageResolver
381379
= await CacheResolverLru.GetOrAddAsync(key, async k => await this.cache.GetAsync(k));
@@ -401,9 +399,9 @@ IImageCacheResolver cachedImageResolver
401399
}
402400
}
403401
}
404-
}
405402

406-
return (true, sourceImageMetadata);
403+
return (true, sourceImageMetadata);
404+
}
407405
}
408406

409407
private async Task SendResponseAsync(

0 commit comments

Comments
 (0)