113 lines
3.5 KiB
C#
113 lines
3.5 KiB
C#
using Microsoft.Extensions.Options;
|
|
using PrintService.Models;
|
|
using System.Drawing;
|
|
using System.Drawing.Printing;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace PrintService.Services;
|
|
|
|
/// <summary>
|
|
/// Processes documents and renders them for printing
|
|
/// </summary>
|
|
public class DocumentProcessor
|
|
{
|
|
private readonly AppSettings _settings;
|
|
private readonly ConfigurationService _configService;
|
|
private readonly ILogger<DocumentProcessor> _logger;
|
|
|
|
public DocumentProcessor(
|
|
IOptions<AppSettings> settings,
|
|
ConfigurationService configService,
|
|
ILogger<DocumentProcessor> logger)
|
|
{
|
|
_settings = settings.Value;
|
|
_configService = configService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Process and transform document content
|
|
/// </summary>
|
|
public string ProcessDocument(string content, DocumentTypeConfig config)
|
|
{
|
|
var processed = content;
|
|
|
|
// Apply text transformations
|
|
foreach (var transform in config.Transformations)
|
|
{
|
|
try
|
|
{
|
|
processed = Regex.Replace(processed, transform.Pattern, transform.Replacement);
|
|
if (_settings.VerboseLogging)
|
|
{
|
|
_logger.LogDebug("Applied transformation: {Description}", transform.Description);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to apply transformation: {Pattern}", transform.Pattern);
|
|
}
|
|
}
|
|
|
|
return processed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get document configuration by type from printer-config.json
|
|
/// </summary>
|
|
public DocumentTypeConfig? GetDocumentConfig(string documentType)
|
|
{
|
|
return _configService.GetDocumentType(documentType);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read file content
|
|
/// </summary>
|
|
public string ReadFile(string filePath)
|
|
{
|
|
return File.ReadAllText(filePath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Archive or delete file after processing
|
|
/// </summary>
|
|
public void PostProcess(PrintJob job, DocumentTypeConfig config)
|
|
{
|
|
try
|
|
{
|
|
// Skip post-processing if configured (useful for testing)
|
|
if (config.SkipPostProcessing)
|
|
{
|
|
_logger.LogInformation("Skipping post-processing for job {JobId} (file remains in queue)", job.Id);
|
|
return;
|
|
}
|
|
|
|
if (config.ArchiveAfterPrint && !string.IsNullOrEmpty(config.ArchivePath))
|
|
{
|
|
Directory.CreateDirectory(config.ArchivePath);
|
|
|
|
var archiveFileName = Path.Combine(config.ArchivePath,
|
|
$"{DateTime.Now:yyyyMMdd_HHmmss}_{job.OriginalFilename}");
|
|
|
|
if (File.Exists(job.QueuedFilePath))
|
|
{
|
|
File.Move(job.QueuedFilePath, archiveFileName);
|
|
_logger.LogInformation("Archived job {JobId} to {Path}", job.Id, archiveFileName);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (File.Exists(job.QueuedFilePath))
|
|
{
|
|
File.Delete(job.QueuedFilePath);
|
|
_logger.LogInformation("Deleted file for job {JobId}", job.Id);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to post-process job {JobId}", job.Id);
|
|
}
|
|
}
|
|
}
|