@@ -191,28 +191,42 @@ protected virtual void WebView_WebResourceRequested(object sender, CoreWebView2W
191191 {
192192 var requestedUri = new Uri ( args . Request . Uri ) ;
193193
194- // Check if the request is for a local file
195- if ( requestedUri . Scheme == "file" && ! File . Exists ( requestedUri . LocalPath ) )
194+ if ( requestedUri . Scheme == "file" )
196195 {
197- // Try loading from fallback directory
198- var fileName = Path . GetFileName ( requestedUri . LocalPath ) ;
199- var fileDirectoryName = Path . GetDirectoryName ( requestedUri . LocalPath ) ;
200-
201- // Convert the primary path to fallback path
202- if ( fileDirectoryName . StartsWith ( _primaryPath ) )
196+ // Check if the request is for a local file
197+ if ( ! File . Exists ( requestedUri . LocalPath ) )
203198 {
204- var fallbackFilePath = Path . Combine (
205- _fallbackPath . Trim ( '/' , '\\ ' ) , // Make it combinable
206- fileDirectoryName . Substring ( _primaryPath . Length ) . Trim ( '/' , '\\ ' ) , // Make it combinable
207- fileName
208- ) ;
199+ // Try loading from fallback directory
200+ var fileName = Path . GetFileName ( requestedUri . LocalPath ) ;
201+ var fileDirectoryName = Path . GetDirectoryName ( requestedUri . LocalPath ) ;
202+
203+ // Convert the primary path to fallback path
204+ if ( fileDirectoryName . StartsWith ( _primaryPath ) )
205+ {
206+ var fallbackFilePath = Path . Combine (
207+ _fallbackPath . Trim ( '/' , '\\ ' ) , // Make it combinable
208+ fileDirectoryName . Substring ( _primaryPath . Length ) . Trim ( '/' , '\\ ' ) , // Make it combinable
209+ fileName
210+ ) ;
209211
210- if ( File . Exists ( fallbackFilePath ) )
212+ if ( File . Exists ( fallbackFilePath ) )
213+ {
214+ // Serve the file from the fallback directory
215+ var fileStream = new FileStream ( fallbackFilePath , FileMode . Open , FileAccess . Read , FileShare . ReadWrite | FileShare . Delete ) ;
216+ var response = _webView . CoreWebView2 . Environment . CreateWebResourceResponse (
217+ fileStream , 200 , "OK" , "Content-Type: application/octet-stream" ) ;
218+ args . Response = response ;
219+ }
220+ }
221+ }
222+ // Check if the request exceeds MAX_PATH (260) limitation
223+ else if ( requestedUri . LocalPath . Length >= 260 )
224+ {
225+ if ( File . Exists ( requestedUri . LocalPath ) )
211226 {
212- // Serve the file from the fallback directory
213- var fileStream = new FileStream ( fallbackFilePath , FileMode . Open , FileAccess . Read , FileShare . ReadWrite | FileShare . Delete ) ;
227+ var fileStream = new FileStream ( requestedUri . LocalPath , FileMode . Open , FileAccess . Read , FileShare . ReadWrite | FileShare . Delete ) ;
214228 var response = _webView . CoreWebView2 . Environment . CreateWebResourceResponse (
215- fileStream , 200 , "OK" , "Content-Type: application/octet-stream" ) ;
229+ fileStream , 200 , "OK" , MimeTypes . GetContentType ( Path . GetExtension ( requestedUri . LocalPath ) ) ) ;
216230 args . Response = response ;
217231 }
218232 }
@@ -245,4 +259,78 @@ private object CreateDownloadButton()
245259
246260 return button ;
247261 }
262+
263+ public static class MimeTypes
264+ {
265+ public const string Html = "text/html" ;
266+ public const string JavaScript = "application/javascript" ;
267+ public const string Css = "text/css" ;
268+ public const string Json = "application/json" ;
269+ public const string Xml = "application/xml" ;
270+ public const string Svg = "image/svg+xml" ;
271+ public const string Png = "image/png" ;
272+ public const string Jpeg = "image/jpeg" ;
273+ public const string Gif = "image/gif" ;
274+ public const string Webp = "image/webp" ;
275+ public const string Ico = "image/x-icon" ;
276+ public const string Avif = "image/avif" ;
277+ public const string Woff = "font/woff" ;
278+ public const string Woff2 = "font/woff2" ;
279+ public const string Ttf = "font/ttf" ;
280+ public const string Otf = "font/otf" ;
281+ public const string Mp3 = "audio/mpeg" ;
282+ public const string Mp4 = "video/mp4" ;
283+ public const string Webm = "video/webm" ;
284+ public const string Pdf = "application/pdf" ;
285+ public const string Binary = "application/octet-stream" ;
286+ public const string Text = "text/plain" ;
287+
288+ public static string GetContentType ( string extension = null ) => $ "Content-Type: { GetMimeType ( extension ) } ";
289+
290+ /// <summary>
291+ /// Only handle known extensions from resources
292+ /// </summary>
293+ public static string GetMimeType ( string extension = null ) => extension ? . ToLowerInvariant ( ) switch
294+ {
295+ // Core web files
296+ ".html" or ".htm" => Html ,
297+ ".js" => JavaScript ,
298+ ".css" => Css ,
299+ ".json" => Json ,
300+ ".xml" => Xml ,
301+
302+ // Images
303+ ".png" => Png ,
304+ ".jpg" or ".jpeg" => Jpeg ,
305+ ".gif" => Gif ,
306+ ".webp" => Webp ,
307+ ".svg" => Svg ,
308+ ".ico" => Ico ,
309+ ".avif" => Avif ,
310+
311+ // Fonts
312+ ".woff" => Woff ,
313+ ".woff2" => Woff2 ,
314+ ".ttf" => Ttf ,
315+ ".otf" => Otf ,
316+
317+ // Media
318+ ".mp3" => Mp3 ,
319+ ".mp4" => Mp4 ,
320+ ".webm" => Webm ,
321+
322+ // Documents
323+ ".pdf" => Pdf ,
324+ ".txt" => Text ,
325+
326+ // Archives
327+ ".zip" => "application/zip" ,
328+ ".gz" => "application/gzip" ,
329+ ".rar" => "application/vnd.rar" ,
330+ ".7z" => "application/x-7z-compressed" ,
331+
332+ // Default
333+ _ => Binary ,
334+ } ;
335+ }
248336}
0 commit comments