Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Apr 6, 2024
1 parent 2eff5a7 commit 8e3e80d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/BUTR.CrashReportServer/Controllers/CrashUploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
Expand Down Expand Up @@ -123,7 +124,7 @@ private async Task<IActionResult> UploadJsonAsync(CancellationToken ct)
return Ok($"{_options.BaseUri}/{idEntity.FileId}");

var json = JsonSerializer.Serialize(crashReport, _jsonSerializerOptionsWeb);
var html = CrashReportHtml.AddData(CrashReportHtml.Build(crashReport, logSources), await CompressJson(crashReport));
var html = CrashReportHtml.AddData(CrashReportHtml.Build(crashReport, logSources), await CompressJson(json));

await using var compressedHtmlStream = await _gZipCompressor.CompressAsync(html.AsStream(), ct);

Expand All @@ -138,15 +139,22 @@ private async Task<IActionResult> UploadJsonAsync(CancellationToken ct)
return Ok($"{_options.BaseUri}/{idEntity.FileId}");
}

private static async Task<string> CompressJson(CrashReportModel crashReport)
private static async Task<string> CompressJson(string jsonModel)
{
using var compressedBase64Stream = new MemoryStream();

await using (var base64Stream = new CryptoStream(compressedBase64Stream, new ToBase64Transform(), CryptoStreamMode.Write, true))
await using (var compressorStream = new GZipStream(base64Stream, CompressionLevel.Optimal, true))
await JsonSerializer.SerializeAsync(compressorStream, crashReport, _jsonSerializerOptionsWeb);
compressedBase64Stream.Seek(0, SeekOrigin.Begin);
using var streamReader = new StreamReader(compressedBase64Stream);
return await streamReader.ReadToEndAsync();
await using (var streamWriter = new StreamWriter(compressorStream, Encoding.UTF8, 1024, true))
{
await streamWriter.WriteAsync(jsonModel);
}

using (var streamReader = new StreamReader(compressedBase64Stream))
{
compressedBase64Stream.Seek(0, SeekOrigin.Begin);
return await streamReader.ReadToEndAsync();
}
}

[AllowAnonymous]
Expand Down

0 comments on commit 8e3e80d

Please sign in to comment.