|
1 | 1 | // Copyright (c) Six Labors. |
2 | 2 | // Licensed under the Apache License, Version 2.0. |
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using System.Collections.Generic; |
5 | 6 | using System.Globalization; |
6 | 7 | using Microsoft.Extensions.Logging; |
@@ -33,15 +34,66 @@ public static FormattedImage Process( |
33 | 34 | CommandParser commandParser, |
34 | 35 | CultureInfo culture) |
35 | 36 | { |
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) |
37 | 60 | { |
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) |
39 | 64 | { |
40 | | - source = processor.Process(source, logger, commands, commandParser, culture); |
| 65 | + indexedProcessors.Add((index, processor)); |
41 | 66 | } |
42 | 67 | } |
43 | 68 |
|
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; |
45 | 97 | } |
46 | 98 | } |
47 | 99 | } |
0 commit comments