@@ -19,39 +19,35 @@ namespace SixLabors.ImageSharp.Web
1919 /// </summary>
2020 public sealed class FormatUtilities
2121 {
22- private readonly List < string > fileExtensions = new List < string > ( ) ;
23- private readonly Dictionary < string , string > fileExtension = new Dictionary < string , string > ( ) ;
22+ private readonly List < string > extensions = new ( ) ;
23+ private readonly Dictionary < string , string > extensionsByMimeType = new ( ) ;
2424
2525 /// <summary>
26- /// Initializes a new instance of the <see cref="FormatUtilities"/> class.
26+ /// Initializes a new instance of the <see cref="FormatUtilities" /> class.
2727 /// </summary>
2828 /// <param name="options">The middleware options.</param>
2929 public FormatUtilities ( IOptions < ImageSharpMiddlewareOptions > options )
3030 {
3131 Guard . NotNull ( options , nameof ( options ) ) ;
3232
33- // The formats contained in the configuration are used a lot in hash generation
34- // so we need them to be enumerated to remove allocations and allow indexing.
35- IImageFormat [ ] imageFormats = options . Value . Configuration . ImageFormats . ToArray ( ) ;
36-
37- for ( int i = 0 ; i < imageFormats . Length ; i ++ )
33+ foreach ( IImageFormat imageFormat in options . Value . Configuration . ImageFormats )
3834 {
39- string [ ] extensions = imageFormats [ i ] . FileExtensions . ToArray ( ) ;
35+ string [ ] extensions = imageFormat . FileExtensions . ToArray ( ) ;
4036
4137 foreach ( string extension in extensions )
4238 {
43- this . fileExtensions . Add ( extension ) ;
39+ this . extensions . Add ( extension ) ;
4440 }
4541
46- this . fileExtension [ imageFormats [ i ] . DefaultMimeType ] = extensions [ 0 ] ;
42+ this . extensionsByMimeType [ imageFormat . DefaultMimeType ] = extensions [ 0 ] ;
4743 }
4844 }
4945
5046 /// <summary>
5147 /// Gets the file extension for the given image uri.
5248 /// </summary>
5349 /// <param name="uri">The full request uri.</param>
54- /// <returns>The <see cref="string"/>.</returns>
50+ /// <returns>The <see cref="string" />.</returns>
5551 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
5652 public string GetExtensionFromUri ( string uri )
5753 {
@@ -75,13 +71,13 @@ public string GetExtensionFromUri(string uri)
7571 int extensionIndex ;
7672 if ( ( extensionIndex = path . LastIndexOf ( '.' ) ) != - 1 )
7773 {
78- ReadOnlySpan < char > extension = path . Slice ( extensionIndex + 1 ) ;
74+ ReadOnlySpan < char > pathExtension = path . Slice ( extensionIndex + 1 ) ;
7975
80- foreach ( string ext in this . fileExtensions )
76+ foreach ( string extension in this . extensions )
8177 {
82- if ( extension . Equals ( ext , StringComparison . OrdinalIgnoreCase ) )
78+ if ( pathExtension . Equals ( extension , StringComparison . OrdinalIgnoreCase ) )
8379 {
84- return ext ;
80+ return extension ;
8581 }
8682 }
8783 }
@@ -93,8 +89,8 @@ public string GetExtensionFromUri(string uri)
9389 /// Gets the correct extension for the given content type (mime-type).
9490 /// </summary>
9591 /// <param name="contentType">The content type (mime-type).</param>
96- /// <returns>The <see cref="string"/>.</returns>
92+ /// <returns>The <see cref="string" />.</returns>
9793 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
98- public string GetExtensionFromContentType ( string contentType ) => this . fileExtension [ contentType ] ;
94+ public string GetExtensionFromContentType ( string contentType ) => this . extensionsByMimeType [ contentType ] ;
9995 }
10096}
0 commit comments