using Chronos.Core.Archiving;
using Chronos.Core.Model;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Server.Configuration;
namespace Chronos.Server.Storage;
///
/// The archive this server serves () and its database row, created at first
/// start. The admin panel can change its name, templates and proxy policy at runtime.
///
public sealed class ServerArchive(Archive archive)
{
private volatile Archive _current = archive;
public Archive Current
{
get => _current;
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 schedules 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, 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;
///
/// What the last found of the archive's folders; null before the first. The dashboard and
/// the health check show an incomplete layout.
///
public ArchiveStructureReport? Structure => _structure;
private volatile ArchiveStructureReport? _structure;
///
/// Creates the folders of the archive's layout that are missing (BRIEF ยง7) and remembers what it found
/// (): at start, and each time the dashboard is read, so a share that comes back heals.
///
public ArchiveStructureReport CheckStructure()
{
var report = ArchiveStructure.Ensure(new ArchivePaths(Root));
_structure = report;
return report;
}
///
/// 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 ().
///
public static async Task 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;
}
}