feat(uploader): watch folders with stable-file detection and processed-file memory

Polls each folder at its interval with FileSystemWatcher hints on local
disks (network shares are polled only); a photo is queued once its size
and write time are unchanged in two consecutive checks. Files the server
confirmed are remembered by path, size and write time in
uploader-watch.json together with each folder's last session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:27:24 +00:00
1 parent 2a74c12fd1
commit 27f0d73f05
5 files changed
+559

No files matched your search

@@ -0,0 +1,49 @@
namespace Titano.Uploader.Watching;
/// <summary>Size and last-write time of a file at one check.</summary>
public readonly record struct FileObservation(string Path, long Size, DateTime ModifiedUtc);
/// <summary>
/// Decides when a file in a watch folder has stopped growing: it is reported once its size and write time were
/// identical in <see cref="RequiredChecks"/> consecutive checks, then forgotten (the caller enqueues it).
/// </summary>
public sealed class StableFileTracker
{
public const int RequiredChecks = 2;
private readonly Dictionary<string, (long Size, DateTime Modified, int Count)> _seen = new(StringComparer.OrdinalIgnoreCase);
/// <summary>Files seen but not yet stable.</summary>
public int PendingCount => _seen.Count;
/// <summary>
/// Records a complete scan: returns the files that are now stable and forgets files missing from the scan.
/// </summary>
public IReadOnlyList<FileObservation> Check(IEnumerable<FileObservation> scan)
{
var present = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var stable = new List<FileObservation>();
foreach (var o in scan)
{
present.Add(o.Path);
if (Observe(o)) stable.Add(o);
}
foreach (var path in _seen.Keys.Where(p => !present.Contains(p)).ToList()) _seen.Remove(path);
return stable;
}
/// <summary>Records one observation; true when the file just became stable.</summary>
public bool Observe(FileObservation o)
{
var count = _seen.TryGetValue(o.Path, out var last) && last.Size == o.Size && last.Modified == o.ModifiedUtc ? last.Count + 1 : 1;
if (count >= RequiredChecks)
{
_seen.Remove(o.Path);
return true;
}
_seen[o.Path] = (o.Size, o.ModifiedUtc, count);
return false;
}
public void Clear() => _seen.Clear();
}