- Implements WritePdfBehavior as an IPipelineBehavior for BurnPdfCommand - Writes generated PDF to configured export path - Automatically creates export directory if missing - Adds logging for export path and file operations
64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
#if WINDOWS
|
|
using EnvelopeGenerator.Application.Configs;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace EnvelopeGenerator.Application.Pdf.Behaviors;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class WritePdfBehavior : IPipelineBehavior<BurnPdfCommand, byte[]>
|
|
{
|
|
private readonly ISender _sender;
|
|
|
|
private readonly ILogger<WritePdfBehavior> _logger;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="logger"></param>
|
|
public WritePdfBehavior(ISender sender, ILogger<WritePdfBehavior> logger)
|
|
{
|
|
_sender = sender;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="next"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<byte[]> Handle(BurnPdfCommand request, RequestHandlerDelegate<byte[]> 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 |