A conversion job could still do something its confirmed dry run did not show, or refuse what it should have converted: - An upload landing between the confirmation and the sequence's turn made the sequence Changed at every retry. Each ConvertSequence now names the photos the plan converts (Photos, their frame ids); the job takes those from its re-plan, checks their content against the confirmed digest and leaves photos that came in since as they are. A photo the plan did not list is never converted, whatever changed on disk or in the index since the dry run. - A file another program held during the dry run was planned as Missing and then converted or not depending on the re-plan. It is now its own reason (inaccessible, "held open or cannot be opened"); a truncated file reads as undecodable. At job time a confirmed photo that cannot be opened leaves the sequence Blocked for the retry; a held photo the job does not convert does not hide a real change. - The same sequences could be converted twice (two dialogs or tabs, a lost answer, no dry run at all). MassConverter.QueueAsync checks the queued, running and paused conversions and queues under one lock, in the caller's context (the desktop's enqueue shows a toast on the UI thread); a job's own re-plan treats sequences of conversions queued before it as busy. - The per-photo space check reserved a full batch of photos even when fewer were left, so a job whose preview had the space could stop at every retry. It now reserves the photo and those after it that may convert at once. - New sequence now says in the dry run, the panel's totals and confirmation, and a new desktop confirmation, how many photos each new sequence lacks; only sequences that are copied use those words. - On the server, a confirmation takes only the plan kept for the index as it is now (503 plan_dropped otherwise, never a plan made inside the request), and busy sequences and free space are as they are at every request. The panel's Apply asks the dry run again first, with Close still available, and confirms only the same fingerprint. - Maintenance keeps another host's conversion staging as long as its journal and purges both together; the unstaged-copy failure and blocked sequences are logged in catalog sentences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit fefd9c34ebfeed1f9e84161610ef286026d38355)
193 lines
11 KiB
C#
193 lines
11 KiB
C#
using System.Globalization;
|
||
using Chronos.App.Infrastructure;
|
||
using Chronos.Core.Conversion;
|
||
using Chronos.Core.Localization;
|
||
|
||
namespace Chronos.App.Archiving;
|
||
|
||
/// <summary>
|
||
/// One sequence of the conversion dry run (owner review 2, R7): its name, what converts (photos, size before and after),
|
||
/// and the photos that stay as they are or why the whole sequence is left as it is, each line with why.
|
||
/// </summary>
|
||
/// <param name="IsNamePath">The sequence has no title: <paramref name="Name"/> is its folder, read left to right.</param>
|
||
/// <param name="IsBlocked">Nothing of the sequence converts now (busy, offline, unreadable).</param>
|
||
public sealed record ConvertRow(Guid SequenceId, string Name, bool IsNamePath, string Note, IReadOnlyList<string> Kept, bool IsBlocked)
|
||
{
|
||
public bool HasNote => Note.Length > 0;
|
||
|
||
public bool HasKept => Kept.Count > 0;
|
||
|
||
/// <summary>What a screen reader says after the name: what converts, then every line of what stays.</summary>
|
||
public string Description => string.Join(Environment.NewLine, [.. HasNote ? [Note] : (string[])[], .. Kept]);
|
||
}
|
||
|
||
/// <summary>The free space line of a conversion dry run, and whether the volume lacks the space.</summary>
|
||
public sealed record ConvertSpaceLine(string Text, bool IsShort);
|
||
|
||
/// <summary>
|
||
/// The words of a mass conversion (owner review 2, R7; BRIEF §12.5): the engine's dry run (<see cref="ConvertPlan"/>) as
|
||
/// rows, a summary and the free space line, the typed confirmation a Replace asks for, and the one New sequence asks for,
|
||
/// which says which photos the new sequences lack.
|
||
/// </summary>
|
||
public static class ConvertPlans
|
||
{
|
||
/// <summary>The targets in the order the dialog offers them.</summary>
|
||
public static readonly IReadOnlyList<ConvertTarget> Targets = [ConvertTarget.Jpeg, ConvertTarget.Tiff16, ConvertTarget.LinearDng];
|
||
|
||
/// <summary>The problem of a JPEG quality as typed, localized; null when it is 1–100.</summary>
|
||
public static string? ValidateQuality(string? text) =>
|
||
int.TryParse(text?.Trim(), NumberStyles.Integer, Loc.Culture, out var quality) && quality is >= 1 and <= 100
|
||
? null
|
||
: Loc.L("Errors.Number.Range", 1.ToString("N0", Loc.Culture), 100.ToString("N0", Loc.Culture));
|
||
|
||
/// <summary>A valid quality as typed (see <see cref="ValidateQuality"/>).</summary>
|
||
public static int ParseQuality(string text) => int.Parse(text.Trim(), NumberStyles.Integer, Loc.Culture);
|
||
|
||
/// <summary>
|
||
/// The rows of a plan in its order: every sequence that converts, or whose photos stay as they are, with why in the
|
||
/// words of the plan's mode (<see cref="Row"/>).
|
||
/// </summary>
|
||
public static IReadOnlyList<ConvertRow> Rows(ConvertPlan plan)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(plan);
|
||
return [.. plan.Sequences.Where(s => s.Frames.Count > 0 || s.Skipped.Count > 0 || s.Blocked.Count > 0).Select(s => Row(s, plan.Mode))];
|
||
}
|
||
|
||
/// <summary>
|
||
/// One sequence of a plan: what converts, then what blocks it, then the photos skipped with why. Of a sequence
|
||
/// <see cref="ConvertMode.NewSequence"/> copies, those are not copied into the new sequence, which holds the converted
|
||
/// copies alone; otherwise (a Replace, or a sequence that converts nothing now, of which no new sequence is made) they
|
||
/// stay as they are.
|
||
/// </summary>
|
||
public static ConvertRow Row(ConvertSequencePlan step, ConvertMode mode = ConvertMode.Replace)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(step);
|
||
var (name, named) = NameOf(step);
|
||
var note = step.Converts
|
||
? Loc.Plural("Shell.Status.Photos", step.Frames.Count) + Loc.Separator + Sizes(step.BytesBefore, step.BytesAfter)
|
||
: string.Empty;
|
||
// What blocks the whole sequence comes first: the photos it would skip anyway do not matter until it can convert.
|
||
List<string> kept = [.. step.Blocked.Distinct().Select(b => Loc.L("Archive.Convert.Blocked." + b))];
|
||
var why = mode == ConvertMode.NewSequence && step.Converts ? "Archive.Convert.NotCopied." : "Archive.Convert.Skipped.";
|
||
kept.AddRange(step.Skipped.Select(s => Loc.L(why + s.Reason) + Loc.Separator + Loc.Plural("Shell.Status.Photos", s.Frames)));
|
||
return new ConvertRow(step.SequenceId, name, !named, note, kept, step.Blocked.Count > 0);
|
||
}
|
||
|
||
/// <summary>A sequence's title, else its folder (a path), else the start of its id; and whether it has a title.</summary>
|
||
private static (string Name, bool Named) NameOf(ConvertSequencePlan step)
|
||
{
|
||
var named = !string.IsNullOrWhiteSpace(step.Title);
|
||
return (named ? step.Title : step.Folder.Length > 0 ? step.Folder : step.SequenceId.ToString("N")[..8], named);
|
||
}
|
||
|
||
/// <summary>The photos a conversion into new sequences leaves out of them: those the sequences it copies skip.</summary>
|
||
public static int LeftOut(ConvertPlan plan)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(plan);
|
||
return plan.Converted.Sum(s => s.SkippedFrames);
|
||
}
|
||
|
||
/// <summary>"12.4 GB → 9.8 GB".</summary>
|
||
public static string Sizes(long before, long after) => Loc.L("Archive.Convert.Sizes", Formatting.Bytes(before), Formatting.Bytes(after));
|
||
|
||
/// <summary>
|
||
/// "12 of 36 change · 3,600 photos · 41 GB → 32 GB · 20 skipped": the sequences converted of those planned, then the
|
||
/// photos and their sizes when something converts, the photos skipped when there are any. With
|
||
/// <see cref="ConvertMode.NewSequence"/>, those of the sequences copied first ("20 not copied": the new sequences lack
|
||
/// them), then those of the sequences that convert nothing now ("4 skipped").
|
||
/// </summary>
|
||
public static string Summary(ConvertPlan plan)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(plan);
|
||
var parts = new List<string> { Loc.Plural("Archive.Rename.Summary", plan.Converted.Count(), plan.Sequences.Count) };
|
||
if (plan.Frames > 0)
|
||
{
|
||
parts.Add(Loc.Plural("Shell.Status.Photos", plan.Frames));
|
||
parts.Add(Sizes(plan.BytesBefore, plan.BytesAfter));
|
||
}
|
||
var leftOut = plan.Mode == ConvertMode.NewSequence ? LeftOut(plan) : 0;
|
||
if (leftOut > 0) parts.Add(Loc.Plural("Archive.Convert.NotCopiedPhotos", leftOut));
|
||
if (plan.Skipped - leftOut > 0) parts.Add(Loc.Plural("Archive.Convert.SkippedPhotos", plan.Skipped - leftOut));
|
||
return string.Join(Loc.Separator, parts);
|
||
}
|
||
|
||
/// <summary>"Needs 12 GB, free 340 GB", or that the volume lacks it, or that its free space cannot be measured.</summary>
|
||
public static ConvertSpaceLine Space(ConvertSpace space)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(space);
|
||
var required = Formatting.Bytes(space.Required);
|
||
if (space.Free is not { } free) return new(Loc.L("Archive.Convert.SpaceUnknown", required), false);
|
||
return space.Enough
|
||
? new(Loc.L("Archive.Convert.Space", required, Formatting.Bytes(free)), false)
|
||
: new(Loc.L("Archive.Convert.SpaceShort", required, Formatting.Bytes(free)), true);
|
||
}
|
||
|
||
/// <summary>"Linear DNG": the target as the dialog names it.</summary>
|
||
public static string TargetText(ConvertTarget target) => Loc.L("Archive.Convert.Target." + target);
|
||
|
||
/// <summary>
|
||
/// The destructive confirmation of a Replace: the sequences whose photos are replaced, that the originals move to the
|
||
/// archive's trash, and the number of photos to type; null when the plan converts nothing.
|
||
/// </summary>
|
||
public static ConfirmRequest? ReplaceRequest(ConvertPlan plan)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(plan);
|
||
var converted = plan.Converted.ToList();
|
||
if (converted.Count == 0) return null;
|
||
var count = plan.Frames.ToString(CultureInfo.InvariantCulture);
|
||
var lines = converted.Take(DeletePlans.ListedLines).Select(s => Loc.L("Archive.Delete.Line", Row(s).Name,
|
||
Loc.Plural("Shell.Status.Photos", s.Frames.Count), Formatting.Bytes(s.BytesBefore))).ToList();
|
||
if (converted.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", converted.Count - DeletePlans.ListedLines));
|
||
return new ConfirmRequest
|
||
{
|
||
Heading = Loc.Plural("Archive.Convert.ReplaceHeading", converted.Count),
|
||
Message = Loc.L("Archive.Convert.ReplaceMessage", Loc.Plural("Shell.Status.Photos", plan.Frames), Formatting.Bytes(plan.BytesBefore),
|
||
TargetText(plan.Options.Target)),
|
||
Detail = string.Join(Environment.NewLine, lines),
|
||
IsDetailMonospaced = false,
|
||
DetailToolTip = Loc.L("Tooltips.Archive.ConvertPlan"),
|
||
SafeText = Loc.L("Archive.Delete.Keep"),
|
||
SafeToolTip = Loc.L("Tooltips.Archive.DeleteKeep"),
|
||
ActionText = Loc.L("Archive.Convert.Replace"),
|
||
ActionToolTip = Loc.Plural("Tooltips.Archive.ConvertReplace", plan.Frames),
|
||
IsDestructive = true,
|
||
TypedConfirmation = count,
|
||
TypedPrompt = Loc.L("Archive.Delete.Type", count),
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// The confirmation of a conversion into new sequences: the photos converted into copies beside their sources, the
|
||
/// originals untouched, how many photos the new sequences lack (those the plan skips there, for the reasons its rows
|
||
/// give), and each sequence copied with its photos, their size and the photos its new sequence lacks; null when the plan
|
||
/// converts nothing.
|
||
/// </summary>
|
||
public static ConfirmRequest? NewRequest(ConvertPlan plan)
|
||
{
|
||
ArgumentNullException.ThrowIfNull(plan);
|
||
var converted = plan.Converted.ToList();
|
||
if (converted.Count == 0) return null;
|
||
var lines = converted.Take(DeletePlans.ListedLines).Select(s =>
|
||
{
|
||
var line = Loc.L("Archive.Delete.Line", NameOf(s).Name, Loc.Plural("Shell.Status.Photos", s.Frames.Count), Sizes(s.BytesBefore, s.BytesAfter));
|
||
return s.SkippedFrames > 0 ? line + Loc.Separator + Loc.Plural("Archive.Convert.NotCopiedPhotos", s.SkippedFrames) : line;
|
||
}).ToList();
|
||
if (converted.Count > DeletePlans.ListedLines) lines.Add(Loc.Plural("Archive.Delete.More", converted.Count - DeletePlans.ListedLines));
|
||
var message = Loc.L("Archive.Convert.NewMessage", Loc.Plural("Shell.Status.Photos", plan.Frames), Formatting.Bytes(plan.BytesBefore),
|
||
TargetText(plan.Options.Target));
|
||
if (LeftOut(plan) is > 0 and var leftOut) message += " " + Loc.Plural("Archive.Convert.NewLeftOut", leftOut);
|
||
return new ConfirmRequest
|
||
{
|
||
Heading = Loc.Plural("Archive.Convert.NewHeading", converted.Count),
|
||
Message = message,
|
||
Detail = string.Join(Environment.NewLine, lines),
|
||
IsDetailMonospaced = false,
|
||
DetailToolTip = Loc.L("Tooltips.Archive.ConvertPlanNew"),
|
||
SafeText = Loc.L("Archive.Dialog.Cancel"),
|
||
SafeToolTip = Loc.L("Tooltips.Archive.ConvertBack"),
|
||
ActionText = Loc.L("Archive.Convert.Confirm"),
|
||
ActionToolTip = Loc.L("Tooltips.Archive.ConvertNew"),
|
||
};
|
||
}
|
||
}
|