Skip to content

Commit

Permalink
Download reports
Browse files Browse the repository at this point in the history
Related work items: #491
  • Loading branch information
mg-dgsspa committed Oct 9, 2024
1 parent f59a65a commit 0f6ec4d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class FattureAccontoFatturaExcelPersistence(FattureAccontoExcelQuery comm
{
var anno = _command.Anno;
var mese = _command.Mese;
var where = " where n.year= @anno and n.month=@mese ";
var where = " where f.Anno= @anno and f.mese=@mese ";

if (!_command.IdEnti!.IsNullNotAny())
where = " AND t.FKIdEnte in @IdEnti ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static IServiceCollection AddModules(this WebApplicationBuilder builder)
var configuration = builder.Configuration;
var isProd = builder.Environment.IsProduction();

builder.WebHost.ConfigureKestrel(options =>
{
options.Configure(configuration.GetSection("Kestrel"));
});

PortaleFattureOptions options = new();
configuration.GetSection(nameof(PortaleFattureOptions)).Bind(options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,30 @@ public static NotificaQueryGetByListaEntiv2 Mapv2(this NotificheRicercaRequestPa

public static async Task Download(this HttpContext context, byte[]? data, string mime, string filename)
{

context.Response.ContentType = mime;
var totalBytes = data!.Length;
context.Response.ContentType = mime;
context.Response.Headers.TryAdd("Content-Disposition", $"attachment; filename={filename}");

const int bufferSize = 16 * 1024;
context.Response.Headers["Content-Length"] = totalBytes.ToString();

const int bufferSize = 32 * 1024;
var buffer = new byte[bufferSize];

var totalBytes = data!.Length;
var bytesRemaining = totalBytes;
var offset = 0;

var offset = 0;

int flushCounter = 0;
while (bytesRemaining > 0)
{
var chunkSize = Math.Min(bufferSize, bytesRemaining);
Buffer.BlockCopy(data, offset, buffer, 0, chunkSize);
await context.Response.Body.WriteAsync(buffer, 0, chunkSize);
await context.Response.Body.FlushAsync();
await context.Response.Body.WriteAsync(buffer, 0, chunkSize);

flushCounter++;
if (flushCounter % 10 == 0)
{
await context.Response.Body.FlushAsync();
}

offset += chunkSize;
bytesRemaining -= chunkSize;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Globalization;
using System.Collections;
using System.Globalization;
using System.IO;
using CsvHelper;
using MediatR;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -140,7 +142,7 @@ private async Task<Results<Ok<Contestazione>, NotFound>> UpdatePagoPAContestazio
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
private async Task GetPagoPANotificheRicercaDocumentAsync(
private async Task<IResult> GetPagoPANotificheRicercaDocumentAsync(
HttpContext context,
[FromBody] NotificheRicercaRequestPagoPA request,
[FromServices] IStringLocalizer<Localization> localizer,
Expand All @@ -151,15 +153,29 @@ private async Task GetPagoPANotificheRicercaDocumentAsync(
var notifiche = await handler.Send(request.Map(authInfo, null, null));
if (notifiche == null || notifiche.Count == 0)
{
await Results.NotFound("Data not found").ExecuteAsync(context);
return;
//await Results.NotFound("Data not found").ExecuteAsync(context);
//return;
return NotFound();
}

var data = await notifiche.Notifiche!.ToArray<SimpleNotificaDto, SimpleNotificaPagoPADtoMap>();

if (data == null || data.Length == 0)
{
//await Results.NotFound("Data not found").ExecuteAsync(context);
//return;
return NotFound();
}

var filename = $"{Guid.NewGuid()}.csv";
var mimeCsv = "text/csv";
await context.Download(data, mimeCsv, filename);
var mimeCsv = "text/csv";
//await context.Download(data, mimeCsv, filename);
var stream = new MemoryStream(data!);
stream.Position = 0;
// Now you can use the stream
// For example, returning it in an ASP.NET Core action
return Results.Stream(stream, mimeCsv, filename);
//return Results.File(data!, mimeCsv, filename);
}
#endregion

Expand Down
11 changes: 10 additions & 1 deletion src/Presentation/PortaleFatture.BE.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@
"Synapse:PipelineNameSAP": "PIPELINE_NAME_SAP",
"Synapse:SubscriptionId": "SYNAPSE_SUBSCRIPTIONID",
"Synapse:ResourceGroupName": "SYNAPSE_RESOURCEGROUPNAME"
}
},
"Kestrel": {
"Limits": {
"MaxRequestBodySize": 2147483648,
"RequestTimeout": "00:10:00",
"MaxResponseBufferSize": 1048576,
"KeepAliveTimeout": "00:10:00",
"RequestHeadersTimeout": "00:10:00"
}
}
}

0 comments on commit 0f6ec4d

Please sign in to comment.