fix(archive): keep project frame positions on their frames after inserts
The project names frames by position (exclusions, hero, virtual-camera keyframes), and an upload, import, scan or sync download of earlier photos merges them by capture time and renumbers the frames after them; nothing moved the project, so a render excluded the wrong photo and the project's hero no longer matched the Library's. project.json keeps its format: when the index takes a sidecar in which frames were inserted before indexed ones, the index's copy of the project has its positions moved from the indexed order to the new one in the same transaction as the frame rows, and project.json mirrors it unless another writer changed it meanwhile. The JSON is edited in place, so members a newer Titano wrote survive; only an insertion is followed (if an indexed frame is gone, the positions stay). The settings version stays, so a synced copy receiving the same frames makes the same move (CD-2). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
7 files changed
+252
-3
No files matched your search
@@ -0,0 +1,75 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Core.IO;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Project;
|
||||
|
||||
namespace Titano.Data.Archiving;
|
||||
|
||||
/// <summary>
|
||||
/// Keeps a project's frame positions (exclusions, hero, virtual-camera keyframes; BRIEF §5, §7) on their frames when the
|
||||
/// index takes a sidecar in which frames were inserted before others: an upload or import of earlier photos, photos a
|
||||
/// scan found, a sync download, a journalled batch completed after a crash. Positions name the frames in the indexed
|
||||
/// order (what editors, renders and the API read), so the old order comes from the index rows. Only an insertion is
|
||||
/// followed: when an indexed frame is no longer in the sidecar, positions cannot be mapped and stay as they are. The move
|
||||
/// is part of the frames change, not a settings edit, so the settings version stays and a synced copy receiving the
|
||||
/// same frames makes the same move.
|
||||
/// </summary>
|
||||
internal static class ProjectPositions
|
||||
{
|
||||
/// <summary>
|
||||
/// The index's copy of the project (the one in force) and the same project with its positions moved to the order of
|
||||
/// <paramref name="manifest"/>; null when nothing moves. A <c>project.json</c> the index has no copy of was written by
|
||||
/// another process against its own frames, so it is left to that writer.
|
||||
/// </summary>
|
||||
public static async Task<(string Before, string Moved)?> MoveAsync(TitanoRepositories db, Sequence indexed, SequenceManifest manifest,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// The sidecar's frames are the ones indexed: nothing was inserted.
|
||||
if (indexed.FramesVersion == manifest.Versions.Frames) return null;
|
||||
if (indexed.SettingsJson is not { } project) return null;
|
||||
var rows = await db.Frames.ListPositionsAsync(indexed.Id, cancellationToken).ConfigureAwait(false);
|
||||
var now = new Dictionary<Guid, int>(manifest.Frames.Count);
|
||||
foreach (var frame in manifest.Frames) now[frame.Id] = frame.I;
|
||||
var moves = new Dictionary<int, int>();
|
||||
foreach (var row in rows)
|
||||
{
|
||||
if (!now.TryGetValue(row.FrameId, out var index)) return null;
|
||||
if (index != row.Index) moves[row.Index] = index;
|
||||
}
|
||||
return ProjectSettings.MoveFramePositions(project, moves) is { } moved ? (project, moved) : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mirrors moved positions into <c>project.json</c> once the index holds them, unless the file was written since
|
||||
/// (it then belongs to that writer, whose next save mirrors the index again). A failed write leaves the index right.
|
||||
/// </summary>
|
||||
public static void WriteProject(SequencePaths sequence, string before, string moved)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(sequence.ProjectPath) && !SameJson(File.ReadAllText(sequence.ProjectPath), before)) return;
|
||||
Directory.CreateDirectory(sequence.MetaDirectory);
|
||||
AtomicFile.WriteAllText(sequence.ProjectPath, moved);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("archive", $"Project of {sequence.RelativeFolder} keeps its old frame positions on disk", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Same JSON content whatever the layout (an index may store it normalised).</summary>
|
||||
private static bool SameJson(string a, string b)
|
||||
{
|
||||
try
|
||||
{
|
||||
return JsonNode.DeepEquals(JsonNode.Parse(a), JsonNode.Parse(b));
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user