using System.IO; using Titano.Core.Diagnostics; using Titano.Data.Archiving; namespace Titano.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 Titano 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. /// 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}"); } } /// Runs an archive action from the UI; a failure becomes a toast instead of an unhandled exception. 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 Data.DatabaseException) { Log.Error("archive", "Archive action failed", ex); shell.Notify(Core.Localization.Loc.L("Errors.Archive.Open"), ex.Message, isError: true); return false; } } } /// Archive roots offered by default and their free space. public static class ArchiveLocations { public const string DefaultFolderName = "TitanoArchive"; /// "<fixed drive with the most free space>\TitanoArchive". 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"); } } }