Skip to content

Commit 5eb5119

Browse files
Merge branch 'master' into faster-invoke
2 parents f65650d + 11caa09 commit 5eb5119

File tree

5 files changed

+124
-18
lines changed

5 files changed

+124
-18
lines changed

src/Directory.Build.targets

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@
1717
<Import Project="$(MSBuildThisFileDirectory)..\Directory.Build.targets" />
1818

1919
<ItemGroup>
20-
<PackageReference Update="Azure.Storage.Blobs" Version="12.4.0" />
21-
<PackageReference Update="Microsoft.IO.RecyclableMemoryStream" Version="1.3.5" />
20+
<PackageReference Update="Azure.Storage.Blobs" Version="12.10.0" />
21+
<PackageReference Update="Microsoft.IO.RecyclableMemoryStream" Version="2.1.3" />
2222
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
2323
<PackageReference Update="MinVer" PrivateAssets="All" Version="2.3.0" />
24-
<PackageReference Update="SixLabors.ImageSharp" Version="1.0.3" />
24+
<PackageReference Update="SixLabors.ImageSharp" Version="1.0.4" />
2525
</ItemGroup>
2626

2727
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
2828
<FrameworkReference Update="Microsoft.AspNetCore.App" />
2929
</ItemGroup>
3030

3131
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
32-
<PackageReference Update="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.0.0" />
33-
<PackageReference Update="Microsoft.AspNetCore.Http.Abstractions" Version="2.0.0" />
34-
<PackageReference Update="Microsoft.AspNetCore.Http.Extensions" Version="2.0.0" />
35-
<PackageReference Update="Microsoft.AspNetCore.WebUtilities" Version="2.0.0" />
36-
<PackageReference Update="Microsoft.Extensions.Caching.Abstractions" Version="2.0.0" />
37-
<PackageReference Update="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
38-
<PackageReference Update="Microsoft.Extensions.FileProviders.Physical" Version="2.0.0"/>
32+
<PackageReference Update="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
33+
<PackageReference Update="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
34+
<PackageReference Update="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
35+
<PackageReference Update="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
36+
<PackageReference Update="Microsoft.Extensions.Caching.Abstractions" Version="2.2.0" />
37+
<PackageReference Update="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
38+
<PackageReference Update="Microsoft.Extensions.FileProviders.Physical" Version="2.2.0"/>
3939
</ItemGroup>
4040
</Project>

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public ImageSharpMiddleware(
160160
? CultureInfo.InvariantCulture
161161
: CultureInfo.CurrentCulture;
162162

163-
var commands = new HashSet<string>();
163+
var commands = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
164164
foreach (IImageWebProcessor processor in this.processors)
165165
{
166166
foreach (string command in processor.Commands)
@@ -208,7 +208,7 @@ public async Task Invoke(HttpContext context)
208208
await this.options.OnParseCommandsAsync.Invoke(
209209
new ImageCommandContext(context, commands, this.commandParser, this.parserCulture));
210210

211-
// Get the correct service for the request.
211+
// Get the correct service for the request
212212
IImageProvider provider = null;
213213
foreach (IImageProvider resolver in this.providers)
214214
{

src/ImageSharp.Web/Processors/WebProcessingExtensions.cs

Lines changed: 56 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.Collections.Generic;
56
using System.Globalization;
67
using Microsoft.Extensions.Logging;
@@ -33,15 +34,66 @@ public static FormattedImage Process(
3334
CommandParser commandParser,
3435
CultureInfo culture)
3536
{
36-
if (commands.Count != 0)
37+
var commandKeys = new List<string>(commands.Keys);
38+
39+
foreach (IImageWebProcessor processor in processors.GetBySupportedCommands(commandKeys))
40+
{
41+
source = processor.Process(source, logger, commands, commandParser, culture);
42+
}
43+
44+
return source;
45+
}
46+
47+
/// <summary>
48+
/// Sorts the processors according to the first supported command and removes processors without supported commands.
49+
/// </summary>
50+
/// <param name="processors">The collection of available processors.</param>
51+
/// <param name="commands">The parsed collection of processing commands.</param>
52+
/// <returns>
53+
/// The sorted proccessors that supports any of the specified commands.
54+
/// </returns>
55+
public static IEnumerable<IImageWebProcessor> GetBySupportedCommands(this IEnumerable<IImageWebProcessor> processors, List<string> commands)
56+
{
57+
var indexedProcessors = new List<(int Index, IImageWebProcessor Processor)>();
58+
59+
foreach (IImageWebProcessor processor in processors)
3760
{
38-
foreach (IImageWebProcessor processor in processors)
61+
// Get index of first supported command
62+
int index = commands.FindIndex(c => processor.IsSupportedCommand(c));
63+
if (index != -1)
3964
{
40-
source = processor.Process(source, logger, commands, commandParser, culture);
65+
indexedProcessors.Add((index, processor));
4166
}
4267
}
4368

44-
return source;
69+
indexedProcessors.Sort((x, y) => x.Index.CompareTo(y.Index));
70+
71+
// Return sorted processors
72+
foreach ((int _, IImageWebProcessor processor) in indexedProcessors)
73+
{
74+
yield return processor;
75+
}
76+
}
77+
78+
/// <summary>
79+
/// Determines whether the specified command is supported by the processor
80+
/// </summary>
81+
/// <param name="processor">The processor.</param>
82+
/// <param name="command">The command.</param>
83+
/// <returns>
84+
/// <c>true</c> if the specified command is supported by the processor; otherwise, <c>false</c>.
85+
/// </returns>
86+
public static bool IsSupportedCommand(this IImageWebProcessor processor, string command)
87+
{
88+
foreach (string processorCommand in processor.Commands)
89+
{
90+
if (processorCommand.Equals(command, StringComparison.OrdinalIgnoreCase))
91+
{
92+
return true;
93+
}
94+
}
95+
96+
return false;
4597
}
4698
}
4799
}

tests/Directory.Build.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
<!-- Test Dependencies -->
2020
<ItemGroup>
21-
<PackageReference Update="Azure.Storage.Blobs" Version="12.4.0" />
22-
<PackageReference Update="BenchmarkDotNet" Version="0.12.1" />
21+
<PackageReference Update="Azure.Storage.Blobs" Version="12.10.0" />
22+
<PackageReference Update="BenchmarkDotNet" Version="0.13.1" />
2323
<PackageReference Update="Microsoft.Azure.KeyVault.Core" Version="3.0.4" />
2424
<PackageReference Update="Microsoft.Azure.Storage.Blob" Version="11.1.2" />
2525

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using SixLabors.ImageSharp.Web.Processors;
7+
using SixLabors.ImageSharp.Web.Tests.DependencyInjection;
8+
using Xunit;
9+
10+
namespace SixLabors.ImageSharp.Web.Tests.Processors
11+
{
12+
public class WebProcessingExtensionsTests
13+
{
14+
[Fact]
15+
public void WebProcessingExtensions_GetBySupportedCommands()
16+
{
17+
var processors = new IImageWebProcessor[]
18+
{
19+
new JpegQualityWebProcessor(),
20+
new ResizeWebProcessor(),
21+
new BackgroundColorWebProcessor(),
22+
new MockWebProcessor()
23+
};
24+
25+
var commands = new List<string>
26+
{
27+
ResizeWebProcessor.Width,
28+
JpegQualityWebProcessor.Quality,
29+
ResizeWebProcessor.Height
30+
};
31+
32+
var supportedProcessors = WebProcessingExtensions.GetBySupportedCommands(processors, commands).ToArray();
33+
34+
Assert.Equal(2, supportedProcessors.Length);
35+
Assert.IsType<ResizeWebProcessor>(supportedProcessors[0]);
36+
Assert.IsType<JpegQualityWebProcessor>(supportedProcessors[1]);
37+
}
38+
39+
[Fact]
40+
public void WebProcessingExtensions_GetBySupportedCommands_Empty()
41+
{
42+
var processors = new IImageWebProcessor[]
43+
{
44+
new MockWebProcessor()
45+
};
46+
47+
var commands = new List<string>();
48+
49+
var supportedProcessors = WebProcessingExtensions.GetBySupportedCommands(processors, commands).ToArray();
50+
51+
Assert.Empty(supportedProcessors);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)