Owner review 2, R8, phase 5b (client part), ported onto the final phase 5a.
A Chronos Server with the "projects" capability is now the home of
workspace projects. A server sequence is edited in the workspace like a
local one, not inside the library, and its versions sync with the server
"like git".
- Data: ServerProjectHome is an IProjectHome over ChronosClient.
- These are HomeUnavailableException, so the workspace shows Offline: a
server out of reach, an expired sign-in, a 503 (lease, offline archive,
projects_unreadable), a 409 folder_in_use, and a server without projects.
- 404 sequence/project/version and refused names are ProjectException;
other refusals keep their meaning.
- A deleted project's head is 404 project, so final 5a's HeadAsync NotFound
rule reads it as Gone. The original 5b's IsLiveAsync and
WorkspaceStatus.Gone are dropped. IProjectHome gains rename and delete.
- Data: WorkspaceJobs.CopyFromServer downloads into the workspace, through
the desktop's session, the proxies of one frames version (or the
thumbnails standing in for them), with the aliases of the server's
conversions. It is rebuilt on final 5a's copy handler:
- the copy:frame:fv:folder checkpoint, EnsureSpace (from the server's proxy
sizes), WritingMedia, and the cancelled-copy clean-up;
- the editor's current file is never overwritten;
- each file is hashed as it arrives and checked on disk, four at a time.
It never writes this PC's archive. Originals are still refused until 5c.
- Data: SyncAgent passes on the server's projectChanged events, and after a
lost event stream it reports that changes may have been missed.
- App: SyncHost gives the server home over one session client. The Server
screen offers Open in workspace, disabled with its reason on older
servers, whose sequences keep opening with proxies.
- App: Edit opens or checks out server projects with final 5a's refusals:
unreadable copies, a disabled damaged or newer Main, a refused folder. The
"sequence left the index" refusal now applies only to projects of this
PC's archive.
- App: the picker and the chip follow projectChanged (Server newer, Not on
server). Every state, conflict, delete, remove and failure text uses its
".Server" form when one exists (WorkspaceText.ForHome). That includes 5a's
newer-format conflict, the save conflict, the taken-values toast, Gone and
version-too-large.
- App: a render of a server project first offers to send what is not on the
server, then renders the version the server holds. Offline, Gone, Damaged
and missing media hold the render. Analyses and renders run as server jobs
(ServerJobs, shared with RemoteSequenceSource).
- Pruning keeps final 5a's rule: a version is pruned only after HoldsAsync
reads it back.
- Strings in en and it; ARCHITECTURE, DECISIONS and CHANGELOG.
Tests:
- ServerProjectHome error mapping, using a fake handler.
- Against the in-process server:
- two workspaces on one project: fast-forward, replay and merge, keep mine,
take server, save both, restore;
- the copy job verifying, resuming and following new frames;
- a project deleted on the server (Gone from its head; a push sends
nothing);
- an offline server.
- SyncAgent project events.
- App tests for the host, the project menu and renders of a server home.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
348 lines
17 KiB
C#
348 lines
17 KiB
C#
using System.Net;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Security.Cryptography;
|
|
using System.Threading.Channels;
|
|
using Chronos.Core.Metadata;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data.Sync;
|
|
using Chronos.Protocol;
|
|
using Chronos.Protocol.Client;
|
|
|
|
namespace Chronos.Data.Tests.Sync;
|
|
|
|
/// <summary>An in-memory Chronos Server for the sync engine: sequences with versions, originals, proxies, thumbnails and events.</summary>
|
|
internal sealed class FakeSyncRemote : ISyncRemote
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private readonly Channel<ServerEvent> _events = Channel.CreateUnbounded<ServerEvent>();
|
|
private readonly Dictionary<string, FakeSequence> _sessions = [];
|
|
|
|
public string Server => "https://nas.test:8443";
|
|
|
|
public Dictionary<Guid, FakeSequence> Sequences { get; } = [];
|
|
|
|
/// <summary>Manifest requests so far (comparisons skip them while frame counters stand still).</summary>
|
|
public int ManifestCalls { get; private set; }
|
|
|
|
/// <summary>Listings still to fail with a network error.</summary>
|
|
public int FailingListings { get; set; }
|
|
|
|
/// <summary>Event streams still to be lost right after they opened.</summary>
|
|
public int FailingEvents { get; set; }
|
|
|
|
/// <summary>The next original download of this frame returns damaged bytes.</summary>
|
|
public Guid? CorruptOnce { get; set; }
|
|
|
|
/// <summary>Sequences the listing skips (a page boundary moved while it was read); they are still there.</summary>
|
|
public HashSet<Guid> Unlisted { get; } = [];
|
|
|
|
/// <summary>Sequences whose manifest the server refuses (403), as for an account that lost a right.</summary>
|
|
public HashSet<Guid> Refused { get; } = [];
|
|
|
|
/// <summary>Proxy and thumbnail downloads fail with a server error.</summary>
|
|
public bool FailAssets { get; set; }
|
|
|
|
/// <summary>
|
|
/// Assets (frame id, kind) the sequence's flags offer that the server has not drawn yet: frames added since its last
|
|
/// thumbnail or proxy pass answer 404 until that pass ends, as the real endpoints do.
|
|
/// </summary>
|
|
public HashSet<(Guid Frame, string Kind)> Undrawn { get; } = [];
|
|
|
|
/// <summary>Proxies clients sent, by frame id (for sequences the server cannot decode).</summary>
|
|
public Dictionary<Guid, byte[]> SentProxies { get; } = [];
|
|
|
|
/// <summary>Edits the server accepted, in order.</summary>
|
|
public List<UpdateSequenceRequest> Updates { get; } = [];
|
|
|
|
public FakeSequence Add(string? title, IEnumerable<byte[]> originals, string? settingsJson = null,
|
|
SequenceStatus status = SequenceStatus.ProxiesBuilt | SequenceStatus.ThumbsBuilt)
|
|
{
|
|
var sequence = new FakeSequence { Id = Guid.CreateVersion7(), Title = title, SettingsJson = settingsJson, Status = status };
|
|
foreach (var bytes in originals) sequence.AddFrame(bytes);
|
|
lock (_gate) Sequences[sequence.Id] = sequence;
|
|
return sequence;
|
|
}
|
|
|
|
/// <summary>Someone else edits the settings on the server.</summary>
|
|
public void EditSettings(Guid id, string settingsJson)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
var s = Sequences[id];
|
|
s.SettingsJson = settingsJson;
|
|
s.SettingsVersion++;
|
|
s.UpdatedAt = DateTimeOffset.UtcNow;
|
|
}
|
|
}
|
|
|
|
public void Publish(ServerEvent e) => _events.Writer.TryWrite(e);
|
|
|
|
public Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (FailingListings > 0)
|
|
{
|
|
FailingListings--;
|
|
throw new HttpRequestException("connection refused");
|
|
}
|
|
return Task.FromResult<IReadOnlyList<SequenceDto>>([.. Sequences.Values.Where(s => !Unlisted.Contains(s.Id)).Select(s => s.Dto())]);
|
|
}
|
|
}
|
|
|
|
/// <summary>The next request for this sequence times out (HttpClient reports a timeout as a cancellation).</summary>
|
|
public Guid? TimeoutOnce { get; set; }
|
|
|
|
public Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (TimeoutOnce == id)
|
|
{
|
|
TimeoutOnce = null;
|
|
throw new TaskCanceledException("The request timed out.");
|
|
}
|
|
return Task.FromResult(Find(id).Dto());
|
|
}
|
|
}
|
|
|
|
/// <summary>Runs once, before the next manifest is answered: the archive on this PC changing while the sync compares.</summary>
|
|
public Func<Task>? OnceBeforeManifest { get; set; }
|
|
|
|
public async Task<SequenceManifest> GetManifestAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
if (OnceBeforeManifest is { } before)
|
|
{
|
|
OnceBeforeManifest = null;
|
|
await before();
|
|
}
|
|
lock (_gate)
|
|
{
|
|
ManifestCalls++;
|
|
if (Refused.Contains(id)) throw new ChronosApiException(HttpStatusCode.Forbidden, new ApiError("forbidden", "refused"));
|
|
return Find(id).Manifest(SentProxies.ContainsKey);
|
|
}
|
|
}
|
|
|
|
/// <summary>Runs once, before the next frame listing is answered (frame positions translated against the server's frames).</summary>
|
|
public Func<Task>? OnceBeforeFrames { get; set; }
|
|
|
|
public async Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
|
|
{
|
|
if (OnceBeforeFrames is { } before)
|
|
{
|
|
OnceBeforeFrames = null;
|
|
await before();
|
|
}
|
|
lock (_gate)
|
|
{
|
|
var sequence = Find(sequenceId);
|
|
return [.. sequence.Frames.Select(sequence.FrameDto)];
|
|
}
|
|
}
|
|
|
|
public Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
var s = Find(id);
|
|
if (request.ExpectedSettingsVersion is { } expected && expected != s.SettingsVersion)
|
|
throw new ChronosApiException(HttpStatusCode.Conflict, new ApiError("settings_conflict", "changed"));
|
|
if (request.ExpectedFramesVersion is { } frames && frames != s.FramesVersion)
|
|
throw new ChronosApiException(HttpStatusCode.Conflict, new ApiError("frames_conflict", "changed"));
|
|
Updates.Add(request);
|
|
if (request.Title is not null) s.Title = request.Title.Length == 0 ? null : request.Title;
|
|
if (request.Tags is not null) s.Tags = [.. request.Tags];
|
|
if (request.Rating is { } rating) s.Rating = rating;
|
|
if (request.Notes is not null) s.Notes = request.Notes.Length == 0 ? null : request.Notes;
|
|
if (request.SettingsJson is not null) s.SettingsJson = request.SettingsJson;
|
|
s.SettingsVersion++;
|
|
s.UpdatedAt = DateTimeOffset.UtcNow;
|
|
return Task.FromResult(s.Dto());
|
|
}
|
|
}
|
|
|
|
/// <summary>Like the server: content it already holds anywhere is not stored again; a session key files the first original into a new sequence.</summary>
|
|
public async Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var bytes = await File.ReadAllBytesAsync(path, cancellationToken);
|
|
if (Convert.ToHexStringLower(SHA256.HashData(bytes)) != sha256)
|
|
throw new ChronosApiException(HttpStatusCode.BadRequest, new ApiError("hash_mismatch", "hash"));
|
|
lock (_gate)
|
|
{
|
|
// Originals it never hashed are not recognised by content.
|
|
if (Sequences.Values.Where(s => !s.Unhashed).SelectMany(s => s.Frames).FirstOrDefault(f => f.Sha256 == sha256) is { } existing)
|
|
return new CompleteUploadResponse(existing.Id, sequenceId ?? Guid.Empty, -1);
|
|
FakeSequence target;
|
|
if (sequenceId is { } id) target = Find(id);
|
|
else if (!_sessions.TryGetValue(sessionKey!, out target!))
|
|
{
|
|
target = new FakeSequence { Id = Guid.CreateVersion7() };
|
|
Sequences[target.Id] = target;
|
|
_sessions[sessionKey!] = target;
|
|
}
|
|
var frame = target.AddFrame(bytes, Path.GetFileName(path), capturedAt);
|
|
return new CompleteUploadResponse(frame.Id, target.Id, target.Frames.Count - 1);
|
|
}
|
|
}
|
|
|
|
public async Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken)
|
|
{
|
|
byte[] bytes;
|
|
lock (_gate)
|
|
{
|
|
var (sequence, frame) = Sequences.Values.SelectMany(s => s.Frames.Select(f => (s, f))).FirstOrDefault(p => p.f.Id == frameId);
|
|
if (frame is null) throw NotFound();
|
|
bytes = kind switch
|
|
{
|
|
"original" when CorruptOnce == frameId => [.. frame.Bytes.Select(b => (byte)~b)],
|
|
"original" => frame.Bytes,
|
|
"proxy" or "thumb" when FailAssets => throw new ChronosApiException(HttpStatusCode.InternalServerError, new ApiError("internal", "failed")),
|
|
"proxy" or "thumb" when Undrawn.Contains((frameId, kind)) => throw NotFound(),
|
|
// A proxy a client or render node sent is served as soon as it is filed, like the manifest says.
|
|
"proxy" when SentProxies.TryGetValue(frameId, out var sent) => sent,
|
|
"proxy" when (sequence.Status & SequenceStatus.ProxiesBuilt) != 0 => frame.Proxy,
|
|
"thumb" when (sequence.Status & SequenceStatus.ThumbsBuilt) != 0 => frame.Proxy[..Math.Min(frame.Proxy.Length, 64)],
|
|
_ => throw NotFound(),
|
|
};
|
|
if (kind == "original" && CorruptOnce == frameId) CorruptOnce = null;
|
|
}
|
|
// Range: continue after what the destination already holds, like the real endpoint.
|
|
var offset = destination.CanSeek ? (int)Math.Min(destination.Length, bytes.Length) : 0;
|
|
if (destination.CanSeek) destination.Seek(0, SeekOrigin.End);
|
|
await destination.WriteAsync(bytes.AsMemory(offset), cancellationToken);
|
|
}
|
|
|
|
/// <summary>Like the server: only a frame whose proxy it asks for takes one, and only a JPEG.</summary>
|
|
public Task UploadProxyAsync(Guid frameId, byte[] jpeg, CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (!Sequences.Values.Any(s => s.Undecodable && s.Frames.Any(f => f.Id == frameId))) throw NotFound();
|
|
if (jpeg is not [0xFF, 0xD8, ..]) throw new ChronosApiException(HttpStatusCode.UnprocessableEntity, new ApiError("invalid_proxy", "not a JPEG"));
|
|
SentProxies[frameId] = jpeg;
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken)
|
|
{
|
|
FakeRender render;
|
|
lock (_gate) render = Sequences.Values.SelectMany(s => s.Renders).FirstOrDefault(r => r.Id == renderId) ?? throw NotFound();
|
|
var offset = destination.CanSeek ? (int)Math.Min(destination.Length, render.Bytes.Length) : 0;
|
|
if (destination.CanSeek) destination.Seek(0, SeekOrigin.End);
|
|
await destination.WriteAsync(render.Bytes.AsMemory(offset), cancellationToken);
|
|
return render.FileName;
|
|
}
|
|
|
|
/// <summary>Like the server: the SHA-256 is checked and a render keeps its id; one it already has is not sent again.</summary>
|
|
public async Task<RenderDto?> UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken)
|
|
{
|
|
var bytes = await File.ReadAllBytesAsync(path, cancellationToken);
|
|
if (Convert.ToHexStringLower(SHA256.HashData(bytes)) != request.Sha256 || bytes.Length != request.Size)
|
|
throw new ChronosApiException(HttpStatusCode.UnprocessableEntity, new ApiError("hash_mismatch", "hash"));
|
|
lock (_gate)
|
|
{
|
|
var sequence = Find(sequenceId);
|
|
if (sequence.Renders.Any(r => r.Id == request.RenderId)) return null;
|
|
var render = new FakeRender(request.RenderId, request.FileName, request.PresetName, bytes);
|
|
sequence.Renders.Add(render);
|
|
return render.Dto();
|
|
}
|
|
}
|
|
|
|
public async IAsyncEnumerable<ServerEvent> EventsAsync([EnumeratorCancellation] CancellationToken cancellationToken)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (FailingEvents > 0)
|
|
{
|
|
FailingEvents--;
|
|
throw new HttpRequestException("socket closed");
|
|
}
|
|
}
|
|
while (await _events.Reader.WaitToReadAsync(cancellationToken))
|
|
{
|
|
while (_events.Reader.TryRead(out var e)) yield return e;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
private FakeSequence Find(Guid id) => Sequences.TryGetValue(id, out var s) ? s : throw NotFound();
|
|
|
|
private static ChronosApiException NotFound() => new(HttpStatusCode.NotFound, new ApiError("not_found", "sequence"));
|
|
}
|
|
|
|
internal sealed class FakeSequence
|
|
{
|
|
public required Guid Id { get; init; }
|
|
public string? Title { get; set; }
|
|
public List<string> Tags { get; set; } = [];
|
|
public int Rating { get; set; }
|
|
public string? Notes { get; set; }
|
|
public string? SettingsJson { get; set; }
|
|
public int SettingsVersion { get; set; } = 1;
|
|
public int FramesVersion { get; set; }
|
|
public SequenceStatus Status { get; set; }
|
|
|
|
/// <summary>The server cannot decode these originals (HEIF): its manifest asks for proxies of them.</summary>
|
|
public bool Undecodable { get; set; }
|
|
|
|
/// <summary>The server adopted these originals and never verified them: it lists no SHA-256 for them.</summary>
|
|
public bool Unhashed { get; set; }
|
|
|
|
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
|
|
public DateTime? FirstCapture { get; set; }
|
|
public string? CameraModel { get; set; } = "Canon EOS R5";
|
|
public List<FakeFrame> Frames { get; } = [];
|
|
public List<FakeRender> Renders { get; } = [];
|
|
|
|
/// <summary>Tombstones of photos removed on the server (owner review 2, R6), as its manifest lists them.</summary>
|
|
public List<RemovedPhotoDto> Removed { get; } = [];
|
|
|
|
/// <summary>Like the server, the capture time comes from the photo's Exif data when none is given.</summary>
|
|
public FakeFrame AddFrame(byte[] bytes, string? name = null, DateTime? capturedAt = null) => InsertFrame(Frames.Count, bytes, name, capturedAt);
|
|
|
|
/// <summary>A photo filed at <paramref name="index"/> (captured before the frames after it): those move up by one.</summary>
|
|
public FakeFrame InsertFrame(int index, byte[] bytes, string? name = null, DateTime? capturedAt = null)
|
|
{
|
|
name ??= $"IMG_{Frames.Count + 1:0000}.JPG";
|
|
using var stream = new MemoryStream(bytes);
|
|
var frame = new FakeFrame(Guid.CreateVersion7(), name, bytes, capturedAt ?? MetadataReader.Read(stream, name).CapturedAt);
|
|
Frames.Insert(index, frame);
|
|
FramesVersion++;
|
|
UpdatedAt = DateTimeOffset.UtcNow;
|
|
return frame;
|
|
}
|
|
|
|
public SequenceDto Dto() => new(Id, Title, Title ?? "Untitled", "2026/" + Id.ToString("N"), "Canon", CameraModel, null, FirstCapture, null, Frames.Count, 10,
|
|
Frames.Sum(f => (long)f.Bytes.Length), (long)Status, Renders.Count, null, Tags, Rating, Notes, SettingsVersion, FramesVersion, 0, null, UpdatedAt);
|
|
|
|
public SequenceManifest Manifest(Func<Guid, bool> proxySent) => new(Id, FramesVersion, SettingsVersion, SettingsJson, Tags,
|
|
[.. Frames.Select((f, i) => new ManifestFrame(f.Id, i, f.FileName, f.Bytes.Length, Unhashed ? string.Empty : f.Sha256,
|
|
(Status & SequenceStatus.ProxiesBuilt) != 0 || proxySent(f.Id), (Status & SequenceStatus.ThumbsBuilt) != 0, WantsProxy: Undecodable && !proxySent(f.Id)))],
|
|
[.. Renders.Select(r => r.Dto())], Undecodable ? new ProxySpec(1280, 90) : null, Removed.Count == 0 ? null : [.. Removed]);
|
|
|
|
public FrameDto FrameDto(FakeFrame frame) => frame.Dto(Id, Frames.IndexOf(frame), Unhashed);
|
|
}
|
|
|
|
internal sealed record FakeRender(Guid Id, string FileName, string PresetName, byte[] Bytes)
|
|
{
|
|
public RenderDto Dto() => new(Id, PresetName, "hevc", 3840, 2160, 30, 12, Bytes.Length, new DateTimeOffset(2026, 9, 20, 21, 0, 0, TimeSpan.Zero), "Studio NAS");
|
|
}
|
|
|
|
internal sealed record FakeFrame(Guid Id, string FileName, byte[] Bytes, DateTime? CapturedAt)
|
|
{
|
|
public string Sha256 { get; } = Convert.ToHexStringLower(SHA256.HashData(Bytes));
|
|
|
|
/// <summary>The original stands in for its proxy: a real JPEG the decoders read.</summary>
|
|
public byte[] Proxy => Bytes;
|
|
|
|
public FrameDto Dto(Guid sequenceId, int index, bool unhashed = false) =>
|
|
new(Id, sequenceId, index, FileName, Bytes.Length, unhashed ? null : Sha256, CapturedAt, null, 1.0 / 250, 8, 100, 24, 1, 48, 32, "jpeg", 0);
|
|
}
|