-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
ImageSharpMiddleware.cs
516 lines (450 loc) · 22.3 KB
/
ImageSharpMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IO;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Web.Caching;
using SixLabors.ImageSharp.Web.Commands;
using SixLabors.ImageSharp.Web.Processors;
using SixLabors.ImageSharp.Web.Providers;
using SixLabors.ImageSharp.Web.Resolvers;
namespace SixLabors.ImageSharp.Web.Middleware
{
/// <summary>
/// Middleware for handling the processing of images via image requests.
/// </summary>
public class ImageSharpMiddleware
{
/// <summary>
/// The write worker used for limiting identical requests.
/// </summary>
private static readonly ConcurrentDictionary<string, Lazy<Task>> WriteWorkers
= new ConcurrentDictionary<string, Lazy<Task>>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// The read worker used for limiting identical requests.
/// </summary>
private static readonly ConcurrentDictionary<string, Lazy<Task<ValueTuple<bool, ImageMetadata>>>> ReadWorkers
= new ConcurrentDictionary<string, Lazy<Task<ValueTuple<bool, ImageMetadata>>>>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Used to temporarily store source metadata reads to reduce the overhead of cache lookups.
/// </summary>
private static readonly ConcurrentTLruCache<string, ImageMetadata> SourceMetadataLru
= new ConcurrentTLruCache<string, ImageMetadata>(1024, TimeSpan.FromMinutes(5));
/// <summary>
/// Used to temporarily store cache resolver reads to reduce the overhead of cache lookups.
/// </summary>
private static readonly ConcurrentTLruCache<string, ValueTuple<IImageCacheResolver, ImageCacheMetadata>> CacheResolverLru
= new ConcurrentTLruCache<string, ValueTuple<IImageCacheResolver, ImageCacheMetadata>>(1024, TimeSpan.FromMinutes(5));
/// <summary>
/// The function processing the Http request.
/// </summary>
private readonly RequestDelegate next;
/// <summary>
/// The configuration options.
/// </summary>
private readonly ImageSharpMiddlewareOptions options;
/// <summary>
/// The type used for performing logging.
/// </summary>
private readonly ILogger logger;
/// <summary>
/// The parser for parsing commands from the current request.
/// </summary>
private readonly IRequestParser requestParser;
/// <summary>
/// The collection of image providers.
/// </summary>
private readonly IImageProvider[] providers;
/// <summary>
/// The collection of image processors.
/// </summary>
private readonly IImageWebProcessor[] processors;
/// <summary>
/// The image cache.
/// </summary>
private readonly IImageCache cache;
/// <summary>
/// The hashing implementation to use when generating cached file names.
/// </summary>
private readonly ICacheHash cacheHash;
/// <summary>
/// The collection of known commands gathered from the processors.
/// </summary>
private readonly HashSet<string> knownCommands;
/// <summary>
/// Contains various helper methods based on the current configuration.
/// </summary>
private readonly FormatUtilities formatUtilities;
/// <summary>
/// Used to parse processing commands.
/// </summary>
private readonly CommandParser commandParser;
/// <summary>
/// The culture to use when parsing processing commands.
/// </summary>
private readonly CultureInfo parserCulture;
/// <summary>
/// Initializes a new instance of the <see cref="ImageSharpMiddleware"/> class.
/// </summary>
/// <param name="next">The next middleware in the pipeline.</param>
/// <param name="options">The middleware configuration options.</param>
/// <param name="loggerFactory">An <see cref="ILoggerFactory"/> instance used to create loggers.</param>
/// <param name="requestParser">An <see cref="IRequestParser"/> instance used to parse image requests for commands.</param>
/// <param name="resolvers">A collection of <see cref="IImageProvider"/> instances used to resolve images.</param>
/// <param name="processors">A collection of <see cref="IImageWebProcessor"/> instances used to process images.</param>
/// <param name="cache">An <see cref="IImageCache"/> instance used for caching images.</param>
/// <param name="cacheHash">An <see cref="ICacheHash"/>instance used for calculating cached file names.</param>
/// <param name="commandParser">The command parser</param>
/// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param>
public ImageSharpMiddleware(
RequestDelegate next,
IOptions<ImageSharpMiddlewareOptions> options,
ILoggerFactory loggerFactory,
IRequestParser requestParser,
IEnumerable<IImageProvider> resolvers,
IEnumerable<IImageWebProcessor> processors,
IImageCache cache,
ICacheHash cacheHash,
CommandParser commandParser,
FormatUtilities formatUtilities)
{
Guard.NotNull(next, nameof(next));
Guard.NotNull(options, nameof(options));
Guard.NotNull(loggerFactory, nameof(loggerFactory));
Guard.NotNull(requestParser, nameof(requestParser));
Guard.NotNull(resolvers, nameof(resolvers));
Guard.NotNull(processors, nameof(processors));
Guard.NotNull(cache, nameof(cache));
Guard.NotNull(cacheHash, nameof(cacheHash));
Guard.NotNull(commandParser, nameof(commandParser));
Guard.NotNull(formatUtilities, nameof(formatUtilities));
this.next = next;
this.options = options.Value;
this.requestParser = requestParser;
this.providers = resolvers as IImageProvider[] ?? resolvers.ToArray();
this.processors = processors as IImageWebProcessor[] ?? processors.ToArray();
this.cache = cache;
this.cacheHash = cacheHash;
this.commandParser = commandParser;
this.parserCulture = this.options.UseInvariantParsingCulture
? CultureInfo.InvariantCulture
: CultureInfo.CurrentCulture;
var commands = new HashSet<string>();
foreach (IImageWebProcessor processor in this.processors)
{
foreach (string command in processor.Commands)
{
commands.Add(command);
}
}
this.knownCommands = commands;
this.logger = loggerFactory.CreateLogger<ImageSharpMiddleware>();
this.formatUtilities = formatUtilities;
}
#pragma warning disable IDE1006 // Naming Styles
/// <summary>
/// Performs operations upon the current request.
/// </summary>
/// <param name="context">The current HTTP request context.</param>
/// <returns>The <see cref="Task"/>.</returns>
public async Task Invoke(HttpContext context)
#pragma warning restore IDE1006 // Naming Styles
{
IDictionary<string, string> commands = this.requestParser.ParseRequestCommands(context);
if (commands.Count > 0)
{
// Strip out any unknown commands.
foreach (string command in new List<string>(commands.Keys))
{
if (!this.knownCommands.Contains(command))
{
commands.Remove(command);
}
}
}
await this.options.OnParseCommandsAsync.Invoke(
new ImageCommandContext(context, commands, this.commandParser, this.parserCulture));
// Get the correct service for the request.
IImageProvider provider = null;
foreach (IImageProvider resolver in this.providers)
{
if (resolver.Match(context))
{
provider = resolver;
break;
}
}
if ((commands.Count == 0 && provider?.ProcessingBehavior != ProcessingBehavior.All)
|| provider?.IsValidRequest(context) != true)
{
// Nothing to do. call the next delegate/middleware in the pipeline
await this.next(context);
return;
}
IImageResolver sourceImageResolver = await provider.GetAsync(context);
if (sourceImageResolver is null)
{
// Log the error but let the pipeline handle the 404
// by calling the next delegate/middleware in the pipeline.
var imageContext = new ImageContext(context, this.options);
this.logger.LogImageResolveFailed(imageContext.GetDisplayUrl());
await this.next(context);
return;
}
await this.ProcessRequestAsync(
context,
sourceImageResolver,
new ImageContext(context, this.options),
commands);
}
private async Task ProcessRequestAsync(
HttpContext context,
IImageResolver sourceImageResolver,
ImageContext imageContext,
IDictionary<string, string> commands)
{
// Create a cache key based on all the components of the requested url
string uri = GetUri(context, commands);
string key = this.cacheHash.Create(uri, this.options.CachedNameLength);
// Check the cache, if present, not out of date and not requiring and update
// we'll simply serve the file from there.
(bool newOrUpdated, ImageMetadata sourceImageMetadata) =
await this.IsNewOrUpdatedAsync(sourceImageResolver, imageContext, key);
if (!newOrUpdated)
{
return;
}
// Not cached? Let's get it from the image resolver.
RecyclableMemoryStream outStream = null;
// Enter a write lock which locks writing and any reads for the same request.
// This reduces the overheads of unnecessary processing plus avoids file locks.
await WriteWorkers.GetOrAdd(
key,
_ => new Lazy<Task>(
async () =>
{
try
{
// Prevent a second request from starting a read during write execution.
if (ReadWorkers.TryGetValue(key, out Lazy<Task<(bool, ImageMetadata)>> readWork))
{
await readWork.Value;
}
ImageCacheMetadata cachedImageMetadata = default;
outStream = new RecyclableMemoryStream(this.options.MemoryStreamManager);
IImageFormat format;
// 14.9.3 CacheControl Max-Age
// Check to see if the source metadata has a CacheControl Max-Age value
// and use it to override the default max age from our options.
TimeSpan maxAge = this.options.BrowserMaxAge;
if (!sourceImageMetadata.CacheControlMaxAge.Equals(TimeSpan.MinValue))
{
maxAge = sourceImageMetadata.CacheControlMaxAge;
}
using (Stream inStream = await sourceImageResolver.OpenReadAsync())
{
// No commands? We simply copy the stream across.
if (commands.Count == 0)
{
await inStream.CopyToAsync(outStream);
outStream.Position = 0;
format = await Image.DetectFormatAsync(this.options.Configuration, outStream);
}
else
{
using var image = FormattedImage.Load(this.options.Configuration, inStream);
image.Process(
this.logger,
this.processors,
commands,
this.commandParser,
this.parserCulture);
await this.options.OnBeforeSaveAsync.Invoke(image);
image.Save(outStream);
format = image.Format;
}
}
// Allow for any further optimization of the image.
outStream.Position = 0;
string contentType = format.DefaultMimeType;
string extension = this.formatUtilities.GetExtensionFromContentType(contentType);
await this.options.OnProcessedAsync.Invoke(new ImageProcessingContext(context, outStream, commands, contentType, extension));
outStream.Position = 0;
cachedImageMetadata = new ImageCacheMetadata(
sourceImageMetadata.LastWriteTimeUtc,
DateTime.UtcNow,
contentType,
maxAge,
outStream.Length);
// Save the image to the cache and send the response to the caller.
await this.cache.SetAsync(key, outStream, cachedImageMetadata);
// Remove the resolver from the cache so we always resolve next request
// for the same key.
CacheResolverLru.TryRemove(key);
await this.SendResponseAsync(imageContext, key, cachedImageMetadata, outStream, null);
}
catch (Exception ex)
{
// Log the error internally then rethrow.
// We don't call next here, the pipeline will automatically handle it
this.logger.LogImageProcessingFailed(imageContext.GetDisplayUrl(), ex);
throw;
}
finally
{
await this.StreamDisposeAsync(outStream);
WriteWorkers.TryRemove(key, out Lazy<Task> _);
}
}, LazyThreadSafetyMode.ExecutionAndPublication)).Value;
}
private ValueTask StreamDisposeAsync(Stream stream)
{
if (stream is null)
{
return default;
}
#if NETCOREAPP2_1
try
{
stream.Dispose();
return default;
}
catch (Exception ex)
{
return new ValueTask(Task.FromException(ex));
}
#else
return stream.DisposeAsync();
#endif
}
private async Task<ValueTuple<bool, ImageMetadata>> IsNewOrUpdatedAsync(
IImageResolver sourceImageResolver,
ImageContext imageContext,
string key)
{
if (WriteWorkers.TryGetValue(key, out Lazy<Task> writeWork))
{
await writeWork.Value;
}
if (ReadWorkers.TryGetValue(key, out Lazy<Task<(bool, ImageMetadata)>> readWork))
{
return await readWork.Value;
}
return await ReadWorkers.GetOrAdd(
key,
_ => new Lazy<Task<ValueTuple<bool, ImageMetadata>>>(
async () =>
{
try
{
// Get the source metadata for processing, storing the result for future checks.
ImageMetadata sourceImageMetadata = await
SourceMetadataLru.GetOrAddAsync(
key,
_ => sourceImageResolver.GetMetaDataAsync());
// Check to see if the cache contains this image.
// If not, we return early. No further checks necessary.
(IImageCacheResolver ImageCacheResolver, ImageCacheMetadata ImageCacheMetadata) cachedImage = await
CacheResolverLru.GetOrAddAsync(
key,
async k =>
{
IImageCacheResolver resolver = await this.cache.GetAsync(k);
ImageCacheMetadata metadata = default;
if (resolver != null)
{
metadata = await resolver.GetMetaDataAsync();
}
return (resolver, metadata);
});
if (cachedImage.ImageCacheResolver is null)
{
// Remove the null resolver from the store.
CacheResolverLru.TryRemove(key);
return (true, sourceImageMetadata);
}
// Has the cached image expired?
// Or has the source image changed since the image was last cached?
if (cachedImage.ImageCacheMetadata.ContentLength == 0 // Fix for old cache without length property
|| cachedImage.ImageCacheMetadata.CacheLastWriteTimeUtc <= (DateTimeOffset.UtcNow - this.options.CacheMaxAge)
|| cachedImage.ImageCacheMetadata.SourceLastWriteTimeUtc != sourceImageMetadata.LastWriteTimeUtc)
{
// We want to remove the resolver from the store so that the next check gets the updated file.
CacheResolverLru.TryRemove(key);
return (true, sourceImageMetadata);
}
// We're pulling the image from the cache.
await this.SendResponseAsync(imageContext, key, cachedImage.ImageCacheMetadata, null, cachedImage.ImageCacheResolver);
return (false, sourceImageMetadata);
}
finally
{
ReadWorkers.TryRemove(key, out Lazy<Task<(bool, ImageMetadata)>> _);
}
}, LazyThreadSafetyMode.ExecutionAndPublication)).Value;
}
private async Task SendResponseAsync(
ImageContext imageContext,
string key,
ImageCacheMetadata metadata,
Stream stream,
IImageCacheResolver cacheResolver)
{
imageContext.ComprehendRequestHeaders(metadata.CacheLastWriteTimeUtc, metadata.ContentLength);
switch (imageContext.GetPreconditionState())
{
case ImageContext.PreconditionState.Unspecified:
case ImageContext.PreconditionState.ShouldProcess:
if (imageContext.IsHeadRequest())
{
await imageContext.SendStatusAsync(ResponseConstants.Status200Ok, metadata);
return;
}
this.logger.LogImageServed(imageContext.GetDisplayUrl(), key);
// When stream is null we're sending from the cache.
await imageContext.SendAsync(stream ?? await cacheResolver.OpenReadAsync(), metadata);
return;
case ImageContext.PreconditionState.NotModified:
this.logger.LogImageNotModified(imageContext.GetDisplayUrl());
await imageContext.SendStatusAsync(ResponseConstants.Status304NotModified, metadata);
return;
case ImageContext.PreconditionState.PreconditionFailed:
this.logger.LogImagePreconditionFailed(imageContext.GetDisplayUrl());
await imageContext.SendStatusAsync(ResponseConstants.Status412PreconditionFailed, metadata);
return;
default:
var exception = new NotImplementedException(imageContext.GetPreconditionState().ToString());
Debug.Fail(exception.ToString());
throw exception;
}
}
private static string GetUri(HttpContext context, IDictionary<string, string> commands)
{
var sb = new StringBuilder(context.Request.Host.ToString());
string pathBase = context.Request.PathBase.ToString();
if (!string.IsNullOrWhiteSpace(pathBase))
{
sb.AppendFormat("{0}/", pathBase);
}
string path = context.Request.Path.ToString();
if (!string.IsNullOrWhiteSpace(path))
{
sb.Append(path);
}
sb.Append(QueryString.Create(commands));
return sb.ToString().ToLowerInvariant();
}
}
}