using Microsoft.AspNetCore.Http;
using NPin.Framework.Core.Extensions;
using Volo.Abp.DependencyInjection;

namespace NPin.Framework.AspNetCore.Microsoft.AspNetCore.Middlewares;

public class ApiInfoMiddleware : IMiddleware, ITransientDependency
{
    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        context.Response.OnStarting(() =>
        {
            if (context.Response.StatusCode == StatusCodes.Status200OK &&
                context.Response.Headers["Content-Type"].ToString() == "application/vnd.ms-excel")
            {
                context.FileAttachmentHandle($"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.xlsx");
            }

            return Task.CompletedTask;
        });
        await next(context);
    }
}