merge: server archive fixes (V-5, V-8, V-11, V-16, V-35, V-36)

Merge branch fix2/server-archive into the verification branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 16:54:04 +00:00
commit 61c7e9e7e3
19 files changed
+671 -39

No files matched your search

+64 -14
View File
@@ -20,7 +20,9 @@ public enum ScanMode
public sealed record ScanProgress(int FoldersDone, int FoldersTotal, string Folder, bool Changed);
/// <param name="Failed">Folders that could not be indexed, with the reason; they are retried by the next scan.</param>
public sealed record ScanSummary(int Folders, int Indexed, int Adopted, int Removed, IReadOnlyList<(string Folder, string Error)> Failed);
/// <param name="Sequences">The sequences this run indexed (<paramref name="Indexed"/> of them), in folder order.</param>
public sealed record ScanSummary(int Folders, int Indexed, int Adopted, int Removed, IReadOnlyList<(string Folder, string Error)> Failed,
IReadOnlyList<Guid> Sequences);
/// <summary>
/// Keeps the database index in line with the archive on disk (BRIEF §7, §12.5): scans by size/time stamps, adopts
@@ -49,11 +51,15 @@ public sealed class ArchiveIndexer
if (mode == ScanMode.Rebuild && resumeAfter is null) await _db.Archives.ClearFolderStatesAsync(archive.Id, cancellationToken).ConfigureAwait(false);
var folders = ArchiveScanner.FindSequenceFolders(paths, cancellationToken);
var states = await _db.Archives.GetFolderStatesAsync(archive.Id, cancellationToken).ConfigureAwait(false);
var locations = new Locations(await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false));
var rows = await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false);
// A sequence waiting for its first frame has no folder yet: the archive root, adopted or re-adopted, never takes its id.
var awaiting = await AwaitingFolderAsync(paths, rows, cancellationToken).ConfigureAwait(false);
var locations = new Locations(rows.Where(r => !awaiting.Contains(r.Id)));
var snapshot = mode == ScanMode.Rebuild ? IndexSnapshot.TryLoad(paths.SnapshotPath) : null;
using var snapshotWriter = mode != ScanMode.Incremental && resumeAfter is null ? new IndexSnapshot.Writer(paths.SnapshotPath) : null;
int indexed = 0, adopted = 0;
int adopted = 0;
var indexed = new List<Guid>();
var failed = new List<(string, string)>();
for (var i = 0; i < folders.Count; i++)
{
@@ -67,14 +73,14 @@ public sealed class ArchiveIndexer
try
{
var reconcile = mode == ScanMode.Full || (state is not null && state.Photos != folder.Photos);
var bytes = await IndexFolderAsync(archive, paths, folder, reconcile, snapshot, locations, cancellationToken).ConfigureAwait(false);
var (id, bytes) = await IndexFolderAsync(archive, paths, folder, reconcile, snapshot, locations, cancellationToken).ConfigureAwait(false);
if (snapshotWriter is not null)
{
var sidecar = paths.Sequence(folder.RelativeFolder).ManifestPath;
snapshotWriter.Add(folder.RelativeFolder, FileStamp.Of(sidecar)!.Value,
bytes ?? await File.ReadAllBytesAsync(sidecar, cancellationToken).ConfigureAwait(false));
}
indexed++;
indexed.Add(id);
if (folder.Sidecar is null) adopted++;
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
@@ -88,7 +94,7 @@ public sealed class ArchiveIndexer
var removed = mode == ScanMode.Rebuild ? await RemoveUnbackedAsync(archive, paths, folders, locations, cancellationToken).ConfigureAwait(false) : 0;
if (failed.Count == 0) snapshotWriter?.Commit();
return new ScanSummary(folders.Count, indexed, adopted, removed, failed);
return new ScanSummary(folders.Count, indexed.Count, adopted, removed, failed, indexed);
}
/// <summary>
@@ -156,8 +162,8 @@ public sealed class ArchiveIndexer
}
}
/// <summary>Indexes one folder; returns the sidecar bytes when they were read and not rewritten since (for the snapshot).</summary>
private async Task<byte[]?> IndexFolderAsync(Archive archive, ArchivePaths paths, SequenceFolder folder, bool reconcile, IndexSnapshot? snapshot,
/// <summary>Indexes one folder; returns its sequence and the sidecar bytes when they were read and not rewritten since (for the snapshot).</summary>
private async Task<(Guid Id, byte[]? Bytes)> IndexFolderAsync(Archive archive, ArchivePaths paths, SequenceFolder folder, bool reconcile, IndexSnapshot? snapshot,
Locations locations, CancellationToken cancellationToken)
{
var sequence = paths.Sequence(folder.RelativeFolder);
@@ -189,10 +195,11 @@ public sealed class ArchiveIndexer
}
if (reconcile || File.Exists(sequence.IngestJournalPath))
{
// Photos added, gone or back: the light analysis no longer describes the frames, so the scan's follow-up runs it again.
// Photos added, gone or back: the light analysis, thumbnails and proxies no longer cover the frames, so the scan's
// follow-ups make them again (the images on a server, ArchiveServiceOptions.ImagesAfterScan).
manifest = await UpdateAndSyncAsync(archive, paths, sequence, manifest.Id,
m => SequenceAdopter.Reconcile(sequence, m, cancellationToken) is { } reconciled
? reconciled with { Status = reconciled.Status & ~SequenceStatus.AnalyzedLight }
? reconciled with { Status = reconciled.Status & ~(SequenceStatus.AnalyzedLight | SequenceStatus.ThumbsBuilt | SequenceStatus.ProxiesBuilt) }
: null,
cancellationToken).ConfigureAwait(false);
bytes = null;
@@ -208,7 +215,45 @@ public sealed class ArchiveIndexer
await SyncAsync(archive, sequence, manifest, cancellationToken).ConfigureAwait(false);
}
locations.Set(manifest.Id, folder.RelativeFolder);
return bytes;
return (manifest.Id, bytes);
}
/// <summary>
/// The sequences among <paramref name="locations"/> created without photos that still wait for their first frame: no
/// folder, no frames, and not the sequence the archive root's sidecar names. Their empty folder is not the archive
/// root: scans do not adopt or rebuild the root under their id, a rebuild keeps them although they have no sidecar yet,
/// orphan checks do not count them missing, and maintenance leaves the root's files to the root's own sequence. Unlike
/// <see cref="ArchiveIngest.AwaitsFolder"/>, whose commit reports it, a root sidecar that cannot be read names nobody
/// here: a scan rebuilds a damaged one under the id of the sequence that holds the root's frames, and one it may not
/// read fails the root folder alone rather than the whole scan or rebuild.
/// </summary>
internal async Task<HashSet<Guid>> AwaitingFolderAsync(ArchivePaths paths, IEnumerable<SequenceLocation> locations, CancellationToken cancellationToken)
{
var awaiting = new HashSet<Guid>();
(Guid? Id, bool Read) root = (null, false);
foreach (var location in locations.Where(l => l.Folder.Length == 0))
{
if (await _db.Sequences.GetAsync(location.Id, cancellationToken).ConfigureAwait(false) is not { Folder.Length: 0, FrameCount: 0 } sequence) continue;
// Read once, and only when a row may be waiting: a root sidecar can list a hundred thousand frames.
if (!root.Read) root = (RootSidecarId(paths), true);
if (root.Id != sequence.Id) awaiting.Add(sequence.Id);
}
return awaiting;
}
/// <summary>The id the archive root's sidecar names; null when there is none or it cannot be read.</summary>
private static Guid? RootSidecarId(ArchivePaths paths)
{
var sidecar = paths.Sequence(string.Empty).ManifestPath;
try
{
return File.Exists(sidecar) ? ManifestStore.ReadSequence(sidecar).Id : null;
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
// Indexing the root reports it (a damaged sidecar is rebuilt, an unreadable one fails that folder alone).
return null;
}
}
/// <summary>The index holds a project for this sequence and other frames than the sidecar: its positions may move.</summary>
@@ -261,16 +306,21 @@ public sealed class ArchiveIndexer
UpdatedAt = DateTimeOffset.UtcNow,
};
/// <summary>Rebuild only: rows whose folder no longer holds a sidecar leave the index (their files are untouched).</summary>
/// <summary>
/// Rebuild only: rows whose folder no longer holds a sidecar leave the index (their files are untouched). A sequence
/// waiting for its first frame has no sidecar yet and stays.
/// </summary>
private async Task<int> RemoveUnbackedAsync(Archive archive, ArchivePaths paths, IReadOnlyList<SequenceFolder> folders, Locations locations,
CancellationToken cancellationToken)
{
ArchiveVolume.EnsureWritable(paths);
var present = folders.Where(f => File.Exists(paths.Sequence(f.RelativeFolder).ManifestPath)).Select(f => f.RelativeFolder).ToHashSet(StringComparer.Ordinal);
var rows = await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false);
var awaiting = await AwaitingFolderAsync(paths, rows, cancellationToken).ConfigureAwait(false);
var removed = 0;
foreach (var location in await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false))
foreach (var location in rows)
{
if (present.Contains(location.Folder) && locations.AtFolder(location.Folder) == location.Id) continue;
if (awaiting.Contains(location.Id) || (present.Contains(location.Folder) && locations.AtFolder(location.Folder) == location.Id)) continue;
if (await _db.Sequences.DeleteAsync(location.Id, cancellationToken).ConfigureAwait(false)) removed++;
await _db.Archives.DeleteFolderStateAsync(archive.Id, location.Folder, cancellationToken).ConfigureAwait(false);
}
@@ -71,7 +71,10 @@ public sealed class ArchiveMaintenance
{
var paths = new ArchivePaths(archive.RootPath);
if (ArchiveVolume.Probe(paths) == ArchiveAvailability.Offline) throw new ArchiveOfflineException(paths.Root, ArchiveAvailability.Offline);
var locations = await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false);
var all = await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false);
// A sequence waiting for its first frame has no folder yet: none of it is missing, and the archive root is not its folder.
var awaiting = await _indexer.AwaitingFolderAsync(paths, all, cancellationToken).ConfigureAwait(false);
var locations = all.Where(l => !awaiting.Contains(l.Id)).ToList();
var sequences = new List<SequenceOrphans>();
var missing = new List<SequenceLocation>();
foreach (var location in locations)
@@ -91,7 +94,10 @@ public sealed class ArchiveMaintenance
return new OrphanReport(sequences, missing, unindexed);
}
/// <summary>Drops index rows of sequences whose sidecar is gone (an orphans fix action); rows whose sidecar is back are kept.</summary>
/// <summary>
/// Drops index rows of sequences whose sidecar is gone (an orphans fix action); rows whose sidecar is back are kept, and
/// so are sequences waiting for their first frame (<see cref="ArchiveIngest.AwaitsFolder"/>), which have none yet.
/// </summary>
public async Task<int> RemoveMissingSequencesAsync(Archive archive, IReadOnlyList<Guid> sequenceIds, CancellationToken cancellationToken = default)
{
var paths = new ArchivePaths(archive.RootPath);
@@ -100,7 +106,7 @@ public sealed class ArchiveMaintenance
foreach (var id in sequenceIds)
{
if (await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { } sequence || sequence.ArchiveId != archive.Id) continue;
if (File.Exists(paths.Sequence(sequence.Folder).ManifestPath)) continue;
if (File.Exists(paths.Sequence(sequence.Folder).ManifestPath) || ArchiveIngest.AwaitsFolder(sequence, paths)) continue;
if (await _db.Sequences.DeleteAsync(id, cancellationToken).ConfigureAwait(false)) removed++;
await _db.Archives.DeleteFolderStateAsync(archive.Id, sequence.Folder, cancellationToken).ConfigureAwait(false);
}
@@ -117,7 +123,11 @@ public sealed class ArchiveMaintenance
{
var paths = new ArchivePaths(archive.RootPath);
if (!dryRun) ArchiveVolume.EnsureWritable(paths);
var locations = await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false);
var all = await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false);
// A sequence waiting for its first frame has no folder yet: the archive root's pack is compacted under the leases of
// the root's own sequence, which its thumbnail passes hold, never under another id's.
var awaiting = await _indexer.AwaitingFolderAsync(paths, all, cancellationToken).ConfigureAwait(false);
var locations = all.Where(l => !awaiting.Contains(l.Id)).ToList();
IReadOnlyList<string> leases = [], temps = [], packs = [];
int groups = 0, flagChanges = 0;
long? reclaimable = null;
+2 -2
View File
@@ -54,8 +54,8 @@ public sealed class ArchiveService : IJobHandlerProvider
/// <summary>Handlers for scan, rebuild, verify, import, thumbs, proxies and maintenance jobs; register them on the <see cref="JobEngine"/>.</summary>
public IReadOnlyList<IJobHandler> CreateJobHandlers() =>
[
new ScanJobHandler(_db, Indexer, JobKind.Scan),
new ScanJobHandler(_db, Indexer, JobKind.Rebuild),
new ScanJobHandler(_db, Indexer, JobKind.Scan, _writer.Options.ImagesAfterScan),
new ScanJobHandler(_db, Indexer, JobKind.Rebuild, _writer.Options.ImagesAfterScan),
new VerifyJobHandler(_db, Verifier),
new ImportJobHandler(_db, Imports),
new ImageJobHandler(_db, Images, JobKind.Thumbs),
+5 -3
View File
@@ -44,7 +44,7 @@ public sealed class ArchiveVerifier
{
var paths = new ArchivePaths(archive.RootPath);
ArchiveVolume.EnsureWritable(paths);
var targets = await TargetsAsync(archive, sequenceIds, cancellationToken).ConfigureAwait(false);
var targets = await TargetsAsync(archive, paths, sequenceIds, cancellationToken).ConfigureAwait(false);
var bytesTotal = targets.Sum(t => t.TotalBytes);
var bytesDone = targets.Take(resume.Sequence).Sum(t => t.TotalBytes);
long checkedFrames = 0, ok = 0, hashed = 0;
@@ -82,13 +82,15 @@ public sealed class ArchiveVerifier
return new VerifyReport(targets.Count, checkedFrames, ok, hashed, problems);
}
private async Task<List<Sequence>> TargetsAsync(Archive archive, IReadOnlyList<Guid>? sequenceIds, CancellationToken cancellationToken)
/// <summary>The sequences to check; one waiting for its first frame (<see cref="ArchiveIngest.AwaitsFolder"/>) has no sidecar to check yet.</summary>
private async Task<List<Sequence>> TargetsAsync(Archive archive, ArchivePaths paths, IReadOnlyList<Guid>? sequenceIds, CancellationToken cancellationToken)
{
var ids = sequenceIds ?? (await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false)).Select(l => l.Id).ToList();
var targets = new List<Sequence>(ids.Count);
foreach (var id in ids)
{
if (await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false) is { } s && s.ArchiveId == archive.Id) targets.Add(s);
if (await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false) is { } s && s.ArchiveId == archive.Id && !ArchiveIngest.AwaitsFolder(s, paths))
targets.Add(s);
}
return targets;
}
@@ -1,7 +1,9 @@
using Titano.Core.Archiving;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Jobs;
using Titano.Data.Processing;
using Titano.Data.Repositories;
namespace Titano.Data.Archiving.Jobs;
@@ -18,9 +20,12 @@ internal static class JobArchive
/// <see cref="JobKind.Scan"/> (incremental, or full with <see cref="ScanParameters.Full"/>) and
/// <see cref="JobKind.Rebuild"/>. Checkpoint: <c>after:&lt;folder&gt;</c>, the last folder indexed. When anything was
/// indexed, the sequences without a current light analysis (adopted folders, new sidecars, photos added to a folder) are
/// queued for it, and for a background deep analysis too when <see cref="ScanParameters.DeepAnalysis"/> asks.
/// queued for it, and for a background deep analysis too when <see cref="ScanParameters.DeepAnalysis"/> asks. On the
/// server (<paramref name="imagesAfterScan"/>, <see cref="ArchiveServiceOptions.ImagesAfterScan"/>) the sequences it indexed
/// without thumbnails or proxies are queued for them too, one pass per sequence as after an upload. Sequences waiting for
/// their first frame (<see cref="ArchiveIndexer.AwaitingFolderAsync"/>) have nothing to analyse or draw yet and are left out.
/// </summary>
internal sealed class ScanJobHandler(TitanoDatabase db, ArchiveIndexer indexer, JobKind kind) : IJobHandler
internal sealed class ScanJobHandler(TitanoDatabase db, ArchiveIndexer indexer, JobKind kind, bool imagesAfterScan = false) : IJobHandler
{
private const string Prefix = "after:";
@@ -42,9 +47,51 @@ internal sealed class ScanJobHandler(TitanoDatabase db, ArchiveIndexer indexer,
// A resumed scan may have indexed its folders before the interruption.
if (summary.Indexed > 0 || resume is not null)
{
var unanalysed = await AnalysisFollowUp.UnanalysedAsync(db, archive.Id, context.Cancellation).ConfigureAwait(false);
var awaiting = await indexer.AwaitingFolderAsync(new ArchivePaths(archive.RootPath),
await db.Sequences.ListLocationsAsync(archive.Id, context.Cancellation).ConfigureAwait(false), context.Cancellation).ConfigureAwait(false);
var unanalysed = (await AnalysisFollowUp.UnanalysedAsync(db, archive.Id, context.Cancellation).ConfigureAwait(false))
.Where(id => !awaiting.Contains(id)).ToList();
await AnalysisFollowUp.QueueAsync(db, context, unanalysed).ConfigureAwait(false);
if (kind == JobKind.Scan && parameters.DeepAnalysis) await AnalysisFollowUp.QueueDeepAsync(db, context, unanalysed).ConfigureAwait(false);
if (imagesAfterScan)
{
var resumed = resume is not null;
await QueueImagesAsync(context, archive, JobKind.Thumbs, SequenceStatus.ThumbsBuilt, summary.Sequences, resumed, awaiting).ConfigureAwait(false);
// An archive whose policy makes no proxies would get a pass that does nothing.
if (archive.ProxyLongEdge != ProxyPolicy.Disabled)
await QueueImagesAsync(context, archive, JobKind.Proxies, SequenceStatus.ProxiesBuilt, summary.Sequences, resumed, awaiting).ConfigureAwait(false);
}
}
}
/// <summary>
/// Queues a background <paramref name="pass"/> (thumbnails or proxies) of each sequence this run indexed whose
/// <paramref name="built"/> flag is clear: new and adopted sequences, and those whose photos changed (the scan clears the
/// flag then). After a resumed run, which may have indexed sequences before the interruption, of every sequence of the
/// archive without the flag but those in <paramref name="awaiting"/>. One pass per sequence, as after an upload: a folder
/// renamed, deleted or damaged before its pass runs fails that pass alone, and the other sequences still get their
/// images. A sequence whose own pass of that kind is already queued is left out.
/// </summary>
private async Task QueueImagesAsync(JobContext context, Archive archive, JobKind pass, SequenceStatus built, IReadOnlyList<Guid> indexed,
bool resumed, IReadOnlySet<Guid> awaiting)
{
var missing = new Dictionary<Guid, Sequence>();
var query = new SequenceQuery { ArchiveId = archive.Id, WithoutStatus = built, Sort = SequenceSort.Oldest, Limit = 1000 };
do
{
var page = await db.Sequences.ListAsync(query, context.Cancellation).ConfigureAwait(false);
foreach (var sequence in page.Items) missing[sequence.Id] = sequence;
query = query with { Cursor = page.NextCursor };
}
while (query.Cursor is not null);
var targets = resumed ? missing.Values.ToList() : indexed.Distinct().Where(missing.ContainsKey).Select(id => missing[id]).ToList();
foreach (var sequence in targets)
{
if (awaiting.Contains(sequence.Id) || await db.Jobs.IsQueuedAsync(pass, sequence.Id, context.Cancellation).ConfigureAwait(false)) continue;
var request = pass == JobKind.Thumbs
? ArchiveJobs.Thumbnails([sequence.Id], sequence.DisplayTitle)
: ArchiveJobs.Proxies([sequence.Id], sequence.DisplayTitle);
await context.EnqueueFollowUpAsync(request.Kind, request.Targets, request.Label, request.ParametersJson, request.Priority).ConfigureAwait(false);
}
}
}
+43 -3
View File
@@ -2,10 +2,14 @@ using Titano.Core.Archiving;
using Titano.Core.Diagnostics;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Repositories;
namespace Titano.Data.Archiving;
/// <summary>The plan no longer matches the archive (a sequence moved or vanished since it was made).</summary>
/// <summary>
/// The plan no longer matches the archive: a sequence moved or vanished since it was made, or one was indexed inside a
/// folder the plan moves.
/// </summary>
public sealed class StaleRenamePlanException(Guid sequenceId)
: InvalidOperationException(Loc.L("Archive.Error.StalePlan"))
{
@@ -62,7 +66,8 @@ public sealed class MassRenamer
private static RenameSubject Subject(Sequence s) => new(s.Id, s.Folder, s.Title, s.FirstCapture, s.CameraMake, s.CameraModel, s.Lens);
/// <summary>Applies a plan; each completed step is journaled before the next starts.</summary>
/// <exception cref="StaleRenamePlanException">A sequence is no longer where the plan expects it; nothing of that step was changed.</exception>
/// <exception cref="StaleRenamePlanException">A sequence is no longer where the plan expects it, or a folder that moves holds
/// a sequence the plan does not take along; nothing of that step was changed.</exception>
public Task ApplyAsync(Archive archive, RenamePlan plan, IProgress<int>? progress = null, CancellationToken cancellationToken = default) =>
RunAsync(archive, new RenameJournal(plan, []), resuming: false, progress, cancellationToken);
@@ -85,12 +90,17 @@ public sealed class MassRenamer
var completed = journal.Completed.ToHashSet();
if (!resuming)
{
// A plan made before another change is refused before anything moves.
// A plan made before another change is refused before anything moves: a sequence moved, or one was indexed
// inside a folder that moves (a scan adopted a subfolder copied in since the dry run).
foreach (var step in journal.Plan.Steps)
{
if ((await _db.Sequences.GetAsync(step.SequenceId, cancellationToken).ConfigureAwait(false))?.Folder != step.FromFolder)
throw new StaleRenamePlanException(step.SequenceId);
}
var planned = journal.Plan.Steps.Select(s => s.SequenceId).ToHashSet();
if (await StrangerInsideAsync(archive, journal.Plan.Steps.Where(s => s.MovesFolder && !s.Carried), planned, cancellationToken)
.ConfigureAwait(false) is { } stranger)
throw new StaleRenamePlanException(stranger.Id);
}
RenameJournal.Write(paths, journal);
var steps = journal.Plan.Steps;
@@ -147,6 +157,7 @@ public sealed class MassRenamer
{
if (await StandsAsync(c, resuming, cancellationToken).ConfigureAwait(false)) follow.Add(c);
}
if (!await CarriesAllInsideAsync(archive, paths, step, carried, resuming, cancellationToken).ConfigureAwait(false)) return;
RenamePlanner.MoveFolder(paths, step);
await RecordAsync(archive, paths, step, cancellationToken).ConfigureAwait(false);
// Moved with their carrier: only their sidecars and index rows follow.
@@ -170,6 +181,35 @@ public sealed class MassRenamer
return false;
}
/// <summary>
/// Whether every sequence the index has inside the folder of a moving step moves with it as planned, checked again under
/// the step's leases just before the folder moves. One indexed there since the dry run would move without its index
/// row following, and was never shown in the plan: the step is refused, or dropped when resuming a folder that has not
/// moved yet (a folder moved before the interruption goes on; the next scan indexes the newcomer where it ended up).
/// </summary>
/// <exception cref="StaleRenamePlanException">Not resuming, and such a sequence is inside the folder.</exception>
private async Task<bool> CarriesAllInsideAsync(Archive archive, ArchivePaths paths, RenameStep step, IReadOnlyList<RenameStep> carried, bool resuming,
CancellationToken cancellationToken)
{
if (!step.MovesFolder) return true;
var taken = carried.Select(c => c.SequenceId).ToHashSet();
if (await StrangerInsideAsync(archive, [step], taken, cancellationToken).ConfigureAwait(false) is not { } stranger) return true;
if (!resuming) throw new StaleRenamePlanException(stranger.Id);
if (!Directory.Exists(paths.Sequence(step.FromFolder).Folder)) return true;
Log.Warning("archive", $"Rename of {step.FromFolder} dropped: {stranger.Folder} was indexed inside it after the plan was made");
return false;
}
/// <summary>A sequence the index has inside the folder of one of <paramref name="moving"/> that is not in <paramref name="taken"/>; null when there is none.</summary>
private async Task<SequenceLocation?> StrangerInsideAsync(Archive archive, IEnumerable<RenameStep> moving, IReadOnlySet<Guid> taken,
CancellationToken cancellationToken)
{
var folders = moving.Select(s => s.FromFolder).ToList();
if (folders.Count == 0) return null;
return (await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false))
.FirstOrDefault(l => !taken.Contains(l.Id) && folders.Any(f => RenamePlanner.IsInside(f, l.Folder)));
}
/// <summary>Sidecar title, then index (folder, frames' paths); each part is idempotent so a resumed step can repeat it.</summary>
private async Task RecordAsync(Archive archive, ArchivePaths paths, RenameStep step, CancellationToken cancellationToken)
{
@@ -17,6 +17,13 @@ public sealed record ArchiveServiceOptions
public TimeSpan LeaseWait { get; init; } = TimeSpan.FromMinutes(2);
public TimeSpan LeaseDuration { get; init; } = LeaseFile.DefaultDuration;
/// <summary>
/// Scans queue thumbnails and proxies of the sequences they index without them, as an upload does (the server, whose
/// clients load both from it: BRIEF §4.3, §14.4). The desktop decodes its thumbnails on demand and makes proxies when
/// asked.
/// </summary>
public bool ImagesAfterScan { get; init; }
}
/// <summary>
+14 -1
View File
@@ -67,7 +67,9 @@ public static class ArchiveEndpoints
/// Writes the name, templates and proxy policy into the archive's <c>archive.json</c> through the archive engine (so a
/// restart and the proxy jobs see them), then into the index. 503 <c>archive_offline</c> while the archive cannot be
/// written; 409 <c>archive_busy</c> while another process holds its settings; 409 <c>archive_manifest</c> when its
/// archive.json is damaged, written by a newer Titano or of another archive (nothing is written).
/// archive.json is damaged, written by a newer Titano or of another archive; 409 <c>archive_policy</c> when it holds a
/// thumbnail or proxy setting this API cannot set and Titano cannot use (details: <c>thumbnailPolicy</c> or
/// <c>proxyPolicy</c>, the property to correct in the file). Nothing is written.
/// </summary>
private static async Task<IResult> UpdateAsync(HttpContext context, Caller caller, ArchiveSettingsDto request, TitanoDatabase db, ServerArchive archive,
[FromServices] ArchiveService? archives, AuditLog audit)
@@ -99,6 +101,17 @@ public static class ArchiveEndpoints
{
return ApiErrors.Conflict(context, "archive_manifest");
}
catch (ArgumentException ex)
{
// The request's fields were checked above: what fails is a value read from archive.json (a thumbnail size, or a
// proxy quality, written by hand or by another tool), which the admin corrects in the file, not in this form.
return ApiErrors.Conflict(context, "archive_policy", ex.ParamName switch
{
"thumbnailLongEdge" => "thumbnailPolicy",
"proxyLongEdge" => "proxyPolicy",
var other => other,
});
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// ArchiveOfflineException included: nothing was written.
+18 -3
View File
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Http.Metadata;
using Titano.Core.Model;
using Titano.Protocol;
using Titano.Server.Security;
@@ -14,11 +15,14 @@ public static class UploadEndpoints
{
private const int MaxCardName = 200;
/// <summary>The largest chunk size a server can be set to (<see cref="Configuration.ServerOptions.ChunkSizeBytes"/>).</summary>
private const long MaxChunkBytes = 64L * 1024 * 1024;
public static void Map(RouteGroupBuilder api)
{
var uploads = api.MapGroup("/uploads").Requires(Permissions.Upload | Permissions.RenderNode);
uploads.MapPost("", StartAsync).Requires(Permissions.Upload);
uploads.MapPut("/{id:guid}/chunks/{index:int}", ChunkAsync);
uploads.MapPut("/{id:guid}/chunks/{index:int}", ChunkAsync).WithMetadata(new ChunkBodyLimit());
uploads.MapGet("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
(await service.GetStateAsync(caller, id, context.RequestAborted)).ToResult(context, s => Results.Json(s, ProtocolJson.Default.UploadState)));
uploads.MapDelete("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
@@ -53,12 +57,23 @@ public static class UploadEndpoints
}
/// <summary>
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. The service
/// raises it to the chunk size of this upload (fixed when it started) before it reads the body.
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. Routing raises
/// it for this route (<see cref="ChunkBodyLimit"/>), and the service keeps it at least at the chunk size of this upload
/// (fixed when it started) and of this server, so a body left unread by any answer can still be drained.
/// </summary>
private static async Task<IResult> ChunkAsync(HttpContext context, Caller caller, Guid id, int index, UploadService service)
{
var outcome = await service.ReceiveChunkAsync(caller, id, index, context.Request, context.RequestAborted);
return outcome.ToResult(context, r => Results.Json(r, ProtocolJson.Default.ChunkReceived));
}
/// <summary>
/// The chunk route's body limit, applied by routing as it selects the endpoint, before authentication can answer 401
/// or 403 with the chunk unread. Kestrel drains an unread body only within the limit; above it, it closes the connection
/// and the client sees a network error instead of the answer (an Uploader would retry rather than ask to sign in).
/// </summary>
private sealed class ChunkBodyLimit : IRequestSizeLimitMetadata
{
public long? MaxRequestBodySize => MaxChunkBytes;
}
}
+4 -3
View File
@@ -8,8 +8,9 @@ namespace Titano.Server.Storage;
/// <summary>
/// The archive engine behind the server (BRIEF §7, §14.4): ingest of verified uploads, lookup of proxies and
/// thumbnails, its job handlers (scan, rebuild, verify, import, thumbnails, proxies, maintenance) and the periodic
/// incremental scan (<see cref="ArchivePolling"/>). Decoding is managed only (BRIEF §4.2). The daily backup of an
/// SQLite index runs with it (<see cref="DatabaseBackupService"/>).
/// incremental scan (<see cref="ArchivePolling"/>); a scan queues thumbnails and proxies of what it indexes, as an upload
/// does. Decoding is managed only (BRIEF §4.2). The daily backup of an SQLite index runs with it
/// (<see cref="DatabaseBackupService"/>).
/// </summary>
public static class ArchiveModule
{
@@ -19,7 +20,7 @@ public static class ArchiveModule
public static void Register(IServiceCollection services)
{
services.AddSingleton(sp => new ArchiveService(sp.GetRequiredService<TitanoDatabase>(),
new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = Owner }));
new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = Owner, ImagesAfterScan = true }));
services.AddSingleton<IArchiveIngest>(sp => sp.GetRequiredService<ArchiveService>().Ingest);
services.AddSingleton<IJobHandlerProvider>(sp => sp.GetRequiredService<ArchiveService>());
services.AddSingleton<IFrameAssetLocator, ArchiveAssetLocator>();
+5 -2
View File
@@ -101,14 +101,17 @@ public sealed class UploadService(
public async Task<Outcome<ChunkReceived>> ReceiveChunkAsync(Caller caller, Guid id, int index, HttpRequest request, CancellationToken cancellationToken)
{
var upload = await db.Uploads.GetAsync(id, cancellationToken);
// A body left unread by an answer below is drained after it: one of a chunk's size must not count as too large, or
// Kestrel would close the connection while the client still sends it and the client would never see the answer. The
// upload keeps the chunk size it started with: a smaller setting since a restart must not refuse its chunks (413).
if (request.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false, MaxRequestBodySize: { } current } limit)
limit.MaxRequestBodySize = Math.Max(current, Math.Max(upload?.ChunkSize ?? 0, options.ChunkSizeBytes));
if (upload is null || upload.UserId != caller.Id) return Fail<ChunkReceived>(404, "not_found", "upload");
if (upload.State != UploadState.Receiving) return Fail<ChunkReceived>(409, "upload_state");
if (index < 0 || index >= upload.ChunkCount) return Fail<ChunkReceived>(400, "chunk_out_of_range");
var expected = Math.Min(upload.ChunkSize, upload.Size - (long)index * upload.ChunkSize);
if (request.ContentLength is { } announced && announced != expected) return Fail<ChunkReceived>(400, "chunk_size", expected.ToString(CultureInfo.InvariantCulture));
if (upload.HasChunk(index)) return new ChunkReceived(index, UploadRules.ReceivedChunks(upload));
// The upload keeps the chunk size it started with: a smaller setting since a restart must not refuse its chunks (413).
if (request.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = upload.ChunkSize;
if (_chunksInFlight.AddOrUpdate(caller.Id, 1, (_, n) => n + 1) > options.ConcurrentChunksPerUser)
{