Files
Chronos/src/Chronos.App/Import/ImportFailures.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

104 lines
5.6 KiB
C#

using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
using Chronos.App.Shell;
using Chronos.Core.Archiving;
using Chronos.Core.Import;
using Chronos.Core.Localization;
using Chronos.Data;
using Chronos.Protocol.Client;
namespace Chronos.App.Import;
/// <summary>
/// What the Import screen says when an import or its plan stops (BRIEF §0.2 rule 6: one sentence saying what
/// happened): a localized sentence chosen by cause. The exception's own text, which may hold full paths or several
/// sentences, goes to the tooltip and the log only.
/// </summary>
public static class ImportFailures
{
private const int DiskFull = unchecked((int)0x80070070);
private const int HandleDiskFull = unchecked((int)0x80070027);
private const int WriteProtect = unchecked((int)0x80070013);
/// <summary>The sentences an archive import job fails with when the archive's engine stops it itself.</summary>
private static readonly string[] EngineSentences =
[
"Archive.Error.Offline", "Archive.Error.ReadOnly", "Archive.Error.Busy", "Archive.Error.NewerFormat", "Archive.Error.SourceGone",
"Archive.Error.DuplicateLost",
];
/// <param name="source">The card or folder as the Devices list names it.</param>
public static string Import(Exception ex, string source) => ex switch
{
ImportSourceUnavailableException => Loc.L("Import.Failed.SourceGone", source),
IOException { HResult: DiskFull or HandleDiskFull } => Loc.L("Import.Failed.Full"),
UnauthorizedAccessException => Loc.L("Import.Failed.Access"),
ChronosApiException or HttpRequestException or UntrustedCertificateException => Loc.L("Import.Failed.Server"),
_ => Loc.L("Import.Failed.Other"),
};
/// <summary>
/// Why the photos an import into a folder or the server never reached were not copied (<see cref="LocalImport"/>):
/// 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. When the sink stages on this PC (<paramref name="stagesOnThisPc"/>,
/// the server's), a full disk or a refused write is this PC's cache folder's: the server answers for its own space.
/// </summary>
public static string Stop(Exception ex, string source, bool stagesOnThisPc = false) => ex switch
{
ChronosApiException 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),
};
/// <summary>
/// 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.
/// </summary>
public static string Job(string? error) =>
error is { Length: > 0 } && EngineSentences.Any(key => IsSentence(key, error)) ? error : Loc.L("Import.Failed.Other");
/// <summary>Why the card or folder could not be planned.</summary>
public static string Plan(Exception ex, string source) => ex switch
{
DirectoryNotFoundException or DriveNotFoundException or ImportSourceUnavailableException => Loc.L("Import.Failed.SourceGone", source),
IOException or UnauthorizedAccessException => Loc.L("Import.Status.Unreadable", source),
_ => Loc.L("Import.Status.PlanError"),
};
/// <summary>Why the check of a card (every card file and its archive copy hashed again) stopped.</summary>
/// <param name="root">Where the card is mounted: a card that left explains any read failure.</param>
public static string Check(Exception ex, string source, string root) => ex switch
{
_ when CardGone(ex, root) => Loc.L("Import.Failed.SourceGone", source),
ArchiveOfflineException => ex.Message,
DatabaseException => Loc.L("Archive.Failed.Index"),
IOException or UnauthorizedAccessException => Loc.L("Import.Failed.CheckRead"),
_ => Loc.L("Errors.Unexpected"),
};
/// <summary>Why deleting verified photos from a card stopped.</summary>
/// <param name="root">Where the card is mounted: a card that left explains any write failure.</param>
public static string Erase(Exception ex, string source, string root) => ex switch
{
_ when CardGone(ex, root) => Loc.L("Import.Failed.SourceGone", source),
// A locked card (write-protect switch) or photos the camera marked protected (read-only).
IOException { HResult: WriteProtect } or UnauthorizedAccessException => Loc.L("Import.Failed.CardLocked", source),
_ => Loc.L("Errors.Unexpected"),
};
/// <summary>Whether <paramref name="text"/> is the sentence of <paramref name="key"/>, whatever its arguments.</summary>
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);
}