Skip to content

Commit bd2d5e6

Browse files
authored
Merge pull request #4971 from Particular/john/zip
Zip report files
2 parents 8bb7a61 + d92cece commit bd2d5e6

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/Particular.LicensingComponent/WebApi/LicensingController.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Particular.LicensingComponent.WebApi
22
{
3+
using System.IO.Compression;
34
using System.Text.Json;
45
using System.Threading;
56
using Contracts;
@@ -42,17 +43,28 @@ public async Task<ReportGenerationState> CanThroughputReportBeGenerated(Cancella
4243
public async Task<IActionResult> GetThroughputReportFile([FromQuery(Name = "spVersion")] string? spVersion, CancellationToken cancellationToken)
4344
{
4445
var reportStatus = await CanThroughputReportBeGenerated(cancellationToken);
45-
if (reportStatus.ReportCanBeGenerated)
46+
if (!reportStatus.ReportCanBeGenerated)
4647
{
47-
var report = await throughputCollector.GenerateThroughputReport(
48-
spVersion ?? "Unknown",
49-
null,
50-
cancellationToken);
48+
return BadRequest($"Report cannot be generated - {reportStatus.Reason}");
49+
}
50+
51+
var report = await throughputCollector.GenerateThroughputReport(
52+
spVersion ?? "Unknown",
53+
null,
54+
cancellationToken);
5155

52-
return File(JsonSerializer.SerializeToUtf8Bytes(report, SerializationOptions.IndentedWithNoEscaping), "application/json", fileDownloadName: $"{report.ReportData.CustomerName}.throughput-report-{report.ReportData.EndTime:yyyyMMdd-HHmmss}.json");
56+
var fileName = $"{report.ReportData.CustomerName}.throughput-report-{report.ReportData.EndTime:yyyyMMdd-HHmmss}";
57+
58+
using var memoryStream = new MemoryStream();
59+
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
60+
{
61+
var entry = archive.CreateEntry($"{fileName}.json");
62+
await using var entryStream = entry.Open();
63+
await JsonSerializer.SerializeAsync(entryStream, report, SerializationOptions.IndentedWithNoEscaping, cancellationToken);
5364
}
5465

55-
return BadRequest($"Report cannot be generated - {reportStatus.Reason}");
66+
memoryStream.Position = 0;
67+
return File(memoryStream, "application/zip", fileDownloadName: $"{fileName}.zip");
5668
}
5769

5870
[Route("settings/info")]

0 commit comments

Comments
 (0)