1- // Copyright (c) Six Labors.
1+ // Copyright (c) Six Labors.
22// Licensed under the Apache License, Version 2.0.
33
44using System ;
@@ -16,7 +16,7 @@ namespace SixLabors.ImageSharp.Web.Commands
1616 /// </summary>
1717 public class PresetOnlyQueryCollectionRequestParser : IRequestParser
1818 {
19- private readonly IDictionary < string , IDictionary < string , string > > presets ;
19+ private readonly IDictionary < string , CommandCollection > presets ;
2020
2121 /// <summary>
2222 /// The command constant for the preset query parameter.
@@ -31,31 +31,33 @@ public PresetOnlyQueryCollectionRequestParser(IOptions<PresetOnlyQueryCollection
3131 this . presets = ParsePresets ( presetOptions . Value . Presets ) ;
3232
3333 /// <inheritdoc/>
34- public IDictionary < string , string > ParseRequestCommands ( HttpContext context )
34+ public CommandCollection ParseRequestCommands ( HttpContext context )
3535 {
3636 if ( context . Request . Query . Count == 0 || ! context . Request . Query . ContainsKey ( QueryKey ) )
3737 {
38- return new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
38+ return new ( ) ;
3939 }
4040
41- var requestedPreset = context . Request . Query [ "preset" ] [ 0 ] ;
42- return this . presets . GetValueOrDefault ( requestedPreset ) ?? new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
41+ string requestedPreset = context . Request . Query [ "preset" ] [ 0 ] ;
42+ return this . presets . GetValueOrDefault ( requestedPreset ) ?? new ( ) ;
4343 }
4444
45- private static IDictionary < string , IDictionary < string , string > > ParsePresets (
45+ private static IDictionary < string , CommandCollection > ParsePresets (
4646 IDictionary < string , string > unparsedPresets ) =>
4747 unparsedPresets
4848 . Select ( keyValue =>
49- new KeyValuePair < string , IDictionary < string , string > > ( keyValue . Key , ParsePreset ( keyValue . Value ) ) )
49+ new KeyValuePair < string , CommandCollection > ( keyValue . Key , ParsePreset ( keyValue . Value ) ) )
5050 . ToDictionary ( keyValue => keyValue . Key , keyValue => keyValue . Value , StringComparer . OrdinalIgnoreCase ) ;
5151
52- private static IDictionary < string , string > ParsePreset ( string unparsedPresetValue )
52+ private static CommandCollection ParsePreset ( string unparsedPresetValue )
5353 {
54+ // TODO: Investigate skipping the double allocation here.
55+ // In .NET 6 we can directly use the QueryStringEnumerable type and enumerate stright to our command collection
5456 Dictionary < string , StringValues > parsed = QueryHelpers . ParseQuery ( unparsedPresetValue ) ;
55- var transformed = new Dictionary < string , string > ( parsed . Count , StringComparer . OrdinalIgnoreCase ) ;
56- foreach ( KeyValuePair < string , StringValues > keyValue in parsed )
57+ CommandCollection transformed = new ( ) ;
58+ foreach ( KeyValuePair < string , StringValues > pair in parsed )
5759 {
58- transformed [ keyValue . Key ] = keyValue . Value . ToString ( ) ;
60+ transformed . Add ( new ( pair . Key , pair . Value . ToString ( ) ) ) ;
5961 }
6062
6163 return transformed ;
0 commit comments