Files
Chronos/src/Chronos.Data/Workspaces/WorkspaceMedia.cs
T
Alby96andClaude Opus 5.5 db389a54f2
CI / Build and test (push) Successful in 9m26s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 8s
Release / Build, test and publish (push) Successful in 14m31s
CI / PostgreSQL 16 integration (push) Successful in 9m53s
CI / PostgreSQL 17 integration (push) Successful in 9m22s
CI / Docker image (push) Successful in 44s
Release / Server image (push) Successful in 1m36s
feat: owner review 2 phase 5c: originals, local render, clean-up and the workspace settings (#18)
Originals in the workspace, local render and send home with provenance, the workspace clean-up, size limit and trash, moving the workspace folder, and the complete Settings › Workspace page (owner review 2, R8, phase 5c). One adversarial review round (35 findings, 32 fixed), a copy-cancel race fixed, 184 strings in all 38 languages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-04 20:03:52 +01:00

246 lines
12 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. One that holds originals or was cleaned is written as
/// format 2 (<see cref="WrittenFormatVersion"/>), which a Chronos before phase 5c refuses as newer and reads as media
/// removed (copied again) rather than as proxies whose files are all missing; proxies alone stay format 1, which it reads.
/// </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>
/// <param name="Cleaned">
/// When the workspace's clean-up removed the files of these media (phase 5c, <see cref="WorkspaceCleaner"/>): the frames stay,
/// none names a file, and the project reads as <see cref="WorkspaceState.MediaRemoved"/> until a copy brings them again (a
/// copy writes a new index, without it). Null for media a copy wrote.
/// </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, DateTimeOffset? Cleaned = null)
{
public const string FormatName = "chronos-workspace-media";
/// <summary>The newest format this Chronos reads (2: media with originals, or cleaned).</summary>
public const int CurrentFormatVersion = 2;
/// <summary>The format of media of proxies only that a copy wrote, which a Chronos before phase 5c reads too.</summary>
public const int ProxiesFormatVersion = 1;
/// <summary>The format these media are written in: 2 when they hold originals (or name one) or were cleaned, else 1.</summary>
[JsonIgnore]
public int WrittenFormatVersion => HoldsOriginals || IsCleaned || Frames.Any(f => f.HasOriginal) ? CurrentFormatVersion : ProxiesFormatVersion;
/// <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>The clean-up removed the files of these media (<see cref="Cleaned"/>): nothing to edit until they are copied again.</summary>
[JsonIgnore]
public bool IsCleaned => Cleaned is not null;
/// <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);
}
}