using System.Text.Json; using System.Text.Json.Nodes; using Chronos.Core.Archiving; using Chronos.Core.Diagnostics; using Chronos.Core.IO; using Chronos.Core.Model; using Chronos.Core.Project; using Chronos.Data.Archiving; using Chronos.Data.Repositories; namespace Chronos.Data.Projects; /// Why a project operation was refused (). public enum ProjectError { /// The sequence is not in the index. SequenceNotFound, /// The project (or the version named) does not exist, or was deleted. NotFound, /// The name is blank or longer than . InvalidName, /// Another project of the sequence has that name (case does not count). NameTaken, /// The Main project cannot be deleted: it holds the sequence's own settings. MainCannotBeDeleted, } /// A project operation was refused; nothing was written. public sealed class ProjectException(ProjectError reason, string message) : InvalidOperationException(message) { public ProjectError Reason { get; } = reason; } /// What became of a version pushed to its home (). public enum PushOutcome { /// Stored as the project's new head (its parent was the head: a fast-forward). Stored, /// A version with that id is stored already (a push repeated after its answer was lost): nothing changed. AlreadyStored, /// The head moved since the version's parent: pull the head, replay the version on it and push again. Diverged, /// The settings do not match the content hash sent with them: nothing was stored. HashMismatch, /// The settings are of a newer format than this home reads: nothing was stored. NewerFormat, /// Not a version of this project: settings that are not portable (they name frames by position), or an id another project holds. Invalid, } /// The answer to a push: the outcome, the version as stored (, ) and the project's head. public sealed record PushResult(PushOutcome Outcome, ProjectVersion? Version, ProjectVersion? Head); /// /// The frames of a sequence at its home, as project settings name them: their frames version, rows in index order and /// photos (, for ). /// public sealed record HomeFrames(int FramesVersion, IReadOnlyList Frames, FramePhotos Photos); /// /// The projects of the sequences of this PC's archives (owner review 2, R8): the home side of a workspace's push and pull. /// Every operation runs under the sequence's lease and first catches up: the sidecars into the index /// (), the Main project made when the sequence has none (version 1, "imported" from the /// sequence's settings), and settings another path wrote into the sequence since the last mirror appended to Main as an /// "external" version (or merged with a push whose mirror a crash cut short). A version is written as a sidecar first /// (immutable), then the project's record, then the index; a push to Main is also mirrored into the sequence's /// project.json and settings (positions placed on its frames, through the settings version check), so the code /// paths that read the sequence's settings keep working and only Main drives the hero frame the Library shows. Other /// projects stay on this home: copies of a sequence linked to a server sync only its settings, i.e. Main. /// public sealed class ProjectService { /// The name of the project made for every sequence (the desktop shows it in the UI language). public const string MainName = "Main"; public const int MaxNameLength = 60; /// Frames read at once when a sequence's photos are gathered. private const int PageSize = 1000; /// How long a read waits for a sequence another writer holds before it answers from the index alone. private static readonly TimeSpan ReadWait = TimeSpan.FromSeconds(3); private readonly ChronosDatabase _db; private readonly SequenceWriter _writer; private readonly ProjectIndexer _indexer; internal ProjectService(ChronosDatabase db, SequenceWriter writer) { _db = db; _writer = writer; _indexer = new ProjectIndexer(db); } /// The client name versions made here carry (the archive engine's lease owner). public string ClientName => _writer.Options.Owner; /// The live projects of a sequence, Main first; read from the index alone while the archive is offline. /// The sequence is not in the index. public async Task> ListAsync(Guid sequenceId, CancellationToken cancellationToken = default) { await ReadAsync(sequenceId, cancellationToken).ConfigureAwait(false); return await _db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false); } /// The sequence's Main project, made now (from its settings) when it has none. /// The sequence is not in the index. /// The archive is offline and the index has no Main project yet. public async Task MainAsync(Guid sequenceId, CancellationToken cancellationToken = default) { await ReadAsync(sequenceId, cancellationToken).ConfigureAwait(false); if (await _db.Projects.GetMainAsync(sequenceId, cancellationToken).ConfigureAwait(false) is { } main) return main; // The catch-up could not run (the archive offline, the sequence busy): it runs now, or says why it cannot. return await UnderLeaseAsync(sequenceId, async (_, ct) => (await _db.Projects.GetMainAsync(sequenceId, ct).ConfigureAwait(false))!, cancellationToken) .ConfigureAwait(false); } public Task GetAsync(Guid projectId, CancellationToken cancellationToken = default) => _db.Projects.GetAsync(projectId, cancellationToken); /// /// A new project of the sequence named , whose first version (kind "imported") holds the settings of /// (a version of any project of the sequence), or of Main's head. /// /// Unknown sequence or version, or the name is invalid or taken. public Task CreateAsync(Guid sequenceId, string name, Guid? fromVersionId = null, string? author = null, CancellationToken cancellationToken = default) { var trimmed = ValidName(name); return UnderLeaseAsync(sequenceId, async (context, ct) => { await EnsureNameFreeAsync(sequenceId, null, trimmed, ct).ConfigureAwait(false); ProjectVersion source; if (fromVersionId is { } from) { source = await _db.Projects.GetVersionAsync(from, ct).ConfigureAwait(false) is { } version && await _db.Projects.GetAsync(version.ProjectId, ct).ConfigureAwait(false) is { SequenceId: var owner } && owner == sequenceId ? version : throw new ProjectException(ProjectError.NotFound, $"Version {from} is not a version of sequence {sequenceId}."); } else { var main = (await _db.Projects.GetMainAsync(sequenceId, ct).ConfigureAwait(false))!; source = (await _db.Projects.GetVersionAsync(main.HeadVersionId!.Value, ct).ConfigureAwait(false))!; } var now = DateTimeOffset.UtcNow; var project = new SequenceProject { Id = Guid.CreateVersion7(), SequenceId = sequenceId, Name = trimmed, CreatedBy = author, CreatedAt = now, UpdatedAt = now, }; var first = new ProjectVersion { Id = Guid.CreateVersion7(), ProjectId = project.Id, Number = 1, Kind = ProjectVersionKind.Imported, Author = author, ClientName = ClientName, SettingsJson = source.SettingsJson, ContentHash = source.ContentHash, SettingsFormat = source.SettingsFormat, FramesVersion = source.FramesVersion, CreatedAt = now, StoredAt = now, }; return await AddProjectAsync(context.Folder, project, first, ct).ConfigureAwait(false); }, cancellationToken); } /// Gives a project another name. /// Unknown project, or the name is invalid or taken. public async Task RenameAsync(Guid projectId, string name, CancellationToken cancellationToken = default) { var trimmed = ValidName(name); var project = await LiveAsync(projectId, cancellationToken).ConfigureAwait(false); return await UnderLeaseAsync(project.SequenceId, async (context, ct) => { var current = await LiveAsync(projectId, ct).ConfigureAwait(false); if (current.Name == trimmed) return current; await EnsureNameFreeAsync(current.SequenceId, projectId, trimmed, ct).ConfigureAwait(false); var renamed = current with { Name = trimmed, UpdatedAt = DateTimeOffset.UtcNow }; ProjectSidecars.WriteRecord(context.Folder.ProjectRecordPath(projectId), ProjectRecord.Of(renamed)); await _db.Projects.UpdateAsync(renamed, ct).ConfigureAwait(false); return renamed; }, cancellationToken).ConfigureAwait(false); } /// /// Deletes a project: its record says so, its folder moves to .chronos/projects/.trash (kept, never erased by /// Chronos) and it leaves the listing. /// /// Unknown project, or the Main project. public async Task DeleteAsync(Guid projectId, CancellationToken cancellationToken = default) { var project = await LiveAsync(projectId, cancellationToken).ConfigureAwait(false); if (project.IsMain) throw new ProjectException(ProjectError.MainCannotBeDeleted, "The Main project cannot be deleted."); await UnderLeaseAsync(project.SequenceId, async (context, ct) => { var current = await LiveAsync(projectId, ct).ConfigureAwait(false); var now = DateTimeOffset.UtcNow; var deleted = current with { DeletedAt = now, UpdatedAt = now }; ProjectSidecars.WriteRecord(context.Folder.ProjectRecordPath(projectId), ProjectRecord.Of(deleted)); Directory.CreateDirectory(context.Folder.ProjectsTrashDirectory); Directory.Move(context.Folder.ProjectDirectory(projectId), Path.Combine(context.Folder.ProjectsTrashDirectory, projectId.ToString("N") + "-" + now.UtcTicks.ToString(System.Globalization.CultureInfo.InvariantCulture))); await _db.Projects.UpdateAsync(deleted, ct).ConfigureAwait(false); return true; }, cancellationToken).ConfigureAwait(false); } /// One page of a project's history, newest first: the versions numbered below (from the head when null). /// Unknown project. public async Task> HistoryAsync(Guid projectId, int? beforeNumber = null, int limit = 50, CancellationToken cancellationToken = default) { var project = await KnownAsync(projectId, cancellationToken).ConfigureAwait(false); await ReadAsync(project.SequenceId, cancellationToken).ConfigureAwait(false); return await _db.Projects.ListVersionsAsync(projectId, beforeNumber, limit, cancellationToken).ConfigureAwait(false); } /// The versions numbered above , oldest first: what a workspace pulls. /// Unknown project. public async Task> VersionsAfterAsync(Guid projectId, int afterNumber, int limit = 100, CancellationToken cancellationToken = default) { var project = await KnownAsync(projectId, cancellationToken).ConfigureAwait(false); await ReadAsync(project.SequenceId, cancellationToken).ConfigureAwait(false); return await _db.Projects.ListVersionsAfterAsync(projectId, afterNumber, limit, cancellationToken).ConfigureAwait(false); } /// A version by id; null when the index has none. public Task GetVersionAsync(Guid versionId, CancellationToken cancellationToken = default) => _db.Projects.GetVersionAsync(versionId, cancellationToken); /// The newest version of a project, after catching up. /// Unknown project. public async Task HeadAsync(Guid projectId, CancellationToken cancellationToken = default) { var project = await KnownAsync(projectId, cancellationToken).ConfigureAwait(false); await ReadAsync(project.SequenceId, cancellationToken).ConfigureAwait(false); project = await KnownAsync(projectId, cancellationToken).ConfigureAwait(false); return await _db.Projects.GetVersionAsync(project.HeadVersionId ?? Guid.Empty, cancellationToken).ConfigureAwait(false) ?? throw new ProjectException(ProjectError.NotFound, $"Project {projectId} has no version."); } /// /// Stores (made in a workspace from its ) as the /// project's new head when its parent is the head: its number is the head's plus one, whatever it says. Repeated with /// the id of a stored version, it answers as the first time. Its settings must be portable (positions parked by photo) /// and match their content hash. A version of Main is mirrored into the sequence's settings. /// /// Unknown project. public async Task PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(version); var project = await LiveAsync(projectId, cancellationToken).ConfigureAwait(false); return await UnderLeaseAsync(project.SequenceId, async (context, ct) => { var current = await LiveAsync(projectId, ct).ConfigureAwait(false); var head = await _db.Projects.GetVersionAsync(current.HeadVersionId ?? Guid.Empty, ct).ConfigureAwait(false); if (await _db.Projects.GetVersionAsync(version.Id, ct).ConfigureAwait(false) is { } existing) { return existing.ProjectId == projectId && string.Equals(existing.ContentHash, version.ContentHash, StringComparison.OrdinalIgnoreCase) ? new PushResult(PushOutcome.AlreadyStored, existing, head) : new PushResult(PushOutcome.Invalid, null, head); } if (!ProjectContent.Verifies(version.SettingsJson, version.ContentHash)) return new PushResult(PushOutcome.HashMismatch, null, head); var format = ProjectContent.FormatOf(version.SettingsJson); if (format > ProjectSettings.CurrentVersion) return new PushResult(PushOutcome.NewerFormat, null, head); if (!IsPortable(version.SettingsJson)) return new PushResult(PushOutcome.Invalid, null, head); if (version.ParentId != current.HeadVersionId) return new PushResult(PushOutcome.Diverged, null, head); var stored = version with { ProjectId = projectId, Number = current.HeadNumber + 1, ContentHash = version.ContentHash.ToLowerInvariant(), SettingsFormat = format, StoredAt = DateTimeOffset.UtcNow, }; current = await AppendAsync(context.Folder, current, stored, ct).ConfigureAwait(false); if (current.IsMain) await MirrorAsync(context, current, stored, ct).ConfigureAwait(false); return new PushResult(PushOutcome.Stored, stored, stored); }, cancellationToken).ConfigureAwait(false); } /// The frames of a sequence as they are indexed now, with its frames version. /// The sequence is not in the index. public async Task FramesAsync(Guid sequenceId, CancellationToken cancellationToken = default) { for (var attempt = 0; ; attempt++) { var before = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId); var frames = await LoadFramesAsync(sequenceId, cancellationToken).ConfigureAwait(false); var after = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId); if (after.FramesVersion == before.FramesVersion || attempt == 2) return new HomeFrames(after.FramesVersion, frames.Frames, frames.Photos); } } /// /// Catches the sequence's projects up under its lease (see the class remarks) before a read; the index alone answers while /// the archive is offline or the sequence busy for longer than the lease wait. /// private async Task ReadAsync(Guid sequenceId, CancellationToken cancellationToken) { try { await UnderLeaseAsync(sequenceId, (_, _) => Task.FromResult(true), cancellationToken, ReadWait).ConfigureAwait(false); } catch (Exception ex) when (ex is ArchiveOfflineException or LeaseUnavailableException or SequenceFolderMovedException) { Log.Info("projects", $"The projects of sequence {sequenceId} are read from the index: {ex.Message}"); } } /// /// Runs under the sequence's lease, after catching its projects up, in the folder the index has /// for the sequence as read under the lease (a Reorganize moves it under the same lease), which must hold it. /// /// /// The folder does not hold the sequence (another index of the archive moved it, maybe another sequence in): nothing is /// read from it or written into it, so no project is taken for gone and no file lands in another sequence's folder. /// private async Task UnderLeaseAsync(Guid sequenceId, Func> work, CancellationToken cancellationToken, TimeSpan? wait = null) { var sequence = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId); var archive = await _db.Archives.GetAsync(sequence.ArchiveId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId); var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); await using (await _writer.LockAsync(paths, sequenceId, wait, cancellationToken).ConfigureAwait(false)) { var row = await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw SequenceMissing(sequenceId); var folder = paths.Sequence(row.Folder); if (!SequenceFolders.Holds(folder, sequenceId)) throw new SequenceFolderMovedException(); var context = new LeaseContext(folder, row); await CatchUpAsync(context, cancellationToken).ConfigureAwait(false); return await work(context, cancellationToken).ConfigureAwait(false); } } /// The sidecars into the index, the Main project made, and Main in step with the sequence's settings. private async Task CatchUpAsync(LeaseContext context, CancellationToken cancellationToken) { await _indexer.ReconcileAsync(context.Folder, context.Row.Id, cancellationToken).ConfigureAwait(false); var main = await _db.Projects.GetMainAsync(context.Row.Id, cancellationToken).ConfigureAwait(false); if (main?.HeadVersionId is null) { await MakeMainAsync(context, main, cancellationToken).ConfigureAwait(false); return; } if (main.MirroredVersionId == main.HeadVersionId && main.MirroredSettingsVersion == context.Row.SettingsVersion) return; var head = (await _db.Projects.GetVersionAsync(main.HeadVersionId.Value, cancellationToken).ConfigureAwait(false))!; var frames = await LoadFramesAsync(context.Row.Id, cancellationToken).ConfigureAwait(false); if (CurrentPortable(context, frames.Photos) is not { } current) { // Settings no Chronos can read (or of a newer one): left as they are, recorded as seen. await _db.Projects.SetMirroredAsync(main.Id, main.HeadVersionId, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false); return; } // The head parks its photos as the frames held them when it was made: a conversion or a hash since names them anew. var sameAsHead = ProjectContent.SameChoices(head.SettingsJson, current, frames.Photos); if (main.MirroredVersionId == main.HeadVersionId || main.MirroredVersionId is null) { // Only the sequence's settings version moved (settings another path wrote, or only its title or tags), or the index // was rebuilt and knows no mirror: settings that differ from the head are its newest state. var latest = sameAsHead ? head : await AppendDerivedAsync(context, main, head, current, ProjectVersionKind.External, cancellationToken).ConfigureAwait(false); await _db.Projects.SetMirroredAsync(main.Id, latest.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false); return; } // The head is newer than the mirror: a push whose mirror a crash (or a settings edit at the same moment) cut short. if (sameAsHead) { await _db.Projects.SetMirroredAsync(main.Id, head.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false); return; } var mirrored = await _db.Projects.GetVersionAsync(main.MirroredVersionId.Value, cancellationToken).ConfigureAwait(false); if (main.MirroredSettingsVersion == context.Row.SettingsVersion || mirrored is null || ProjectContent.SameChoices(mirrored.SettingsJson, current, frames.Photos)) { // Nobody changed the settings since the last mirror: the mirror completes. await MirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false); return; } // Both moved: the settings written meanwhile merge with the head (the version made in a workspace wins where both changed), // each photo named as the frames hold it now so the same photo merges as one. var merged = ProjectMerge.Merge(ProjectMerge.Read(ProjectContent.Rebind(mirrored.SettingsJson, frames.Photos)), ProjectMerge.Read(ProjectContent.Rebind(head.SettingsJson, frames.Photos)), ProjectMerge.Read(ProjectContent.Rebind(current, frames.Photos))); var version = await AppendDerivedAsync(context, main, head, ProjectContent.Canonical(merged.ToJson()), ProjectVersionKind.Merge, cancellationToken) .ConfigureAwait(false); main = (await _db.Projects.GetAsync(main.Id, cancellationToken).ConfigureAwait(false))!; await MirrorAsync(context, main, version, cancellationToken).ConfigureAwait(false); } /// The Main project of a sequence that has none (or one without versions): version 1 from the sequence's settings. private async Task MakeMainAsync(LeaseContext context, SequenceProject? existing, CancellationToken cancellationToken) { var frames = await LoadFramesAsync(context.Row.Id, cancellationToken).ConfigureAwait(false); var portable = CurrentPortable(context, frames.Photos) ?? ProjectContent.ToPortable(new ProjectSettings(), frames.Photos); var now = DateTimeOffset.UtcNow; var project = existing ?? new SequenceProject { Id = Guid.CreateVersion7(), SequenceId = context.Row.Id, Name = await FreeMainNameAsync(context.Row.Id, cancellationToken).ConfigureAwait(false), IsMain = true, CreatedAt = now, UpdatedAt = now, }; var first = NewVersion(project.Id, null, project.HeadNumber + 1, ProjectVersionKind.Imported, portable, context.Row.FramesVersion, now); if (existing is null) { await AddProjectAsync(context.Folder, project with { MirroredSettingsVersion = context.Row.SettingsVersion, MirroredVersionId = first.Id }, first, cancellationToken).ConfigureAwait(false); } else { var updated = await AppendAsync(context.Folder, existing, first, cancellationToken).ConfigureAwait(false); await _db.Projects.SetMirroredAsync(updated.Id, first.Id, context.Row.SettingsVersion, cancellationToken).ConfigureAwait(false); } } /// "Main", or "Main (2)"… when a project of that name exists already (one made before the sequence had its Main). private async Task FreeMainNameAsync(Guid sequenceId, CancellationToken cancellationToken) { var names = (await _db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false)) .Select(p => p.Name).ToHashSet(StringComparer.OrdinalIgnoreCase); var name = MainName; for (var n = 2; names.Contains(name); n++) name = $"{MainName} ({n})"; return name; } /// The portable form of the sequence's settings in force (index, else project.json, else defaults); null when unreadable or newer. private static string? CurrentPortable(LeaseContext context, FramePhotos photos) { var json = context.Row.SettingsJson ?? (File.Exists(context.Folder.ProjectPath) ? AtomicFile.ReadAllText(context.Folder.ProjectPath) : null) ?? new ProjectSettings().ToJson(); try { if (ProjectContent.FormatOf(json) > ProjectSettings.CurrentVersion) return null; return ProjectContent.ToPortable(json, photos); } catch (JsonException ex) { Log.Warning("projects", $"The settings of sequence {context.Row.Id} cannot be read", ex); return null; } } /// A version made here (catch-up or merge) appended to Main. private async Task AppendDerivedAsync(LeaseContext context, SequenceProject main, ProjectVersion head, string portable, ProjectVersionKind kind, CancellationToken cancellationToken) { var version = NewVersion(main.Id, head.Id, main.HeadNumber + 1, kind, portable, context.Row.FramesVersion, DateTimeOffset.UtcNow); await AppendAsync(context.Folder, main, version, cancellationToken).ConfigureAwait(false); return version; } private ProjectVersion NewVersion(Guid projectId, Guid? parentId, int number, ProjectVersionKind kind, string portable, int framesVersion, DateTimeOffset now) => new() { Id = Guid.CreateVersion7(), ProjectId = projectId, ParentId = parentId, Number = number, Kind = kind, ClientName = ClientName, SettingsJson = portable, ContentHash = ProjectContent.Hash(portable), SettingsFormat = ProjectContent.FormatOf(portable), FramesVersion = framesVersion, CreatedAt = now, StoredAt = now, }; /// A new project with its first version: sidecars (version, then record), then both rows in one transaction. private async Task AddProjectAsync(SequencePaths folder, SequenceProject project, ProjectVersion first, CancellationToken cancellationToken) { var withHead = project with { HeadVersionId = first.Id, HeadNumber = first.Number }; ProjectSidecars.WriteVersion(folder.VersionPath(project.Id, first.Number, first.Id), first); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(project.Id), ProjectRecord.Of(withHead)); await using var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false); await tx.Projects.InsertAsync(withHead with { HeadVersionId = null, HeadNumber = 0 }, cancellationToken).ConfigureAwait(false); await tx.Projects.InsertVersionAsync(first, cancellationToken).ConfigureAwait(false); await tx.Projects.UpdateAsync(withHead, cancellationToken).ConfigureAwait(false); await tx.CommitAsync(cancellationToken).ConfigureAwait(false); return withHead; } /// A version appended to a project: its sidecar, the record's head, then the version row and the head in one transaction. private async Task AppendAsync(SequencePaths folder, SequenceProject project, ProjectVersion version, CancellationToken cancellationToken) { var now = DateTimeOffset.UtcNow; var advanced = project with { HeadVersionId = version.Id, HeadNumber = version.Number, UpdatedAt = now }; ProjectSidecars.WriteVersion(folder.VersionPath(project.Id, version.Number, version.Id), version); ProjectSidecars.WriteRecord(folder.ProjectRecordPath(project.Id), ProjectRecord.Of(advanced)); await using var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false); await tx.Projects.InsertVersionAsync(version, cancellationToken).ConfigureAwait(false); if (!await tx.Projects.AdvanceHeadAsync(project.Id, project.HeadNumber, version.Id, version.Number, now, cancellationToken).ConfigureAwait(false)) throw new IOException($"The head of project {project.Id} moved in the index while its lease was held."); await tx.CommitAsync(cancellationToken).ConfigureAwait(false); return advanced; } /// /// Writes Main's into the sequence: its positions placed on the sequence's frames, then /// project.json and the index's settings (and hero frame) with the settings version as the check. Recorded as /// mirrored when the index took it; a settings edit that won the race is caught up by the next operation (merged). /// private async Task MirrorAsync(LeaseContext context, SequenceProject main, ProjectVersion version, CancellationToken cancellationToken) { var frames = await LoadFramesAsync(context.Row.Id, cancellationToken).ConfigureAwait(false); var placed = ProjectSettings.FromJson(ProjectContent.Place(version.SettingsJson, frames.Photos)); var json = placed.ToJson(); Directory.CreateDirectory(context.Folder.MetaDirectory); AtomicFile.WriteAllText(context.Folder.ProjectPath, json); var hero = placed.HeroFrame is { } index && (uint)index < (uint)frames.Frames.Count ? frames.Frames[index].Id : Guid.Empty; var result = await _db.Sequences.EditAsync(context.Row.Id, new SequenceEdit { SettingsJson = json, HeroFrameId = hero }, context.Row.SettingsVersion, context.Row.FramesVersion, cancellationToken).ConfigureAwait(false); if (result is { Outcome: SequenceEditOutcome.Updated, Sequence: { } updated }) { context.Row = updated; await _db.Projects.SetMirroredAsync(main.Id, version.Id, updated.SettingsVersion, cancellationToken).ConfigureAwait(false); } else { Log.Info("projects", $"The settings of sequence {context.Row.Id} changed while version {version.Number} of Main was mirrored; merged next time"); } } /// Whether settings name no frame by position (every position parked by photo). private static bool IsPortable(string json) { try { return JsonNode.Parse(json, new JsonNodeOptions { PropertyNameCaseInsensitive = true }) is JsonObject project && project["excludedFrames"] is null or JsonArray { Count: 0 } && project["heroFrame"] is null && (project["camera"] is not JsonObject camera || camera["keyframes"] is null or JsonArray { Count: 0 }); } catch (Exception ex) when (ex is JsonException or ArgumentException) { return false; } } private async Task<(IReadOnlyList Frames, FramePhotos Photos)> LoadFramesAsync(Guid sequenceId, CancellationToken cancellationToken) { var frames = new List(); for (var after = -1; ;) { var page = await _db.Frames.ListBySequenceAsync(sequenceId, after, PageSize, cancellationToken).ConfigureAwait(false); frames.AddRange(page); if (page.Count < PageSize) break; after = page[^1].Index; } // With the encodings conversions joined: a version parked on a photo before its file was converted still finds its frame. return (frames, HomePhotos.Of(frames, await HomePhotos.AliasesAsync(_db, sequenceId, cancellationToken).ConfigureAwait(false))); } private async Task EnsureNameFreeAsync(Guid sequenceId, Guid? projectId, string name, CancellationToken cancellationToken) { var projects = await _db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false); if (projects.Any(p => p.Id != projectId && string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase))) throw new ProjectException(ProjectError.NameTaken, $"Sequence {sequenceId} has a project named '{name}'."); } private static string ValidName(string name) { var trimmed = name?.Trim() ?? string.Empty; return trimmed.Length is > 0 and <= MaxNameLength && !trimmed.Any(char.IsControl) ? trimmed : throw new ProjectException(ProjectError.InvalidName, "A project name has 1 to 60 characters."); } private async Task KnownAsync(Guid projectId, CancellationToken cancellationToken) => await _db.Projects.GetAsync(projectId, cancellationToken).ConfigureAwait(false) ?? throw new ProjectException(ProjectError.NotFound, $"Project {projectId} does not exist."); private async Task LiveAsync(Guid projectId, CancellationToken cancellationToken) => await KnownAsync(projectId, cancellationToken).ConfigureAwait(false) is { DeletedAt: null } project ? project : throw new ProjectException(ProjectError.NotFound, $"Project {projectId} was deleted."); private static ProjectException SequenceMissing(Guid sequenceId) => new(ProjectError.SequenceNotFound, $"Sequence {sequenceId} is not in the index."); /// The sequence as read under its lease (the mirror moves its settings version) and its folder. private sealed class LeaseContext(SequencePaths folder, Sequence row) { public SequencePaths Folder { get; } = folder; public Sequence Row { get; set; } = row; } }