using System.IO; using Chronos.App.Archiving; using Chronos.Core.Diagnostics; using Chronos.Data.Archiving; namespace Chronos.App.Infrastructure; /// /// First-run and Library archive choices (BRIEF §12.1): create a local archive or adopt an existing folder tree. /// public interface IArchiveSetup { /// Creates (or opens) the local archive rooted at . Task CreateAsync(string root, CancellationToken cancellationToken = default); /// Indexes the photos under in place, or opens a previous Chronos archive there. Task AdoptAsync(string folder, CancellationToken cancellationToken = default); } /// /// 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. /// 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 () => { Directory.CreateDirectory(root); return await archives.AdoptAsync(root, defaults, 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}"); } } /// /// 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 (); the exception's own text stays in the log. /// public static class ArchiveActions { public static async Task TryAsync(Shell.ShellViewModel shell, Func 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; } } /// /// Why creating, opening or changing an archive failed: the archive's own sentences for files (an unreadable /// archive.json included), access and the index (), otherwise /// (a catalog sentence, or "Something went wrong"). /// public static string Cause(Exception ex) => ex is IOException or InvalidDataException or UnauthorizedAccessException or Data.DatabaseException ? ArchiveFailures.Cause(ex) : Shell.ErrorText.For(ex); } /// Archive roots offered by default and their free space. public static class ArchiveLocations { public const string DefaultFolderName = "ChronosArchive"; /// "<fixed drive with the most free space>\ChronosArchive". 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); } /// Free bytes on the volume of ; null when unknown. 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; } } /// The problem of an archive root as a localized sentence; null when it can be used. 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"); } } }