The owner asked (2026-11-06) that whatever path is given as the archive root, on the server in Docker or on Windows, Chronos creates inside it every folder the archive keeps. Until now only .chronos was made up front; locks, journal, staging, trash and its photos and videos areas appeared on first use, and an archive copied or made by an older build could lack them. ArchiveStructure.Ensure (Core) creates the missing folders of BRIEF §7 idempotently, never touching existing folders or files, and reports why it could not (drive or share unavailable, permission denied, read-only, a file in the way) as one localized sentence. ArchiveService runs it when it creates an archive (refused with the reason when the tree cannot be made) and at every open. The server checks it at start-up and on each dashboard read: it logs a warning, the dashboard shows an "Archive incomplete" chip with the reason, and /health reports archive "incomplete". The desktop runs it when an archive is created, adopted or opened at start, and a folder typed in Settings that is not an archive yet becomes one in place, as Welcome's Create does; a writable archive whose tree cannot be completed is refused with the reason. Refs #31 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
3.8 KiB
C#
83 lines
3.8 KiB
C#
using Chronos.Core.Archiving;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Server.Configuration;
|
|
|
|
namespace Chronos.Server.Storage;
|
|
|
|
/// <summary>
|
|
/// The archive this server serves (<see cref="ServerOptions.ArchivePath"/>) and its database row, created at first
|
|
/// start. The admin panel can change its name, templates and proxy policy at runtime.
|
|
/// </summary>
|
|
public sealed class ServerArchive(Archive archive)
|
|
{
|
|
private volatile Archive _current = archive;
|
|
|
|
public Archive Current
|
|
{
|
|
get => _current;
|
|
set => _current = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serves <paramref name="value"/> unless <see cref="Current"/> is no longer <paramref name="expected"/>, the archive
|
|
/// it was read from: an edit an administrator saved meanwhile is never undone. False when it was not replaced.
|
|
/// </summary>
|
|
public bool TryReplace(Archive expected, Archive value) => ReferenceEquals(Interlocked.CompareExchange(ref _current, value, expected), expected);
|
|
|
|
/// <summary>
|
|
/// <see cref="Current"/> once it and the index row hold the settings another writer sharing the archive (a desktop)
|
|
/// put into its archive.json (<see cref="ArchiveService.RefreshSettingsAsync"/>): uploads, the schedules and the admin
|
|
/// panel, whose form saves every field, never work from older ones. Unchanged while the file cannot be read.
|
|
/// </summary>
|
|
public async Task<Archive> RefreshAsync(ArchiveService archives, ChronosDatabase 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;
|
|
|
|
/// <summary>
|
|
/// What the last <see cref="CheckStructure"/> found of the archive's folders; null before the first. The dashboard and
|
|
/// the health check show an incomplete layout.
|
|
/// </summary>
|
|
public ArchiveStructureReport? Structure => _structure;
|
|
|
|
private volatile ArchiveStructureReport? _structure;
|
|
|
|
/// <summary>
|
|
/// Creates the folders of the archive's layout that are missing (BRIEF §7) and remembers what it found
|
|
/// (<see cref="Structure"/>): at start, and each time the dashboard is read, so a share that comes back heals.
|
|
/// </summary>
|
|
public ArchiveStructureReport CheckStructure()
|
|
{
|
|
var report = ArchiveStructure.Ensure(new ArchivePaths(Root));
|
|
_structure = report;
|
|
return report;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Opens the archive at the configured root through the archive engine: an existing Chronos archive is registered
|
|
/// (and relinked if it moved), any other folder becomes a new archive with the default templates. Either way the
|
|
/// folders of the archive's layout are made (<see cref="CheckStructure"/>).
|
|
/// </summary>
|
|
public static async Task<ServerArchive> OpenAsync(ChronosDatabase db, ServerOptions options, CancellationToken cancellationToken)
|
|
{
|
|
var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(options.ArchivePath));
|
|
var archive = await new ArchiveService(db).AdoptAsync(root, options.ServerName, cancellationToken).ConfigureAwait(false);
|
|
var served = new ServerArchive(archive);
|
|
served.CheckStructure();
|
|
return served;
|
|
}
|
|
}
|