@@ -262,10 +262,9 @@ private async Task Invoke(HttpContext httpContext, bool retry)
262
262
}
263
263
264
264
await this . ProcessRequestAsync (
265
- httpContext ,
265
+ imageCommandContext ,
266
266
sourceImageResolver ,
267
267
new ImageContext ( httpContext , this . options ) ,
268
- commands ,
269
268
retry ) ;
270
269
}
271
270
@@ -278,12 +277,14 @@ private static void SetBadRequest(HttpContext httpContext)
278
277
}
279
278
280
279
private async Task ProcessRequestAsync (
281
- HttpContext httpContext ,
280
+ ImageCommandContext imageCommandContext ,
282
281
IImageResolver sourceImageResolver ,
283
282
ImageContext imageContext ,
284
- CommandCollection commands ,
285
283
bool retry )
286
284
{
285
+ HttpContext httpContext = imageCommandContext . Context ;
286
+ CommandCollection commands = imageCommandContext . Commands ;
287
+
287
288
// Create a hashed cache key
288
289
string key = this . cacheHash . Create (
289
290
this . cacheKey . Create ( httpContext , commands ) ,
@@ -344,55 +345,45 @@ private async Task ProcessRequestAsync(
344
345
{
345
346
DecoderOptions decoderOptions = new ( ) { Configuration = this . options . Configuration } ;
346
347
347
- // TODO: We need some way to set options based upon processors.
348
- await this . options . OnBeforeLoadAsync . Invoke ( httpContext , decoderOptions ) ;
348
+ // TODO: Do we need some way to set options based upon processors?
349
+ await this . options . OnBeforeLoadAsync . Invoke ( imageCommandContext , decoderOptions ) ;
349
350
350
- // No commands? We simply copy the stream across.
351
- if ( commands . Count == 0 )
351
+ FormattedImage ? image = null ;
352
+ try
352
353
{
353
- await inStream . CopyToAsync ( outStream ) ;
354
- outStream . Position = 0 ;
355
- format = await Image . DetectFormatAsync ( decoderOptions , outStream ) ;
356
- }
357
- else
358
- {
359
- FormattedImage ? image = null ;
360
- try
354
+ // Now we can finally process the image.
355
+ // We first sort the processor collection by command order then use that collection to determine whether the decoded image pixel format
356
+ // explicitly requires an alpha component in order to allow correct processing.
357
+ //
358
+ // The non-generic variant will decode to the correct pixel format based upon the encoded image metadata which can yield
359
+ // massive memory savings.
360
+ IReadOnlyList < ( int Index , IImageWebProcessor Processor ) > sortedProcessors = this . processors . OrderBySupportedCommands ( commands ) ;
361
+ bool requiresAlpha = sortedProcessors . RequiresTrueColorPixelFormat ( commands , this . commandParser , this . parserCulture ) ;
362
+
363
+ if ( requiresAlpha )
361
364
{
362
- // Now we can finally process the image.
363
- // We first sort the processor collection by command order then use that collection to determine whether the decoded image pixel format
364
- // explicitly requires an alpha component in order to allow correct processing.
365
- //
366
- // The non-generic variant will decode to the correct pixel format based upon the encoded image metadata which can yield
367
- // massive memory savings.
368
- IReadOnlyList < ( int Index , IImageWebProcessor Processor ) > sortedProcessors = this . processors . OrderBySupportedCommands ( commands ) ;
369
- bool requiresAlpha = sortedProcessors . RequiresTrueColorPixelFormat ( commands , this . commandParser , this . parserCulture ) ;
370
-
371
- if ( requiresAlpha )
372
- {
373
- image = await FormattedImage . LoadAsync < Rgba32 > ( decoderOptions , inStream ) ;
374
- }
375
- else
376
- {
377
- image = await FormattedImage . LoadAsync ( decoderOptions , inStream ) ;
378
- }
379
-
380
- image . Process (
381
- this . logger ,
382
- sortedProcessors ,
383
- commands ,
384
- this . commandParser ,
385
- this . parserCulture ) ;
386
-
387
- await this . options . OnBeforeSaveAsync . Invoke ( image ) ;
388
-
389
- image . Save ( outStream ) ;
390
- format = image . Format ;
365
+ image = await FormattedImage . LoadAsync < Rgba32 > ( decoderOptions , inStream ) ;
391
366
}
392
- finally
367
+ else
393
368
{
394
- image ? . Dispose ( ) ;
369
+ image = await FormattedImage . LoadAsync ( decoderOptions , inStream ) ;
395
370
}
371
+
372
+ image . Process (
373
+ this . logger ,
374
+ sortedProcessors ,
375
+ commands ,
376
+ this . commandParser ,
377
+ this . parserCulture ) ;
378
+
379
+ await this . options . OnBeforeSaveAsync . Invoke ( image ) ;
380
+
381
+ image . Save ( outStream ) ;
382
+ format = image . Format ;
383
+ }
384
+ finally
385
+ {
386
+ image ? . Dispose ( ) ;
396
387
}
397
388
}
398
389
0 commit comments