Skip to content

Commit

Permalink
[FileExplorer Add-ons] If SVG file is bigger than 2MB save it as html…
Browse files Browse the repository at this point in the history
… page and preview the page (#18186)

* If SVG file is bigger than 2MB save it as html page and navigate WebView2 to it

Reason: WebView2.NavigateToString() has 2MB string limitation

* Cleanup and add limitation link to comment

* Lower the limit after testing it
  • Loading branch information
stefansjfw committed May 15, 2022
1 parent 3443c73 commit 3548e68
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 19 deletions.
60 changes: 47 additions & 13 deletions src/modules/previewpane/SvgPreviewHandler/SvgPreviewControl.cs
Expand Up @@ -16,7 +16,6 @@
using Microsoft.PowerToys.Telemetry;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using PreviewHandlerCommon;

namespace Microsoft.PowerToys.PreviewHandler.Svg
{
Expand All @@ -38,15 +37,15 @@ public class SvgPreviewControl : FormHandlerControl
/// <summary>
/// Name of the virtual host
/// </summary>
public const string VirtualHostName = "PowerToysLocalSvg";
private const string VirtualHostName = "PowerToysLocalSvg";

/// <summary>
/// Gets the path of the current assembly.
/// </summary>
/// <remarks>
/// Source: https://stackoverflow.com/a/283917/14774889
/// </remarks>
public static string AssemblyDirectory
private static string AssemblyDirectory
{
get
{
Expand All @@ -67,12 +66,20 @@ public static string AssemblyDirectory
/// </summary>
private bool _infoBarAdded;

/// <summary>
/// Represent WebView2 user data folder path.
/// </summary>
private string _webView2UserDataFolder = System.Environment.GetEnvironmentVariable("USERPROFILE") +
"\\AppData\\LocalLow\\Microsoft\\PowerToys\\SvgPreview-Temp";

/// <summary>
/// Start the preview on the Control.
/// </summary>
/// <param name="dataSource">Stream reference to access source file.</param>
public override void DoPreview<T>(T dataSource)
{
CleanupWebView2UserDataFolder();

string svgData = null;
bool blocked = false;

Expand All @@ -88,9 +95,7 @@ public override void DoPreview<T>(T dataSource)

blocked = SvgPreviewHandlerHelper.CheckBlockedElements(svgData);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
PreviewError(ex, dataSource);
return;
Expand All @@ -100,9 +105,7 @@ public override void DoPreview<T>(T dataSource)
{
svgData = SvgPreviewHandlerHelper.AddStyleSVG(svgData);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewError { Message = ex.Message });
}
Expand All @@ -125,9 +128,7 @@ public override void DoPreview<T>(T dataSource)
base.DoPreview(dataSource);
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewed());
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
PreviewError(ex, dataSource);
}
Expand Down Expand Up @@ -169,8 +170,7 @@ private void AddWebViewControl(string svgData)

ConfiguredTaskAwaitable<CoreWebView2Environment>.ConfiguredTaskAwaiter
webView2EnvironmentAwaiter = CoreWebView2Environment
.CreateAsync(userDataFolder: System.Environment.GetEnvironmentVariable("USERPROFILE") +
"\\AppData\\LocalLow\\Microsoft\\PowerToys\\SvgPreview-Temp")
.CreateAsync(userDataFolder: _webView2UserDataFolder)
.ConfigureAwait(true).GetAwaiter();
webView2EnvironmentAwaiter.OnCompleted(() =>
{
Expand All @@ -183,10 +183,24 @@ private void AddWebViewControl(string svgData)
await _browser.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.addEventListener('contextmenu', window => {window.preventDefault();});");
_browser.CoreWebView2.SetVirtualHostNameToFolderMapping(VirtualHostName, AssemblyDirectory, CoreWebView2HostResourceAccessKind.Allow);
_browser.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
_browser.NavigateToString(svgData);
// WebView2.NavigateToString() limitation
// See https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.navigatetostring?view=webview2-dotnet-1.0.864.35#remarks
// While testing the limit, it turned out it is ~1.5MB, so to be on a safe side we go for 1.5m bytes
if (svgData.Length > 1_500_000)
{
string filename = _webView2UserDataFolder + "\\" + Guid.NewGuid().ToString() + ".html";
File.WriteAllText(filename, svgData);
_browser.Source = new Uri(filename);
}
else
{
_browser.NavigateToString(svgData);
}
Controls.Add(_browser);
}
catch (NullReferenceException)
catch (Exception)
{
}
});
Expand Down Expand Up @@ -224,5 +238,25 @@ private void PreviewError<T>(Exception exception, T dataSource)
AddTextBoxControl(Properties.Resource.SvgNotPreviewedError);
base.DoPreview(dataSource);
}

