FolderWatcher: gedruckte Dateien merken wenn DeleteAfterPrint=false

This commit is contained in:
administrator 2026-04-19 21:49:17 +02:00
parent 10ef84a73c
commit a35b4ada20

View file

@ -8,6 +8,7 @@ public class FolderWatcherService : IDisposable
private readonly PrintService _printService; private readonly PrintService _printService;
private readonly MailPrintOptions _options; private readonly MailPrintOptions _options;
private readonly List<FileSystemWatcher> _watchers = new(); private readonly List<FileSystemWatcher> _watchers = new();
private readonly HashSet<string> _printed = new(StringComparer.OrdinalIgnoreCase);
public FolderWatcherService( public FolderWatcherService(
ILogger<FolderWatcherService> logger, ILogger<FolderWatcherService> logger,
@ -27,6 +28,9 @@ public class FolderWatcherService : IDisposable
return; return;
} }
// Gedruckte Dateien laden (damit nach Neustart nicht nochmal gedruckt wird)
LoadPrintedLog();
foreach (var config in _options.FolderWatchers) foreach (var config in _options.FolderWatchers)
{ {
if (string.IsNullOrEmpty(config.Path) || !Directory.Exists(config.Path)) if (string.IsNullOrEmpty(config.Path) || !Directory.Exists(config.Path))
@ -73,6 +77,13 @@ public class FolderWatcherService : IDisposable
private void OnFileDetected(string filePath, FolderWatcher config) private void OnFileDetected(string filePath, FolderWatcher config)
{ {
// Bereits gedruckte Dateien überspringen (nur relevant wenn DeleteAfterPrint=false)
if (_printed.Contains(filePath))
{
_logger.LogDebug("[{Name}] Bereits gedruckt, überspringe: {File}", config.Name, filePath);
return;
}
// Kurz warten bis Datei vollständig geschrieben ist // Kurz warten bis Datei vollständig geschrieben ist
if (!WaitForFile(filePath)) if (!WaitForFile(filePath))
{ {
@ -104,6 +115,12 @@ public class FolderWatcherService : IDisposable
_logger.LogWarning(ex, "[{Name}] Löschen fehlgeschlagen: {File}", config.Name, filePath); _logger.LogWarning(ex, "[{Name}] Löschen fehlgeschlagen: {File}", config.Name, filePath);
} }
} }
else
{
// Datei bleibt merken damit sie nicht erneut gedruckt wird
_printed.Add(filePath);
SavePrintedLog();
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -129,6 +146,23 @@ public class FolderWatcherService : IDisposable
return false; return false;
} }
private static string PrintedLogPath =>
Path.Combine(AppContext.BaseDirectory, "folder_printed.log");
private void LoadPrintedLog()
{
if (!File.Exists(PrintedLogPath)) return;
foreach (var line in File.ReadAllLines(PrintedLogPath))
if (!string.IsNullOrWhiteSpace(line)) _printed.Add(line.Trim());
_logger.LogInformation("Gedruckte-Dateien-Liste geladen: {Count} Einträge", _printed.Count);
}
private void SavePrintedLog()
{
try { File.WriteAllLines(PrintedLogPath, _printed); }
catch (Exception ex) { _logger.LogWarning(ex, "Gedruckte-Dateien-Liste konnte nicht gespeichert werden"); }
}
public void Dispose() public void Dispose()
{ {
foreach (var w in _watchers) foreach (var w in _watchers)