diff --git a/src/Chronos.App/Workspace/WorkspaceHost.cs b/src/Chronos.App/Workspace/WorkspaceHost.cs index 4739dddb..f45ffae6 100644 --- a/src/Chronos.App/Workspace/WorkspaceHost.cs +++ b/src/Chronos.App/Workspace/WorkspaceHost.cs @@ -249,17 +249,38 @@ public sealed class WorkspaceHost return; } // The folder is named by the project's id: only a project this workspace holds, of that server and sequence, is told. + BundleHome home; try { - if (!IsAvailable || Store.Find(change.ProjectId)?.ReadBundle().Workspace?.Home is not { IsArchive: false } home || home.SequenceId != change.SequenceId - || !string.Equals(home.Server, server, StringComparison.OrdinalIgnoreCase)) + if (!IsAvailable || Store.Find(change.ProjectId)?.ReadBundle().Workspace?.Home is not { IsArchive: false } held || held.SequenceId != change.SequenceId + || !string.Equals(held.Server, server, StringComparison.OrdinalIgnoreCase)) return; + home = held; } catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException) { Log.Info("workspace", $"A change of project {change.ProjectId} on {server} could not be matched: {ex.Message}"); return; } + if (change.Change != ProjectChanges.Renamed) + { + HomeChanged?.Invoke(new WorkspaceHomeChange(server, change.ProjectId, change.Change)); + return; + } + // A rename at the server is written to the copy here before it is told, so the state read again shows the new name. + _ = FollowNameThenTellAsync(server, home, change); + } + + private async Task FollowNameThenTellAsync(string server, BundleHome home, ProjectChangedEvent change) + { + try + { + if (HomeOf(home) is { } reached) await Sync.FollowNameAsync(change.ProjectId, reached).ConfigureAwait(false); + } + catch (Exception ex) when (ex is not OutOfMemoryException) + { + Log.Info("workspace", $"The new name of project {change.ProjectId} on {server} could not be followed: {ex.Message}"); + } HomeChanged?.Invoke(new WorkspaceHomeChange(server, change.ProjectId, change.Change)); } diff --git a/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs b/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs index f8c9de97..1cf3f769 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceCopyJobHandler.cs @@ -115,6 +115,9 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService private static readonly TimeSpan LockWait = TimeSpan.FromMinutes(2); private static readonly TimeSpan CheckpointEvery = TimeSpan.FromSeconds(5); + /// Beside a server's thumbnail downloaded for a frame until the copy commits: what it is, should the run stop first. + internal const string StandInSuffix = ".thumb"; + /// Bytes left free on the workspace's drive besides the copy, as a download keeps (). internal const long SpaceReserve = 512L << 20; @@ -540,11 +543,13 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService { var keptSha = await ContentHash.Sha256Async(target, cancellationToken: cancellationToken).ConfigureAwait(false); // The editor's file of another photo of the frame (a conversion since) was left by an earlier copy, not this run. - if (!listed || keptSha != current!.FileSha256 || SamePhoto(current, frame)) + // A run stopped before it recorded the file's kind left a marker beside a thumbnail it took for a missing proxy. + var keptKind = KindOf(fetched, keptSha) ?? KindOf(current, keptSha) + ?? (File.Exists(StandInMarker(target)) ? ProxyCache.Thumb : offered.FirstOrDefault() ?? ProxyCache.Proxy); + if ((!listed || keptSha != current!.FileSha256 || SamePhoto(current, frame)) && !(keptKind == ProxyCache.Thumb && offered.Contains(ProxyCache.Proxy))) { tally.Keep(); - return WorkspaceFrame.Of(frame, name, keptSha, new FileInfo(target).Length, - KindOf(fetched, keptSha) ?? KindOf(current, keptSha) ?? offered.FirstOrDefault() ?? ProxyCache.Proxy); + return WorkspaceFrame.Of(frame, name, keptSha, new FileInfo(target).Length, keptKind); } } // What this run downloaded in an earlier pass, then the editor's file of the same photo, unless a thumbnail stands in @@ -602,6 +607,9 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService file = WorkspaceProject.ProxyName(frame.Id, received); path = project.MediaFile(file); } + // The kind is known on disk before the file is: a run stopped after the move resumes with it. + if (kind == ProxyCache.Thumb) File.WriteAllBytes(StandInMarker(path), []); + else if (File.Exists(StandInMarker(path))) File.Delete(StandInMarker(path)); File.Move(part, path, overwrite: true); if (await ContentHash.Sha256Async(path, cancellationToken: cancellationToken).ConfigureAwait(false) == received) { @@ -612,6 +620,8 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService if (attempt == 2) throw new IOException(Loc.L("Errors.Workspace.CopyMismatch", path)); } + static string StandInMarker(string file) => file + StandInSuffix; + static string? KindOf(WorkspaceFrame? entry, string sha) => entry is { FileSha256: { } held } && string.Equals(held, sha, StringComparison.OrdinalIgnoreCase) ? entry.FileKind ?? ProxyCache.Proxy : null; } @@ -867,6 +877,8 @@ internal sealed class WorkspaceCopyJobHandler(ChronosDatabase db, ArchiveService { if (!listed.Contains(Path.GetFileName(file))) TryDelete(file); } + // The media committed record each file's kind: the markers of a run are done with. + foreach (var marker in Directory.EnumerateFiles(project.ProxiesDirectory, "*" + StandInSuffix)) TryDelete(marker); } private static void TryDelete(string file) diff --git a/src/Chronos.Data/Workspaces/WorkspaceSync.cs b/src/Chronos.Data/Workspaces/WorkspaceSync.cs index 24a446ca..9819427d 100644 --- a/src/Chronos.Data/Workspaces/WorkspaceSync.cs +++ b/src/Chronos.Data/Workspaces/WorkspaceSync.cs @@ -370,10 +370,41 @@ public sealed class WorkspaceSync(WorkspaceStore store, string clientName) return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled); } if (project.ReadSync().Conflict is null) PruneReplaced(project); + await FollowNameLockedAsync(project, home, cancellationToken).ConfigureAwait(false); return new WorkspaceSyncResult(await StatusLockedAsync(project, home, cancellationToken).ConfigureAwait(false), [], pulled); } } + /// + /// The project's name (and whether it is Main) as its home has them now, written to the copy here: a rename made at the + /// home reaches the workspace. True when the copy's name changed; false when it already matched, or the home could not + /// say (unreachable, or no longer listing the project): the next pull or status tells those. + /// + public async Task FollowNameAsync(Guid projectId, IProjectHome home, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(home); + var project = Existing(projectId); + using (await project.LockAsync(LockWait, cancellationToken).ConfigureAwait(false)) + return await FollowNameLockedAsync(project, home, cancellationToken).ConfigureAwait(false); + } + + private static async Task FollowNameLockedAsync(WorkspaceProject project, IProjectHome home, CancellationToken cancellationToken) + { + SequenceProject? listed; + try + { + listed = (await home.ListAsync(cancellationToken).ConfigureAwait(false)).FirstOrDefault(p => p.Id == project.ProjectId); + } + catch (Exception ex) when (ex is HomeUnavailableException or ProjectException) + { + return false; + } + var bundle = project.ReadBundle(); + if (listed is null || bundle.Workspace is not { } workspace || (workspace.Name == listed.Name && workspace.IsMain == listed.IsMain)) return false; + project.WriteBundle(bundle with { Workspace = workspace with { Name = listed.Name, IsMain = listed.IsMain } }); + return true; + } + /// /// Settles the conflict a pull or push stopped at, as the user was shown it (, else the one /// recorded). keeps this workspace's values (but for the members in diff --git a/tests/Chronos.Server.Tests/Workspaces/ServerWorkspaceTests.cs b/tests/Chronos.Server.Tests/Workspaces/ServerWorkspaceTests.cs index d3ffcbfb..d53335c8 100644 --- a/tests/Chronos.Server.Tests/Workspaces/ServerWorkspaceTests.cs +++ b/tests/Chronos.Server.Tests/Workspaces/ServerWorkspaceTests.cs @@ -235,6 +235,132 @@ public sealed class ServerWorkspaceTests(TestContext context) Assert.SequenceEqual([1], (await home.HistoryAsync(vertical.Id, null, 10, context.Cancellation)).Select(v => v.Number)); } + [Test] + public async Task AResumedCopyNeverRecordsAThumbnailAsAProxy() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (_, _, token) = await server.UserAsync("alberto", "editor"); + var sid = await SequenceAsync(server, token, 3); + await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A"); + var home = a.Home(sid); + var main = (await home.ListAsync(context.Cancellation)).Single(); + await a.CheckoutAsync(home, main.Id); + var project = a.Store.Find(main.Id)!; + var media = Assert.NotNull(project.ReadMedia()); + var manifest = await a.Client.GetManifestAsync(sid, context.Cancellation); + var frame = manifest.Frames.OrderBy(f => f.Index).First(f => f.HasProxy); + var entry = media.Frames.Single(f => f.Id == frame.Id); + Assert.Equal("proxy", entry.FileKind); + Assert.Empty(Directory.EnumerateFiles(project.ProxiesDirectory, "*.thumb")); + + // A run stopped after it took the thumbnail for a proxy the server had not made yet, which it has now: the resumed + // run (every frame done by its checkpoint) downloads the proxy instead of keeping the thumbnail as one. + var file = project.MediaFile(entry.File!); + await using (var thumb = File.Create(file)) + await a.Client.DownloadFrameAsync(frame.Id, "thumb", thumb, null, context.Cancellation); + File.WriteAllBytes(file + ".thumb", []); + File.Delete(project.MediaIndexPath); + await a.CopyAsync(sid, main.Id, $"copy:{manifest.Frames.Count}:{manifest.FramesVersion}"); + var resumed = Assert.NotNull(project.ReadMedia()).Frames.Single(f => f.Id == frame.Id); + Assert.Equal(("proxy", entry.FileSha256), (resumed.FileKind, resumed.FileSha256)); + Assert.Equal(entry.FileSha256, Sha(project.MediaFile(resumed.File!))); + Assert.Empty(Directory.EnumerateFiles(project.ProxiesDirectory, "*.thumb")); + } + + [Test] + public async Task AViewersPushIsRefusedForGoodAndItsVersionWaits() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (_, _, token) = await server.UserAsync("alberto", "editor"); + var (_, _, viewing) = await server.UserAsync("vera", "viewer"); + var sid = await SequenceAsync(server, token, 2); + await using var v = await WorkspaceClient.StartAsync(context, server, viewing, "PC-V"); + var home = v.Home(sid); + var main = (await home.ListAsync(context.Cancellation)).Single(); + await v.CheckoutAsync(home, main.Id); + await v.EditAsync(main.Id, s => s with { HeroFrame = 1 }); + Assert.NotNull(await v.Sync.CommitAsync(main.Id, cancellationToken: context.Cancellation)); + + // 403: a refusal, never "out of reach" (nothing to wait out); the version stays here, the server's head as it was. + var refused = await Assert.ThrowsAsync(() => v.Sync.PushAsync(main.Id, home, context.Cancellation)); + Assert.Equal(System.Net.HttpStatusCode.Forbidden, refused.Status); + Assert.False(ServerProjectHome.IsUnavailable(refused)); + Assert.Equal(1, v.Sync.Unpushed(main.Id).Count); + Assert.Equal(1, (await home.HeadAsync(main.Id, context.Cancellation)).Number); + Assert.Equal(WorkspaceState.Ahead, (await v.Sync.StatusAsync(main.Id, home, context.Cancellation)).State); + } + + [Test] + public async Task TheStatusReadsTheServersFramesAgainOnlyWhenTheirStampMoved() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (_, _, token) = await server.UserAsync("alberto", "editor"); + var sid = await SequenceAsync(server, token, 2); + var tap = new Tap(); + await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A", tap); + var home = a.Home(sid); + var main = (await home.ListAsync(context.Cancellation)).Single(); + await a.CheckoutAsync(home, main.Id); + + // The frames are downloaded once; asked again with nothing changed, only their stamp is. + await a.Sync.StatusAsync(main.Id, home, context.Cancellation); + var read = tap.Frames; + Assert.True(read > 0, "the frames were read"); + await a.Sync.StatusAsync(main.Id, home, context.Cancellation); + await a.Sync.StatusAsync(main.Id, home, context.Cancellation); + Assert.Equal(read, tap.Frames); + Assert.True(tap.Stamps >= 3, "the stamp was asked each time"); + + // A photo more on the server moves the stamp: the frames are read again, and the copy reads as outdated. + await Uploads.UploadAsync(server.NewClient(token), Photo(42), "IMG_0042.JPG", sid, capturedAt: new DateTime(2026, 9, 28, 18, 30, 11)); + await JobsDoneAsync(server); + var status = await a.Sync.StatusAsync(main.Id, home, context.Cancellation); + Assert.Equal(read + 1, tap.Frames); + Assert.True(status.MediaOutdated, "the server's frames moved"); + } + + [Test] + public async Task ACopyFromAServerThatKeepsNoProjectsIsRefusedSayingSo() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (_, _, token) = await server.UserAsync("alberto", "editor"); + var sid = await SequenceAsync(server, token, 2); + var tap = new Tap(); + await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A", tap); + var home = a.Home(sid); + var main = (await home.ListAsync(context.Cancellation)).Single(); + await a.Sync.CheckoutAsync(home, main.Id, cancellationToken: context.Cancellation); + + // An older server's API knows no such call (404 naming nothing): the copy fails saying the server keeps no projects. + tap.OlderServer = true; + var job = await a.EnqueueAsync(WorkspaceJobs.CopyFromServer(sid, Title, a.Server, a.Store.Root, main.Id)); + Assert.Equal(Loc.L("Errors.Workspace.NoProjects"), (await a.WaitAsync(job.Id, JobState.Failed)).Error); + Assert.Null(a.Store.Find(main.Id)!.ReadMedia()); + } + + [Test] + public async Task ARenameOnTheServerReachesTheCopyAtItsNextPull() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (_, _, token) = await server.UserAsync("alberto", "editor"); + var sid = await SequenceAsync(server, token, 2); + await using var a = await WorkspaceClient.StartAsync(context, server, token, "PC-A"); + var home = a.Home(sid); + var vertical = await home.CreateAsync("Vertical", null, null, context.Cancellation); + await a.CheckoutAsync(home, vertical.Id); + + // Renamed from another program: the copy here takes the name at its next pull, and says so when asked alone. + using var other = new ChronosClient(server.HttpBase, new CertificatePinning([]), "PC-B") { Token = token }; + var otherHome = new ServerProjectHome(other, sid); + await otherHome.RenameAsync(vertical.Id, "Square", context.Cancellation); + await a.Sync.PullAsync(vertical.Id, home, context.Cancellation); + Assert.Equal("Square", a.Bundle(vertical.Id).Workspace!.Name); + await otherHome.RenameAsync(vertical.Id, "Wide", context.Cancellation); + Assert.True(await a.Sync.FollowNameAsync(vertical.Id, home, context.Cancellation)); + Assert.False(await a.Sync.FollowNameAsync(vertical.Id, home, context.Cancellation)); + Assert.Equal("Wide", a.Bundle(vertical.Id).Workspace!.Name); + } + [Test] public async Task AServerOutOfReachLeavesTheProjectOfflineAndItsWorkWaits() { @@ -315,6 +441,36 @@ public sealed class ServerWorkspaceTests(TestContext context) return JpegEncoder.Encode(pixels, width, height, 3, new JpegEncodeOptions { Quality = 90 }); } + /// The desktop's requests as they go: the frames and stamps read, and an older server's answer to the frames. + private sealed class Tap() : DelegatingHandler(new HttpClientHandler()) + { + private int _frames; + private int _stamps; + + public int Frames => Volatile.Read(ref _frames); + + public int Stamps => Volatile.Read(ref _stamps); + + /// The frames answer 404 naming nothing, as the API's fallback of a server without the projects does. + public bool OlderServer { get; set; } + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var path = request.RequestUri!.AbsolutePath; + if (path.EndsWith("/projects/frames/stamp", StringComparison.Ordinal)) Interlocked.Increment(ref _stamps); + if (path.EndsWith("/projects/frames", StringComparison.Ordinal)) + { + Interlocked.Increment(ref _frames); + if (OlderServer) + return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.NotFound) + { + Content = new StringContent("""{"code":"not_found","message":"Not found"}""", System.Text.Encoding.UTF8, "application/json"), + }); + } + return base.SendAsync(request, cancellationToken); + } + } + /// A desktop's side: its own index (for the job engine), the workspace copy job connected to the test server, a workspace and its sync. private sealed class WorkspaceClient : IAsyncDisposable { @@ -342,13 +498,13 @@ public sealed class ServerWorkspaceTests(TestContext context) /// Whether the copy job finds this PC signed in to the server. public bool Connected { get; set; } = true; - public static async Task StartAsync(TestContext context, TestServer server, string token, string name) + public static async Task StartAsync(TestContext context, TestServer server, string token, string name, HttpMessageHandler? handler = null) { var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory(name + "-db"), "chronos.db")), context.Cancellation); var archives = new ArchiveService(db, new ArchiveServiceOptions { Owner = name }); // This PC's own library: the copy of a server project never writes into it. var archive = await archives.CreateAsync(context.CreateTempDirectory(name + "-archive"), "Local", cancellationToken: context.Cancellation); - var client = new ChronosClient(server.HttpBase, new CertificatePinning([]), name) { Token = token }; + var client = new ChronosClient(server.HttpBase, new CertificatePinning([]), name, handler) { Token = token }; WorkspaceClient? created = null; var engine = new JobEngine(db, WorkspaceJobs.CreateHandlers(db, archives, new Connector(client, () => created?.Connected ?? true)), new JobEngineOptions { Workers = 1, WorkerId = name, PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });