Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: "blazor.boot.json.gz" and "blazor.boot.json.br" are removed undesirably #32

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

Expand Down
47 changes: 41 additions & 6 deletions src/BlazorWasmAntivirusProtection.Tasks/RenameDlls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class RenameDlls : Task

public string RenameDllsTo { get; set; } = "bin";
public bool DisableRenamingDlls { get; set; }
public bool BlazorEnableCompression { get; set; } = true;
public string CompressionLevel { get; set; }

public override bool Execute()
{
Expand Down Expand Up @@ -75,15 +77,15 @@ public override bool Execute()
File.WriteAllText(serviceWorkerPathAssets, serviceWorkerAssets);
}

if (File.Exists(bootJsonGzPath))
if (File.Exists(bootJsonGzPath) && BlazorEnableCompression)
{
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Deleting \"{bootJsonGzPath}\"");
File.Delete(bootJsonGzPath);
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Recompressing \"{bootJsonGzPath}\"");
GZipCompress(bootJsonPath, bootJsonGzPath);
}
if (File.Exists(bootJsonBrPath))
if (File.Exists(bootJsonBrPath) && BlazorEnableCompression)
{
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Deleting \"{bootJsonBrPath}\"");
File.Delete(bootJsonBrPath);
Log.LogMessage(MessageImportance.High, $"BlazorWasmAntivirusProtection: Recompressing \"{bootJsonBrPath}\"");
BrotliCompress(bootJsonPath, bootJsonBrPath);
}
}

Expand All @@ -92,6 +94,39 @@ public override bool Execute()
return true;
}

private void GZipCompress(string bootJsonPath, string bootJsonGzPath)
{
try
{
File.Delete(bootJsonGzPath);
using var fileStream = File.OpenRead(bootJsonPath);
using var stream = File.Create(bootJsonGzPath);
using var destination = new GZipStream(stream, System.IO.Compression.CompressionLevel.Optimal);
fileStream.CopyTo(destination);
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
}
}

private void BrotliCompress(string bootJsonPath, string bootJsonBrPath)
{
try
{
File.Delete(bootJsonBrPath);
var compressionLevel = Enum.TryParse<CompressionLevel>(CompressionLevel, out var level) ? level : System.IO.Compression.CompressionLevel.Optimal;
using var fileStream = File.OpenRead(bootJsonPath);
using var stream = File.Create(bootJsonBrPath);
using var destination = new BrotliStream(stream, compressionLevel);
fileStream.CopyTo(destination);
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
}
}

string ComputeSha256Hash(string rawData)
{
using var sha256Hash = SHA256.Create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<!-- Runs in the published project (server if hosted)-->
<Target Name="_ChangeDLLFileExtensions" AfterTargets="Publish">
<RenameDlls PublishDir="$(PublishDir)" RenameDllsTo="$(RenameDllsTo)" DisableRenamingDlls="$(DisableRenamingDlls)"></RenameDlls>
<RenameDlls PublishDir="$(PublishDir)" RenameDllsTo="$(RenameDllsTo)" DisableRenamingDlls="$(DisableRenamingDlls)" BlazorEnableCompression="$(BlazorEnableCompression)" CompressionLevel="$(_BlazorBrotliCompressionLevel)"></RenameDlls>
</Target>

</Project>