|
1 | 1 | namespace Particular.LicensingComponent.WebApi |
2 | 2 | { |
| 3 | + using System.IO.Compression; |
3 | 4 | using System.Text.Json; |
4 | 5 | using System.Threading; |
5 | 6 | using Contracts; |
@@ -42,17 +43,28 @@ public async Task<ReportGenerationState> CanThroughputReportBeGenerated(Cancella |
42 | 43 | public async Task<IActionResult> GetThroughputReportFile([FromQuery(Name = "spVersion")] string? spVersion, CancellationToken cancellationToken) |
43 | 44 | { |
44 | 45 | var reportStatus = await CanThroughputReportBeGenerated(cancellationToken); |
45 | | - if (reportStatus.ReportCanBeGenerated) |
| 46 | + if (!reportStatus.ReportCanBeGenerated) |
46 | 47 | { |
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); |
51 | 55 |
|
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); |
53 | 64 | } |
54 | 65 |
|
55 | | - return BadRequest($"Report cannot be generated - {reportStatus.Reason}"); |
| 66 | + memoryStream.Position = 0; |
| 67 | + return File(memoryStream, "application/zip", fileDownloadName: $"{fileName}.zip"); |
56 | 68 | } |
57 | 69 |
|
58 | 70 | [Route("settings/info")] |
|
0 commit comments