@@ -53,12 +53,19 @@ public FormatUtilities(IOptions<ImageSharpMiddlewareOptions> options)
5353 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
5454 public bool TryGetExtensionFromUri ( string uri , [ NotNullWhen ( true ) ] out string ? extension )
5555 {
56+ // Attempts to extract a valid image file extension from the URI.
57+ // If the path contains a recognized extension, it is used.
58+ // If the path lacks an extension and a query string is present,
59+ // the method checks for a valid 'format' parameter as a fallback.
60+ // Returns true if a supported extension is found in either location.
5661 extension = null ;
5762 int query = uri . IndexOf ( '?' ) ;
5863 ReadOnlySpan < char > path ;
5964
6065 if ( query > - 1 )
6166 {
67+ path = uri . AsSpan ( 0 , query ) ;
68+
6269 if ( uri . Contains ( FormatWebProcessor . Format , StringComparison . OrdinalIgnoreCase )
6370 && QueryHelpers . ParseQuery ( uri [ query ..] ) . TryGetValue ( FormatWebProcessor . Format , out StringValues ext ) )
6471 {
@@ -68,15 +75,13 @@ public bool TryGetExtensionFromUri(string uri, [NotNullWhen(true)] out string? e
6875 {
6976 if ( extSpan . Equals ( e , StringComparison . OrdinalIgnoreCase ) )
7077 {
78+ // We've found a valid extension in the query.
79+ // Now we need to check the path to see if there is a file extension and validate that.
7180 extension = e ;
72- return true ;
81+ break ;
7382 }
7483 }
75-
76- return false ;
7784 }
78-
79- path = uri . AsSpan ( 0 , query ) ;
8085 }
8186 else
8287 {
@@ -96,9 +101,11 @@ public bool TryGetExtensionFromUri(string uri, [NotNullWhen(true)] out string? e
96101 return true ;
97102 }
98103 }
104+
105+ return false ;
99106 }
100107
101- return false ;
108+ return extension != null ;
102109 }
103110
104111 /// <summary>
0 commit comments