using System.IO; using Titano.Core.Diagnostics; using Titano.Uploader.Sources; namespace Titano.Uploader.Watching; /// /// Polls each watch folder at its interval, with hints on local disks (change /// notifications are unreliable on network shares, which are only polled). A photo is reported once it is stable /// (), not yet confirmed by the server () and not queued. /// public sealed class WatchFolderService(ProcessedFileMemory memory, Func isQueued) : IDisposable { /// Re-check cadence while some file is still growing. private static readonly TimeSpan QuickCheck = TimeSpan.FromSeconds(10); /// Lets a burst of change notifications settle before scanning. private static readonly TimeSpan HintDebounce = TimeSpan.FromSeconds(2); private readonly Dictionary _folders = new(StringComparer.OrdinalIgnoreCase); private readonly ProcessedFileMemory _memory = memory; private readonly Func _isQueued = isQueued; /// Stable new photos of a folder; raised on the thread that called . public event Action>? FilesReady; /// A folder became reachable (true) or unreachable (false); raised like . public event Action? AvailabilityChanged; /// UNC paths and mapped network drives. public static bool IsNetworkPath(string path) { if (path.StartsWith(@"\\", StringComparison.Ordinal)) return true; try { var root = Path.GetPathRoot(Path.GetFullPath(path)); return !string.IsNullOrEmpty(root) && new DriveInfo(root).DriveType == DriveType.Network; } catch (Exception ex) when (ex is ArgumentException or IOException) { return false; } } /// 60 s on local disks; 5 min on network shares, where every poll lists the share. public static TimeSpan DefaultInterval(string path) => IsNetworkPath(path) ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(60); /// Starts (or restarts with a new interval) watching . public void Watch(string root, TimeSpan interval) { Stop(root); var folder = new Folder(root, interval, IsNetworkPath(root), SynchronizationContext.Current, this); _folders[root] = folder; folder.Start(); } public void Stop(string root) { if (_folders.Remove(root, out var folder)) folder.Dispose(); } public void Dispose() { foreach (var f in _folders.Values) f.Dispose(); _folders.Clear(); } private void Raise(SynchronizationContext? context, Action action) { if (context is null) action(); else context.Post(_ => action(), null); } private sealed class Folder(string root, TimeSpan interval, bool network, SynchronizationContext? context, WatchFolderService owner) : IDisposable { private readonly StableFileTracker _tracker = new(); private readonly CancellationTokenSource _cts = new(); private FileSystemWatcher? _watcher; private TaskCompletionSource _hint = NewHint(); private bool? _available; public void Start() => _ = Task.Run(() => LoopAsync(_cts.Token)); public void Dispose() { _cts.Cancel(); _watcher?.Dispose(); _watcher = null; } private async Task LoopAsync(CancellationToken ct) { while (!ct.IsCancellationRequested) { // Replaced before the scan so notifications arriving during it trigger the next one. Volatile.Write(ref _hint, NewHint()); var available = Scan(ct); if (available != _available) { _available = available; if (available && !network) StartWatcher(); owner.Raise(context, () => owner.AvailabilityChanged?.Invoke(root, available)); } var wait = _tracker.PendingCount > 0 && QuickCheck < interval ? QuickCheck : interval; try { var hint = Volatile.Read(ref _hint).Task; if (await Task.WhenAny(Task.Delay(wait, ct), hint).ConfigureAwait(false) == hint) await Task.Delay(HintDebounce, ct).ConfigureAwait(false); } catch (OperationCanceledException) { return; } } } /// One complete listing; false when the folder cannot be reached. private bool Scan(CancellationToken ct) { if (!Directory.Exists(root)) return false; var present = new List(); var candidates = new Dictionary(StringComparer.OrdinalIgnoreCase); try { foreach (var file in PhotoScanner.Files(root, ct)) { present.Add(file.FullName); if (IsKnown(file)) continue; candidates[file.FullName] = file; } } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { // A partial listing must not prune the memory or report files. Log.Info("watch", $"{root} not readable: {ex.Message}"); return false; } catch (OperationCanceledException) { return _available ?? false; } owner._memory.Prune(root, present); var stable = _tracker.Check(candidates.Values.Select(f => new FileObservation(f.FullName, f.Length, f.LastWriteTimeUtc))); if (stable.Count > 0) { var files = stable.Select(o => PhotoScanner.ToSource(candidates[o.Path], root)).ToList(); Log.Info("watch", $"{files.Count} new file(s) in {root}"); owner.Raise(context, () => owner.FilesReady?.Invoke(root, files)); } return true; } /// Already confirmed by the server, or already in the queue. private bool IsKnown(FileInfo file) => owner._memory.Contains(root, file.FullName, file.Length, file.LastWriteTimeUtc) || owner._isQueued(file.FullName); private void StartWatcher() { _watcher?.Dispose(); try { _watcher = new FileSystemWatcher(root) { IncludeSubdirectories = true, NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | NotifyFilters.LastWrite, }; _watcher.Created += (_, _) => Hint(); _watcher.Changed += (_, _) => Hint(); _watcher.Renamed += (_, _) => Hint(); // A lost buffer means unknown changes: scan. _watcher.Error += (_, _) => Hint(); _watcher.EnableRaisingEvents = true; } catch (Exception ex) when (ex is ArgumentException or IOException or PlatformNotSupportedException) { Log.Info("watch", $"No change notifications for {root}; polling only: {ex.Message}"); _watcher?.Dispose(); _watcher = null; } } private void Hint() => Volatile.Read(ref _hint).TrySetResult(); private static TaskCompletionSource NewHint() => new(TaskCreationOptions.RunContinuationsAsynchronously); } }