fix(server): keep convert dry runs like Reorganize's, say what a new sequence lacks

The convert endpoint planned the whole selection inside the HTTP request, again for
every mode, quality keystroke and confirmation, with no limit: behind a proxy an
all-sequence conversion could never get past its dry run or its apply.

- ReorganizePlanCache becomes PlanCache, shared by Reorganize and Convert: one plan
  per key made apart from the request, two at once across both kinds, eight kept,
  15-minute timeout, 5-minute lifetime, 503 plan_dropped. Keys are typed records
  (ReorganizePlanKey, ConvertPlanKey) that also say which exceptions are refusals.
- A conversion plan is keyed by archive, a convert stamp of the index
  (SequenceRepository.GetConvertStampAsync: the Reorganize stamp plus, per planned
  sequence, the photos' count, sizes, formats, pixel sizes and missing, corrupt,
  preview-only and undecodable flags), the sequences and the target with the
  quality that counts. Not the mode: the request's mode and options are set on the
  kept plan, and the free space is measured again at every request through the
  engine. A plan made while the volume was away is not kept (503 archive_offline).
- New sequence holds the converted copies alone: the dry run says the skipped
  photos are not copied into it (Archive.Convert.NotCopied.*, Server.Convert.OnPcNew),
  the panel's totals, column and confirmation give the count, and so does the
  desktop's summary.
- Panel: the preview is asked in Replace's neutral words; a dry run cut off by a
  proxy is asked again while the totals show how long it takes, plan_dropped is
  retried once; an apply the server let go plans again, a changed one shows
  Admin.Convert.PlanChanged, and one with no answer is not resent (its job may be
  queued).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit f0b1e379ce7c08b6d3459ab25ca6214c8f366f6c)
This commit is contained in:
Alby96 committed 2026-10-27 22:15:51 +01:00
1 parent d0bbd77c92
commit 8174852a2c
22 files changed
+927 -342

No files matched your search

@@ -469,6 +469,50 @@ public sealed class SequenceRepository
}
}
/// <summary>
/// A digest of what a conversion of <paramref name="sequenceIds"/> (null: every sequence) plans from in the index of
/// <paramref name="archiveId"/> (owner review 2, R7): what a Reorganize's does (<see cref="GetStampAsync"/>: every sequence
/// and its folder, which decide what "every sequence" is and its order; of the sequences planned, their title, photo
/// count, frames version and how many of their photos are hashed), and of the photos of the sequences planned, what
/// decides whether each converts and the size it is estimated at: their format, size and pixel size, and the flags a
/// scan, a verify or a decode records without another frames version (missing, corrupt, read only from the preview,
/// undecodable). Another value once one of these changes. A change made on disk alone, which the index does not have
/// (a file gone before a scan finds it), leaves it as it was.
/// </summary>
public async Task<string> GetConvertStampAsync(Guid archiveId, IReadOnlyCollection<Guid>? sequenceIds, CancellationToken cancellationToken = default)
{
var stamp = await GetStampAsync(archiveId, sequenceIds, cancellationToken).ConfigureAwait(false);
// Per sequence planned: its photos, their bytes, formats and pixel sizes, and how many carry each flag that decides.
var columns = "count(*), COALESCE(SUM(f.size), 0), COALESCE(SUM(f.format), 0), COALESCE(SUM(f.width), 0), COALESCE(SUM(f.height), 0), " +
string.Join(", ", new[] { FrameFlags.Missing, FrameFlags.Corrupt, FrameFlags.PreviewOnly, FrameFlags.Undecodable }
.Select(flag => $"COALESCE(SUM(CASE WHEN (f.flags & {(int)flag}) <> 0 THEN 1 ELSE 0 END), 0)"));
static (Guid Id, string Photos) Read(DbRow r) =>
(r.GetGuid(0), string.Join('\u001f', Enumerable.Range(1, 9).Select(i => r.GetInt64(i).ToString(CultureInfo.InvariantCulture))));
var sums = new List<(Guid Id, string Photos)>();
if (sequenceIds is null)
{
sums.AddRange(await _db.ListAsync(
$"SELECT f.sequence_id, {columns} FROM frames f JOIN sequences s ON s.id = f.sequence_id WHERE s.archive_id = $1 GROUP BY f.sequence_id",
[archiveId], Read, cancellationToken).ConfigureAwait(false));
}
else
{
// 500 ids per statement stays below SQLite's historical 999-variable limit.
foreach (var chunk in sequenceIds.Distinct().Chunk(500))
{
var p = new SqlParameters();
var ids = string.Join(", ", chunk.Select(id => p.Add(id)));
sums.AddRange(await _db.ListAsync($"SELECT f.sequence_id, {columns} FROM frames f WHERE f.sequence_id IN ({ids}) GROUP BY f.sequence_id",
p.ToArray(), Read, cancellationToken).ConfigureAwait(false));
}
}
using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
hash.AppendData(Encoding.UTF8.GetBytes(stamp));
foreach (var (id, photos) in sums.OrderBy(s => s.Id))
hash.AppendData(Encoding.UTF8.GetBytes(string.Create(CultureInfo.InvariantCulture, $"\n{id:N}\u001f{photos}")));
return Convert.ToHexStringLower(hash.GetHashAndReset());
}
/// <summary>
/// How many photos of each of <paramref name="sequenceIds"/> (null: every sequence of <paramref name="archiveId"/>) have
/// their SHA-256 in the index; a sequence without one is not listed.