diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index ccded0aa..84b9172c 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -154,7 +154,8 @@ exports a server files, exports a sync fetches, editor saves, sync applies and " sequence is. The image passes hold the thumbnail lease, which a Reorganize takes too: the thumbnail pass for all of it, the proxy pass a batch at a time (a quarter of the lease wait), reading the folder and sidecar again for each batch. A folder another index moved is found by this index's next scan, which moves the row, its frames and its exports' paths -(`ArchiveIndexer.RebaseRendersAsync`; an export whose file is still at the old place keeps its path); until then these +(`ArchiveIndexer.RebaseRendersAsync`; an export, a file or an image-sequence folder, still at the old place keeps its +path, unless the old folder is another sequence's now); until then these writers, a render job as it starts and a proxy pass at each batch refuse when they find the folder gone, and a deep analysis that loses it records nothing. A render whose folder another index moves while it runs is not stopped: its output can land in a folder created again at the old place, where its row keeps finding it. diff --git a/i18n/en/Server.resx b/i18n/en/Server.resx index ff1f51e7..5e7a8059 100644 --- a/i18n/en/Server.resx +++ b/i18n/en/Server.resx @@ -42,6 +42,7 @@ The connection failed; the log has the details.long A file of the sequence is open in another program; close it and try again.long Its folder is no longer on disk; nothing is left to move to the trash.long + The folder of this sequence is not where the server's index has it; try again after its next scan.long Your account cannot do this.long The photos of the sequence changed on the server; reload it and apply your edit again.long The file arrived damaged; upload it again.long diff --git a/i18n/it/Server.it.resx b/i18n/it/Server.it.resx index e49b2c47..e76fc6b9 100644 --- a/i18n/it/Server.it.resx +++ b/i18n/it/Server.it.resx @@ -40,6 +40,7 @@ La connessione non è riuscita; i dettagli sono nel log. Un file della sequenza è aperto in un altro programma; chiudilo e riprova. La sua cartella non è più sul disco; non resta nulla da spostare nel cestino. + La cartella di questa sequenza non è dove la indica l'indice del server; riprova dopo la sua prossima scansione. Il tuo account non può farlo. Le foto della sequenza sono cambiate sul server; ricaricala e riapplica la modifica. Il file è arrivato danneggiato; caricalo di nuovo. diff --git a/src/Chronos.App/Archive/ReorganizeDialogViewModel.cs b/src/Chronos.App/Archive/ReorganizeDialogViewModel.cs index b028fac7..6c90fa95 100644 --- a/src/Chronos.App/Archive/ReorganizeDialogViewModel.cs +++ b/src/Chronos.App/Archive/ReorganizeDialogViewModel.cs @@ -61,7 +61,8 @@ public sealed class ReorganizeTemplate : ObservableObject /// Reorganize (owner review 2, R7; BRIEF §7.1, §12.5): new folder, file and title templates applied to sequences already in /// the archive — a set, or every sequence. Each template has its switch and starts from the archive's settings; the dry run /// (the engine's plan, nothing changes on disk) follows the typing and lists each sequence that changes with its new -/// folder, title and first file names, and the photos that keep their names with why. Apply queues the Reorganize job of +/// folder, title and first file names, and the photos that keep their names with why, or why the whole sequence stays as +/// it is (a sidecar that cannot be read). Apply queues the Reorganize job of /// exactly that plan (): resumable, paused and followed in Jobs, refused when the /// archive changed after the dry run. /// diff --git a/src/Chronos.Core/Archiving/MassRename.cs b/src/Chronos.Core/Archiving/MassRename.cs index 51aff951..0d85ea6c 100644 --- a/src/Chronos.Core/Archiving/MassRename.cs +++ b/src/Chronos.Core/Archiving/MassRename.cs @@ -260,7 +260,8 @@ public static class RenamePlanner /// Whether a folder of exactly that name, in every level's case, is on disk /// (): on a case-sensitive file system Trip (2) beside a sequence at trip (2) is /// another folder, which a change of case could never move onto; on a case-insensitive one only the sequence's own - /// folder is found, under one of the two names. Null when no folder but the sequences' own counts. + /// folder can be found, under at most one of the two names (under neither when the disk spells it another way, such as + /// TRIP (2)). Null when no folder but the sequences' own counts. /// A target lies inside a sequence's folder, or inside another target. public static RenamePlan Plan(Guid archiveId, IReadOnlyList subjects, string? folderTemplate, string? titleTemplate, IEnumerable occupied, Func existsOnDisk, IReadOnlyList? nested = null, string? fileTemplate = null, diff --git a/src/Chronos.Data/Archiving/ArchiveIndexer.cs b/src/Chronos.Data/Archiving/ArchiveIndexer.cs index 488e78ad..845920b6 100644 --- a/src/Chronos.Data/Archiving/ArchiveIndexer.cs +++ b/src/Chronos.Data/Archiving/ArchiveIndexer.cs @@ -187,25 +187,34 @@ public sealed class ArchiveIndexer /// /// The render rows of the archive that follow a sequence's folder from to , - /// in the caller's transaction (). A file is at its old path - /// still when it is there and the new path is not the same file (on a file system that ignores case it is, when only the - /// case changed). The sequence's own exports inside the folder follow unless their file is at the old path still and not - /// at the new one: written there after the move by a writer that read the old folder, which created it again, so the row - /// keeps naming where the file is. Another sequence's export follows only when its file moved with the folder (at its new - /// path and no longer at the old one). An export missing from both places follows if it is the sequence's own. + /// in the caller's transaction (). An export is a file, or + /// a folder for an image sequence. It is at its old path still when it is there, the new path is not the same one (on a + /// file system that ignores case it is, when only the case changed) and the old folder holds no other sequence now. The + /// sequence's own exports inside the folder follow unless they are at the old path still and not at the new one: + /// written there after the move by a writer that read the old folder, which created it again, so the row keeps naming + /// where the export is. Another sequence's export follows only when it moved with the folder (at its new path and no + /// longer at the old one). An export missing from both places follows if it is the sequence's own, and so does one whose + /// old path is another sequence's: a sequence that moved into the old folder since brought an export of that name. /// internal static Task RebaseRendersAsync(ChronosRepositories tx, Archive archive, Guid sequenceId, string from, string to, CancellationToken cancellationToken) { var paths = new ArchivePaths(archive.RootPath); + // Read once, and only for an export found at its old path. + bool? taken = null; return tx.Renders.RebaseFolderAsync(archive.Id, sequenceId, from, to, (before, after, own) => { - var moved = File.Exists(paths.FromRelative(after)); - var behind = !string.Equals(before, after, StringComparison.OrdinalIgnoreCase) && File.Exists(paths.FromRelative(before)); + var moved = Path.Exists(paths.FromRelative(after)); + var behind = !string.Equals(before, after, StringComparison.OrdinalIgnoreCase) && Path.Exists(paths.FromRelative(before)) + && !(taken ??= TakenByAnother(paths.Sequence(from), sequenceId)); return own ? moved || !behind : moved && !behind; }, cancellationToken); } + /// Whether is another sequence's now: its sidecar names another id. + private static bool TakenByAnother(SequencePaths folder, Guid sequenceId) => + File.Exists(folder.ManifestPath) && SequenceTrash.SidecarId(folder.ManifestPath) is { } id && id != sequenceId; + /// /// Remembers the current stamps of a folder whose sidecar and index were just updated together, so the next /// incremental scan does not re-read what is already indexed. diff --git a/src/Chronos.Data/Archiving/SequenceTrash.cs b/src/Chronos.Data/Archiving/SequenceTrash.cs index afcbaaca..9e8acacd 100644 --- a/src/Chronos.Data/Archiving/SequenceTrash.cs +++ b/src/Chronos.Data/Archiving/SequenceTrash.cs @@ -511,7 +511,7 @@ public sealed partial class SequenceTrash /// The id a sidecar names, from its head (the id is written second), so checking a 35 MB sidecar does not parse it; /// the whole file is read only when the head does not hold the id. Null when the file is unreadable. /// - private static Guid? SidecarId(string path) + internal static Guid? SidecarId(string path) { try { diff --git a/src/Chronos.Data/Repositories/RenderRepository.cs b/src/Chronos.Data/Repositories/RenderRepository.cs index 6ba53e89..40bf460e 100644 --- a/src/Chronos.Data/Repositories/RenderRepository.cs +++ b/src/Chronos.Data/Repositories/RenderRepository.cs @@ -52,7 +52,9 @@ public sealed class RenderRepository /// the folder takes the new path: the caller tells the sequence's own exports there (which /// follow unless their file stayed behind) from an export of another sequence saved inside it through the Export /// folder picker (which follows only when its file moved with the folder: a sequence that took the old folder since, by - /// a later Reorganize another index made, keeps its own). Outputs elsewhere (absolute paths, other folders) stay as they are. + /// a later Reorganize another index made, keeps its own). An export of the sequence whose path an export of another + /// sequence names follows without asking: that path is the other sequence's now, which brought an export of that name + /// into the old folder. Outputs elsewhere (absolute paths, other folders) stay as they are. /// /// Whether the render at the first archive-relative path follows to the second one; the third /// argument says whether it is one of the sequence's own. @@ -73,7 +75,9 @@ public sealed class RenderRepository foreach (var (id, owner, path) in under) { var to = newFolder.Length == 0 ? path[prefix.Length..] : newFolder + path[oldFolder.Length..]; - if (!follows(path, to, owner == sequenceId)) continue; + var own = owner == sequenceId; + var taken = own && under.Any(o => o.SequenceId != sequenceId && string.Equals(o.Path, path, StringComparison.Ordinal)); + if (!taken && !follows(path, to, own)) continue; rebased += await _db.ExecuteAsync("UPDATE renders SET output_path = $2 WHERE id = $1", [id, to], cancellationToken).ConfigureAwait(false); } return rebased; diff --git a/src/Chronos.Data/Sync/SyncService.cs b/src/Chronos.Data/Sync/SyncService.cs index 298dc26d..8ff1ab76 100644 --- a/src/Chronos.Data/Sync/SyncService.cs +++ b/src/Chronos.Data/Sync/SyncService.cs @@ -177,14 +177,16 @@ public sealed class SyncService /// /// Settles a conflict as the user chose (BRIEF §12.7), against the versions the user was shown: when either side /// changed again since, nothing is written, the sequence is compared again and - /// is thrown, as it is when cannot keep this PC's version first (the folder is not - /// where the index has it). Returns the file this PC's settings were saved to for , - /// else null. + /// is thrown, as it is when cannot keep this PC's version first or the server's + /// version cannot be written because the folder is not where the index has it (another index moved it; its message then + /// says to try again after the next scan). Returns the file this PC's settings were saved to for + /// , else null. /// public async Task ResolveAsync(ISyncRemote remote, Guid remoteId, SyncResolution resolution, CancellationToken cancellationToken) { string? saved = null; var changed = false; + var moved = false; await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { @@ -229,6 +231,7 @@ public sealed class SyncService // This PC's version could not be kept (another index moved the folder): the server's does not replace it, // even where this index's scan finds the folder before the server's version would be written. changed = true; + moved = true; } else { @@ -239,8 +242,10 @@ public sealed class SyncService } else { - // Edited on this PC since it was read: the copy of a version the user did not choose to keep goes. + // Edited on this PC since it was read, or the folder moved by another index: the copy of a version the + // user did not choose to keep goes. changed = true; + moved = await FolderMovedAsync(local, cancellationToken).ConfigureAwait(false); if (saved is not null) await DeleteCopyAsync(local, saved, cancellationToken).ConfigureAwait(false); saved = null; } @@ -259,7 +264,8 @@ public sealed class SyncService if (changed) { await RefreshSequencesAsync(remote, [remoteId], CompareOnly, cancellationToken).ConfigureAwait(false); - throw new SyncConflictChangedException(Loc.L("Errors.Sync.ChangedAgain")); + // Choosing again changes nothing while the folder is not where the index has it: the next scan finds it. + throw new SyncConflictChangedException(Loc.L(moved ? "Errors.Archive.FolderMoved" : "Errors.Sync.ChangedAgain")); } await StoreStatesAsync(remote.Server, null, cancellationToken).ConfigureAwait(false); Changed?.Invoke(remote.Server); @@ -823,6 +829,19 @@ public sealed class SyncService return path; } + /// + /// Whether the sequence is not in the folder the index has for it now, on an archive that is there: another index moved + /// it, and this index's next scan finds it. Read without the lease, to tell why a resolution wrote nothing. + /// + private async Task FolderMovedAsync(Sequence local, CancellationToken cancellationToken) + { + if (await _db.Sequences.GetAsync(local.Id, cancellationToken).ConfigureAwait(false) is not { } row + || await _db.Archives.GetAsync(row.ArchiveId, cancellationToken).ConfigureAwait(false) is not { } archive) + return false; + var paths = new ArchivePaths(archive.RootPath); + return Directory.Exists(paths.Root) && !Directory.Exists(paths.Sequence(row.Folder).Folder); + } + /// /// Deletes a "Save both" copy of a version the user did not keep from the sequence's folder as the index has it now: a /// Reorganize may have moved the folder, the copy with it, since the copy was written. Best effort: a copy that cannot diff --git a/src/Chronos.Protocol/Jobs.cs b/src/Chronos.Protocol/Jobs.cs index 5a212dbd..4ef5a995 100644 --- a/src/Chronos.Protocol/Jobs.cs +++ b/src/Chronos.Protocol/Jobs.cs @@ -58,8 +58,9 @@ public sealed record RenderJobParameters(string PresetName, string PresetJson, s /// /// POST /api/v1/nodes/{id}/jobs/{jobId}/result: the finished output a node is about to upload in chunks. A -/// result/complete that cannot file it for now answers 503 sequence_busy or archive_offline; the -/// upload and the job stay the node's, to be completed again. +/// result/complete that cannot file it for now answers 503 sequence_busy, archive_offline or +/// folder_moved (another index moved the sequence's folder; the server's next scan finds it); the upload and the +/// job stay the node's, to be completed again. /// public sealed record RenderResultRequest( string FileName, diff --git a/src/Chronos.Protocol/Uploads.cs b/src/Chronos.Protocol/Uploads.cs index 1df2267c..b0a2108d 100644 --- a/src/Chronos.Protocol/Uploads.cs +++ b/src/Chronos.Protocol/Uploads.cs @@ -44,7 +44,8 @@ public sealed record CardImportReport(Guid ImportId, string Card, string State, /// server already has that id it answers 200 with . Chunks go to /// /uploads/{uploadId}/chunks/{n}; POST /sequences/{id}/renders/{uploadId}/complete verifies the SHA-256 /// and files the render under the sequence's exports. A completion that cannot file it for now answers 503 -/// sequence_busy or archive_offline and keeps the upload, to be completed again. +/// sequence_busy, archive_offline or folder_moved (another index moved the sequence's folder; the +/// server's next scan finds it) and keeps the upload, to be completed again. /// public sealed record RenderUploadRequest( Guid RenderId, diff --git a/src/Chronos.Server/Api/ArchiveEndpoints.cs b/src/Chronos.Server/Api/ArchiveEndpoints.cs index 37d1ad71..900f7c11 100644 --- a/src/Chronos.Server/Api/ArchiveEndpoints.cs +++ b/src/Chronos.Server/Api/ArchiveEndpoints.cs @@ -72,11 +72,12 @@ public static class ArchiveEndpoints /// Writes the name, templates and proxy policy into the archive's archive.json through the archive engine (so a /// restart and the proxy jobs see them), then into the index. 400 bad_request (details name, /// proxyLongEdge), 400 invalid_template (: folderTemplate or - /// fileTemplate, with the rule it breaks); 503 archive_offline while the archive cannot be - /// written; 409 archive_busy while another process holds its settings; 409 archive_manifest when its - /// archive.json is damaged, written by a newer Chronos or of another archive; 409 archive_policy when it holds a - /// thumbnail or proxy setting this API cannot set and Chronos cannot use (details: thumbnailPolicy or - /// proxyPolicy, the property to correct in the file). Nothing is written. + /// fileTemplate, with the rule it breaks; only a template the request changes is held to the rules, so one + /// archive.json holds from before them never fails a save of the other fields); 503 archive_offline while the + /// archive cannot be written; 409 archive_busy while another process holds its settings; 409 + /// archive_manifest when its archive.json is damaged, written by a newer Chronos or of another archive; 409 + /// archive_policy when it holds a thumbnail or proxy setting this API cannot set and Chronos cannot use (details: + /// thumbnailPolicy or proxyPolicy, the property to correct in the file). Nothing is written. /// private static async Task UpdateAsync(HttpContext context, Caller caller, ArchiveSettingsDto request, ChronosDatabase db, ServerArchive archive, [FromServices] ArchiveService? archives, AuditLog audit) @@ -85,8 +86,12 @@ public static class ArchiveEndpoints if (string.IsNullOrEmpty(name) || name.Length > ArchiveSettings.MaxNameLength) return ApiErrors.BadRequest(context, "bad_request", "name"); if (string.IsNullOrWhiteSpace(request.FolderTemplate)) return ApiErrors.BadRequest(context, "invalid_template", "folderTemplate"); if (string.IsNullOrWhiteSpace(request.FileTemplate)) return ApiErrors.BadRequest(context, "invalid_template", "fileTemplate"); - // The rules Reorganize checks: a folder scans never enter, a lost extension or names that repeat are refused alike. - if (new RenameTemplates(request.FolderTemplate, request.FileTemplate).Check() is { } problem) return InvalidTemplate(context, problem); + // The rules Reorganize checks (a folder scans never enter, a lost extension or names that repeat) hold a template the + // request changes, as the engine does: one archive.json holds already, saved before them, never fails another save. + var templates = new LayoutTemplates { Folder = request.FolderTemplate, File = request.FileTemplate }; + var stored = new LayoutTemplates { Folder = archive.Current.FolderTemplate, File = archive.Current.FileTemplate }; + if (new ArchiveSettings(name, templates, new ProxyPolicy(), new ThumbnailPolicy()).CheckTemplates(stored) is { } problem) + return InvalidTemplate(context, problem); if (!ProxyPolicy.IsValidLongEdge(request.ProxyLongEdge)) return ApiErrors.BadRequest(context, "bad_request", "proxyLongEdge"); var engine = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner }); @@ -108,6 +113,11 @@ public static class ArchiveEndpoints { return ApiErrors.Conflict(context, "archive_manifest"); } + catch (TemplateRuleException ex) + { + // archive.json held other templates than the index (a desktop's edit since): the request's breaks a rule for it. + return InvalidTemplate(context, new TemplateProblem(ex.ParamName!, ex.Rule)); + } catch (ArgumentException ex) { // The request's fields were checked above: what fails is a value read from archive.json (a thumbnail size, or a diff --git a/src/Chronos.Server/Jobs/RenderNodeService.cs b/src/Chronos.Server/Jobs/RenderNodeService.cs index 7f1bf866..8118e3da 100644 --- a/src/Chronos.Server/Jobs/RenderNodeService.cs +++ b/src/Chronos.Server/Jobs/RenderNodeService.cs @@ -181,7 +181,7 @@ public sealed class RenderNodeService( { render = sequence is null ? null : await renders.CommitAsync(sequence, staged, output, node.ClientName, cancellationToken); } - catch (Exception ex) when (ex is LeaseUnavailableException or ArchiveOfflineException) + catch (Exception ex) when (ex is LeaseUnavailableException or ArchiveOfflineException or FolderMovedException) { // The node keeps the job and completes again. return await uploads.ReceiveAgainAsync(upload, staged, ex); diff --git a/src/Chronos.Server/Storage/RenderStore.cs b/src/Chronos.Server/Storage/RenderStore.cs index 349b26af..6fb03fa5 100644 --- a/src/Chronos.Server/Storage/RenderStore.cs +++ b/src/Chronos.Server/Storage/RenderStore.cs @@ -1,4 +1,5 @@ using Chronos.Core.IO; +using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Data; using Chronos.Data.Archiving; @@ -16,21 +17,24 @@ public sealed class RenderStore(ChronosDatabase db, ServerArchive archive, TimeP /// Moves (on the archive volume) to <sequence folder>/exports/, never /// replacing an existing export, and inserts the render row, both under the sequence's lease with its folder as the /// index has it then: a Reorganize may have moved it since was read, and moves the row's - /// path along when it moves it later. Null when the sequence left the index, has no folder in the archive yet or is not - /// in the folder the index has (another index moved it); nothing is moved then. + /// path along when it moves it later. Null when the sequence left the index or has no folder in the archive yet; nothing + /// is moved then. /// + /// The sequence is not in the folder the index has (another index moved it): + /// nothing is moved, and the filing can be done again once this index's next scan found the folder. public Task CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, CancellationToken cancellationToken) => CommitAsync(sequence, stagedFile, output, producedBy, Guid.CreateVersion7(), cancellationToken); /// As above, keeping : a render a client sends keeps the id it has on that client. + /// The sequence is not in the folder the index has. public Task CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, Guid id, CancellationToken cancellationToken) => _archives.UnderLeaseAsync(sequence, async row => { - // A folder another index moved is found by this index's next scan; until then nothing is filed, so it is not created again. - if (row is null || string.IsNullOrEmpty(row.Folder) || ArchivePathGuard.Resolve(archive.Root, row.Folder.TrimEnd('/') + "/exports") is not { } exports - || !Directory.Exists(Path.GetDirectoryName(exports))) + if (row is null || string.IsNullOrEmpty(row.Folder) || ArchivePathGuard.Resolve(archive.Root, row.Folder.TrimEnd('/') + "/exports") is not { } exports) return null; + // A folder another index moved is found by this index's next scan; until then nothing is filed, so it is not created again. + if (!Directory.Exists(Path.GetDirectoryName(exports))) throw new FolderMovedException(); Directory.CreateDirectory(exports); var target = Unique(exports, output.FileName); File.Move(stagedFile, target); @@ -62,3 +66,9 @@ public sealed class RenderStore(ChronosDatabase db, ServerArchive archive, TimeP return candidate; } } + +/// +/// A sequence is not in the folder the index has (another index of the archive moved it): nothing was filed into it, and +/// this index's next scan finds the folder, after which the filing can be done again. +/// +public sealed class FolderMovedException() : IOException(Loc.L("Errors.Archive.FolderMoved")); diff --git a/src/Chronos.Server/Storage/RenderUploads.cs b/src/Chronos.Server/Storage/RenderUploads.cs index 33bb017b..80e55955 100644 --- a/src/Chronos.Server/Storage/RenderUploads.cs +++ b/src/Chronos.Server/Storage/RenderUploads.cs @@ -95,7 +95,7 @@ public sealed class RenderUploads( render = sequence is null ? null : await renders.CommitAsync(sequence, staged, result, output.ProducedBy ?? caller.ClientName ?? caller.User.Username, output.RenderId, cancellationToken); } - catch (Exception ex) when (ex is LeaseUnavailableException or ArchiveOfflineException) + catch (Exception ex) when (ex is LeaseUnavailableException or ArchiveOfflineException or FolderMovedException) { return await uploads.ReceiveAgainAsync(upload, staged, ex); } diff --git a/src/Chronos.Server/Storage/UploadService.cs b/src/Chronos.Server/Storage/UploadService.cs index b46aaf18..1455256d 100644 --- a/src/Chronos.Server/Storage/UploadService.cs +++ b/src/Chronos.Server/Storage/UploadService.cs @@ -306,9 +306,10 @@ public sealed class UploadService( /// /// A verified upload whose filing was refused before anything moved, for a reason that passes: its sequence held by - /// another writer past the lease wait (503 sequence_busy), or the archive offline or read-only (503 - /// archive_offline). The staged copy goes and the upload receives again with its part file, so the client - /// completes it again, as it does after any 503. + /// another writer past the lease wait (503 sequence_busy), the archive offline or read-only (503 + /// archive_offline), or the sequence's folder not where the index has it until this index's next scan (503 + /// folder_moved, ). The staged copy goes and the upload receives again with its + /// part file, so the client completes it again, as it does after any 503. /// public async Task> ReceiveAgainAsync(Upload upload, string staged, Exception refusal) { @@ -316,7 +317,12 @@ public sealed class UploadService( logger.LogWarning("Upload {Upload} ({File}) not filed for now: {Reason}", upload.Id, upload.FileName, refusal.Message); UploadStore.TryDelete(staged); await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Receiving, cancellationToken: CancellationToken.None); - return Fail(503, refusal is ArchiveOfflineException ? "archive_offline" : "sequence_busy"); + return Fail(503, refusal switch + { + ArchiveOfflineException => "archive_offline", + FolderMovedException => "folder_moved", + _ => "sequence_busy", + }); } private async Task FailAsync(Upload upload) diff --git a/tests/Chronos.Data.Tests/Archiving/MassRenameTests.cs b/tests/Chronos.Data.Tests/Archiving/MassRenameTests.cs index a22a7ba6..e1cc6dbd 100644 --- a/tests/Chronos.Data.Tests/Archiving/MassRenameTests.cs +++ b/tests/Chronos.Data.Tests/Archiving/MassRenameTests.cs @@ -328,6 +328,62 @@ public sealed class MassRenameTests(TestContext context) foreach (var path in expected[..2]) Assert.FileExists(fx.Paths.FromRelative(path)); } + [Test] + public async Task AnImageSequenceExportIsAFolderThatFollowsAsAFileDoes() + { + await using var fx = await CreateAsync(context); + var ids = await PopulateAsync(fx); + // Image-sequence exports: of "inbox/a" in its folder, and of "inbox/b" saved in the exports of "inbox/a". + List renders = + [ + await RenderAsync(fx, ids[0], "inbox/a/exports/A frames", frames: true), + await RenderAsync(fx, ids[1], "inbox/a/exports/B frames", frames: true), + ]; + var (server, archive) = await fx.OtherWriterAsync(); + + // The other index moves "inbox/a"; a writer here that read the folder before files one where it was, creating it again. + var plan = await server.PlanRenameAsync(archive.Id, [ids[0]], "moved/{date}", null, fx.Ct); + await server.ApplyRenameAsync(plan, null, fx.Ct); + renders.Add(await RenderAsync(fx, ids[0], "inbox/a/exports/Late frames", frames: true)); + + // Each folder follows, or stays, as a file would: both that moved with the folder, not the one written where it was. + await fx.ScanAsync(); + string[] expected = ["moved/2026-09-28/exports/A frames", "moved/2026-09-28/exports/B frames", "inbox/a/exports/Late frames"]; + Assert.SequenceEqual(expected, await OutputsAsync(fx, renders)); + foreach (var path in expected) Assert.FileExists(Path.Combine(fx.Paths.FromRelative(path), "000001.png")); + } + + [Test] + public async Task AnExportNeverNamesTheOneAnotherSequenceBroughtIntoItsOldFolder() + { + // The new folder is scanned after "inbox/a", then before it. + foreach (var template in new[] { "moved/{date}", "done/{date}" }) + { + await using var fx = await CreateAsync(context); + var ids = await PopulateAsync(fx); + // The export of "inbox/a" is gone; "inbox/c" has one of the same name. + List renders = + [ + await RenderAsync(fx, ids[0], "inbox/a/exports/Clip.mp4", write: false), + await RenderAsync(fx, ids[2], "inbox/c/exports/Clip.mp4"), + ]; + var (server, archive) = await fx.OtherWriterAsync(); + + // The other index moves "inbox/a", then "inbox/c" into the folder it left, its export along. + var first = await server.PlanRenameAsync(archive.Id, [ids[0]], template, null, fx.Ct); + await server.ApplyRenameAsync(first, null, fx.Ct); + await server.ApplyRenameAsync(await server.PlanRenameAsync(archive.Id, [ids[2]], "inbox/a", null, fx.Ct), null, fx.Ct); + + // The first sequence's row follows its folder, missing there too, and never names the other one's file. + await fx.ScanAsync(); + string[] expected = [first.Steps.Single().ToFolder + "/exports/Clip.mp4", "inbox/a/exports/Clip.mp4"]; + Assert.SequenceEqual(expected, await OutputsAsync(fx, renders), template); + Assert.FileExists(fx.Paths.FromRelative(expected[1])); + Assert.Equal(0, (await fx.ScanAsync()).Indexed); + Assert.SequenceEqual(expected, await OutputsAsync(fx, renders), template); + } + } + [Test] public async Task APhotoDeletionWhoseFolderMovedWhileItWaitedForTheLeaseIsRefused() { @@ -439,12 +495,16 @@ public sealed class MassRenameTests(TestContext context) 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) + /// + /// A render row of at (archive-relative or absolute), with its + /// file unless told otherwise; with an image sequence's export, a folder of frames. + /// + private static async Task RenderAsync(ArchiveFixture fx, Guid sequenceId, string output, bool write = true, bool frames = false) { if (write) { var file = Path.IsPathRooted(output) ? output : fx.Paths.FromRelative(output); + if (frames) file = Path.Combine(file, "000001.png"); Directory.CreateDirectory(Path.GetDirectoryName(file)!); await File.WriteAllBytesAsync(file, [1, 2, 3], fx.Ct); } diff --git a/tests/Chronos.Data.Tests/Sync/SyncServiceTests.cs b/tests/Chronos.Data.Tests/Sync/SyncServiceTests.cs index 2ce330d1..a7817b2c 100644 --- a/tests/Chronos.Data.Tests/Sync/SyncServiceTests.cs +++ b/tests/Chronos.Data.Tests/Sync/SyncServiceTests.cs @@ -1,3 +1,4 @@ +using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Core.Project; using Chronos.Data.Archiving; @@ -485,7 +486,8 @@ public sealed class SyncServiceTests(TestContext context) moved = plan.Steps.Single().ToFolder; }; f.Remote.OnceBeforeFrames = () => f.Archive.ScanAsync(); - await Assert.ThrowsAsync(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct)); + var refused = await Assert.ThrowsAsync(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct)); + Assert.Equal(Loc.L("Errors.Archive.FolderMoved"), refused.Message, "the user hears to try again after the next scan"); // The server's frames were not read for a version that is not taken: the scan comes now. f.Remote.OnceBeforeFrames = null; await f.Archive.ScanAsync(); @@ -518,8 +520,9 @@ public sealed class SyncServiceTests(TestContext context) var plan = await other.PlanRenameAsync(archive.Id, [local.Id], "moved/{title}", null, f.Ct); await other.ApplyRenameAsync(plan, null, f.Ct); }; - await Assert.ThrowsAsync(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.TakeServer, f.Ct)); + var refused = await Assert.ThrowsAsync(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.TakeServer, f.Ct)); Assert.Null(f.Remote.OnceBeforeManifest, "the folder moved during the resolution"); + Assert.Equal(Loc.L("Errors.Archive.FolderMoved"), refused.Message, "the user hears to try again after the next scan"); Assert.True(SyncRules.SameSettings(SyncFixture.Window41, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson), "nothing applied"); Assert.False(Directory.Exists(left), "the folder it left is not created again"); @@ -565,7 +568,8 @@ public sealed class SyncServiceTests(TestContext context) var later = SyncFixture.Window41.Replace("41", "45", StringComparison.Ordinal); await f.EditLocalSettingsAsync(local.Id, later); - await Assert.ThrowsAsync(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.TakeServer, f.Ct)); + var refused = await Assert.ThrowsAsync(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.TakeServer, f.Ct)); + Assert.Equal(Loc.L("Errors.Sync.ChangedAgain"), refused.Message); Assert.True(SyncRules.SameSettings(later, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson), "this PC's later edit is kept"); Assert.True(SyncRules.SameSettings(SyncFixture.Window33, remote.SettingsJson)); diff --git a/tests/Chronos.Server.Tests/Api/AdminTests.cs b/tests/Chronos.Server.Tests/Api/AdminTests.cs index 74b55e16..1e1284fe 100644 --- a/tests/Chronos.Server.Tests/Api/AdminTests.cs +++ b/tests/Chronos.Server.Tests/Api/AdminTests.cs @@ -266,4 +266,54 @@ public sealed class AdminTests(TestContext context) } await File.WriteAllBytesAsync(path, saved, context.Cancellation); } + + [Test] + public async Task TemplatesSavedBeforeTheRulesNeverFailASaveOfTheOtherFields() + { + await using var server = await TestServer.StartAsync(context, services: Chronos.Server.Storage.ArchiveModule.Register); + var admin = await server.AdminAsync(); + // An archive.json of 0.1.0-rc.5 or earlier: both templates break a rule new templates are held to. + var path = new Chronos.Core.Archiving.ArchivePaths(server.Options.ArchivePath).ManifestPath; + var manifest = Chronos.Core.Archiving.ManifestStore.ReadArchive(path); + Chronos.Core.Archiving.ManifestStore.WriteArchive(path, manifest with + { + LayoutTemplates = new Chronos.Core.Archiving.LayoutTemplates { Folder = "exports/{date}", File = "{date}" }, + }); + using var read = await admin.GetAsync("/api/v1/archive/settings"); + var archive = await read.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto); + Assert.Equal(("exports/{date}", "{date}"), (archive.FolderTemplate, archive.FileTemplate)); + + // The form sends every field with each save: the name and the proxies change, the templates stay as they are. + using (var put = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { Name = "Studio", ProxyLongEdge = 2560 }, ProtocolJson.Default.ArchiveSettingsDto)) + { + var saved = await put.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto); + Assert.Equal(("Studio", "exports/{date}", "{date}", 2560), (saved.Name, saved.FolderTemplate, saved.FileTemplate, saved.ProxyLongEdge)); + } + archive = archive with { Name = "Studio", ProxyLongEdge = 2560 }; + + // A template given anew is held to every rule; each one is fixed alone. + using (var refused = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { FileTemplate = "{n}{ext}" }, ProtocolJson.Default.ArchiveSettingsDto)) + Assert.Equal("fileTemplate:extension", (await refused.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details); + using (var folder = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { FolderTemplate = "{year}/{date}" }, ProtocolJson.Default.ArchiveSettingsDto)) + { + var saved = await folder.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto); + Assert.Equal(("{year}/{date}", "{date}"), (saved.FolderTemplate, saved.FileTemplate)); + } + archive = archive with { FolderTemplate = "{year}/{date}" }; + + // A desktop fixed the file template in archive.json since the form was read: the old one the form still sends is no + // longer the file's, so it is held to the rules and the save says which one it breaks. + Chronos.Core.Archiving.ManifestStore.WriteArchive(path, Chronos.Core.Archiving.ManifestStore.ReadArchive(path) with + { + LayoutTemplates = new Chronos.Core.Archiving.LayoutTemplates { Folder = "{year}/{date}", File = "{date}_{n:0000}" }, + }); + var before = await File.ReadAllBytesAsync(path, context.Cancellation); + using (var stale = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { ProxyLongEdge = 1280 }, ProtocolJson.Default.ArchiveSettingsDto)) + { + var error = await stale.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template"); + Assert.Equal(("fileTemplate:not_unique", Chronos.Core.Localization.Loc.Get("Errors.Template.NotUnique", System.Globalization.CultureInfo.GetCultureInfo("en"))), + (error.Details, error.Message)); + } + Assert.SequenceEqual(before, await File.ReadAllBytesAsync(path, context.Cancellation)); + } } diff --git a/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs b/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs index e90c99aa..730e5c9a 100644 --- a/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs +++ b/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs @@ -162,7 +162,7 @@ public sealed class ReorganizeTests(TestContext context) var video = Uploads.Photo(4096, 30); var staged = Path.Combine(context.CreateTempDirectory("staged"), "render.mp4"); await File.WriteAllBytesAsync(staged, video); - Assert.Null(await server.Services.GetRequiredService().CommitAsync(sequence, staged, + await Assert.ThrowsAsync(() => server.Services.GetRequiredService().CommitAsync(sequence, staged, new RenderResultRequest("Roof - 4K.mp4", video.Length, Uploads.Sha(video), "4K HEVC", "hevc", 3840, 2160, 25, 4), "studio-pc", CancellationToken.None)); Assert.FileExists(staged); diff --git a/tests/Chronos.Server.Tests/Jobs/RenderNodeTests.cs b/tests/Chronos.Server.Tests/Jobs/RenderNodeTests.cs index 654b3c53..678f37dc 100644 --- a/tests/Chronos.Server.Tests/Jobs/RenderNodeTests.cs +++ b/tests/Chronos.Server.Tests/Jobs/RenderNodeTests.cs @@ -190,6 +190,20 @@ public sealed class RenderNodeTests(TestContext context) { File.Move(paths.ManifestPath + ".away", paths.ManifestPath); } + // A desktop sharing the archive moved the sequence's folder: this server's index has it at the old place until its + // next scan, and nothing is filed there meanwhile. + var folder = (await server.Database.Sequences.GetAsync(frame.SequenceId))!.Folder; + var left = paths.FromRelative(folder); + Directory.Move(left, paths.FromRelative(folder + " moved")); + await RefusedAsync("folder_moved"); + Assert.False(Directory.Exists(left), "the folder it left is not created again"); + // The next scan finds it where it is now (moved here as a scan moves it: this composition writes no sidecars), and the + // renders are filed there. + await using (var tx = await server.Database.BeginAsync(context.Cancellation)) + { + await tx.Sequences.RelocateAsync(frame.SequenceId, folder + " moved", null, context.Cancellation); + await tx.CommitAsync(context.Cancellation); + } foreach (var complete in completions) {