Files
Chronos/src/Titano.Data/Archiving/SequenceMapping.cs
T
Claude 1c19b00a00 feat(data): archive services, ingest and job handlers
ArchiveService facade over indexer (scan, adopt, rebuild), ArchiveIngest
(IArchiveIngest with per-sequence group commit, leases and a write-ahead
journal), verifier, maintenance (duplicates, orphans, cleanup), mass rename
with resumable journal, thumbnails/proxies and card import; job handlers for
scan, rebuild, verify, import, thumbs, proxies and maintenance with checkpoints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 12:30:15 +00:00

107 lines
4.5 KiB
C#

using System.Text.Json;
using Titano.Core.Archiving;
using Titano.Core.Model;
namespace Titano.Data.Archiving;
/// <summary>Sidecar records to index rows. Fields the sidecar does not carry are kept from the existing row.</summary>
internal static class SequenceMapping
{
public static Frame ToFrame(SidecarFrame f, Guid sequenceId, SequencePaths sequence) => new()
{
Id = f.Id,
SequenceId = sequenceId,
Index = f.I,
RelativePath = sequence.ArchiveRelative(f.File),
FileName = Path.GetFileName(f.File),
OriginalFileName = f.Original,
Size = f.Size,
Sha256 = f.Sha256,
CapturedAt = f.CapturedAt,
UtcOffsetMinutes = f.UtcOffset,
TimestampSource = f.TimeSource,
ExposureTime = f.Exposure,
FNumber = f.FNumber,
Iso = f.Iso,
FocalLength = f.Focal,
ExposureBias = f.Bias,
WhiteBalance = f.Wb,
Orientation = f.Orientation,
Width = f.W,
Height = f.H,
Format = f.Format,
PreviewOffset = f.PreviewOffset,
PreviewLength = f.PreviewLength,
Flags = f.Flags,
};
public static List<Frame> ToFrames(SequenceManifest manifest, SequencePaths sequence) =>
manifest.Frames.Select(f => ToFrame(f, manifest.Id, sequence)).ToList();
/// <param name="existing">The current row; its render, analysis, sync and ownership fields are kept, and so are its
/// title, tags, rating and notes when its settings version is ahead of the sidecar's (edited in the index only).</param>
public static Sequence ToSequence(SequenceManifest m, Guid archiveId, SequencePaths sequence, Sequence? existing)
{
var indexAhead = existing is not null && existing.SettingsVersion > m.Versions.Settings;
var times = m.Frames.Where(f => f.CapturedAt is not null).Select(f => f.CapturedAt!.Value).Order().ToList();
var flagged = m.Frames.Any(f => (f.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0);
return new Sequence
{
Id = m.Id,
ArchiveId = archiveId,
Title = indexAhead ? existing!.Title : m.Title,
Folder = sequence.RelativeFolder,
CameraMake = m.Camera?.Make,
CameraModel = m.Camera?.Model,
CameraSerial = m.Camera?.Serial,
Lens = m.Lens,
FirstCapture = times.Count > 0 ? times[0] : null,
LastCapture = times.Count > 0 ? times[^1] : null,
FrameCount = m.Frames.Count,
MedianInterval = MedianInterval(times),
TotalBytes = m.Frames.Sum(f => f.Size),
Status = flagged ? m.Status | SequenceStatus.HasErrors : m.Status & ~SequenceStatus.HasErrors,
VerifiedAt = m.VerifiedAt,
Tags = indexAhead ? existing!.Tags : m.Tags,
Rating = indexAhead ? existing!.Rating : m.Rating,
Notes = indexAhead ? existing!.Notes : m.Notes,
SettingsVersion = indexAhead ? existing!.SettingsVersion : m.Versions.Settings,
FramesVersion = m.Versions.Frames,
UpdatedAt = m.UpdatedAt,
RenderedCount = existing?.RenderedCount ?? 0,
HeroFrameId = existing?.HeroFrameId,
SettingsJson = existing?.SettingsJson ?? ReadProject(sequence),
AnalysisJson = existing?.AnalysisJson,
Origin = existing?.Origin ?? SequenceOrigin.Local,
SyncState = existing?.SyncState ?? SyncState.LocalOnly,
OwnerUserId = existing?.OwnerUserId ?? (Guid.TryParse(m.CreatedBy, out var owner) ? owner : null),
WarningCount = existing?.WarningCount ?? 0,
};
}
private static double? MedianInterval(List<DateTime> times)
{
if (times.Count < 2) return null;
var gaps = new double[times.Count - 1];
for (var i = 1; i < times.Count; i++) gaps[i - 1] = (times[i] - times[i - 1]).TotalSeconds;
Array.Sort(gaps);
var mid = gaps.Length / 2;
return gaps.Length % 2 == 1 ? gaps[mid] : (gaps[mid - 1] + gaps[mid]) / 2;
}
/// <summary>The project file is owned by the editor; a rebuilt index takes its text as-is when it is valid JSON.</summary>
private static string? ReadProject(SequencePaths sequence)
{
if (!File.Exists(sequence.ProjectPath)) return null;
var text = File.ReadAllText(sequence.ProjectPath);
try
{
using (JsonDocument.Parse(text)) return text;
}
catch (JsonException)
{
return null;
}
}
}