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;
///
/// One frame of a workspace's media (media/media.json): 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.
///
/// The file relative to the media folder ("proxies/<frame id>.jpg"); null when the workspace holds none.
///
/// What the file is when a server sent it (phase 5b): proxy, or thumb, 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).
///
///
/// The frame's original the workspace holds, relative to the media folder (:
/// "originals/<frame id>-<sha8><ext>", phase 5c), in media of kind ; null
/// when it holds none (proxies only, or the home had no original of the frame: the proxy stands in for editing).
///
/// The original's SHA-256 as copied and checked (lower case); null with .
/// The original's size as copied; 0 with no original.
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)
{
/// The record of a home's frame, with the file the workspace holds for it (: what a server sent).
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);
}
/// A thumbnail stands in for the frame's proxy (a server had none when it was copied).
[JsonIgnore]
public bool IsStandIn => FileKind == Sync.ProxyCache.Thumb;
/// The workspace holds the frame's original (media of kind ).
[JsonIgnore]
public bool HasOriginal => Original is not null;
///
/// The frame as a render from the workspace's originals reads it (phase 5c): its file is , and
/// it is flagged missing while the workspace holds no original for it (a render refuses such media first).
///
public Frame ToOriginalFrame(Guid sequenceId)
{
var frame = ToFrame(sequenceId);
return frame with
{
RelativePath = Original ?? FileName,
Flags = Original is null ? Flags | FrameFlags.Missing : Flags,
};
}
/// The frame as the processing engine reads it; flagged missing while the workspace holds no file for it.
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,
};
}
///
/// What a workspace project holds of its sequence's photos (media/media.json, owner review 2, R8): the kind
/// (, or from phase 5c : the proxies plus each frame's
/// 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 (), 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.
///
/// 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.
///
/// When the workspace's clean-up removed the files of these media (phase 5c, ): the frames stay,
/// none names a file, and the project reads as until a copy brings them again (a
/// copy writes a new index, without it). Null for media a copy wrote.
///
public sealed record WorkspaceMedia(string Format, int FormatVersion, string Kind, Guid SequenceId, int FramesVersion, IReadOnlyList Frames,
DateTimeOffset Copied, string? AnalysisSha256 = null, IReadOnlyList? Aliases = null, DateTimeOffset? Cleaned = null)
{
public const string FormatName = "chronos-workspace-media";
/// The newest format this Chronos reads (2: media with originals, or cleaned).
public const int CurrentFormatVersion = 2;
/// The format of media of proxies only that a copy wrote, which a Chronos before phase 5c reads too.
public const int ProxiesFormatVersion = 1;
/// The format these media are written in: 2 when they hold originals (or name one) or were cleaned, else 1.
[JsonIgnore]
public int WrittenFormatVersion => HoldsOriginals || IsCleaned || Frames.Any(f => f.HasOriginal) ? CurrentFormatVersion : ProxiesFormatVersion;
/// The frames' photos in index order, as project positions name them, with the encodings conversions joined.
[JsonIgnore]
public FramePhotos Photos => new(Frames.Select(f => (f.Index, new FramePhoto(f.Sha256, f.Size, f.CapturedAt))), Aliases);
/// The frames held, as the processing engine reads them.
public IReadOnlyList ToFrames() => [.. Frames.Select(f => f.ToFrame(SequenceId))];
/// The frames over the originals the workspace holds (), as a local render reads them.
public IReadOnlyList ToOriginalFrames() => [.. Frames.Select(f => f.ToOriginalFrame(SequenceId))];
/// The clean-up removed the files of these media (): nothing to edit until they are copied again.
[JsonIgnore]
public bool IsCleaned => Cleaned is not null;
/// Media of kind : the proxies plus the originals the frames name.
[JsonIgnore]
public bool HoldsOriginals => Kind == BundleMedia.Originals;
///
/// These media with their photos named as 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 . 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.
///
public WorkspaceMedia NamedAsAt(HomeFrames home)
{
ArgumentNullException.ThrowIfNull(home);
var there = new Dictionary(home.Frames.Count);
foreach (var frame in home.Frames) there[frame.Id] = frame;
var learned = false;
var frames = new List(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());
}
}
///
/// 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).
///
public sealed class WorkspaceSequence
{
public WorkspaceSequence(Sequence sequence, IReadOnlyList frames, IReadOnlyList 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 Frames { get; }
public IReadOnlyList Files { get; }
///
/// The editor's frame source over the workspace's files, through the same read-ahead window as archive renders; it also
/// implements and (dispose it when done).
///
public IRenderFrameSource OpenFrames(ImageDecoder decoder, FrameWindowOptions? window = null)
{
var frames = new SequenceFrames(Sequence, Frames, Files);
return new SequenceFrameSource(frames, frames.LightFrames(), decoder, window);
}
}