[Peek]Peek and Monaco file encoding issues are solved with a encoding detector (#26955)

* [Peek] Peek and Monaco file encoding issues are solved with a encoding detector.

* [Peek] Monaco encoding parameter is moved to another function.

* [Peek] NOTICE.md update.

* [Peek] Spell Check update.

* UTF-Unknown is added to Nuget list in Notice.md

* System.Text.Encoding.CodePages is added to Nuget list in Notice.md

* [Peek] Unncessary mainfest files are deleted.

* [Peek ] Unncessary mainfest file are deleted.

* [Peek] Encoding null check is added.

* Update NOTICE.md

* Update NOTICE.md

* ci: Add signing to UtfUnknown
This commit is contained in:
gokcekantarci
2023-11-14 18:27:45 +03:00
committed by GitHub
parent 59a133b9e2
commit 89a87e6db4
9 changed files with 991 additions and 2 deletions

View File

@@ -14,6 +14,8 @@
<ItemGroup>
<PackageReference Include="Markdig.Signed" />
<PackageReference Include="System.Text.Encoding.CodePages" />
<PackageReference Include="UTF.Unknown" />
</ItemGroup>
<ItemGroup>

View File

@@ -107,6 +107,7 @@ namespace Microsoft.PowerToys.FilePreviewCommon
public static string ReadIndexHtml()
{
string html;
using (StreamReader htmlFileReader = new StreamReader(new FileStream(MonacoDirectory + "\\index.html", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
html = htmlFileReader.ReadToEnd();

View File

@@ -31,6 +31,8 @@
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" />
<PackageReference Include="SharpCompress" />
<PackageReference Include="System.Drawing.Common" />
<PackageReference Include="System.Text.Encoding.CodePages" />
<PackageReference Include="UTF.Unknown" />
<PackageReference Include="Microsoft.Windows.CsWin32">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -5,6 +5,7 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;
using UtfUnknown;
namespace Peek.FilePreviewer.Previewers
{
@@ -12,8 +13,14 @@ namespace Peek.FilePreviewer.Previewers
{
public static async Task<string> Read(string path)
{
DetectionResult result = CharsetDetector.DetectFromFile(path);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Check if the detected encoding is not null, otherwise default to UTF-8
Encoding encodingToUse = result.Detected?.Encoding ?? Encoding.UTF8;
using var fs = OpenReadOnly(path);
using var sr = new StreamReader(fs, Encoding.UTF8);
using var sr = new StreamReader(fs, encodingToUse);
string content = await sr.ReadToEndAsync();
return content;

View File

@@ -4,11 +4,13 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using Common;
using ManagedCommon;
using Microsoft.PowerToys.PreviewHandler.Monaco.Properties;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using UtfUnknown;
using Windows.System;
namespace Microsoft.PowerToys.PreviewHandler.Monaco
@@ -358,7 +360,13 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
Logger.LogInfo("Starting getting monaco language id out of filetype");
_vsCodeLangSet = FileHandler.GetLanguage(Path.GetExtension(filePath));
using (StreamReader fileReader = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
DetectionResult result = CharsetDetector.DetectFromFile(filePath);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Check if the detected encoding is not null, otherwise default to UTF-8
Encoding encodingToUse = result.Detected?.Encoding ?? Encoding.UTF8;
using (StreamReader fileReader = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), encodingToUse))
{
Logger.LogInfo("Starting reading requested file");
var fileContent = fileReader.ReadToEnd();