diff --git a/src/Titano.Data/Archiving/ArchiveService.cs b/src/Titano.Data/Archiving/ArchiveService.cs index ddd1fce3..be8b1150 100644 --- a/src/Titano.Data/Archiving/ArchiveService.cs +++ b/src/Titano.Data/Archiving/ArchiveService.cs @@ -163,15 +163,15 @@ public sealed class ArchiveService : IJobHandlerProvider { var archive = await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false); if (TryReadManifest(archive) is not { } manifest) return null; - if (manifest.Name != archive.Name || manifest.LayoutTemplates.Folder != archive.FolderTemplate || manifest.LayoutTemplates.File != archive.FileTemplate - || manifest.ProxyPolicy.LongEdge != archive.ProxyLongEdge) + var settings = ArchiveSettings.Of(manifest); + if (!settings.IsRegisteredIn(archive)) { // Read without the settings lease, as at open: an edit made meanwhile writes its own row, and the next refresh // settles a file that changes again. await RegisterAsync(new ArchivePaths(archive.RootPath), manifest, cancellationToken).ConfigureAwait(false); Log.Info("archive", $"Settings of {archive.RootPath} changed elsewhere: {manifest.LayoutTemplates.Folder} · {manifest.LayoutTemplates.File}, proxies {manifest.ProxyPolicy.LongEdge}"); } - return ArchiveSettings.Of(manifest); + return settings; } /// @@ -339,7 +339,10 @@ public sealed class ArchiveService : IJobHandlerProvider return archive; } - /// The archive's archive.json; null (logged) while it cannot be read or when it belongs to another archive. + /// + /// The archive's archive.json; null (logged) while it cannot be read, when a newer Titano wrote it (this one + /// would drop what it does not know) or when it belongs to another archive. + /// private static ArchiveManifest? TryReadManifest(Archive archive) { var paths = new ArchivePaths(archive.RootPath); @@ -347,10 +350,12 @@ public sealed class ArchiveService : IJobHandlerProvider { if (!File.Exists(paths.ManifestPath)) return null; var manifest = ManifestStore.ReadArchive(paths.ManifestPath); - if (manifest.Id == archive.Id) return manifest; - Log.Warning("archive", $"{paths.ManifestPath} belongs to archive {manifest.Id}, not {archive.Id}; the index's settings are used"); + if (manifest.SchemaVersion > ArchiveManifest.CurrentSchemaVersion) + Log.Warning("archive", $"{paths.ManifestPath} has schema version {manifest.SchemaVersion}, newer than this Titano's; the index's settings are used"); + else if (manifest.Id == archive.Id) return manifest; + else Log.Warning("archive", $"{paths.ManifestPath} belongs to archive {manifest.Id}, not {archive.Id}; the index's settings are used"); } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or NewerFormatException) + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException) { Log.Warning("archive", $"The settings of {paths.Root} cannot be read; the index's are used", ex); } @@ -381,6 +386,14 @@ public sealed record ArchiveSettings(string Name, LayoutTemplates Templates, Pro public static ArchiveSettings Of(ArchiveManifest manifest) => new(manifest.Name, manifest.LayoutTemplates, manifest.ProxyPolicy, manifest.ThumbnailPolicy); + /// Whether the index row holds these settings (all it keeps: not the thumbnail policy). + public bool IsRegisteredIn(Archive archive) + { + ArgumentNullException.ThrowIfNull(archive); + return Name == archive.Name && Templates.Folder == archive.FolderTemplate && Templates.File == archive.FileTemplate + && Proxies.LongEdge == archive.ProxyLongEdge; + } + /// The first invalid field by its API name (name, folderTemplate, fileTemplate, /// proxyLongEdge, thumbnailLongEdge); null when every field is valid. public string? Problem() diff --git a/src/Titano.Server/Api/ArchiveEndpoints.cs b/src/Titano.Server/Api/ArchiveEndpoints.cs index 953df725..f3b62057 100644 --- a/src/Titano.Server/Api/ArchiveEndpoints.cs +++ b/src/Titano.Server/Api/ArchiveEndpoints.cs @@ -19,7 +19,9 @@ public static class ArchiveEndpoints public static void Map(RouteGroupBuilder api) { var archive = api.MapGroup("/archive").Requires(Permissions.ManageArchive); - archive.MapGet("/settings", (ServerArchive a) => Results.Json(Settings(a.Current), ProtocolJson.Default.ArchiveSettingsDto)); + // The form saves every field: it starts from what archive.json holds now, also a desktop's change since the last poll. + archive.MapGet("/settings", async (HttpContext c, ServerArchive a, TitanoDatabase db, [FromServices] ArchiveService? archives) => + Results.Json(Settings(archives is null ? a.Current : await a.RefreshAsync(archives, db, c.RequestAborted)), ProtocolJson.Default.ArchiveSettingsDto)); archive.MapPut("/settings", UpdateAsync); archive.MapGet("/stats", async (HttpContext c, TitanoDatabase db, ServerArchive a) => Results.Json(await StatsAsync(db, a, c.RequestAborted), ProtocolJson.Default.ArchiveStatsDto)); diff --git a/src/Titano.Server/Storage/ArchivePolling.cs b/src/Titano.Server/Storage/ArchivePolling.cs index 739d6451..32a0d3ff 100644 --- a/src/Titano.Server/Storage/ArchivePolling.cs +++ b/src/Titano.Server/Storage/ArchivePolling.cs @@ -28,8 +28,7 @@ public sealed class ArchivePolling(TitanoDatabase db, ServerArchive archive, Arc if (!jobs.CanRun(JobKind.Scan)) return null; var current = archive.Current; if (ArchiveService.GetAvailability(current) != ArchiveAvailability.Online) return null; - if (await archives.RefreshSettingsAsync(current.Id, cancellationToken) is not null && await db.Archives.GetAsync(current.Id, cancellationToken) is { } row) - archive.Current = current = row; + current = await archive.RefreshAsync(archives, db, cancellationToken); foreach (var kind in new[] { JobKind.Scan, JobKind.Rebuild }) { foreach (var state in Pending) diff --git a/src/Titano.Server/Storage/ServerArchive.cs b/src/Titano.Server/Storage/ServerArchive.cs index f6ca64b6..8565acb7 100644 --- a/src/Titano.Server/Storage/ServerArchive.cs +++ b/src/Titano.Server/Storage/ServerArchive.cs @@ -19,6 +19,29 @@ public sealed class ServerArchive(Archive archive) set => _current = value; } + /// + /// Serves unless is no longer , the archive + /// it was read from: an edit an administrator saved meanwhile is never undone. False when it was not replaced. + /// + public bool TryReplace(Archive expected, Archive value) => ReferenceEquals(Interlocked.CompareExchange(ref _current, value, expected), expected); + + /// + /// once it and the index row hold the settings another writer sharing the archive (a desktop) + /// put into its archive.json (): uploads, the polling and the admin + /// panel, whose form saves every field, never work from older ones. Unchanged while the file cannot be read. + /// + public async Task RefreshAsync(ArchiveService archives, TitanoDatabase db, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(archives); + ArgumentNullException.ThrowIfNull(db); + var current = Current; + // Only a change the refresh registered is served, and never over an edit an administrator saved since the read above. + if (await archives.RefreshSettingsAsync(current.Id, cancellationToken).ConfigureAwait(false) is { } settings && !settings.IsRegisteredIn(current) + && await db.Archives.GetAsync(current.Id, cancellationToken).ConfigureAwait(false) is { } row) + TryReplace(current, row); + return Current; + } + public Guid Id => _current.Id; public string Root => _current.RootPath; diff --git a/tests/Titano.App.Tests/Infrastructure/AppServicesTests.cs b/tests/Titano.App.Tests/Infrastructure/AppServicesTests.cs index 187a40b0..f8ade358 100644 --- a/tests/Titano.App.Tests/Infrastructure/AppServicesTests.cs +++ b/tests/Titano.App.Tests/Infrastructure/AppServicesTests.cs @@ -74,17 +74,30 @@ public sealed class AppServicesTests Assert.Equal(TimeSpan.FromMinutes(30), services.PollingPeriod); // The first-run dialog creates an archive: the next rescan is of it. - await services.ArchiveSetup.CreateAsync(Path.Combine(environment.DataDirectory, "first")); - Assert.Equal("first", (await services.RescanAsync())?.Name); + var firstRoot = Path.Combine(environment.DataDirectory, "first"); + await services.ArchiveSetup.CreateAsync(firstRoot); + var first = Assert.NotNull(await services.RescanAsync()); + Assert.Equal("first", first.Name); - // Settings › Archive moves to another archive: the next rescan is of that one, no longer of the first. - var second = Path.Combine(environment.DataDirectory, "second"); - await new ArchiveService(services.Database!).CreateAsync(second, "Second"); - settings.LastArchivePath = second; + // Settings › Archive moves to another archive, which the index lists after the first one: the next rescan + // is of the archive at the folder, no longer of the first one. + var other = Path.Combine(environment.DataDirectory, "zeta"); + await new ArchiveService(services.Database!).CreateAsync(other, "zeta"); + Assert.Equal("first", (await services.Database!.Archives.ListAsync())[0].Name); + async Task ScansOfAsync(Guid archiveId) => + (await services.Database!.Jobs.ListAsync(null, JobKind.Scan)).Items.Count(j => j.TargetIds.Contains(archiveId)); + var scansOfFirst = await ScansOfAsync(first.Id); + settings.LastArchivePath = other; services.SaveSettings(); var rescanned = Assert.NotNull(await services.RescanAsync()); - Assert.Equal("Second", rescanned.Name); - Assert.Contains((await services.Database!.Jobs.ListAsync(null, JobKind.Scan)).Items, j => j.TargetIds.Contains(rescanned.Id)); + Assert.Equal("zeta", rescanned.Name); + Assert.True(await ScansOfAsync(rescanned.Id) > 0, "the archive at the folder is rescanned"); + Assert.Equal(scansOfFirst, await ScansOfAsync(first.Id)); + + // And back: the folder decides, whatever the order of the index. + settings.LastArchivePath = firstRoot; + services.SaveSettings(); + Assert.Equal(first.Id, (await services.RescanAsync())?.Id); } finally { diff --git a/tests/Titano.Data.Tests/Archiving/ArchiveSettingsTests.cs b/tests/Titano.Data.Tests/Archiving/ArchiveSettingsTests.cs index fe5ab3bb..ea057f04 100644 --- a/tests/Titano.Data.Tests/Archiving/ArchiveSettingsTests.cs +++ b/tests/Titano.Data.Tests/Archiving/ArchiveSettingsTests.cs @@ -143,6 +143,15 @@ public sealed class ArchiveSettingsTests(TestContext context) Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct)); Assert.Equal("{year}/{camera}", (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!.FolderTemplate); + // A newer Titano rewrote the file: what this one reads of it is neither shown nor registered. + ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { Name = "Newer", LayoutTemplates = manifest.LayoutTemplates with { Folder = "{camera}" } }); + var newer = JsonNode.Parse(await File.ReadAllTextAsync(fx.Paths.ManifestPath, fx.Ct))!; + newer["schemaVersion"] = ArchiveManifest.CurrentSchemaVersion + 1; + await File.WriteAllTextAsync(fx.Paths.ManifestPath, newer.ToJsonString(), fx.Ct); + Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct)); + var kept = (await fx.Db.Archives.GetAsync(fx.Archive.Id, fx.Ct))!; + Assert.Equal(("Shared", "{year}/{camera}"), (kept.Name, kept.FolderTemplate)); + // The folder now holds another archive: its settings are neither shown nor registered as this one's. ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { Id = Guid.CreateVersion7(), Name = "Other" }); Assert.Null(await fx.Service.RefreshSettingsAsync(fx.Archive.Id, fx.Ct)); diff --git a/tests/Titano.Server.Tests/Storage/ArchiveModuleTests.cs b/tests/Titano.Server.Tests/Storage/ArchiveModuleTests.cs index 68520d42..24c2ae65 100644 --- a/tests/Titano.Server.Tests/Storage/ArchiveModuleTests.cs +++ b/tests/Titano.Server.Tests/Storage/ArchiveModuleTests.cs @@ -103,10 +103,16 @@ public sealed class ArchiveModuleTests(TestContext context) } [Test] - public async Task SettingsADesktopChangedInArchiveJsonReachTheServerAtTheNextPoll() + public async Task SettingsADesktopChangedInArchiveJsonReachUploadsAndTheAdminPanel() { await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); var archive = server.Services.GetRequiredService(); + var polling = server.Services.GetRequiredService(); + // Nothing changed: the served archive (the one an administrator's edit put there) is not replaced by a row read again. + var served = archive.Current; + await polling.PollAsync(context.Cancellation); + Assert.True(ReferenceEquals(served, archive.Current), "a poll that registered nothing keeps the served archive"); + // A desktop sharing the archive changed its folder template and proxy size in archive.json. var path = new ArchivePaths(archive.Root).ManifestPath; var manifest = ManifestStore.ReadArchive(path); @@ -116,12 +122,25 @@ public sealed class ArchiveModuleTests(TestContext context) ProxyPolicy = manifest.ProxyPolicy with { LongEdge = 2560 }, }); - await server.Services.GetRequiredService().PollAsync(context.Cancellation); + await polling.PollAsync(context.Cancellation); // Uploads (the index row) and the admin panel (the served archive) follow it. Assert.Equal(("{camera}/{date}", 2560), (archive.Current.FolderTemplate, archive.Current.ProxyLongEdge)); var row = Assert.NotNull(await server.Database.Archives.GetAsync(archive.Id, context.Cancellation)); Assert.Equal(("{camera}/{date}", 2560), (row.FolderTemplate, row.ProxyLongEdge)); + + // The admin panel opens before the next poll: its form, which saves every field, starts from the desktop's change. + ManifestStore.WriteArchive(path, ManifestStore.ReadArchive(path) with { LayoutTemplates = manifest.LayoutTemplates with { Folder = "{year}/{camera}" } }); + var admin = await server.AdminAsync(); + using (var read = await admin.GetAsync("/api/v1/archive/settings")) + Assert.Equal("{year}/{camera}", (await read.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto)).FolderTemplate); + Assert.Equal("{year}/{camera}", archive.Current.FolderTemplate); + + // A refresh that read the archive before an administrator saved an edit never puts it back over that edit. + var before = archive.Current; + archive.Current = before with { Name = "Edited" }; + Assert.False(archive.TryReplace(before, before with { Name = "Stale" }), "the edit saved meanwhile stays"); + Assert.Equal("Edited", archive.Current.Name); } private static async Task UntilIndexedAsync(TestServer server, Guid archiveId, string folder)