#if WINDOWS
using EnvelopeGenerator.Application.Configs;
using MediatR;
using Microsoft.Extensions.Logging;
namespace EnvelopeGenerator.Application.Pdf.Behaviors;
///
///
///
public class WritePdfBehavior : IPipelineBehavior
{
private readonly ISender _sender;
private readonly ILogger _logger;
///
///
///
///
///
public WritePdfBehavior(ISender sender, ILogger logger)
{
_sender = sender;
_logger = logger;
}
///
///
///
///
///
///
///
public async Task Handle(BurnPdfCommand request, RequestHandlerDelegate next, CancellationToken cancel)
{
var docResult = await next(cancel);
var config = await _sender.ReadDefaultConfigAsync(cancel);
var exportPath = config.ExportPath ?? throw new InvalidOperationException(nameof(WritePdfBehavior) + " is not possible."
+ "No export path found in config table.");
var dirPath = Path.Combine(exportPath, request.Envelope!.Uuid);
_logger.LogDebug("dirPath is {dirPath}", dirPath);
if (!Directory.Exists(dirPath))
{
_logger.LogDebug("Directory not existing. Creating ...");
Directory.CreateDirectory(dirPath);
}
var outputFilePath = Path.Combine(dirPath, $"{request.Envelope.Uuid}.pdf");
_logger.LogDebug("Writing finalized Pdf to disk..");
_logger.LogInformation("Output path is {outputFilePath}", outputFilePath);
await File.WriteAllBytesAsync(outputFilePath, docResult, cancel);
return docResult;
}
}
#endif