diff --git a/i18n/en/Import.resx b/i18n/en/Import.resx index 4ded1106..950a7ddf 100644 --- a/i18n/en/Import.resx +++ b/i18n/en/Import.resx @@ -105,6 +105,8 @@ {0:N0} verified on the card {0:N0} verified on the card Titano may not write to the destination.long + Titano may not write to this PC's cache folder.long + The drive of this PC's cache folder is full.long {0} is write-protected or holds protected photos.long; {0} card name A photo or its archive copy could not be read.long The destination is full.long diff --git a/i18n/it/Import.it.resx b/i18n/it/Import.it.resx index aa9a3724..5770f72f 100644 --- a/i18n/it/Import.it.resx +++ b/i18n/it/Import.it.resx @@ -103,6 +103,8 @@ {0:N0} verificata sulla scheda {0:N0} verificate sulla scheda Titano non può scrivere nella destinazione. + Titano non può scrivere nella cartella cache di questo PC. + Il disco della cartella cache di questo PC è pieno. {0}: la scheda è protetta da scrittura o contiene foto protette. Impossibile leggere una foto o la sua copia nell'archivio. La destinazione è piena. diff --git a/src/Titano.App/Import/ImportDestinations.cs b/src/Titano.App/Import/ImportDestinations.cs index 89136bd1..0070f684 100644 --- a/src/Titano.App/Import/ImportDestinations.cs +++ b/src/Titano.App/Import/ImportDestinations.cs @@ -233,7 +233,7 @@ public static class LocalImport catch (Exception ex) when (!cancellationToken.IsCancellationRequested && Stops(ex)) { Log.Warning("import", $"Import into {plan.DestinationRoot} stopped", ex); - stopped = ImportFailures.Stop(ex, source); + stopped = ImportFailures.Stop(ex, source, sink.StagesOnThisPc); } return ImportOutcome.From(plan, results, verify, report, stopped); } diff --git a/src/Titano.App/Import/ImportFailures.cs b/src/Titano.App/Import/ImportFailures.cs index b1308733..50f4120c 100644 --- a/src/Titano.App/Import/ImportFailures.cs +++ b/src/Titano.App/Import/ImportFailures.cs @@ -1,5 +1,6 @@ using System.IO; using System.Net.Http; +using System.Text.RegularExpressions; using Titano.App.Shell; using Titano.Core.Archiving; using Titano.Core.Import; @@ -20,6 +21,13 @@ public static class ImportFailures private const int HandleDiskFull = unchecked((int)0x80070027); private const int WriteProtect = unchecked((int)0x80070013); + /// The sentences an archive import job fails with when the archive's engine stops it itself. + private static readonly string[] EngineSentences = + [ + "Archive.Error.Offline", "Archive.Error.ReadOnly", "Archive.Error.Busy", "Archive.Error.NewerFormat", "Archive.Error.SourceGone", + "Archive.Error.DuplicateLost", + ]; + /// The card or folder as the Devices list names it. public static string Import(Exception ex, string source) => ex switch { @@ -33,14 +41,26 @@ public static class ImportFailures /// /// Why the photos an import into a folder or the server never reached were not copied (): /// the card left, the destination is full or refuses writing, or the server answered with its own sentence, does - /// not answer (a timeout too) or is no longer trusted. + /// not answer (a timeout too) or is no longer trusted. When the sink stages on this PC (, + /// the server's), a full disk or a refused write is this PC's cache folder's: the server answers for its own space. /// - public static string Stop(Exception ex, string source) => ex switch + public static string Stop(Exception ex, string source, bool stagesOnThisPc = false) => ex switch { TitanoApiException or HttpRequestException or UntrustedCertificateException or OperationCanceledException => ErrorText.ForServer(ex), + IOException { HResult: DiskFull or HandleDiskFull } when stagesOnThisPc => Loc.L("Import.Failed.CacheFull"), + UnauthorizedAccessException when stagesOnThisPc => Loc.L("Import.Failed.CacheAccess"), _ => Import(ex, source), }; + /// + /// What a failed archive import job says, from the text the job engine kept of what stopped it: the engine's own + /// sentence (archive offline, read-only or busy, a newer format, the card gone, a photo it could not add) in the + /// language shown now, else 'The import stopped before the end.' Framework, database and system text goes to the + /// tooltip and the logs only. + /// + public static string Job(string? error) => + error is { Length: > 0 } && EngineSentences.Any(key => IsSentence(key, error)) ? error : Loc.L("Import.Failed.Other"); + /// Why the card or folder could not be planned. public static string Plan(Exception ex, string source) => ex switch { @@ -70,6 +90,14 @@ public static class ImportFailures _ => Loc.L("Errors.Unexpected"), }; + /// Whether is the sentence of , whatever its arguments. + private static bool IsSentence(string key, string text) + { + const string Argument = "\u0001"; + var pattern = Regex.Escape(Loc.L(key, Argument, Argument)).Replace(Argument, ".+", StringComparison.Ordinal); + return Regex.IsMatch(text, "^" + pattern + "$", RegexOptions.CultureInvariant | RegexOptions.Singleline); + } + private static bool CardGone(Exception ex, string root) => ex is DirectoryNotFoundException or DriveNotFoundException or ImportSourceUnavailableException || !Directory.Exists(root); } diff --git a/src/Titano.App/Import/ServerImportSink.cs b/src/Titano.App/Import/ServerImportSink.cs index 311c34fe..7df1138d 100644 --- a/src/Titano.App/Import/ServerImportSink.cs +++ b/src/Titano.App/Import/ServerImportSink.cs @@ -24,6 +24,9 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector public string StagingDirectory { get; } = stagingDirectory; + /// The copies wait in this PC's cache folder until they are uploaded: the server's space is its quota. + public bool StagesOnThisPc => true; + /// Every file committed or refused so far, by card path; pass it to . public ImportSinkReport Report { get; } = new(); diff --git a/src/Titano.App/Sections/ImportViewModel.cs b/src/Titano.App/Sections/ImportViewModel.cs index fcc45e58..effed0ef 100644 --- a/src/Titano.App/Sections/ImportViewModel.cs +++ b/src/Titano.App/Sections/ImportViewModel.cs @@ -1279,8 +1279,9 @@ public sealed class ImportViewModel : SectionViewModel await QueueDeepAnalysisAsync(plan); break; case JobState.Failed: - // The engine wrote the job's error (a localized sentence for the archive's own failures); the full text stays in the tooltip. - Fail(job.Error ?? Loc.L("Import.Failed.Other"), job.Error); + // The engine kept the text of what stopped the job: the archive's own failures are localized sentences, anything + // else reads as the catalog's and its text stays in the tooltip. + Fail(ImportFailures.Job(job.Error), job.Error); break; case JobState.Cancelled: _jobId = null; diff --git a/src/Titano.Core/Import/ImportExecutor.cs b/src/Titano.Core/Import/ImportExecutor.cs index 5a7f166e..5833a75d 100644 --- a/src/Titano.Core/Import/ImportExecutor.cs +++ b/src/Titano.Core/Import/ImportExecutor.cs @@ -27,6 +27,12 @@ public interface IImportSink /// Folder for verified copies, on the destination volume so commits are renames. string StagingDirectory { get; } + /// + /// True when lies on this PC rather than at the destination (a server import stages its + /// copies in this PC's cache folder): a full disk or a refused write while staging then concerns this PC. + /// + bool StagesOnThisPc => false; + /// Moves staged files of one session into place; returns one result per file, in order. Files it does /// not move stay where they are and the executor deletes them. Task> CommitAsync(PlannedSession session, IReadOnlyList files, CancellationToken cancellationToken); @@ -138,7 +144,7 @@ public sealed class ImportExecutor(ImportPlan plan, IImportSink sink) } Log.Warning("import", $"Copy of {item.SourcePath} failed", ex); failed++; - results.Add(new ImportItemResult(session, item, ImportItemStatus.Failed, null, null, FailureText(ex))); + results.Add(new ImportItemResult(session, item, ImportItemStatus.Failed, null, null, FailureText(ex, sink.StagesOnThisPc))); } } await FlushAsync().ConfigureAwait(false); @@ -148,13 +154,14 @@ public sealed class ImportExecutor(ImportPlan plan, IImportSink sink) /// /// Why one photo was not copied, as one localized sentence (BRIEF §0.2 rule 6): the Import screen lists it beside the /// photo and the import job writes it to its log. The executor's own checks say what they found; the operating - /// system's text, technical English, goes to the application log only. + /// system's text, technical English, goes to the application log only. A full disk is the staging folder's: the + /// destination's, or this PC's when the sink stages there (). /// - internal static string FailureText(Exception ex) => ex switch + internal static string FailureText(Exception ex, bool stagesOnThisPc = false) => ex switch { // Already one localized sentence each. ImportSourceChangedException or CopyVerificationException => ex.Message, - IOException { HResult: DiskFull or HandleDiskFull } => Loc.L("Import.Failed.Full"), + IOException { HResult: DiskFull or HandleDiskFull } => Loc.L(stagesOnThisPc ? "Import.Failed.CacheFull" : "Import.Failed.Full"), UnauthorizedAccessException => Loc.L("Import.Failed.PhotoAccess"), _ => Loc.L("Import.Failed.Photo"), }; diff --git a/tests/Titano.App.Tests/Import/ImportFailuresTests.cs b/tests/Titano.App.Tests/Import/ImportFailuresTests.cs index 8c1d5094..f22761a4 100644 --- a/tests/Titano.App.Tests/Import/ImportFailuresTests.cs +++ b/tests/Titano.App.Tests/Import/ImportFailuresTests.cs @@ -49,6 +49,47 @@ public sealed class ImportFailuresTests Assert.Equal(Loc.L("Server.Error.Untrusted"), ImportFailures.Stop(new UntrustedCertificateException("AB:CD"), Card)); } + [Test] + public void AServerImportStoppedOnThisPcNamesItsCacheFolderNotTheServer() + { + // The server import stages its copies in this PC's cache folder; the server answers for its own space and rights. + const string Card = "EOS R5 · card A"; + using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests"); + var staging = Path.Combine(Path.GetTempPath(), "titano-server-import"); + Assert.True(((IImportSink)new ServerImportSink(client, staging)).StagesOnThisPc); + Assert.Equal("The drive of this PC's cache folder is full.", + ImportFailures.Stop(new IOException("There is not enough space on the disk.", unchecked((int)0x80070070)), Card, stagesOnThisPc: true)); + Assert.Equal("Titano may not write to this PC's cache folder.", + ImportFailures.Stop(new UnauthorizedAccessException(@"Access to the path 'C:\Users\me\AppData\Local\Titano\cache\server-import' is denied."), Card, + stagesOnThisPc: true)); + Assert.Equal("EOS R5 · card A is no longer available.", ImportFailures.Stop(new ImportSourceUnavailableException(@"E:\"), Card, stagesOnThisPc: true)); + Assert.Equal("The import stopped before the end.", ImportFailures.Stop(new IOException("The specified network name is no longer available."), Card, stagesOnThisPc: true)); + Assert.Equal(Loc.L("Server.Error.Unreachable"), ImportFailures.Stop(new HttpRequestException("No such host is known."), Card, stagesOnThisPc: true)); + } + + [Test] + public void AFailedImportJobShowsOnlyTheArchivesOwnSentences() + { + // The job engine keeps the text of what stopped the job: the archive's own failures are already catalog sentences. + Assert.Equal("The archive D:\\Shoots is offline.", ImportFailures.Job(new ArchiveOfflineException(@"D:\Shoots", ArchiveAvailability.Offline).Message)); + Assert.Equal("The archive D:\\Shoots is read-only.", ImportFailures.Job(new ArchiveOfflineException(@"D:\Shoots", ArchiveAvailability.ReadOnly).Message)); + Assert.Equal("The card or folder E:\\ is no longer available.", ImportFailures.Job(new ImportSourceUnavailableException(@"E:\").Message)); + var busy = new LeaseUnavailableException("settings", new LeaseRecord("t", "Titano Server (nas)", "nas", 42, DateTimeOffset.UnixEpoch, DateTimeOffset.UnixEpoch)); + Assert.Equal(busy.Message, ImportFailures.Job(busy.Message)); + var newer = new NewerFormatException(Path.Combine(Path.GetTempPath(), "archive.json"), 3); + Assert.Equal("archive.json was written by a newer Titano (format 3).", ImportFailures.Job(newer.Message)); + Assert.Equal("IMG_0001.CR3 could not be added to the sequence.", ImportFailures.Job(Loc.L("Archive.Error.DuplicateLost", "IMG_0001.CR3"))); + + // A database, framework or system text reads as the catalog's sentence; the tooltip keeps it. + Assert.Equal("The import stopped before the end.", ImportFailures.Job("database is locked: no write lock within 30 s")); + Assert.Equal("The import stopped before the end.", + ImportFailures.Job(@"The process cannot access the file 'D:\Shoots\2026\IMG_0001.CR3' because it is being used by another process.")); + Assert.Equal("The import stopped before the end.", ImportFailures.Job("There is not enough space on the disk. : 'D:\\Shoots\\.titano\\journal'")); + Assert.Equal("The import stopped before the end.", ImportFailures.Job($"Sequence {Guid.Empty} is not indexed.")); + Assert.Equal("The import stopped before the end.", ImportFailures.Job(null)); + Assert.Equal("The import stopped before the end.", ImportFailures.Job(string.Empty)); + } + [Test] public void APlanningFailureNamesTheSource() { diff --git a/tests/Titano.Core.Tests/Import/ImportExecutorTests.cs b/tests/Titano.Core.Tests/Import/ImportExecutorTests.cs index 1d1c2d39..7dbbf13b 100644 --- a/tests/Titano.Core.Tests/Import/ImportExecutorTests.cs +++ b/tests/Titano.Core.Tests/Import/ImportExecutorTests.cs @@ -174,6 +174,13 @@ public sealed class ImportExecutorTests(TestContext context) Assert.Equal(Loc.L("Import.Failed.PhotoAccess"), ImportExecutor.FailureText(new UnauthorizedAccessException(@"Access to the path 'E:\DCIM\IMG_0001.CR3' is denied."))); Assert.Equal(Loc.L("Import.Failed.Photo"), ImportExecutor.FailureText(new IOException("The semaphore timeout period has expired."))); Assert.Equal(Loc.L("Import.Failed.Photo"), ImportExecutor.FailureText(new FileNotFoundException(@"Could not find file 'E:\DCIM\IMG_0001.CR3'."))); + // A sink that stages its copies on this PC (a server import, in the cache folder): a full disk there is this PC's. + Assert.Equal(Loc.L("Import.Failed.CacheFull"), + ImportExecutor.FailureText(new IOException("There is not enough space on the disk.", unchecked((int)0x80070070)), stagesOnThisPc: true)); + Assert.Equal(Loc.L("Import.Failed.CacheFull"), ImportExecutor.FailureText(new IOException("The disk is full.", unchecked((int)0x80070027)), stagesOnThisPc: true)); + Assert.Equal(Loc.L("Import.Failed.PhotoAccess"), ImportExecutor.FailureText(new UnauthorizedAccessException("Access denied."), stagesOnThisPc: true)); + Assert.Equal(Loc.L("Import.Failed.Photo"), ImportExecutor.FailureText(new IOException("The semaphore timeout period has expired."), stagesOnThisPc: true)); + Assert.False(((IImportSink)new FolderImportSink(Path.GetTempPath())).StagesOnThisPc, "a folder import stages inside the destination"); // The executor's own checks are one sentence each already. Assert.Equal(Loc.L("Archive.Error.CopyMismatch", "IMG_0001.CR3"), ImportExecutor.FailureText(new CopyVerificationException("/staging/IMG_0001.CR3"))); Assert.Equal(Loc.L("Archive.Error.SourceChanged", "DCIM/IMG_0001.CR3"), ImportExecutor.FailureText(new ImportSourceChangedException("DCIM/IMG_0001.CR3")));