feat(editor): open server sequences with proxies, hero frame to the index, export sync and saved copies
Server sequences that are not in the local index open through a RemoteSequenceSource: proxies from the cache, project from the server's settings with their version (a 409 merges again), analysis report from the server, analyses and renders posted as server jobs (one per preset) and followed through the server's events. Set hero now reaches the index (SequenceEdit.HeroFrameId) so the Library card shows it. The Export tab gains the Sync option (remembered in the project) and restores "Save both" copies through undo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
36 files changed
+1307
-116
No files matched your search
@@ -20,33 +20,56 @@ namespace Titano.App.Editor;
|
||||
|
||||
/// <summary>
|
||||
/// A sequence of an archive on this PC: rows from the index, originals decoded by the client's decoder chain, the
|
||||
/// project in <c>.titano/project.json</c> mirrored in the index's settings column, jobs on this PC's engine. Project
|
||||
/// writes follow the archive engine's protocol: under the sequence's lease file (so the server and the job engine wait
|
||||
/// for it), file first (atomically), then the index with its settings version as an optimistic check.
|
||||
/// project in <c>.titano/project.json</c> mirrored in the index's settings column (with its hero frame, which the
|
||||
/// Library shows), jobs on this PC's engine. Project writes follow the archive engine's protocol: under the sequence's
|
||||
/// lease file (so the server and the job engine wait for it), file first (atomically), then the index with its settings
|
||||
/// version as an optimistic check.
|
||||
/// </summary>
|
||||
public sealed class LocalSequenceSource(TitanoDatabase db, Guid sequenceId, ImageDecoder decoder, JobEngine? engine, ArchiveServiceOptions? lease = null)
|
||||
: ISequenceSource
|
||||
public sealed class LocalSequenceSource : ISequenceSource
|
||||
{
|
||||
private const int PageSize = 1000;
|
||||
private const int SaveAttempts = 3;
|
||||
private const int JobsListed = 200;
|
||||
|
||||
private readonly ArchiveServiceOptions _lease = lease ?? new ArchiveServiceOptions();
|
||||
private readonly TitanoDatabase _db;
|
||||
private readonly Guid _sequenceId;
|
||||
private readonly ImageDecoder _decoder;
|
||||
private readonly JobEngine? _engine;
|
||||
private readonly ArchiveServiceOptions _lease;
|
||||
private string?[] _files = [];
|
||||
private Guid[] _frameIds = [];
|
||||
private ArchivePaths? _paths;
|
||||
private SequencePaths? _folder;
|
||||
|
||||
public LocalSequenceSource(TitanoDatabase db, Guid sequenceId, ImageDecoder decoder, JobEngine? engine, ArchiveServiceOptions? lease = null)
|
||||
{
|
||||
_db = db;
|
||||
_sequenceId = sequenceId;
|
||||
_decoder = decoder;
|
||||
_engine = engine;
|
||||
_lease = lease ?? new ArchiveServiceOptions();
|
||||
if (engine is not null) engine.JobChanged += OnJobChanged;
|
||||
}
|
||||
|
||||
public bool IsRemote => false;
|
||||
|
||||
public event Action<Job>? JobChanged;
|
||||
|
||||
/// <summary>A save changed the frame the Library shows for the sequence: the stored row (any thread).</summary>
|
||||
public event Action<Sequence>? HeroChanged;
|
||||
|
||||
public async Task<SequenceContent> LoadAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var sequence = await db.Sequences.GetAsync(sequenceId, cancellationToken) ?? throw Missing();
|
||||
var archive = await db.Archives.GetAsync(sequence.ArchiveId, cancellationToken) ?? throw Missing();
|
||||
var sequence = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing();
|
||||
var archive = await _db.Archives.GetAsync(sequence.ArchiveId, cancellationToken) ?? throw Missing();
|
||||
var frames = new List<Frame>();
|
||||
while (true)
|
||||
{
|
||||
var page = await db.Frames.ListBySequenceAsync(sequenceId, frames.Count > 0 ? frames[^1].Index : -1, PageSize, cancellationToken);
|
||||
var page = await _db.Frames.ListBySequenceAsync(_sequenceId, frames.Count > 0 ? frames[^1].Index : -1, PageSize, cancellationToken);
|
||||
frames.AddRange(page);
|
||||
if (page.Count < PageSize) break;
|
||||
}
|
||||
var renders = await db.Renders.ListBySequenceAsync(sequenceId, cancellationToken);
|
||||
var renders = await _db.Renders.ListBySequenceAsync(_sequenceId, cancellationToken);
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
@@ -56,10 +79,10 @@ public sealed class LocalSequenceSource(TitanoDatabase db, Guid sequenceId, Imag
|
||||
{
|
||||
if ((frames[i].Flags & FrameFlags.Missing) == 0) files[i] = paths.FromRelative(frames[i].RelativePath);
|
||||
}
|
||||
(_paths, _folder, _files) = (paths, folder, files);
|
||||
(_paths, _folder, _files, _frameIds) = (paths, folder, files, [.. frames.Select(f => f.Id)]);
|
||||
var context = SequenceContext.FromFrames(frames);
|
||||
return new SequenceContent(sequence, frames, ReadProject(sequence, folder), LightAnalyzer.Analyze(frames), ReadReport(folder, frames.Count),
|
||||
context.SourceWidth, context.SourceHeight, folder.Exports, [.. renders.OrderByDescending(r => r.Created)]);
|
||||
context.SourceWidth, context.SourceHeight, folder.Exports, [.. renders.OrderByDescending(r => r.Created)], SavedCopies.List(folder.MetaDirectory));
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -72,7 +95,7 @@ public sealed class LocalSequenceSource(TitanoDatabase db, Guid sequenceId, Imag
|
||||
{
|
||||
// Thumbnails take an embedded preview when it is large enough (no RAW development for a filmstrip cell).
|
||||
var request = new DecodeRequest(file, maxLongEdge, purpose, ForcePreview: purpose == DecodePurpose.Thumbnail);
|
||||
return decoder.Decode(request, cancellationToken)?.Pixels;
|
||||
return _decoder.Decode(request, cancellationToken)?.Pixels;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
@@ -86,25 +109,27 @@ public sealed class LocalSequenceSource(TitanoDatabase db, Guid sequenceId, Imag
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(project);
|
||||
ArgumentNullException.ThrowIfNull(basis);
|
||||
var (paths, folder) = (_paths, _folder);
|
||||
var (paths, folder, frameIds) = (_paths, _folder, _frameIds);
|
||||
if (paths is null || folder is null) throw new InvalidOperationException("LoadAsync first.");
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
ArchiveVolume.EnsureWritable(paths);
|
||||
using var held = await LeaseFile.AcquireAsync(paths.LocksDirectory, $"seq-{sequenceId:N}", _lease.Owner, _lease.LeaseWait, _lease.LeaseDuration,
|
||||
using var held = await LeaseFile.AcquireAsync(paths.LocksDirectory, $"seq-{_sequenceId:N}", _lease.Owner, _lease.LeaseWait, _lease.LeaseDuration,
|
||||
cancellationToken);
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
var row = await db.Sequences.GetAsync(sequenceId, cancellationToken) ?? throw Missing();
|
||||
var row = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing();
|
||||
var stored = ReadProject(row, folder);
|
||||
var merged = ProjectMerge.SameValues(stored, basis) ? project : ProjectMerge.Merge(basis, project, stored);
|
||||
var json = merged.ToJson();
|
||||
if (json == stored.ToJson()) return merged;
|
||||
AtomicFile.WriteAllText(folder.ProjectPath, json);
|
||||
var result = await db.Sequences.EditAsync(sequenceId, new SequenceEdit { SettingsJson = json }, row.SettingsVersion, cancellationToken);
|
||||
var edit = new SequenceEdit { SettingsJson = json, HeroFrameId = merged.HeroFrame == stored.HeroFrame ? null : HeroFrameId(merged, frameIds) };
|
||||
var result = await _db.Sequences.EditAsync(_sequenceId, edit, row.SettingsVersion, cancellationToken);
|
||||
switch (result.Outcome)
|
||||
{
|
||||
case SequenceEditOutcome.Updated:
|
||||
if (result.Sequence is { } updated && updated.HeroFrameId != row.HeroFrameId) HeroChanged?.Invoke(updated);
|
||||
return merged;
|
||||
case SequenceEditOutcome.NotFound:
|
||||
throw Missing();
|
||||
@@ -118,12 +143,19 @@ public sealed class LocalSequenceSource(TitanoDatabase db, Guid sequenceId, Imag
|
||||
|
||||
public async Task<Guid> SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (engine is null) throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine"));
|
||||
var job = await engine.EnqueueAsync(request, cancellationToken: cancellationToken);
|
||||
if (_engine is null) throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine"));
|
||||
var job = await _engine.EnqueueAsync(request, cancellationToken: cancellationToken);
|
||||
return job.Id;
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
public async Task<IReadOnlyList<Job>> ListJobsAsync(CancellationToken cancellationToken) =>
|
||||
(await _db.Jobs.ListAsync(null, JobsListed, null, cancellationToken)).Items;
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
if (_engine is not null) _engine.JobChanged -= OnJobChanged;
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>The settings in force: the index copy, else <c>project.json</c>, else defaults.</summary>
|
||||
/// <exception cref="InvalidDataException">The settings are not a project or come from a newer Titano.</exception>
|
||||
@@ -141,6 +173,10 @@ public sealed class LocalSequenceSource(TitanoDatabase db, Guid sequenceId, Imag
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The frame of the project's hero, or <see cref="Guid.Empty"/> (the first frame shows) without one.</summary>
|
||||
private static Guid HeroFrameId(ProjectSettings project, Guid[] frameIds) =>
|
||||
project.HeroFrame is { } hero && (uint)hero < (uint)frameIds.Length ? frameIds[hero] : Guid.Empty;
|
||||
|
||||
/// <summary>The saved deep report when it describes the current frames.</summary>
|
||||
private static DeepAnalysisReport? ReadReport(SequencePaths folder, int frameCount)
|
||||
{
|
||||
@@ -157,5 +193,7 @@ public sealed class LocalSequenceSource(TitanoDatabase db, Guid sequenceId, Imag
|
||||
}
|
||||
}
|
||||
|
||||
private void OnJobChanged(Job job) => JobChanged?.Invoke(job);
|
||||
|
||||
private static InvalidOperationException Missing() => new(Loc.L("Editor.Error.Missing"));
|
||||
}
|
||||
Reference in new issue
Block a user