From a35b4ada20bd1113497cace1f47ef2ec19864134 Mon Sep 17 00:00:00 2001 From: administrator Date: Sun, 19 Apr 2026 21:49:17 +0200 Subject: [PATCH] FolderWatcher: gedruckte Dateien merken wenn DeleteAfterPrint=false --- MailPrint/Services/FolderWatcherService.cs | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/MailPrint/Services/FolderWatcherService.cs b/MailPrint/Services/FolderWatcherService.cs index 7a8c6ea..4b737e3 100644 --- a/MailPrint/Services/FolderWatcherService.cs +++ b/MailPrint/Services/FolderWatcherService.cs @@ -8,6 +8,7 @@ public class FolderWatcherService : IDisposable private readonly PrintService _printService; private readonly MailPrintOptions _options; private readonly List _watchers = new(); + private readonly HashSet _printed = new(StringComparer.OrdinalIgnoreCase); public FolderWatcherService( ILogger logger, @@ -27,6 +28,9 @@ public class FolderWatcherService : IDisposable return; } + // Gedruckte Dateien laden (damit nach Neustart nicht nochmal gedruckt wird) + LoadPrintedLog(); + foreach (var config in _options.FolderWatchers) { if (string.IsNullOrEmpty(config.Path) || !Directory.Exists(config.Path)) @@ -73,6 +77,13 @@ public class FolderWatcherService : IDisposable 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 if (!WaitForFile(filePath)) { @@ -104,6 +115,12 @@ public class FolderWatcherService : IDisposable _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) { @@ -129,6 +146,23 @@ public class FolderWatcherService : IDisposable 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() { foreach (var w in _watchers)