Skip to content

Commit e37317e

Browse files
committed
Fix long path handling issue in HtmlViewer QL-Win#1643
1 parent 064d03e commit e37317e

3 files changed

Lines changed: 106 additions & 35 deletions

File tree

‎QuickLook.Plugin/QuickLook.Plugin.FontViewer/WebfontPanel.cs‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,4 @@ public static string ReadString(string key)
200200
using var reader = new StreamReader(ReadStream(key), Encoding.UTF8);
201201
return reader.ReadToEnd();
202202
}
203-
204-
public static class MimeTypes
205-
{
206-
public const string Html = "text/html";
207-
public const string JavaScript = "application/javascript";
208-
public const string Css = "text/css";
209-
public const string Binary = "application/octet-stream";
210-
211-
public static string GetContentType(string extension = null) => $"Content-Type: {GetMimeType(extension)}";
212-
213-
public static string GetMimeType(string extension = null) => extension?.ToLowerInvariant() switch
214-
{
215-
".js" => JavaScript, // Only handle known extensions from resources
216-
".css" => Css,
217-
".html" => Html,
218-
_ => Binary,
219-
};
220-
}
221203
}

‎QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public void View(string path, ContextObject context)
5858

5959
if (path.ToLower().EndsWith(".url"))
6060
path = Helper.GetUrlPath(path);
61+
_panel.FallbackPath = Path.GetDirectoryName(path); // Reserve opportunities for requesting exceeds MAX_PATH (260) limitation
6162
_panel.NavigateToFile(path);
6263
_panel.Dispatcher.Invoke(() => { context.IsBusy = false; }, DispatcherPriority.Loaded);
6364
}

‎QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.cs‎

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)