@@ -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 {
@@ -92,13 +97,17 @@ public bool TryGetExtensionFromUri(string uri, [NotNullWhen(true)] out string? e
9297 {
9398 if ( pathExtension . Equals ( e , StringComparison . OrdinalIgnoreCase ) )
9499 {
95- extension = e ;
100+ // We've found a valid extension in the path, however we do not
101+ // want to overwrite an existing extension.
102+ extension ??= e ;
96103 return true ;
97104 }
98105 }
106+
107+ return false ;
99108 }
100109
101- return false ;
110+ return extension != null ;
102111 }
103112
104113 /// <summary>
0 commit comments