Shell.Notify takes isError; every toast that reports a failure or a refusal sets it (archive actions and fixes, sync, settings import and cache, proxy fetch, welcome folder check, card check, erase, format and eject). The Uploader marks its unexpected-error, sign-out and choose-a-sequence toasts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
126 lines
5.1 KiB
C#
126 lines
5.1 KiB
C#
using System.IO;
|
|
using Titano.Core.Diagnostics;
|
|
using Titano.Data.Archiving;
|
|
|
|
namespace Titano.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 Titano 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.
|
|
/// </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 archive = await Task.Run(async () =>
|
|
{
|
|
Directory.CreateDirectory(root);
|
|
return await archives.AdoptAsync(root, null, cancellationToken).ConfigureAwait(false);
|
|
}, 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.</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 Data.DatabaseException)
|
|
{
|
|
Log.Error("archive", "Archive action failed", ex);
|
|
shell.Notify(Core.Localization.Loc.L("Errors.Archive.Open"), ex.Message, isError: true);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Archive roots offered by default and their free space.</summary>
|
|
public static class ArchiveLocations
|
|
{
|
|
public const string DefaultFolderName = "TitanoArchive";
|
|
|
|
/// <summary>"<fixed drive with the most free space>\TitanoArchive".</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 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");
|
|
}
|
|
}
|
|
}
|