Files
Chronos/src/Chronos.App/Infrastructure/ArchiveSetup.cs
T
Alby96andClaude Opus 5.5 0075065884
CI / Build and test (push) Successful in 10m37s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 9s
Release / Build, test and publish (push) Successful in 16m19s
CI / PostgreSQL 16 integration (push) Successful in 10m19s
CI / PostgreSQL 17 integration (push) Successful in 11m27s
CI / Docker image (push) Successful in 57s
Release / Server image (push) Successful in 1m34s
feat: work board, no usage data, archive folder tree, deploy guide (owner review 4 + owner review 1 phase 6) (#62)
Owner review 4 and owner review 1 phase 6.

- Work board in Videos (desktop and admin panel): Published and Workspace lanes (#29).
- Anonymous usage counts removed: Chronos sends nothing about its use (#30).
- The archive's whole folder tree is created inside any archive root, on the server and the desktop (#31).
- Video cards start with the preset; sizes keep number and unit together (#28).
- Deploy guide docs/deploy (Docker, Unraid, desktop), BRIEF §24, original documentation in docs/reference/original (#22).

Closes #22 #28 #29 #30 #31

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-06 18:46:22 +01:00

147 lines
6.7 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 Chronos.App.Archiving;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Data.Archiving;
namespace Chronos.App.Infrastructure;
/// <summary>
/// First-run and Library archive choices (BRIEF §12.1): create a local archive or adopt an existing folder tree.
/// </summary>
public interface IArchiveSetup
{
/// <summary>Creates (or opens) the local archive rooted at <paramref name="root"/>.</summary>
Task CreateAsync(string root, CancellationToken cancellationToken = default);
/// <summary>Indexes the photos under <paramref name="folder"/> in place, or opens a previous Chronos archive there.</summary>
Task AdoptAsync(string folder, CancellationToken cancellationToken = default);
}
/// <summary>
/// Creates or adopts the archive through the archive engine (originals stay where they are), remembers it as the
/// archive to open at start and queues a full scan that indexes its sequences. A folder that becomes a new archive gets
/// the naming templates, proxy size and thumbnail size of Settings › Archive; an existing archive keeps its own.
/// </summary>
public sealed class EngineArchiveSetup(AppServices services) : IArchiveSetup
{
public Task CreateAsync(string root, CancellationToken cancellationToken = default) => OpenAsync(root, cancellationToken);
public Task AdoptAsync(string folder, CancellationToken cancellationToken = default) => OpenAsync(folder, cancellationToken);
private async Task OpenAsync(string path, CancellationToken cancellationToken)
{
var archives = services.Archives ?? throw new InvalidOperationException("The index is not open.", services.OpenError);
var root = Path.GetFullPath(path);
var defaults = services.Settings.ArchiveDefaults();
var archive = await Task.Run(async () =>
{
// A new archive is made with its whole layout (the folder itself included), or not at all; an existing one gets
// the folders it lacks. One that can be written but not completed is refused with the reason, before it becomes
// the archive of this PC; a read-only one opens as before, to be browsed.
var opened = await archives.AdoptAsync(root, defaults, cancellationToken).ConfigureAwait(false);
var paths = new ArchivePaths(root);
if (ArchiveStructure.Ensure(paths) is { IsComplete: false } structure && ArchiveVolume.Probe(paths) == ArchiveAvailability.Online)
throw new ArchiveStructureException(structure);
return opened;
}, cancellationToken);
services.Settings.LastArchivePath = root;
services.SaveSettings();
await services.QueueScanAsync(archive, full: true, cancellationToken);
Log.Info("archive", $"Archive {archive.Name} ready at {root}");
}
}
/// <summary>
/// Runs an archive action from the UI; a failure becomes a toast instead of an unhandled exception. The toast says why
/// in one localized sentence (<see cref="Cause"/>); the exception's own text stays in the log.
/// </summary>
public static class ArchiveActions
{
public static async Task<bool> TryAsync(Shell.ShellViewModel shell, Func<IArchiveSetup, Task> action)
{
try
{
await action(shell.Services.ArchiveSetup);
return true;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidOperationException or InvalidDataException
or ArgumentException or Data.DatabaseException)
{
Log.Error("archive", "Archive action failed", ex);
shell.Notify(Core.Localization.Loc.L("Errors.Archive.Open"), Cause(ex), isError: true);
return false;
}
}
/// <summary>
/// Why creating, opening or changing an archive failed: the archive's own sentences for files (an unreadable
/// archive.json included), access and the index (<see cref="ArchiveFailures"/>), otherwise <see cref="Shell.ErrorText"/>
/// (a catalog sentence, or "Something went wrong").
/// </summary>
public static string Cause(Exception ex) =>
ex is IOException or InvalidDataException or UnauthorizedAccessException or Data.DatabaseException ? ArchiveFailures.Cause(ex) : Shell.ErrorText.For(ex);
}
/// <summary>Archive roots offered by default and their free space.</summary>
public static class ArchiveLocations
{
public const string DefaultFolderName = "ChronosArchive";
/// <summary>"&lt;fixed drive with the most free space&gt;\ChronosArchive".</summary>
public static string SuggestRoot()
{
try
{
var best = DriveInfo.GetDrives()
.Where(d => d.DriveType == DriveType.Fixed && d.IsReady)
.OrderByDescending(d => d.AvailableFreeSpace)
.FirstOrDefault();
if (best is not null) return Path.Combine(best.RootDirectory.FullName, DefaultFolderName);
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), DefaultFolderName);
}
/// <summary>Free bytes on the volume of <paramref name="path"/>; null when unknown.</summary>
public static long? FreeSpace(string? path)
{
if (string.IsNullOrWhiteSpace(path)) return null;
try
{
var root = Path.GetPathRoot(Path.GetFullPath(path));
if (string.IsNullOrEmpty(root)) return null;
var drive = new DriveInfo(root);
return AppClock.ShownFree(drive.IsReady ? drive.AvailableFreeSpace : null);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
{
return null;
}
}
/// <summary>The problem of an archive root as a localized sentence; null when it can be used.</summary>
public static string? Validate(string? path)
{
if (string.IsNullOrWhiteSpace(path)) return Core.Localization.Loc.L("Errors.Path.Empty");
try
{
if (!Path.IsPathFullyQualified(path)) return Core.Localization.Loc.L("Errors.Path.NotAbsolute");
var full = Path.GetFullPath(path);
if (File.Exists(full)) return Core.Localization.Loc.L("Errors.Path.IsFile");
var root = Path.GetPathRoot(full);
if (string.IsNullOrEmpty(root) || !Directory.Exists(root)) return Core.Localization.Loc.L("Errors.Path.NoDrive");
return null;
}
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
{
return Core.Localization.Loc.L("Errors.Path.Invalid");
}
}
}