using System.IO;
using System.Text.Json;
using Titano.Core.Advice;
using Titano.Core.Analysis;
using Titano.Core.Analysis.Deep;
using Titano.Core.Archiving;
using Titano.Core.Diagnostics;
using Titano.Core.Imaging;
using Titano.Core.IO;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Data;
using Titano.Data.Archiving;
using Titano.Data.Archiving.Jobs;
using Titano.Data.Jobs;
using Titano.Data.Repositories;
namespace Titano.App.Editor;
///
/// A sequence of an archive on this PC: rows from the index, originals decoded by the client's decoder chain, the
/// project in .titano/project.json mirrored in the index's settings column (with its hero frame, which the
/// Library shows), jobs on this PC's engine. Project writes follow the archive engine's protocol: under the sequence's
/// lease file (so the server and the job engine wait for it), file first (atomically), then the index with its settings
/// version as an optimistic check.
///
public sealed class LocalSequenceSource : ISequenceSource
{
private const int PageSize = 1000;
private const int SaveAttempts = 3;
private const int JobsListed = 200;
private readonly TitanoDatabase _db;
private readonly Guid _sequenceId;
private readonly ImageDecoder _decoder;
private readonly JobEngine? _engine;
private readonly ArchiveServiceOptions _lease;
private string?[] _files = [];
private Guid[] _frameIds = [];
private ArchivePaths? _paths;
private SequencePaths? _folder;
public LocalSequenceSource(TitanoDatabase db, Guid sequenceId, ImageDecoder decoder, JobEngine? engine, ArchiveServiceOptions? lease = null)
{
_db = db;
_sequenceId = sequenceId;
_decoder = decoder;
_engine = engine;
_lease = lease ?? new ArchiveServiceOptions();
if (engine is not null) engine.JobChanged += OnJobChanged;
}
public bool IsRemote => false;
public bool IsReadOnly => false;
public event Action? JobChanged;
/// A save changed the frame the Library shows for the sequence: the stored row (any thread).
public event Action? HeroChanged;
public async Task LoadAsync(CancellationToken cancellationToken)
{
var sequence = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing();
var archive = await _db.Archives.GetAsync(sequence.ArchiveId, cancellationToken) ?? throw Missing();
var frames = new List();
while (true)
{
var page = await _db.Frames.ListBySequenceAsync(_sequenceId, frames.Count > 0 ? frames[^1].Index : -1, PageSize, cancellationToken);
frames.AddRange(page);
if (page.Count < PageSize) break;
}
var renders = await _db.Renders.ListBySequenceAsync(_sequenceId, cancellationToken);
return await Task.Run(() =>
{
var paths = new ArchivePaths(archive.RootPath);
var folder = paths.Sequence(sequence.Folder);
var files = new string?[frames.Count];
for (var i = 0; i < files.Length; i++)
{
if ((frames[i].Flags & FrameFlags.Missing) == 0) files[i] = paths.FromRelative(frames[i].RelativePath);
}
(_paths, _folder, _files, _frameIds) = (paths, folder, files, [.. frames.Select(f => f.Id)]);
var context = SequenceContext.FromFrames(frames);
return new SequenceContent(sequence, frames, ReadProject(sequence, folder), LightAnalyzer.Analyze(frames), ReadReport(folder, frames.Count),
context.SourceWidth, context.SourceHeight, folder.Exports, [.. renders.OrderByDescending(r => r.Created)], SavedCopies.List(folder.MetaDirectory));
}, cancellationToken);
}
public async ValueTask DecodeAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken)
{
if ((uint)index >= (uint)_files.Length || _files[index] is not { } file) return null;
return await Task.Run(() =>
{
try
{
// Thumbnails take an embedded preview when it is large enough (no RAW development for a filmstrip cell).
var request = new DecodeRequest(file, maxLongEdge, purpose, ForcePreview: purpose == DecodePurpose.Thumbnail);
return _decoder.Decode(request, cancellationToken)?.Pixels;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("editor", $"Frame {file} could not be read", ex);
return null;
}
}, cancellationToken);
}
public Task SaveProjectAsync(ProjectSettings project, ProjectSettings basis, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(project);
ArgumentNullException.ThrowIfNull(basis);
var (paths, folder, frameIds) = (_paths, _folder, _frameIds);
if (paths is null || folder is null) throw new InvalidOperationException("LoadAsync first.");
return Task.Run(async () =>
{
ArchiveVolume.EnsureWritable(paths);
using var held = await LeaseFile.AcquireAsync(paths.LocksDirectory, $"seq-{_sequenceId:N}", _lease.Owner, _lease.LeaseWait, _lease.LeaseDuration,
cancellationToken);
for (var attempt = 1; ; attempt++)
{
var row = await _db.Sequences.GetAsync(_sequenceId, cancellationToken) ?? throw Missing();
var stored = ReadProject(row, folder);
var merged = ProjectMerge.SameValues(stored, basis) ? project : ProjectMerge.Merge(basis, project, stored);
var json = merged.ToJson();
if (json == stored.ToJson()) return merged;
AtomicFile.WriteAllText(folder.ProjectPath, json);
var edit = new SequenceEdit { SettingsJson = json, HeroFrameId = merged.HeroFrame == stored.HeroFrame ? null : HeroFrameId(merged, frameIds) };
var result = await _db.Sequences.EditAsync(_sequenceId, edit, row.SettingsVersion, cancellationToken);
switch (result.Outcome)
{
case SequenceEditOutcome.Updated:
if (result.Sequence is { } updated && updated.HeroFrameId != row.HeroFrameId) HeroChanged?.Invoke(updated);
return merged;
case SequenceEditOutcome.NotFound:
throw Missing();
// The settings moved in the index meanwhile (server API): merge again on top of them; the file follows the next write.
case SequenceEditOutcome.Conflict when attempt == SaveAttempts:
throw new IOException(Loc.L("Editor.Error.SaveConflict"));
}
}
}, cancellationToken);
}
public async Task SubmitAsync(ArchiveJobRequest request, CancellationToken cancellationToken)
{
if (_engine is null) throw new InvalidOperationException(Loc.L("Editor.Error.NoEngine"));
var job = await _engine.EnqueueAsync(request, cancellationToken: cancellationToken);
return job.Id;
}
public async Task> ListJobsAsync(CancellationToken cancellationToken) =>
(await _db.Jobs.ListAsync(null, JobsListed, null, cancellationToken)).Items;
public ValueTask DisposeAsync()
{
if (_engine is not null) _engine.JobChanged -= OnJobChanged;
return ValueTask.CompletedTask;
}
/// The settings in force: the index copy, else project.json, else defaults.
/// The settings are not a project or come from a newer Titano.
internal static ProjectSettings ReadProject(Sequence sequence, SequencePaths folder)
{
var json = sequence.SettingsJson ?? (File.Exists(folder.ProjectPath) ? File.ReadAllText(folder.ProjectPath) : null);
if (string.IsNullOrWhiteSpace(json)) return new ProjectSettings();
try
{
return ProjectSettings.FromJson(json);
}
catch (JsonException ex)
{
throw new InvalidDataException(Loc.L("Editor.Error.Project"), ex);
}
}
/// The frame of the project's hero, or (the first frame shows) without one.
private static Guid HeroFrameId(ProjectSettings project, Guid[] frameIds) =>
project.HeroFrame is { } hero && (uint)hero < (uint)frameIds.Length ? frameIds[hero] : Guid.Empty;
/// The saved deep report when it describes the current frames.
private static DeepAnalysisReport? ReadReport(SequencePaths folder, int frameCount)
{
if (!File.Exists(folder.AnalysisPath)) return null;
try
{
var report = DeepAnalysisReport.Load(folder.AnalysisPath);
return report.FrameCount == frameCount ? report : null;
}
catch (Exception ex) when (ex is IOException or InvalidDataException or JsonException)
{
Log.Warning("editor", $"{folder.AnalysisPath} cannot be read", ex);
return null;
}
}
private void OnJobChanged(Job job) => JobChanged?.Invoke(job);
private static InvalidOperationException Missing() => new(Loc.L("Editor.Error.Missing"));
}