Files
Chronos/src/Chronos.Data/Workspaces/WorkspaceMedia.cs
T
Alby96andClaude Opus 5.5 2e1c67f531 feat(workspace): lay the phase 5c foundation
Phase 5c (owner review 2, R8) is built by six packages in parallel. This
commit settles everything they share, so each one touches its own files:

- job kinds WorkspaceRender (21), WorkspaceSend (22) and WorkspaceMove (23)
  with their wire names, default priorities, notices and parameter records;
  their handlers are registered as skeletons that throw until their
  package lands
- WorkspaceJobOptions and a new CreateHandlers overload; the earlier ones
  stay and default the options
- originals in the media index (an index of phase 5b reads as proxies
  only), their file names, and the exports/renders.json ledger of local
  renders, written atomically
- the optional project and version of an uploaded render
- the workspace settings (size limit, default media, version on render,
  clean-up age, project to open) and when each project was opened, which
  Reset and Import keep and Export leaves out
- WorkspaceHost split into partial files, with stubs for usage, trash and
  the folder move, removal hooks for unsent renders and last originals,
  and a guard that holds work while the workspace moves
- every new string of the phase in English and Italian, with tooltips

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 15:05:09 +01:00

226 lines
11 KiB
C#

using System.Text.Json.Serialization;
using Chronos.Core.Imaging;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Data.Processing;
using Chronos.Data.Projects;
namespace Chronos.Data.Workspaces;
/// <summary>
/// One frame of a workspace's media (<c>media/media.json</c>): the frame's record at its home (what analyses and the
/// editor read) and the file the workspace holds for it, with its SHA-256 and size as copied.
/// </summary>
/// <param name="File">The file relative to the media folder ("proxies/&lt;frame id&gt;.jpg"); null when the workspace holds none.</param>
/// <param name="FileKind">
/// What the file is when a server sent it (phase 5b): <c>proxy</c>, or <c>thumb</c>, a thumbnail standing in while the server
/// had no proxy of the frame, which a later copy replaces once it has one; null for a proxy (copies from this PC's archive).
/// </param>
/// <param name="Original">
/// The frame's original the workspace holds, relative to the media folder (<see cref="WorkspaceProject.OriginalName"/>:
/// "originals/&lt;frame id&gt;-&lt;sha8&gt;&lt;ext&gt;", phase 5c), in media of kind <see cref="BundleMedia.Originals"/>; null
/// when it holds none (proxies only, or the home had no original of the frame: the proxy stands in for editing).
/// </param>
/// <param name="OriginalSha256">The original's SHA-256 as copied and checked (lower case); null with <paramref name="Original"/>.</param>
/// <param name="OriginalBytes">The original's size as copied; 0 with no original.</param>
public sealed record WorkspaceFrame(
Guid Id,
int Index,
string FileName,
string? OriginalFileName,
long Size,
string? Sha256,
DateTime? CapturedAt,
int? UtcOffsetMinutes,
TimestampSource TimestampSource,
double? ExposureTime,
double? FNumber,
int? Iso,
double? FocalLength,
double? ExposureBias,
int? WhiteBalance,
int Orientation,
int Width,
int Height,
ImageFileFormat Format,
int PreviewWidth,
int PreviewHeight,
double? MeanLogLuminance,
double? ClipLow,
double? ClipHigh,
FrameFlags Flags,
string? File,
string? FileSha256,
long FileBytes,
string? FileKind = null,
string? Original = null,
string? OriginalSha256 = null,
long OriginalBytes = 0)
{
/// <summary>The record of a home's frame, with the file the workspace holds for it (<paramref name="fileKind"/>: what a server sent).</summary>
public static WorkspaceFrame Of(Frame frame, string? file, string? fileSha256, long fileBytes, string? fileKind = null)
{
ArgumentNullException.ThrowIfNull(frame);
return new WorkspaceFrame(frame.Id, frame.Index, frame.FileName, frame.OriginalFileName, frame.Size, frame.Sha256, frame.CapturedAt, frame.UtcOffsetMinutes,
frame.TimestampSource, frame.ExposureTime, frame.FNumber, frame.Iso, frame.FocalLength, frame.ExposureBias, frame.WhiteBalance, frame.Orientation,
frame.Width, frame.Height, frame.Format, frame.PreviewWidth, frame.PreviewHeight, frame.MeanLogLuminance, frame.ClipLow, frame.ClipHigh, frame.Flags,
file, fileSha256, fileBytes, fileKind);
}
/// <summary>A thumbnail stands in for the frame's proxy (a server had none when it was copied).</summary>
[JsonIgnore]
public bool IsStandIn => FileKind == Sync.ProxyCache.Thumb;
/// <summary>The workspace holds the frame's original (media of kind <see cref="BundleMedia.Originals"/>).</summary>
[JsonIgnore]
public bool HasOriginal => Original is not null;
/// <summary>
/// The frame as a render from the workspace's originals reads it (phase 5c): its file is <see cref="Original"/>, and
/// it is flagged missing while the workspace holds no original for it (a render refuses such media first).
/// </summary>
public Frame ToOriginalFrame(Guid sequenceId)
{
var frame = ToFrame(sequenceId);
return frame with
{
RelativePath = Original ?? FileName,
Flags = Original is null ? Flags | FrameFlags.Missing : Flags,
};
}
/// <summary>The frame as the processing engine reads it; flagged missing while the workspace holds no file for it.</summary>
public Frame ToFrame(Guid sequenceId) => new()
{
Id = Id,
SequenceId = sequenceId,
Index = Index,
RelativePath = File ?? FileName,
FileName = FileName,
OriginalFileName = OriginalFileName,
Size = Size,
Sha256 = Sha256,
CapturedAt = CapturedAt,
UtcOffsetMinutes = UtcOffsetMinutes,
TimestampSource = TimestampSource,
ExposureTime = ExposureTime,
FNumber = FNumber,
Iso = Iso,
FocalLength = FocalLength,
ExposureBias = ExposureBias,
WhiteBalance = WhiteBalance,
Orientation = Orientation,
Width = Width,
Height = Height,
Format = Format,
PreviewWidth = PreviewWidth,
PreviewHeight = PreviewHeight,
MeanLogLuminance = MeanLogLuminance,
ClipLow = ClipLow,
ClipHigh = ClipHigh,
Flags = File is null ? Flags | FrameFlags.Missing : Flags,
};
}
/// <summary>
/// What a workspace project holds of its sequence's photos (<c>media/media.json</c>, owner review 2, R8): the kind
/// (<see cref="BundleMedia.Proxies"/>, or from phase 5c <see cref="BundleMedia.Originals"/>: the proxies plus each frame's
/// original, <see cref="WorkspaceFrame.Original"/>), the sequence and its frames version at the home when they were
/// copied, and every frame in index order. Written last by a copy, so a copy cut short leaves the previous index. An index
/// of phase 5b has no original fields: it reads as proxies only.
/// </summary>
/// <param name="Aliases">The encodings of the sequence's photos a conversion joined at the home (R7, its tombstones of reason
/// "replaced") when the media were copied: a version parked on a photo before its file was converted still finds its frame.</param>
public sealed record WorkspaceMedia(string Format, int FormatVersion, string Kind, Guid SequenceId, int FramesVersion, IReadOnlyList<WorkspaceFrame> Frames,
DateTimeOffset Copied, string? AnalysisSha256 = null, IReadOnlyList<PhotoAlias>? Aliases = null)
{
public const string FormatName = "chronos-workspace-media";
public const int CurrentFormatVersion = 1;
/// <summary>The frames' photos in index order, as project positions name them, with the encodings conversions joined.</summary>
[JsonIgnore]
public FramePhotos Photos => new(Frames.Select(f => (f.Index, new FramePhoto(f.Sha256, f.Size, f.CapturedAt))), Aliases);
/// <summary>The frames held, as the processing engine reads them.</summary>
public IReadOnlyList<Frame> ToFrames() => [.. Frames.Select(f => f.ToFrame(SequenceId))];
/// <summary>The frames over the originals the workspace holds (<see cref="WorkspaceFrame.ToOriginalFrame"/>), as a local render reads them.</summary>
public IReadOnlyList<Frame> ToOriginalFrames() => [.. Frames.Select(f => f.ToOriginalFrame(SequenceId))];
/// <summary>Media of kind <see cref="BundleMedia.Originals"/>: the proxies plus the originals the frames name.</summary>
[JsonIgnore]
public bool HoldsOriginals => Kind == BundleMedia.Originals;
/// <summary>
/// These media with their photos named as <paramref name="home"/> names them now (owner review 2, R8): a frame copied
/// before the home hashed its photo (a folder adopted, verified since) takes the SHA-256 the home's frame of that id has
/// for the same photo (the same size and capture time; a conversion that gave the frame another file is not the same
/// photo), and the encodings the home's conversions joined since the copy are added to <see cref="Aliases"/>. Versions
/// the home made since name those photos so; named alike here, one photo merges as one. The frames, their files and the
/// frames version stay as they are. This instance when there is nothing to learn.
/// </summary>
public WorkspaceMedia NamedAsAt(HomeFrames home)
{
ArgumentNullException.ThrowIfNull(home);
var there = new Dictionary<Guid, Frame>(home.Frames.Count);
foreach (var frame in home.Frames) there[frame.Id] = frame;
var learned = false;
var frames = new List<WorkspaceFrame>(Frames.Count);
foreach (var frame in Frames)
{
if (string.IsNullOrEmpty(frame.Sha256) && there.TryGetValue(frame.Id, out var held) && !string.IsNullOrEmpty(held.Sha256)
&& held.Size == frame.Size && Millisecond(held.CapturedAt) == Millisecond(frame.CapturedAt))
{
frames.Add(frame with { Sha256 = held.Sha256.ToLowerInvariant() });
learned = true;
}
else
{
frames.Add(frame);
}
}
var known = Aliases ?? [];
var links = known.Select(Link).ToHashSet();
var added = home.Photos.Aliases.Where(a => links.Add(Link(a))).ToList();
if (!learned && added.Count == 0) return this;
return this with { Frames = learned ? frames : Frames, Aliases = added.Count > 0 ? [.. known, .. added] : Aliases };
static long? Millisecond(DateTime? time) => time is { } at ? at.Ticks / TimeSpan.TicksPerMillisecond : null;
// A media index read back may hold a damaged link: compared as it is, and left out of the classes like any other.
static (string?, string?) Link(PhotoAlias alias) => (alias.Sha256?.ToLowerInvariant(), alias.Replacement?.ToLowerInvariant());
}
}
/// <summary>
/// A workspace project's sequence opened for editing (like a server sequence opened with proxies): the home's sequence
/// row, the frames as the workspace holds them and the absolute path of each frame's file (null, and the frame flagged
/// missing, when it holds none).
/// </summary>
public sealed class WorkspaceSequence
{
public WorkspaceSequence(Sequence sequence, IReadOnlyList<Frame> frames, IReadOnlyList<string?> files)
{
if (files.Count != frames.Count) throw new ArgumentException("One file entry per frame is required.", nameof(files));
Sequence = sequence;
Frames = frames;
Files = files;
}
public Sequence Sequence { get; }
public IReadOnlyList<Frame> Frames { get; }
public IReadOnlyList<string?> Files { get; }
/// <summary>
/// The editor's frame source over the workspace's files, through the same read-ahead window as archive renders; it also
/// implements <see cref="Core.Analysis.Deep.IFrameSource"/> and <see cref="IAsyncDisposable"/> (dispose it when done).
/// </summary>
public IRenderFrameSource OpenFrames(ImageDecoder decoder, FrameWindowOptions? window = null)
{
var frames = new SequenceFrames(Sequence, Frames, Files);
return new SequenceFrameSource(frames, frames.LightFrames(), decoder, window);
}
}