Files
Chronos/src/Chronos.App/About/UpdateFlow.cs
T
Alby96andClaude Opus 5.5 1f089888e8
CI / Build and test (push) Successful in 5m11s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 8m57s
CI / PostgreSQL 16 integration (push) Successful in 3m10s
CI / PostgreSQL 17 integration (push) Successful in 3m30s
CI / Docker image (push) Successful in 49s
Release / Server image (push) Successful in 1m4s
feat: owner review 2 phase 1: :latest, Unraid array space, Settings with About, whole-folder import (#9)
- :latest follows the highest stable release, or the highest pre-release while none is stable (Chronos.Tools release-channels); bad tags are refused before anything is built.
- Unraid: CHRONOS_SPACE_PATH and the template's Array view show the array's free space; the write volume (cache) is shown apart; completions check the archive volume (507).
- Settings is a gear, docked at the foot of the admin sidebar; About is the last part of Settings on the desktop and the panel.
- Import > Folder > Whole folder: one sequence per format (desktop only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-27 01:09:59 +01:00

182 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.IO;
using System.Net.Http;
using Chronos.Core.Diagnostics;
using Chronos.Ui.Mvvm;
namespace Chronos.App.About;
/// <summary>Where the update notice stands.</summary>
public enum UpdateState
{
Unknown,
Checking,
UpToDate,
Available,
Failed,
Downloading,
Downloaded,
DownloadFailed,
NotVerified,
}
/// <summary>What <see cref="UpdateFlow"/> needs from the application: the running version, the mode, the network and the shell.</summary>
public sealed record UpdateHost(
Version Running,
bool IsCapture,
bool IsPortable,
Func<CancellationToken, Task<ReleaseInfo?>> CheckAsync,
Func<ReleaseInfo, IProgress<double>?, CancellationToken, Task<string>> DownloadAsync,
Action<string> OpenUrl,
Action<string> Reveal)
{
/// <summary>The running build is a pre-release ("0.1.0-rc.1"): the final release of the same version is newer.</summary>
public bool RunningIsPrerelease { get; init; }
}
/// <summary>
/// The update notice of Settings › Updates (BRIEF §18): checks the latest release, downloads and verifies the
/// package that upgrades this copy (<see cref="UpdateDownload"/>: the setup, or the portable zip for a portable copy)
/// and shows it in Explorer. Running it is left to the user because BRIEF §8.6 lets Chronos only open links and show
/// files. A release without that package or without <c>SHA256SUMS</c> only opens its page; a capture run never touches
/// the network. The flow owns its two commands so that their can-execute state follows every change.
/// </summary>
public sealed class UpdateFlow
{
private static readonly TimeSpan CheckTimeout = TimeSpan.FromSeconds(30);
private readonly UpdateHost _host;
public UpdateFlow(UpdateHost host)
{
_host = host;
Check = new AsyncRelayCommand((_, ct) => CheckAsync(ct), _ => State is not (UpdateState.Checking or UpdateState.Downloading));
Download = new AsyncRelayCommand((_, ct) => DownloadAsync(ct), _ => IsDownloadOffered);
}
/// <summary>Raised after every change of state, release, progress or downloaded file.</summary>
public event Action? Changed;
public UpdateState State { get; private set; } = UpdateState.Unknown;
/// <summary>The newer release, or null when there is none (or no check has succeeded yet).</summary>
public ReleaseInfo? Release { get; private set; }
/// <summary>Fraction of the package downloaded so far.</summary>
public double Downloaded { get; private set; }
/// <summary>The verified package; null until a download succeeded.</summary>
public string? PackagePath { get; private set; }
public bool IsPortable => _host.IsPortable;
public AsyncRelayCommand Check { get; }
public AsyncRelayCommand Download { get; }
/// <summary>Download is offered: a newer release exists and no verified package is waiting (a failed download can be retried).</summary>
public bool IsDownloadOffered =>
Release is not null && State is UpdateState.Available or UpdateState.DownloadFailed or UpdateState.NotVerified;
/// <summary>The package Download fetches; null when Download opens the release page instead.</summary>
public ReleaseAsset? Package =>
Release is { } release && UpdateDownload.CanDownload(release, _host.IsPortable) ? UpdateDownload.Package(release, _host.IsPortable) : null;
/// <summary>Stops a download in progress; its partial file is kept and the next Download resumes it.</summary>
public void CancelDownload() => Download.Cancel();
/// <summary>The start-up check (BRIEF §18): only when the user left it on and never in a capture run.</summary>
public Task<ReleaseInfo?> CheckOnStartAsync(bool enabled) => enabled ? CheckAsync(CancellationToken.None) : Task.FromResult<ReleaseInfo?>(null);
/// <summary>Asks for the latest release and returns it when it is newer than the running version.</summary>
public async Task<ReleaseInfo?> CheckAsync(CancellationToken ct)
{
if (_host.IsCapture || State is UpdateState.Checking or UpdateState.Downloading) return null;
SetState(UpdateState.Checking);
ReleaseInfo? latest = null;
var failed = true;
try
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeout.CancelAfter(CheckTimeout);
latest = await _host.CheckAsync(timeout.Token);
failed = false;
}
catch (Exception ex) when (ex is HttpRequestException or OperationCanceledException or IOException)
{
Log.Info("update", "Update check failed: " + ex.Message);
}
finally
{
SetResult(latest, failed);
}
return Release;
}
/// <summary>Sets a check result without the network (capture, tests). A package already verified for the same release stays offered.</summary>
public void SetResult(ReleaseInfo? release, bool failed = false)
{
var newer = !failed && release is not null && UpdateCheck.IsNewer(release, _host.Running, _host.RunningIsPrerelease) ? release : null;
var keepPackage = newer is not null && PackagePath is not null && newer.Tag == Release?.Tag && File.Exists(PackagePath);
Release = newer;
if (!keepPackage) PackagePath = null;
SetState(failed ? UpdateState.Failed
: newer is null ? UpdateState.UpToDate
: keepPackage ? UpdateState.Downloaded
: UpdateState.Available);
}
private async Task DownloadAsync(CancellationToken ct)
{
if (Release is not { } release || _host.IsCapture) return;
if (Package is null)
{
_host.OpenUrl(release.PageUrl);
return;
}
Downloaded = 0;
SetState(UpdateState.Downloading);
var progress = new Progress<double>(fraction =>
{
if (State != UpdateState.Downloading || fraction - Downloaded < 0.01) return;
Downloaded = fraction;
Changed?.Invoke();
});
try
{
var path = await _host.DownloadAsync(release, progress, ct);
PackagePath = path;
Log.Info("update", $"{Path.GetFileName(path)} of {release.Tag} downloaded and verified: {path}");
SetState(UpdateState.Downloaded);
_host.Reveal(path);
}
catch (UpdateVerificationException ex)
{
Log.Warning("update", "Update download refused: " + ex.Message);
SetState(UpdateState.NotVerified);
}
catch (OperationCanceledException) when (ct.IsCancellationRequested)
{
Log.Info("update", $"Download of {release.Tag} cancelled; the next one resumes it.");
SetState(UpdateState.Available);
}
catch (Exception ex) when (ex is HttpRequestException or TimeoutException or OperationCanceledException or IOException or UnauthorizedAccessException)
{
Log.Warning("update", "Update download failed: " + ex.Message);
SetState(UpdateState.DownloadFailed);
}
finally
{
// Anything unexpected still reaches the command's error report; the notice must not stay "Downloading".
if (State == UpdateState.Downloading) SetState(UpdateState.DownloadFailed);
}
}
private void SetState(UpdateState state)
{
State = state;
Changed?.Invoke();
Check.RaiseCanExecuteChanged();
Download.RaiseCanExecuteChanged();
}
}