@@ -42,86 +42,69 @@ public async Task Invoke(HttpContext httpContext)
4242
4343 var myAssembly = typeof ( BatchEngineSwaggerMiddleware ) . GetTypeInfo ( ) . Assembly ;
4444
45- using ( var stream = myAssembly . GetManifestResourceStream ( filePath ) )
45+ using var stream = myAssembly . GetManifestResourceStream ( filePath ) ;
46+ if ( options . ResolveCustomResource == null )
4647 {
47- if ( options . ResolveCustomResource == null )
48+ if ( stream == null )
4849 {
49- if ( stream == null )
50- {
51- // not found, standard request.
52- await next ( httpContext ) ;
53- return ;
54- }
50+ // not found, standard request.
51+ await next ( httpContext ) ;
52+ return ;
53+ }
5554
56- httpContext . Response . Headers [ "Content-Type" ] = new [ ] { mediaType } ;
57- httpContext . Response . StatusCode = 200 ;
58- var response = httpContext . Response . Body ;
59- await stream . CopyToAsync ( response ) ;
55+ httpContext . Response . Headers [ "Content-Type" ] = new [ ] { mediaType } ;
56+ httpContext . Response . StatusCode = 200 ;
57+ var response = httpContext . Response . Body ;
58+ await stream . CopyToAsync ( response ) ;
59+ }
60+ else
61+ {
62+ byte [ ] bytes ;
63+ if ( stream == null )
64+ {
65+ bytes = options . ResolveCustomResource ( path , null ) ;
6066 }
6167 else
6268 {
63- byte [ ] bytes ;
64- if ( stream == null )
65- {
66- bytes = options . ResolveCustomResource ( path , null ) ;
67- }
68- else
69- {
70- using ( var ms = new MemoryStream ( ) )
71- {
72- await stream . CopyToAsync ( ms ) ;
73- bytes = options . ResolveCustomResource ( path , ms . ToArray ( ) ) ;
74- }
75- }
76-
77- if ( bytes == null )
78- {
79- // not found, standard request.
80- await next ( httpContext ) ;
81- return ;
82- }
69+ using var ms = new MemoryStream ( ) ;
70+ await stream . CopyToAsync ( ms ) ;
71+ bytes = options . ResolveCustomResource ( path , ms . ToArray ( ) ) ;
72+ }
8373
84- httpContext . Response . Headers [ "Content-Type" ] = new [ ] { mediaType } ;
85- httpContext . Response . StatusCode = 200 ;
86- var response = httpContext . Response . Body ;
87- await response . WriteAsync ( bytes , 0 , bytes . Length ) ;
74+ if ( bytes == null )
75+ {
76+ // not found, standard request.
77+ await next ( httpContext ) ;
78+ return ;
8879 }
80+
81+ httpContext . Response . Headers [ "Content-Type" ] = new [ ] { mediaType } ;
82+ httpContext . Response . StatusCode = 200 ;
83+ var response = httpContext . Response . Body ;
84+ await response . WriteAsync ( bytes , 0 , bytes . Length ) ;
8985 }
9086 }
9187
92- static string GetMediaType ( string path )
88+ private static string GetMediaType ( string path )
9389 {
9490 var extension = path . Split ( '.' ) . Last ( ) ;
9591
96- switch ( extension )
92+ return extension switch
9793 {
98- case "css" :
99- return "text/css" ;
100- case "js" :
101- return "text/javascript" ;
102- case "json" :
103- return "application/json" ;
104- case "gif" :
105- return "image/gif" ;
106- case "png" :
107- return "image/png" ;
108- case "eot" :
109- return "application/vnd.ms-fontobject" ;
110- case "woff" :
111- return "application/font-woff" ;
112- case "woff2" :
113- return "application/font-woff2" ;
114- case "otf" :
115- return "application/font-sfnt" ;
116- case "ttf" :
117- return "application/font-sfnt" ;
118- case "svg" :
119- return "image/svg+xml" ;
120- case "ico" :
121- return "image/x-icon" ;
122- default :
123- return "text/html" ;
124- }
94+ "css" => "text/css" ,
95+ "js" => "text/javascript" ,
96+ "json" => "application/json" ,
97+ "gif" => "image/gif" ,
98+ "png" => "image/png" ,
99+ "eot" => "application/vnd.ms-fontobject" ,
100+ "woff" => "application/font-woff" ,
101+ "woff2" => "application/font-woff2" ,
102+ "otf" => "application/font-sfnt" ,
103+ "ttf" => "application/font-sfnt" ,
104+ "svg" => "image/svg+xml" ,
105+ "ico" => "image/x-icon" ,
106+ _ => "text/html" ,
107+ } ;
125108 }
126109 }
127110}
0 commit comments