diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 2a0c0352..142b150c 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -58,7 +58,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S | `Jobs` | `JobEngine` (one job at a time; reviews the priority queue and makes a lower running job yield, requeued with its checkpoint; no history purge, the housekeeping schedule's clean-up does it), `JobPriorityQueue` (a level per job kind, the claim order, settings round trip), `IJobHandler`, `IJobHandlerProvider`, `JobContext` (progress, checkpoints, follow-up jobs that never interrupt their parent), `JobThroughput`/`JobEta` (rate and bounded time left) | | `Scheduling` | `Scheduler` (one per host: seeds the host's system schedules, claims due runs by compare-and-set, catch-up, clock changes, create/edit/reset/delete/run now as API v1 DTOs), `ScheduleRecurrence` (the in-house `every:`/`daily:`/`weekly:`/`monthly:` form, wall-clock times across DST), `ScheduleActions` (action names, parameters, availability, the archive jobs a run queues), `IScheduleHost` and `SystemSchedule` (what the server and the desktop supply), `ScheduleOutcome`, `ScheduleMapping` (model ↔ DTO) | | `Housekeeping` | `BackupJobHandler`, `CleanupJobHandler`, `ICleanupStep` with the shared steps (`CleanupSteps`: job history, audit log, trash report that never deletes); hosts add their own steps through `ArchiveServiceOptions.CleanupSteps` | -| `Archiving` | `ArchiveService` (create/adopt/open, archive settings written to `archive.json` then the index), `ArchiveIndexer` (scan, rebuild), `ArchiveIngest` (uploads and imports), `ImportService`, `ArchiveVerifier`, `ArchiveImages` (thumbnail and proxy passes, several photos at once through `OrderedWork`, which hands results back in frame order), `FrameTiers` (decode tier, `PreviewOnly`/`Undecodable` flags and preview size of frames, written to sidecar and index by the light analysis, deep analysis, renders and image passes), `GprCompanions` (the JPEG of each GoPro GPR, found in the index when an import kept it in another sequence), `ArchiveMaintenance`, `MassRenamer` (mass rename and Reorganize: the dry run reads each sidecar for a file template; each step checked under its leases before anything of it moves (where its sequences are, what is inside and at its new folder, a sidecar of a newer format, the title it was planned from, its file renames by digest at the planned frames version), then the folder, every index row, then the files under the plan's journal in `journal/` (a sequence whose files are found changed or in use there keeps their names, and the step completes); the journal keeps the tally of the whole plan, and a plan its job held (paused, cancelled or failed) is left to that job, only its half-done file renames completed at an archive open; the file renames of a step: sidecar, a sequence whose sidecar cannot be read staying as it is with every folder around it, index (`FrameRepository.RenameFilesAsync`) and the sequence's journal), `SequenceTrash` (delete into the archive's hidden trash with a dry run, restore; the folder moves back when the index step fails; lists sequence and photo entries; `SequenceTrash.Empty`: the emptying's dry run, fingerprint and erase, each entry checked against its fingerprint again under its lease), `FrameTrash` (photos of a sequence into `trash/photos` with a dry run, a journal that undoes an interrupted deletion whose entry stays unlisted until its sidecar is written, tombstones (a photo never hashed is hashed as it goes; one whose original is missing goes without a file or tombstone; a frame that stays and may be a twin is hashed, its hash kept), positions parked and placed back, proxies and thumbnails moved to follow their frames; `FrameTrash.Restore`: restore re-hashes each file and says why each kept photo stayed (`KeptReason`), under a journal of its own into the sequence that another writer settles only under the entry's lease, into a new sequence through the ingest when theirs is gone, each original staged inside its own entry and the record rewritten after each photo filed), job handlers | +| `Archiving` | `ArchiveService` (create/adopt/open, archive settings written to `archive.json` then the index), `ArchiveIndexer` (scan, rebuild), `ArchiveIngest` (uploads and imports), `ImportService`, `ArchiveVerifier`, `ArchiveImages` (thumbnail and proxy passes, several photos at once through `OrderedWork`, which hands results back in frame order), `FrameTiers` (decode tier, `PreviewOnly`/`Undecodable` flags and preview size of frames, written to sidecar and index by the light analysis, deep analysis, renders and image passes), `GprCompanions` (the JPEG of each GoPro GPR, found in the index when an import kept it in another sequence), `ArchiveMaintenance`, `MassRenamer` (mass rename and Reorganize: the dry run reads each sidecar for a file template; each step checked under its leases before anything of it moves (where its sequences are, what is inside and at its new folder, a sidecar of a newer format, the title it was planned from, its file renames by digest at the planned frames version), then the folder, every index row (the frame and render paths follow it in one transaction), then the files under the plan's journal in `journal/` (a sequence whose files are found changed or in use there keeps their names, and the step completes); the journal keeps the tally of the whole plan, and a plan its job held (paused, cancelled or failed) is left to that job, only its half-done file renames completed at an archive open; the file renames of a step: sidecar, a sequence whose sidecar cannot be read staying as it is with every folder around it, index (`FrameRepository.RenameFilesAsync`) and the sequence's journal), `SequenceTrash` (delete into the archive's hidden trash with a dry run, restore; the folder moves back when the index step fails; lists sequence and photo entries; `SequenceTrash.Empty`: the emptying's dry run, fingerprint and erase, each entry checked against its fingerprint again under its lease), `FrameTrash` (photos of a sequence into `trash/photos` with a dry run, a journal that undoes an interrupted deletion whose entry stays unlisted until its sidecar is written, tombstones (a photo never hashed is hashed as it goes; one whose original is missing goes without a file or tombstone; a frame that stays and may be a twin is hashed, its hash kept), positions parked and placed back, proxies and thumbnails moved to follow their frames; `FrameTrash.Restore`: restore re-hashes each file and says why each kept photo stayed (`KeptReason`), under a journal of its own into the sequence that another writer settles only under the entry's lease, into a new sequence through the ingest when theirs is gone, each original staged inside its own entry and the record rewritten after each photo filed), job handlers | | `Processing` | `ProcessingService`, `SequenceAnalyser`, `DeepReports` (a saved deep report while it still describes the frames), `SequenceRender`, `ProjectStore`, `ProjectBundles` (a `.chronos` file for a sequence, and the sequence a file names), `RenderNodeClient`, job handlers | | `Sync` | `SyncService` (sync table of one server: compare, link by content, settle conflicts, enqueue transfers), `SyncRules` (status, settings merge and differences, no I/O), `SyncTombstones` (photos either side removed are not offered back, a replaced photo counts as its replacement), `SyncPositions` (the settings' frame positions translated by photo to the frames of the copy they go to), `SyncLinkStore` (`sync_links`, `sync_state`), `SyncTransfers` and `SyncJobHandler` (resumable, hash-verified uploads and downloads as jobs; downloads commit through the archive ingest), `SyncAgent` (background comparison, server events, backoff, offline proxies), `ProxyCache` and `RemoteSequence` (server sequences opened with proxies), `ISyncRemote` (the server calls; `ChronosSyncRemote` over `ChronosClient`) | @@ -147,7 +147,9 @@ after an import or a scan that indexed something; screens never queue it again. ``` The sidecars are the truth; the index is derived and can always be rebuilt (`ScanMode.Rebuild`, using the index -snapshot when fresh). Writers coordinate through lease files, so a desktop and a server may share an archive. +snapshot when fresh). Writers coordinate through lease files, so a desktop and a server may share an archive. A writer takes a sequence's +folder from its index row read under the sequence's lease (`SequenceWriter.FolderAsync`): a Reorganize moves the folder +and its row under that lease, and a folder the sequence left is never created again. Sync (desktop ↔ server, BRIEF §14.5) reuses the same paths: `SyncService` compares the local index with the server's listing (settings and tags carry version counters), uploads go through the chunked protocol into the server's diff --git a/src/Chronos.App/Editor/ISequenceSource.cs b/src/Chronos.App/Editor/ISequenceSource.cs index 4f41af01..2ecd873f 100644 --- a/src/Chronos.App/Editor/ISequenceSource.cs +++ b/src/Chronos.App/Editor/ISequenceSource.cs @@ -45,9 +45,10 @@ public interface ISequenceSource : IAsyncDisposable Task LoadAsync(CancellationToken cancellationToken); /// - /// Whether the frames read last are still the sequence's, in the same order: frames inserted - /// or removed since (a sync download, an import of earlier photos, a scan) moved the stored project's positions. A - /// server sequence whose frames changed becomes . + /// Whether the frames read last are still the sequence's, in the same order and folder: frames + /// inserted or removed since (a sync download, an import of earlier photos, a scan) moved the stored project's + /// positions, and a Reorganize may have moved the folder. A server sequence whose frames changed becomes + /// . /// /// The sequence is gone. Task FramesCurrentAsync(CancellationToken cancellationToken); diff --git a/src/Chronos.App/Editor/LocalSequenceSource.cs b/src/Chronos.App/Editor/LocalSequenceSource.cs index 741088db..0aa4fb58 100644 --- a/src/Chronos.App/Editor/LocalSequenceSource.cs +++ b/src/Chronos.App/Editor/LocalSequenceSource.cs @@ -51,6 +51,9 @@ public sealed class LocalSequenceSource : ISequenceSource /// The frames version of the frames last read (), whose order the project's positions follow. private int _framesVersion = -1; + /// The archive-relative folder the last read found the sequence in (); a Reorganize moves it. + private string? _relativeFolder; + private ArchivePaths? _paths; private SequencePaths? _folder; @@ -98,7 +101,8 @@ public sealed class LocalSequenceSource : ISequenceSource if ((frames[i].Flags & FrameFlags.Missing) == 0) files[i] = paths.FromRelative(frames[i].RelativePath); } _companions = [.. frames.Select(f => companions.GetValueOrDefault(f.Id))]; - (_paths, _folder, _files, _frameIds, _framesVersion) = (paths, folder, files, [.. frames.Select(f => f.Id)], sequence.FramesVersion); + (_paths, _folder, _relativeFolder, _files, _frameIds, _framesVersion) = + (paths, folder, sequence.Folder, files, [.. frames.Select(f => f.Id)], sequence.FramesVersion); var context = SequenceContext.FromFrames(frames); var (report, otherFrames) = ReadReport(folder, frames.Count); return new SequenceContent(sequence, frames, ReadProject(sequence, folder), LightAnalyzer.Analyze(frames), report, @@ -109,12 +113,15 @@ public sealed class LocalSequenceSource : ISequenceSource }, cancellationToken); } - /// An insertion bumps the row's frames version in the transaction that renumbers the frames. + /// + /// An insertion bumps the row's frames version in the transaction that renumbers the frames; a Reorganize that moved + /// the folder leaves the version but not the files where they were read. + /// public async Task FramesCurrentAsync(CancellationToken cancellationToken) { - var framesVersion = _framesVersion; + var (framesVersion, folder) = (_framesVersion, _relativeFolder); var row = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing(); - return row.FramesVersion == framesVersion; + return row.FramesVersion == framesVersion && row.Folder == folder; } public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) @@ -157,7 +164,7 @@ public sealed class LocalSequenceSource : ISequenceSource { ArgumentNullException.ThrowIfNull(project); ArgumentNullException.ThrowIfNull(basis); - var (paths, folder, frameIds, framesVersion) = (_paths, _folder, _frameIds, _framesVersion); + var (paths, folder, relativeFolder, frameIds, framesVersion) = (_paths, _folder, _relativeFolder, _frameIds, _framesVersion); if (paths is null || folder is null) throw new InvalidOperationException("LoadAsync first."); return Task.Run(async () => { @@ -168,8 +175,10 @@ public sealed class LocalSequenceSource : ISequenceSource { var row = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing(); // Frames were inserted or removed since the editor read them, so its positions name the old order. Insertions - // hold this lease, so the frames do not change again before this save ends. - if (row.FramesVersion != framesVersion) throw new FramesChangedException(Loc.L("Editor.Error.FramesArriving")); + // hold this lease, so the frames do not change again before this save ends. A Reorganize moves the folder under + // it too: moved since the read, the project goes to the new folder once the editor read the sequence there, + // and the folder it left is never created again. + if (row.FramesVersion != framesVersion || row.Folder != relativeFolder) throw new FramesChangedException(Loc.L("Editor.Error.FramesArriving")); var stored = ReadProject(row, folder); var merged = ProjectMerge.SameValues(stored, basis) ? project : ProjectMerge.Merge(basis, project, stored); var json = merged.ToJson(); @@ -281,7 +290,8 @@ public sealed class LocalSequenceSource : ISequenceSource } /// -/// The frames of a sequence of this PC changed since the editor read them (frames inserted or removed): nothing was -/// saved. The editor reads them again, moves its positions as the index moved the stored ones, and saves again. +/// The frames of a sequence of this PC changed since the editor read them (frames inserted or removed, or their folder +/// moved by a Reorganize): nothing was saved. The editor reads them again, moves its positions as the index moved the +/// stored ones, and saves again. /// public sealed class FramesChangedException(string message) : EditorException(message); diff --git a/src/Chronos.Data/Archiving/ArchiveImages.cs b/src/Chronos.Data/Archiving/ArchiveImages.cs index 10a254e6..7f1a069e 100644 --- a/src/Chronos.Data/Archiving/ArchiveImages.cs +++ b/src/Chronos.Data/Archiving/ArchiveImages.cs @@ -199,17 +199,20 @@ public sealed class ArchiveImages ArchiveVolume.EnsureWritable(paths); var archiveManifest = ManifestStore.ReadArchive(paths.ManifestPath); var policy = archiveManifest.ThumbnailPolicy; - var folder = paths.Sequence(sequence.Folder); - var manifest = ManifestStore.ReadSequence(folder.ManifestPath); + SequenceManifest manifest; int built = 0, reused = 0, undecodable = 0; var complete = true; var tiers = new Dictionary(); - var companions = await GprCompanions.FindAsync(_db, archive, manifest.Frames, cancellationToken).ConfigureAwait(false); - // Thumbnail writers exclude each other only; ingest keeps committing meanwhile. + // Thumbnail writers exclude each other only; ingest keeps committing meanwhile. A Reorganize moves the folder under + // this lease too, so the folder is the one the index has once it is held: the pack never opens in a folder the + // sequence left, which opening it would create again. using (await LeaseFile.AcquireAsync(paths.LocksDirectory, $"thumbs-{sequence.Id:N}", _writer.Options.Owner, _writer.Options.LeaseWait, _writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false)) - using (var pack = ThumbPackWriter.Open(folder.ThumbsPackPath)) { + var folder = await SequenceWriter.FolderAsync(_db, paths, sequence, cancellationToken).ConfigureAwait(false); + manifest = ManifestStore.ReadSequence(folder.ManifestPath); + var companions = await GprCompanions.FindAsync(_db, archive, manifest.Frames, cancellationToken).ConfigureAwait(false); + using var pack = ThumbPackWriter.Open(folder.ThumbsPackPath); for (var i = 0; i < startFrame && i < manifest.Frames.Count; i++) { var frame = manifest.Frames[i]; @@ -626,9 +629,9 @@ public sealed class ArchiveImages private async Task MarkAsync(Archive archive, ArchivePaths paths, Sequence sequence, SequenceManifest read, JobKind pass, SequenceStatus flag, bool complete, CancellationToken cancellationToken) { - var folder = paths.Sequence(sequence.Folder); await using (await _writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false)) { + var folder = await SequenceWriter.FolderAsync(_db, paths, sequence, cancellationToken).ConfigureAwait(false); var manifest = SequenceWriter.ReadLocked(folder, sequence.Id); var built = complete && (SameFrames(manifest.Frames, read.Frames) || await _db.Jobs.IsQueuedAsync(pass, sequence.Id, cancellationToken).ConfigureAwait(false)); diff --git a/src/Chronos.Data/Archiving/ArchiveIngest.cs b/src/Chronos.Data/Archiving/ArchiveIngest.cs index e57753d4..95ac7368 100644 --- a/src/Chronos.Data/Archiving/ArchiveIngest.cs +++ b/src/Chronos.Data/Archiving/ArchiveIngest.cs @@ -292,9 +292,9 @@ public sealed class ArchiveIngest : IArchiveIngest ?? throw new InvalidOperationException($"Sequence {sequenceId} is not indexed."); var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); - var sequence = paths.Sequence(row.Folder); await using (await _writer.LockAsync(paths, sequenceId, cancellationToken).ConfigureAwait(false)) { + var sequence = await SequenceWriter.FolderAsync(_db, paths, row, cancellationToken).ConfigureAwait(false); var manifest = SequenceWriter.ReadLocked(sequence, sequenceId); // Asked before this batch changes the folder: whether this index holds it as it is (ArchiveIndexer.InStepAsync), and // when it does not, whether photos were copied in or removed by hand since the last scan, which this batch's diff --git a/src/Chronos.Data/Archiving/ArchiveMaintenance.cs b/src/Chronos.Data/Archiving/ArchiveMaintenance.cs index 412316b2..8bc41d12 100644 --- a/src/Chronos.Data/Archiving/ArchiveMaintenance.cs +++ b/src/Chronos.Data/Archiving/ArchiveMaintenance.cs @@ -230,7 +230,13 @@ public sealed class ArchiveMaintenance await using (await _writer.LockAsync(paths, location.Id, cancellationToken).ConfigureAwait(false)) using (await LeaseFile.AcquireAsync(paths.LocksDirectory, $"thumbs-{location.Id:N}", _writer.Options.Owner, _writer.Options.LeaseWait, _writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false)) - ThumbPackWriter.Compact(sequence.ThumbsPackPath); + { + // Where the pack is now: a Reorganize may have moved the folder since the listing, and compacting a pack + // that is not there would create it (and the folder) again. + var pack = paths.Sequence((await _db.Sequences.GetAsync(location.Id, cancellationToken).ConfigureAwait(false))?.Folder ?? location.Folder) + .ThumbsPackPath; + if (File.Exists(pack)) ThumbPackWriter.Compact(pack); + } } return compacted; } @@ -251,9 +257,9 @@ public sealed class ArchiveMaintenance if (await _db.Sequences.GetAsync(bySequence.Key, cancellationToken).ConfigureAwait(false) is not { } sequence) continue; var changes = bySequence.ToDictionary(x => x.Key, x => (Func)(f => f with { Flags = x.Set ? f.Flags | FrameFlags.Duplicate : f.Flags & ~FrameFlags.Duplicate })); - var folder = paths.Sequence(sequence.Folder); await using (await _writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false)) { + var folder = await SequenceWriter.FolderAsync(_db, paths, sequence, cancellationToken).ConfigureAwait(false); // The scan stamps follow only when the index held the folder as it was: frames another process listed in the // sidecar meanwhile are still indexed by the next scan. var manifest = SequenceWriter.ReadLocked(folder, sequence.Id); diff --git a/src/Chronos.Data/Archiving/ArchiveService.cs b/src/Chronos.Data/Archiving/ArchiveService.cs index a2c1be2b..77dd9625 100644 --- a/src/Chronos.Data/Archiving/ArchiveService.cs +++ b/src/Chronos.Data/Archiving/ArchiveService.cs @@ -268,9 +268,9 @@ public sealed class ArchiveService : IJobHandlerProvider var (archive, sequence) = await LoadSequenceAsync(sequenceId, cancellationToken).ConfigureAwait(false); var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); - var folder = paths.Sequence(sequence.Folder); await using (await _writer.LockAsync(paths, sequenceId, cancellationToken).ConfigureAwait(false)) { + var folder = await SequenceWriter.FolderAsync(_db, paths, sequence, cancellationToken).ConfigureAwait(false); var manifest = SequenceWriter.ReadLocked(folder, sequenceId); var inStep = await Indexer.InStepAsync(archive, folder, manifest, cancellationToken).ConfigureAwait(false); for (var attempt = 1; ; attempt++) diff --git a/src/Chronos.Data/Archiving/ArchiveVerifier.cs b/src/Chronos.Data/Archiving/ArchiveVerifier.cs index 0baff932..65c4a456 100644 --- a/src/Chronos.Data/Archiving/ArchiveVerifier.cs +++ b/src/Chronos.Data/Archiving/ArchiveVerifier.cs @@ -112,9 +112,10 @@ public sealed class ArchiveVerifier private async Task FlushAsync(Archive archive, ArchivePaths paths, Sequence target, List pending, CancellationToken cancellationToken) { if (pending.Count == 0) return; - var sequence = paths.Sequence(target.Folder); await using (await _writer.LockAsync(paths, target.Id, cancellationToken).ConfigureAwait(false)) { + // Where the sequence is now (a Reorganize may have moved it); a check of a file that moved since no longer applies. + var sequence = await SequenceWriter.FolderAsync(_db, paths, target, cancellationToken).ConfigureAwait(false); var manifest = SequenceWriter.ReadLocked(sequence, target.Id); var current = manifest.Frames.ToDictionary(f => f.Id); var found = pending.Where(p => current.TryGetValue(p.Check.Frame.Id, out var now) && StillApplies(sequence, p, now)).Select(p => p.Check).ToList(); @@ -164,9 +165,9 @@ public sealed class ArchiveVerifier /// private async Task StampAsync(Archive archive, ArchivePaths paths, Sequence target, CancellationToken cancellationToken) { - var sequence = paths.Sequence(target.Folder); await using (await _writer.LockAsync(paths, target.Id, cancellationToken).ConfigureAwait(false)) { + var sequence = await SequenceWriter.FolderAsync(_db, paths, target, cancellationToken).ConfigureAwait(false); var m = SequenceWriter.ReadLocked(sequence, target.Id); var inStep = await _indexer.InStepAsync(archive, sequence, m, cancellationToken).ConfigureAwait(false); var clean = m.Frames.All(f => (f.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) == 0); diff --git a/src/Chronos.Data/Archiving/FrameTiers.cs b/src/Chronos.Data/Archiving/FrameTiers.cs index 9850f04f..e0128c0b 100644 --- a/src/Chronos.Data/Archiving/FrameTiers.cs +++ b/src/Chronos.Data/Archiving/FrameTiers.cs @@ -79,9 +79,9 @@ internal sealed class FrameTiers(ChronosDatabase db, SequenceWriter writer, Arch if (tiers.Count == 0 && !proxies) return []; var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); - var folder = paths.Sequence(sequence.Folder); await using (await writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false)) { + var folder = await SequenceWriter.FolderAsync(db, paths, sequence, cancellationToken).ConfigureAwait(false); var m = SequenceWriter.ReadLocked(folder, sequence.Id); var changed = new List(); var gained = false; diff --git a/src/Chronos.Data/Archiving/FrameTrash.Restore.cs b/src/Chronos.Data/Archiving/FrameTrash.Restore.cs index 7462b80f..19c077a5 100644 --- a/src/Chronos.Data/Archiving/FrameTrash.Restore.cs +++ b/src/Chronos.Data/Archiving/FrameTrash.Restore.cs @@ -150,13 +150,13 @@ public sealed partial class FrameTrash private async Task<(PhotoRestoreResult, List)> IntoSequenceAsync(Archive archive, ArchivePaths paths, Sequence sequence, string recordPath, string entryFolder, string lease, List intact, CancellationToken cancellationToken) { - var folder = paths.Sequence(sequence.Folder); var left = new List(); int restored; SequenceManifest updated; var indexed = true; await using (await _writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false)) { + var folder = await SequenceWriter.FolderAsync(_db, paths, sequence, cancellationToken).ConfigureAwait(false); var manifest = SequenceWriter.ReadLocked(folder, sequence.Id, lease); // A restore of another entry, busy now, is not settled yet: its journal is not written over. EnsureSettled(folder); diff --git a/src/Chronos.Data/Archiving/FrameTrash.cs b/src/Chronos.Data/Archiving/FrameTrash.cs index c652574e..f47e2f57 100644 --- a/src/Chronos.Data/Archiving/FrameTrash.cs +++ b/src/Chronos.Data/Archiving/FrameTrash.cs @@ -229,6 +229,9 @@ public sealed partial class FrameTrash bool indexed; await using (await _writer.LockAsync(paths, id, cancellationToken).ConfigureAwait(false)) { + // A Reorganize moves the folder under this lease: moved since the plan, the plan names other paths. + if ((await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false))?.Folder != revised.Sequence.Folder) + throw new StaleDeletePlanException(id); var manifest = SequenceWriter.ReadLocked(folder, id); var plannedPaths = plan.Frames.ToDictionary(f => f.Id, f => f.RelativePath); var planned = plannedPaths.Keys.ToHashSet(); diff --git a/src/Chronos.Data/Archiving/MassRenamer.cs b/src/Chronos.Data/Archiving/MassRenamer.cs index 287ef6e4..7ffa5b5e 100644 --- a/src/Chronos.Data/Archiving/MassRenamer.cs +++ b/src/Chronos.Data/Archiving/MassRenamer.cs @@ -578,7 +578,8 @@ public sealed class MassRenamer } /// - /// Sidecar title, then index (folder, title, frames' paths); each part is idempotent so a resumed step can repeat it. + /// Sidecar title, then index (folder, title, frames' paths and the paths of renders whose export moved with the + /// folder, in one transaction); each part is idempotent so a resumed step can repeat it. /// The index row changes in its folder and title only (): a settings edit /// committed meanwhile stays, and frames another process listed in the sidecar are left to the next scan, for which /// the folder keeps no stamps unless the index held it as it was @@ -606,7 +607,11 @@ public sealed class MassRenamer await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false)) { await tx.Sequences.RelocateAsync(step.SequenceId, step.ToFolder, retitle ? step.ToTitle : null, cancellationToken).ConfigureAwait(false); - if (step.MovesFolder) await tx.Frames.RebaseFolderAsync(step.SequenceId, step.FromFolder, step.ToFolder, cancellationToken).ConfigureAwait(false); + if (step.MovesFolder) + { + await tx.Frames.RebaseFolderAsync(step.SequenceId, step.FromFolder, step.ToFolder, cancellationToken).ConfigureAwait(false); + await tx.Renders.RebaseFolderAsync(step.SequenceId, step.FromFolder, step.ToFolder, cancellationToken).ConfigureAwait(false); + } await tx.Archives.DeleteFolderStateAsync(archive.Id, step.FromFolder, cancellationToken).ConfigureAwait(false); await tx.CommitAsync(cancellationToken).ConfigureAwait(false); } diff --git a/src/Chronos.Data/Archiving/SequenceWriter.cs b/src/Chronos.Data/Archiving/SequenceWriter.cs index 6d6fa277..28901eba 100644 --- a/src/Chronos.Data/Archiving/SequenceWriter.cs +++ b/src/Chronos.Data/Archiving/SequenceWriter.cs @@ -1,6 +1,7 @@ using System.Collections.Concurrent; using Chronos.Core.Archiving; using Chronos.Core.Imaging; +using Chronos.Core.Model; namespace Chronos.Data.Archiving; @@ -127,6 +128,15 @@ internal sealed class SequenceWriter(ArchiveServiceOptions options) return manifest; } + /// + /// The folder of a sequence whose lock the caller holds, as the index has it now. A Reorganize moves a folder and + /// updates its row under the sequence's lease (and its thumbnail lease), so a writer that read the row before taking + /// the lease writes where the sequence is, never into the folder it left, which the write would create again. The + /// folder of when the row is gone (reading its sidecar then fails as it did). + /// + public static async Task FolderAsync(ChronosDatabase db, ArchivePaths archive, Sequence sequence, CancellationToken cancellationToken) => + archive.Sequence((await db.Sequences.GetAsync(sequence.Id, cancellationToken).ConfigureAwait(false))?.Folder ?? sequence.Folder); + /// Applies to the sidecar under the lock; a null result writes nothing. public async Task UpdateAsync(ArchivePaths archive, Guid sequenceId, string folder, Func edit, CancellationToken cancellationToken) diff --git a/src/Chronos.Data/Processing/ProjectStore.cs b/src/Chronos.Data/Processing/ProjectStore.cs index b72a5226..915e4648 100644 --- a/src/Chronos.Data/Processing/ProjectStore.cs +++ b/src/Chronos.Data/Processing/ProjectStore.cs @@ -44,14 +44,17 @@ internal sealed class ProjectStore(ChronosDatabase db, SequenceWriter writer) return await UpdateLockedAsync(paths, sequence, edit, cancellationToken).ConfigureAwait(false); } - /// for a caller that holds the sequence lock already. + /// + /// for a caller that holds the sequence lock already. The file goes to the folder of the row + /// read under the lock: a Reorganize moves the folder and its row under the same lock. + /// /// The stored settings cannot be read; nothing is written. public async Task UpdateLockedAsync(ArchivePaths paths, Sequence sequence, Func edit, CancellationToken cancellationToken) { - var folder = paths.Sequence(sequence.Folder); for (var attempt = 1; ; attempt++) { var row = await db.Sequences.GetAsync(sequence.Id, cancellationToken).ConfigureAwait(false) ?? sequence; + var folder = paths.Sequence(row.Folder); var current = Read(row, folder); var before = current.ToJson(); var edited = edit(current); diff --git a/src/Chronos.Data/Processing/SequenceAnalyser.cs b/src/Chronos.Data/Processing/SequenceAnalyser.cs index 61bea8b5..60149d91 100644 --- a/src/Chronos.Data/Processing/SequenceAnalyser.cs +++ b/src/Chronos.Data/Processing/SequenceAnalyser.cs @@ -98,6 +98,8 @@ internal sealed class SequenceAnalyser(ChronosDatabase db, ProcessingOptions opt // then follows the report analysis.json holds: its own, or the one it left in place. await using (await writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false)) { + // Where the sequence is now: a Reorganize may have moved its folder while the pass ran. + folder = await SequenceWriter.FolderAsync(db, paths, sequence, cancellationToken).ConfigureAwait(false); var manifest = SequenceWriter.ReadLocked(folder, sequence.Id); var change = await FramesChangeAsync(sequence, manifest, cancellationToken).ConfigureAwait(false); // The frames as this host reads them now: a proxy that arrived while the pass ran moves no frames version but diff --git a/src/Chronos.Data/Repositories/RenderRepository.cs b/src/Chronos.Data/Repositories/RenderRepository.cs index 21f658cb..0defa177 100644 --- a/src/Chronos.Data/Repositories/RenderRepository.cs +++ b/src/Chronos.Data/Repositories/RenderRepository.cs @@ -45,6 +45,23 @@ public sealed class RenderRepository return true; }, cancellationToken); + /// + /// Rewrites the output paths of a sequence's renders after its folder moved (Reorganize): an export inside the folder + /// moved with it. Outputs elsewhere (absolute paths, other folders of the archive) stay as they are. + /// + /// Only paths still under change, so repeating it after a crash is harmless. + public Task RebaseFolderAsync(Guid sequenceId, string oldFolder, string newFolder, CancellationToken cancellationToken = default) + { + // The archive root never moves, and its prefix ("/") would match absolute paths outside the archive. + if (oldFolder.Length == 0) return Task.FromResult(0); + // substr counts characters (code points) on both dialects; the tail starts with the '/' after the folder. + var prefix = oldFolder + "/"; + var length = prefix.EnumerateRunes().Count(); + return _db.ExecuteAsync( + "UPDATE renders SET output_path = $2 || substr(output_path, $3) WHERE sequence_id = $1 AND substr(output_path, 1, $4) = $5", + [sequenceId, newFolder, length, length, prefix], cancellationToken); + } + /// /// Serialises render changes per sequence on PostgreSQL, so the recount (a new statement, hence a new snapshot) /// sees every committed render; SQLite's write lock already does this. diff --git a/src/Chronos.Data/Sync/SyncService.cs b/src/Chronos.Data/Sync/SyncService.cs index ade9a216..325d61e4 100644 --- a/src/Chronos.Data/Sync/SyncService.cs +++ b/src/Chronos.Data/Sync/SyncService.cs @@ -734,12 +734,13 @@ public sealed class SyncService ?? throw new InvalidDataException($"Archive {local.ArchiveId} is not indexed."); var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); - var folder = paths.Sequence(local.Folder); await using (await _writer.LockAsync(paths, local.Id, cancellationToken).ConfigureAwait(false)) { // The frames cannot change under the lease: the positions are moved to them as they are now. if (await positions.ToLocalAsync(payload.SettingsJson, local.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } current) return null; var row = current.Local; + // The folder too: a Reorganize moves it and its row under this lease. + var folder = paths.Sequence(row.Folder); payload = payload with { SettingsJson = current.SettingsJson }; var metadata = !payload.SameMetadata(SyncPayload.Of(row)); var settings = !SyncRules.SameSettings(payload.SettingsJson, row.SettingsJson); diff --git a/tests/Chronos.App.Tests/Editor/ProjectStoreTests.cs b/tests/Chronos.App.Tests/Editor/ProjectStoreTests.cs index 9074b9f7..375994fc 100644 --- a/tests/Chronos.App.Tests/Editor/ProjectStoreTests.cs +++ b/tests/Chronos.App.Tests/Editor/ProjectStoreTests.cs @@ -215,6 +215,44 @@ public sealed class ProjectStoreTests(TestContext context) } } + [Test] + public async Task AnEditorOpenWhileAReorganizeMovesItsFolderSavesWhereTheSequenceIsNow() + { + var (db, sequenceId, root) = await ArchiveAsync(6); + await using (db) + { + var options = new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }; + await using var source = new LocalSequenceSource(db, sequenceId, DecoderFactory.CreateClient(null), null, options); + var content = await source.LoadAsync(context.Cancellation); + var session = new EditorSession(source, content, [], TimeSpan.FromMilliseconds(20)); + await using (session) + { + Assert.True(session.SetHero(2)); + Assert.True(await session.FlushAsync()); + + // Only the folder template changed: the Reorganize moves the folder and keeps the frames version. + var service = new ArchiveService(db, options); + var plan = await service.PlanRenameAsync(content.Sequence.ArchiveId, [sequenceId], "Moved/{title}", null, context.Cancellation); + await service.ApplyRenameAsync(plan, null, context.Cancellation); + var moved = new ArchivePaths(root).Sequence(plan.Steps.Single().ToFolder); + Assert.Equal(content.Sequence.FramesVersion, (await db.Sequences.GetAsync(sequenceId))!.FramesVersion); + Assert.False(await source.FramesCurrentAsync(context.Cancellation), "the files are no longer where the editor read them"); + + session.Edit(p => p with { Exposure = p.Exposure with { ExposureOffsetEv = 0.4 } }); + Assert.True(await session.FlushAsync()); + + var row = (await db.Sequences.GetAsync(sequenceId))!; + Assert.Equal(0.4, ProjectSettings.FromJson(row.SettingsJson!).Exposure.ExposureOffsetEv); + Assert.Equal(row.SettingsJson, File.ReadAllText(moved.ProjectPath), "the moved folder's project has the edit"); + Assert.False(Directory.Exists(Path.Combine(root, "Synthetic")), "the folder it left is not created again"); + Assert.True(await source.FramesCurrentAsync(context.Cancellation)); + Assert.Equal(moved.Exports, session.Content.ExportsFolder); + using var frame = await source.DecodeAsync(1, 32, DecodePurpose.Thumbnail, context.Cancellation); + Assert.NotNull(frame, "the frames are read where they are now"); + } + } + } + [Test] public async Task ASaveThatDoesNotReachTheIndexThrows() { diff --git a/tests/Chronos.Data.Tests/Archiving/MassRenameTests.cs b/tests/Chronos.Data.Tests/Archiving/MassRenameTests.cs index 0f4f5db6..47cc5e03 100644 --- a/tests/Chronos.Data.Tests/Archiving/MassRenameTests.cs +++ b/tests/Chronos.Data.Tests/Archiving/MassRenameTests.cs @@ -1,5 +1,8 @@ using Chronos.Core.Archiving; +using Chronos.Core.Model; +using Chronos.Core.Project; using Chronos.Data.Archiving; +using Chronos.Data.Processing; using Chronos.Testing; using static Chronos.Data.Tests.Archiving.ArchiveFixture; @@ -233,6 +236,135 @@ public sealed class MassRenameTests(TestContext context) Assert.Equal(0, (await fx.ScanAsync()).Indexed); } + [Test] + public async Task ExportsInsideAMovedFolderAreStillReachable() + { + await using var fx = await CreateAsync(context); + var ids = await NestedAsync(fx); + // Exports of "trips" and of "trips/day2" in their folders, one of "trips" outside the archive, and a row whose + // path only starts with the same letters. + var outside = Path.Combine(context.CreateTempDirectory("exports"), "Trips elsewhere.mp4"); + List renders = + [ + await RenderAsync(fx, ids[0], "trips/exports/Trips.mp4"), + await RenderAsync(fx, ids[1], "trips/day2/exports/Day 2.mp4"), + await RenderAsync(fx, ids[0], outside), + await RenderAsync(fx, ids[0], "trips 2/exports/Other.mp4", write: false), + ]; + var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "moved/{date}", null, fx.Ct); + Assert.SequenceEqual([false, true, true], plan.Steps.Select(s => s.Carried)); + + await fx.Service.ApplyRenameAsync(plan, null, fx.Ct); + string[] expected = ["moved/2026-09-28/exports/Trips.mp4", "moved/2026-09-28/day2/exports/Day 2.mp4", outside, "trips 2/exports/Other.mp4"]; + Assert.SequenceEqual(expected, await OutputsAsync(fx, renders)); + foreach (var path in expected[..3]) Assert.FileExists(Path.IsPathRooted(path) ? path : fx.Paths.FromRelative(path)); + + // A crash after the steps were recorded but before the journal said so: completed again, and again, nothing moves twice. + RenameJournal.Write(fx.Paths, new RenameJournal(plan, [])); + Assert.Equal(1, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct)); + Assert.Equal(0, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct)); + Assert.SequenceEqual(expected, await OutputsAsync(fx, renders)); + } + + [Test] + public async Task AnInterruptedMoveTakesTheExportsAlongWhenItResumes() + { + await using var fx = await CreateAsync(context); + var ids = await PopulateAsync(fx); + List renders = []; + foreach (var (id, folder) in ids.Zip(["inbox/a", "inbox/b", "inbox/c"])) renders.Add(await RenderAsync(fx, id, $"{folder}/exports/Clip.mp4")); + var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, ids, "renamed/{date}", null, fx.Ct); + using var cts = new CancellationTokenSource(); + await Assert.ThrowsAsync(() => fx.Service.ApplyRenameAsync(plan, new SyncProgress(_ => cts.Cancel()), cts.Token)); + Assert.Count(1, RenameJournal.ListPending(fx.Paths).Single().Completed); + + // Crash in the middle of the next step: its folder moved with its export, the index not yet updated. + var next = plan.Steps[1]; + Directory.CreateDirectory(Path.GetDirectoryName(fx.Paths.FromRelative(next.ToFolder))!); + Directory.Move(fx.Paths.FromRelative(next.FromFolder), fx.Paths.FromRelative(next.ToFolder)); + + await fx.Service.OpenAsync(fx.Archive.RootPath, fx.Ct); // the next start finds the journal + Assert.Empty(RenameJournal.ListPending(fx.Paths)); + Assert.Equal(0, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct)); + var outputs = await OutputsAsync(fx, renders); + Assert.SequenceEqual(ids.Select(id => $"{plan.Steps.Single(s => s.SequenceId == id).ToFolder}/exports/Clip.mp4"), outputs); + foreach (var path in outputs) Assert.FileExists(fx.Paths.FromRelative(path)); + } + + [Test] + public async Task WritersThatReadTheSequenceBeforeItsFolderMovedWriteWhereItIsNow() + { + await using var fx = await CreateAsync(context); + var ids = await PopulateAsync(fx); + var stale = (await fx.Db.Sequences.GetAsync(ids[0], fx.Ct))!; + var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "moved/{date}", null, fx.Ct); + await fx.Service.ApplyRenameAsync(plan, null, fx.Ct); + var now = fx.Paths.Sequence(plan.Steps.Single().ToFolder); + var left = fx.Paths.Sequence(stale.Folder).Folder; + Assert.False(Directory.Exists(left)); + + // A thumbnail pass and an analysis's project update that read the row before the move take their leases after it. + await fx.Service.Images.BuildThumbnailsAsync(fx.Archive, stale, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); + Assert.FileExists(now.ThumbsPackPath); + Assert.True(((await fx.Db.Sequences.GetAsync(ids[0], fx.Ct))!.Status & SequenceStatus.ThumbsBuilt) != 0, "the pass is recorded"); + var projects = new ProjectStore(fx.Db, new SequenceWriter(new ArchiveServiceOptions { Owner = "analysis" })); + Assert.True(await projects.UpdateAsync(fx.Paths, stale, p => p with { HeroFrame = 1 }, fx.Ct)); + Assert.Equal(1, ProjectSettings.FromJson(await File.ReadAllTextAsync(now.ProjectPath, fx.Ct)).HeroFrame); + Assert.False(Directory.Exists(left), "the folder it left is not created again"); + } + + [Test] + public async Task APhotoFiledWhileTheFolderMovesGoesWhereTheSequenceIsNow() + { + await using var fx = await CreateAsync(context); + var ids = await PopulateAsync(fx); + var plan = await fx.Service.PlanRenameAsync(fx.Archive.Id, [ids[0]], "moved/{date}", null, fx.Ct); + var staged = fx.Stage(Start.AddMinutes(30), 99); + var pause = TimeSpan.FromSeconds(TestTiming.Budget(0.5)); + + // Another process writes the sequence: the move waits for it, and an upload that read the row meanwhile waits behind the move. + Task apply; + Task upload; + using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"seq-{ids[0]:N}", "other", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct)) + { + apply = fx.Service.ApplyRenameAsync(plan, null, fx.Ct); + await Task.Delay(pause, fx.Ct); + upload = fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, ids[0], null, null, staged.Path, "LATE.JPG", staged.Size, staged.Sha256, + null, null), fx.Ct); + await Task.Delay(pause, fx.Ct); + Assert.False(apply.IsCompleted || upload.IsCompleted, "both wait for the other process"); + } + + await apply; + var filed = await upload; + var now = plan.Steps.Single().ToFolder; + Assert.True(filed.RelativePath.StartsWith(now + "/", StringComparison.Ordinal), filed.RelativePath); + Assert.FileExists(fx.Paths.FromRelative(filed.RelativePath)); + Assert.Count(4, await fx.Db.Frames.ListBySequenceAsync(ids[0], cancellationToken: fx.Ct)); + Assert.False(Directory.Exists(fx.Paths.FromRelative("inbox/a")), "the folder it left is not created again"); + } + + /// A render row of at (archive-relative or absolute), with its file unless told otherwise. + private static async Task RenderAsync(ArchiveFixture fx, Guid sequenceId, string output, bool write = true) + { + if (write) + { + var file = Path.IsPathRooted(output) ? output : fx.Paths.FromRelative(output); + Directory.CreateDirectory(Path.GetDirectoryName(file)!); + await File.WriteAllBytesAsync(file, [1, 2, 3], fx.Ct); + } + var render = new Render { Id = Guid.CreateVersion7(), SequenceId = sequenceId, PresetName = "1080p", OutputPath = output, Bytes = 3, Created = DateTimeOffset.UtcNow }; + await fx.Db.Renders.InsertAsync(render, fx.Ct); + return render.Id; + } + + private static async Task> OutputsAsync(ArchiveFixture fx, IEnumerable renders) + { + var outputs = new List(); + foreach (var id in renders) outputs.Add((await fx.Db.Renders.GetAsync(id, fx.Ct))!.OutputPath); + return outputs; + } + [Test] public async Task AFolderInsideASequenceFolderIsRefused() { diff --git a/tests/Chronos.Data.Tests/Sync/FakeSyncRemote.cs b/tests/Chronos.Data.Tests/Sync/FakeSyncRemote.cs index bbfc6bd6..ce4f9d38 100644 --- a/tests/Chronos.Data.Tests/Sync/FakeSyncRemote.cs +++ b/tests/Chronos.Data.Tests/Sync/FakeSyncRemote.cs @@ -103,13 +103,21 @@ internal sealed class FakeSyncRemote : ISyncRemote } } - public Task GetManifestAsync(Guid id, CancellationToken cancellationToken) + /// Runs once, before the next manifest is answered: the archive on this PC changing while the sync compares. + public Func? OnceBeforeManifest { get; set; } + + public async Task GetManifestAsync(Guid id, CancellationToken cancellationToken) { + if (OnceBeforeManifest is { } before) + { + OnceBeforeManifest = null; + await before(); + } lock (_gate) { ManifestCalls++; if (Refused.Contains(id)) throw new ChronosApiException(HttpStatusCode.Forbidden, new ApiError("forbidden", "refused")); - return Task.FromResult(Find(id).Manifest(SentProxies.ContainsKey)); + return Find(id).Manifest(SentProxies.ContainsKey); } } diff --git a/tests/Chronos.Data.Tests/Sync/SyncServiceTests.cs b/tests/Chronos.Data.Tests/Sync/SyncServiceTests.cs index d3b5d18f..6fa3d23b 100644 --- a/tests/Chronos.Data.Tests/Sync/SyncServiceTests.cs +++ b/tests/Chronos.Data.Tests/Sync/SyncServiceTests.cs @@ -97,6 +97,35 @@ public sealed class SyncServiceTests(TestContext context) Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status); } + [Test] + public async Task SettingsTakenWhileAReorganizeMovesTheFolderGoWhereTheSequenceIsNow() + { + await using var f = await SyncFixture.CreateAsync(context); + var photos = f.NewPhotos(2); + var local = await f.LocalAsync("Harbour", photos); + var remote = f.Remote.Add("Harbour", photos); + await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct); + await f.RefreshAsync(); + var left = f.Archive.Paths.Sequence((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Folder).Folder; + + // The server's edit is taken after a Reorganize moved the folder of the copy the comparison had read. + f.Remote.EditSettings(remote.Id, SyncFixture.Window33); + f.Remote.OnceBeforeManifest = async () => + { + var plan = await f.Archive.Service.PlanRenameAsync(f.Archive.Archive.Id, [local.Id], "moved/{title}", null, f.Ct); + await f.Archive.Service.ApplyRenameAsync(plan, null, f.Ct); + }; + await f.RefreshAsync(); + Assert.Null(f.Remote.OnceBeforeManifest, "the folder moved during the comparison"); + + var taken = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!; + Assert.Equal("moved/Harbour", taken.Folder); + Assert.True(SyncRules.SameSettings(SyncFixture.Window33, taken.SettingsJson), "the server's edit reached this PC"); + var project = await File.ReadAllTextAsync(f.Archive.Paths.Sequence(taken.Folder).ProjectPath, f.Ct); + Assert.True(SyncRules.SameSettings(SyncFixture.Window33, project), "project.json in the moved folder follows the index"); + Assert.False(Directory.Exists(left), "the folder it left is not created again"); + } + [Test] public async Task PositionsSentToTheServerNameItsFramesAndWaitForPhotosItLacks() { diff --git a/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs b/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs index c1f1e3dd..66a86b1b 100644 --- a/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs +++ b/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs @@ -80,6 +80,43 @@ public sealed class ReorganizeTests(TestContext context) } } + [Test] + public async Task AnExportMovesWithItsFolderAndStaysDownloadable() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var admin = await server.AdminAsync(); + var id = (await Uploads.UploadAsync(editor, Photo(1), "IMG_0001.JPG", sessionKey: "roof")).SequenceId; + await UntilIdleAsync(server); + var folder = (await server.Database.Sequences.GetAsync(id))!.Folder; + var video = Uploads.Photo(4096, 28); + var relative = $"{folder}/exports/Roof - 4K.mp4"; + Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(server.Options.ArchivePath, relative))!); + await File.WriteAllBytesAsync(Path.Combine(server.Options.ArchivePath, relative), video); + var render = new Render { Id = Guid.CreateVersion7(), SequenceId = id, PresetName = "4K HEVC", OutputPath = relative, Bytes = video.Length, Created = DateTimeOffset.UtcNow }; + await server.Database.Renders.InsertAsync(render); + + // A new folder and title, the files keep their names: the folder moves with its exports. + var request = new ReorganizeRequest([id], false, "reorganized/{title}", null, "Roof", DryRun: true); + ReorganizePlanDto plan; + using (var dry = await admin.PostAsJsonAsync(Url, request, ProtocolJson.Default.ReorganizeRequest)) + plan = await dry.ReadAsync(ProtocolJson.Default.ReorganizePlanDto); + JobDto job; + using (var applied = await admin.PostAsJsonAsync(Url, request with { DryRun = false, Fingerprint = plan.Fingerprint }, ProtocolJson.Default.ReorganizeRequest)) + { + await applied.ExpectAsync(HttpStatusCode.Accepted); + job = await applied.ReadAsync(ProtocolJson.Default.JobDto); + } + await UntilJobAsync(admin, job.Id, "done"); + + Assert.Equal("reorganized/Roof", (await server.Database.Sequences.GetAsync(id))!.Folder); + Assert.Equal("reorganized/Roof/exports/Roof - 4K.mp4", (await server.Database.Renders.GetAsync(render.Id))!.OutputPath); + using var response = await editor.GetAsync($"/api/v1/renders/{render.Id}/file"); + await response.ExpectAsync(HttpStatusCode.OK); + await Http.BytesEqualAsync(video, response.Content.ReadAsByteArrayAsync()); + Assert.False(Directory.Exists(Path.Combine(server.Options.ArchivePath, folder)), "nothing is left at the old place"); + } + [Test] public async Task AReorganizeAsksForTheArchiveAndAValidRequest() {