/// <summary>
/// Cleanup the previously created tmp html files from svg files bigger than 2MB.
/// </summary>
private void CleanupWebView2UserDataFolder()
{
try
{
// Cleanup temp dir
var dir = new DirectoryInfo(_webView2UserDataFolder);

foreach (var file in dir.EnumerateFiles("*.html"))
{
file.Delete();
}
}
catch (Exception)
{
}
}
}
}
Expand Up @@ -49,15 +49,15 @@ public class SvgThumbnailProvider : IInitializeWithStream, IThumbnailProvider, I
/// <summary>
/// Name of the virtual host
/// </summary>
public const string VirtualHostName = "PowerToysLocalSvgThumbnail";
private const string VirtualHostName = "PowerToysLocalSvgThumbnail";

/// <summary>
/// Gets the path of the current assembly.
/// </summary>
/// <remarks>
/// Source: https://stackoverflow.com/a/283917/14774889
/// </remarks>
public static string AssemblyDirectory
private static string AssemblyDirectory
{
get
{
Expand All @@ -68,6 +68,12 @@ public static string AssemblyDirectory
}
}

/// <summary>
/// Represent WebView2 user data folder path.
/// </summary>
private string _webView2UserDataFolder = System.Environment.GetEnvironmentVariable("USERPROFILE") +
"\\AppData\\LocalLow\\Microsoft\\PowerToys\\SvgThumbnailPreview-Temp";

/// <summary>
/// Render SVG using WebView2 control, capture the WebView2
/// preview and create Bitmap out of it.
Expand All @@ -76,6 +82,8 @@ public static string AssemblyDirectory
/// <param name="cx">The maximum thumbnail size, in pixels.</param>
public Bitmap GetThumbnail(string content, uint cx)
{
CleanupWebView2UserDataFolder();

if (cx == 0 || cx > MaxThumbnailSize || string.IsNullOrEmpty(content) || !content.Contains("svg"))
{
return null;
Expand Down Expand Up @@ -117,8 +125,7 @@ public Bitmap GetThumbnail(string content, uint cx)

ConfiguredTaskAwaitable<CoreWebView2Environment>.ConfiguredTaskAwaiter
webView2EnvironmentAwaiter = CoreWebView2Environment
.CreateAsync(userDataFolder: System.Environment.GetEnvironmentVariable("USERPROFILE") +
"\\AppData\\LocalLow\\Microsoft\\PowerToys\\SvgThumbnailPreview-Temp")
.CreateAsync(userDataFolder: _webView2UserDataFolder)
.ConfigureAwait(true).GetAwaiter();
webView2EnvironmentAwaiter.OnCompleted(async () =>
{
Expand All @@ -129,9 +136,22 @@ public Bitmap GetThumbnail(string content, uint cx)
await _browser.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.addEventListener('contextmenu', window => {window.preventDefault();});");
_browser.CoreWebView2.SetVirtualHostNameToFolderMapping(VirtualHostName, AssemblyDirectory, CoreWebView2HostResourceAccessKind.Allow);
_browser.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
_browser.NavigateToString(wrappedContent);
// WebView2.NavigateToString() limitation
// See https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.navigatetostring?view=webview2-dotnet-1.0.864.35#remarks
// While testing the limit, it turned out it is ~1.5MB, so to be on a safe side we go for 1.5m bytes
if (wrappedContent.Length > 1_500_000)
{
string filename = _webView2UserDataFolder + "\\" + Guid.NewGuid().ToString() + ".html";
File.WriteAllText(filename, wrappedContent);
_browser.Source = new Uri(filename);
}
else
{
_browser.NavigateToString(wrappedContent);
}
}
catch (NullReferenceException)
catch (Exception)
{
}
});
Expand Down Expand Up @@ -249,5 +269,25 @@ public void Dispose()
{
GC.SuppressFinalize(this);
}

/// <summary>
/// Cleanup the previously created tmp html files from svg files bigger than 2MB.
/// </summary>
private void CleanupWebView2UserDataFolder()
{
try
{
// Cleanup temp dir
var dir = new DirectoryInfo(_webView2UserDataFolder);

foreach (var file in dir.EnumerateFiles("*.html"))
{
file.Delete();
}
}
catch (Exception)
{
}
}
}
}

0 comments on commit 3548e68

Please sign in to comment.