feat(server): the server keeps its sequences' projects, pushed and pulled like a PC's archive
Owner review 2, phase 5b (R8): a server is a project home. A workspace can
list a sequence's projects, make, rename and delete them, read their
versions and push new ones through /api/v1/sequences/{sid}/projects. The
endpoints run on the archive's ProjectService exactly as a desktop's archive
does (sidecars first, then the index, under the sequence's lease), so no
version is stored apart and a desktop sharing the archive sees the same
projects.
- Protocol: project, version, create, rename, version page, push answer
(outcome, stored version, head, plus code and message on 409/422) and
home frames DTOs, the projectChanged event, the "projects" capability and
the ChronosClient calls; a push returns its outcome without throwing on
409 or 422. ProjectWire maps them to and from the model for both sides.
- Server: Read lists and reads, Edit makes, renames and pushes, Delete (the
sequence's owner, the project's maker or an admin) deletes; refusals with
stable codes, 503 while the sequence is busy, offline or moved; the
pushing account becomes the version's author; creations, renames and
deletions audited, pushes logged.
- ProjectService tells the host what each call changed, compared under the
lease, so projectChanged also follows Main's external versions; a Main
version mirrored into the settings sends sequenceChanged too, for older
clients. ProjectCatchUp takes settings a PATCH, an analysis or a
conversion wrote into Main at once, for sequences that have a Main.
- Renders of a project version: POST /jobs checks the project and version
against the sequence and pins a project alone to its head; render nodes
get the version's settings placed on the frames they claim; the render
records the project and version.
- Admin panel: the sequence page lists its projects (Main marked, head,
last change, versions on demand) with Rename and Delete for the others,
live on projectChanged. It edits no settings.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
917cddde41
commit
e2ecc409a3
30 files changed
+2261
-35
No files matched your search
@@ -62,6 +62,15 @@ public sealed record ArchiveServiceOptions
|
||||
/// </summary>
|
||||
public Func<Guid, string, CancellationToken, Task>? SequenceChanged { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Told after a call of <see cref="Projects.ProjectService"/> changed a sequence's projects (owner review 2, R8): a project
|
||||
/// made, renamed or deleted, a new head (a push, settings another path wrote taken as Main's external version, a merge, the
|
||||
/// versions another program sharing the archive wrote found in the sidecars), and whether the call wrote Main's head into
|
||||
/// the sequence's settings. Told once per call, after the sequence's lease is let go. A server tells its connected clients
|
||||
/// (BRIEF §14.3); a failure is logged and never fails the call.
|
||||
/// </summary>
|
||||
public Func<Projects.ProjectChangeSet, CancellationToken, Task>? ProjectsChanged { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The free bytes a write under a folder of an archive (its root, its staging folder) may count on, as the host counts
|
||||
/// them: a server takes the tighter of its space volume (owner review 2, R2) and the folder's own volume, the desktop asks
|
||||
|
||||
@@ -7,9 +7,9 @@ namespace Chronos.Data.Projects;
|
||||
/// <summary>
|
||||
/// The photos of a sequence's frames at its home as project positions name them (owner review 2, R8), with the encodings
|
||||
/// a conversion joined (R7): a version parked on a photo before its file was converted, or after a conversion was undone,
|
||||
/// still finds its frame.
|
||||
/// still finds its frame. A server builds the same for a sequence it is the home of, and sends its aliases to workspaces.
|
||||
/// </summary>
|
||||
internal static class HomePhotos
|
||||
public static class HomePhotos
|
||||
{
|
||||
/// <summary>The links between encodings of the sequence's photos: its tombstones of reason "replaced".</summary>
|
||||
public static async Task<IReadOnlyList<PhotoAlias>> AliasesAsync(ChronosRepositories db, Guid sequenceId, CancellationToken cancellationToken) =>
|
||||
|
||||
@@ -79,6 +79,19 @@ public sealed record PushResult(PushOutcome Outcome, ProjectVersion? Version, Pr
|
||||
/// </summary>
|
||||
public sealed record HomeFrames(int FramesVersion, IReadOnlyList<Frame> Frames, FramePhotos Photos);
|
||||
|
||||
/// <summary>
|
||||
/// One project a <see cref="ProjectService"/> call changed: <see cref="Change"/> is one of <see cref="Protocol.ProjectChanges"/>
|
||||
/// (<c>created</c>, <c>renamed</c>, <c>deleted</c>, <c>pushed</c>: a new head), with the project's head after the call.
|
||||
/// </summary>
|
||||
public sealed record ProjectChange(Guid ProjectId, string Change, int HeadNumber, Guid? HeadVersionId);
|
||||
|
||||
/// <summary>
|
||||
/// What one <see cref="ProjectService"/> call changed in a sequence's projects (<see cref="ArchiveServiceOptions.ProjectsChanged"/>):
|
||||
/// the projects, in index order, and whether it wrote Main's head into the sequence's settings (<see cref="SettingsChanged"/>:
|
||||
/// the sequence's settings version moved, which the code paths that read the sequence's settings want to know).
|
||||
/// </summary>
|
||||
public sealed record ProjectChangeSet(Guid SequenceId, IReadOnlyList<ProjectChange> Changes, bool SettingsChanged);
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
@@ -412,6 +425,20 @@ public sealed class ProjectService
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Brings the sequence's projects in step now (see the class remarks: the sidecars into the index, Main made, settings
|
||||
/// another path wrote appended to Main), as every call does first, but saying why when it cannot: what a host runs soon
|
||||
/// after it wrote a sequence's settings outside the projects, so Main takes them (and is told, <see
|
||||
/// cref="ArchiveServiceOptions.ProjectsChanged"/>) before anyone asks.
|
||||
/// </summary>
|
||||
/// <exception cref="ProjectException">The sequence is not in the index.</exception>
|
||||
/// <exception cref="ArchiveOfflineException">The archive is offline or read-only.</exception>
|
||||
/// <exception cref="LeaseUnavailableException">Another writer held the sequence for longer than a read waits.</exception>
|
||||
/// <exception cref="SequenceFolderMovedException">The sequence's folder does not hold it.</exception>
|
||||
/// <exception cref="ProjectsUnreadableException">The index has no Main and a project record cannot be read now (it may be Main's).</exception>
|
||||
public Task CatchUpAsync(Guid sequenceId, CancellationToken cancellationToken = default) =>
|
||||
UnderLeaseAsync(sequenceId, (_, _) => Task.FromResult(true), cancellationToken, ReadWait);
|
||||
|
||||
/// <summary>
|
||||
/// 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, the sequence busy for longer than the lease wait, or a project of it cannot be read now. A
|
||||
@@ -483,14 +510,87 @@ public sealed class ProjectService
|
||||
var (paths, _) = await LocateAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
||||
var availability = Availability(paths);
|
||||
if (availability != ArchiveAvailability.Online) throw new ArchiveOfflineException(paths.Root, availability);
|
||||
await using (await _writer.LockAsync(paths, sequenceId, wait, cancellationToken).ConfigureAwait(false))
|
||||
ProjectChangeSet? changed = null;
|
||||
try
|
||||
{
|
||||
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 _indexer.ReconcileAsync(folder, sequenceId, cancellationToken).ConfigureAwait(false));
|
||||
await CatchUpAsync(context, cancellationToken).ConfigureAwait(false);
|
||||
return await work(context, cancellationToken).ConfigureAwait(false);
|
||||
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();
|
||||
// Taken before the reconcile: the versions another program wrote into the sidecars are told too.
|
||||
var before = await WatchedAsync(sequenceId, cancellationToken).ConfigureAwait(false);
|
||||
LeaseContext? context = null;
|
||||
try
|
||||
{
|
||||
context = new LeaseContext(folder, row, await _indexer.ReconcileAsync(folder, sequenceId, cancellationToken).ConfigureAwait(false));
|
||||
await CatchUpAsync(context, cancellationToken).ConfigureAwait(false);
|
||||
return await work(context, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// What the reconcile, the catch-up and the work wrote stays written even when the work then refused or failed.
|
||||
if (before is not null)
|
||||
changed = await ChangesAsync(sequenceId, before, context is not null && row.SettingsVersion != context.Row.SettingsVersion).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (changed is not null) await TellAsync(changed).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The sequence's projects as the index has them now (deleted ones too), when someone is to be told what changes; else null.</summary>
|
||||
private async Task<Dictionary<Guid, SequenceProject>?> WatchedAsync(Guid sequenceId, CancellationToken cancellationToken) =>
|
||||
_writer.Options.ProjectsChanged is null
|
||||
? null
|
||||
: (await _db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: true, cancellationToken).ConfigureAwait(false)).ToDictionary(p => p.Id);
|
||||
|
||||
/// <summary>
|
||||
/// What changed in the sequence's projects since <paramref name="before"/> (a project new or back, deleted, renamed, or
|
||||
/// with a new head); null when nothing did. Never throws: it runs after the work, whatever became of it.
|
||||
/// </summary>
|
||||
private async Task<ProjectChangeSet?> ChangesAsync(Guid sequenceId, Dictionary<Guid, SequenceProject> before, bool settingsChanged)
|
||||
{
|
||||
IReadOnlyList<SequenceProject> after;
|
||||
try
|
||||
{
|
||||
after = await _db.Projects.ListBySequenceAsync(sequenceId, includeDeleted: true, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is DatabaseException or IOException or InvalidOperationException)
|
||||
{
|
||||
Log.Warning("projects", $"The project changes of sequence {sequenceId} cannot be read", ex);
|
||||
return null;
|
||||
}
|
||||
var changes = new List<ProjectChange>();
|
||||
foreach (var project in after)
|
||||
{
|
||||
before.TryGetValue(project.Id, out var was);
|
||||
var (live, wasLive) = (project.DeletedAt is null, was is { DeletedAt: null });
|
||||
if (live && !wasLive) changes.Add(Change(project, Protocol.ProjectChanges.Created));
|
||||
else if (!live && wasLive) changes.Add(Change(project, Protocol.ProjectChanges.Deleted));
|
||||
else if (live)
|
||||
{
|
||||
if (project.Name != was!.Name) changes.Add(Change(project, Protocol.ProjectChanges.Renamed));
|
||||
if (project.HeadVersionId != was.HeadVersionId) changes.Add(Change(project, Protocol.ProjectChanges.Pushed));
|
||||
}
|
||||
}
|
||||
return changes.Count == 0 && !settingsChanged ? null : new ProjectChangeSet(sequenceId, changes, settingsChanged);
|
||||
|
||||
static ProjectChange Change(SequenceProject project, string change) => new(project.Id, change, project.HeadNumber, project.HeadVersionId);
|
||||
}
|
||||
|
||||
/// <summary>Tells the host what a call changed; a failure is logged, never passed on.</summary>
|
||||
private async Task TellAsync(ProjectChangeSet changed)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _writer.Options.ProjectsChanged!(changed, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning("projects", $"The project changes of sequence {changed.SequenceId} were not told", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Processing;
|
||||
using Chronos.Protocol;
|
||||
|
||||
namespace Chronos.Data.Projects;
|
||||
|
||||
/// <summary>
|
||||
/// Projects and their versions on the wire (owner review 2, R8): what a server answers from its index, and how a client
|
||||
/// reads it back into the model a workspace works with, so a server is a project home like this PC's archive; with the
|
||||
/// wire names of the push outcomes.
|
||||
/// </summary>
|
||||
public static class ProjectWire
|
||||
{
|
||||
/// <summary>A project as the API shows it; <paramref name="head"/> (its head version) says who changed it last, and when.</summary>
|
||||
public static ProjectDto ToDto(SequenceProject project, ProjectVersion? head = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(project);
|
||||
return new ProjectDto(project.Id, project.SequenceId, project.Name, project.IsMain, project.HeadVersionId, project.HeadNumber, project.CreatedBy,
|
||||
project.CreatedAt, project.UpdatedAt, head is null ? null : ProjectVersionKinds.Name(head.Kind), head?.Author, head?.ClientName, head?.StoredAt);
|
||||
}
|
||||
|
||||
public static ProjectVersionDto ToDto(ProjectVersion version)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(version);
|
||||
return new ProjectVersionDto(version.Id, version.ProjectId, version.ParentId, version.Number, ProjectVersionKinds.Name(version.Kind), version.Author,
|
||||
version.ClientName, version.Note, version.SettingsJson, version.ContentHash, version.SettingsFormat, version.FramesVersion, version.RestoredFrom,
|
||||
version.CreatedAt, version.StoredAt);
|
||||
}
|
||||
|
||||
/// <summary>A project of a server sequence as the model knows it (the mirror fields stay the server's own).</summary>
|
||||
public static SequenceProject ToProject(ProjectDto dto)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(dto);
|
||||
return new SequenceProject
|
||||
{
|
||||
Id = dto.Id,
|
||||
SequenceId = dto.SequenceId,
|
||||
Name = dto.Name,
|
||||
IsMain = dto.IsMain,
|
||||
HeadVersionId = dto.HeadVersionId,
|
||||
HeadNumber = dto.HeadNumber,
|
||||
CreatedBy = dto.CreatedBy,
|
||||
CreatedAt = dto.CreatedAt,
|
||||
UpdatedAt = dto.UpdatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>A version as the model knows it; a kind this build does not know reads as a commit (<see cref="ProjectVersionKinds.Parse"/>).</summary>
|
||||
public static ProjectVersion ToVersion(ProjectVersionDto dto)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(dto);
|
||||
return new ProjectVersion
|
||||
{
|
||||
Id = dto.Id,
|
||||
ProjectId = dto.ProjectId,
|
||||
ParentId = dto.ParentId,
|
||||
Number = dto.Number,
|
||||
Kind = ProjectVersionKinds.Parse(dto.Kind),
|
||||
Author = dto.Author,
|
||||
ClientName = dto.ClientName,
|
||||
Note = dto.Note,
|
||||
SettingsJson = dto.SettingsJson,
|
||||
ContentHash = dto.ContentHash,
|
||||
SettingsFormat = dto.SettingsFormat,
|
||||
FramesVersion = dto.FramesVersion,
|
||||
RestoredFrom = dto.RestoredFrom,
|
||||
CreatedAt = dto.CreatedAt,
|
||||
StoredAt = dto.StoredAt,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The wire name of <paramref name="outcome"/> (<see cref="ProjectPushOutcomes"/>).</summary>
|
||||
public static string Outcome(PushOutcome outcome) => outcome switch
|
||||
{
|
||||
PushOutcome.Stored => ProjectPushOutcomes.Stored,
|
||||
PushOutcome.AlreadyStored => ProjectPushOutcomes.AlreadyStored,
|
||||
PushOutcome.Diverged => ProjectPushOutcomes.Diverged,
|
||||
PushOutcome.HashMismatch => ProjectPushOutcomes.HashMismatch,
|
||||
PushOutcome.NewerFormat => ProjectPushOutcomes.NewerFormat,
|
||||
PushOutcome.TooLarge => ProjectPushOutcomes.TooLarge,
|
||||
_ => ProjectPushOutcomes.Invalid,
|
||||
};
|
||||
|
||||
/// <summary>The outcome a wire name stands for; one this build does not know is a refusal (<see cref="PushOutcome.Invalid"/>).</summary>
|
||||
public static PushOutcome ParseOutcome(string? name) => name switch
|
||||
{
|
||||
ProjectPushOutcomes.Stored => PushOutcome.Stored,
|
||||
ProjectPushOutcomes.AlreadyStored => PushOutcome.AlreadyStored,
|
||||
ProjectPushOutcomes.Diverged => PushOutcome.Diverged,
|
||||
ProjectPushOutcomes.HashMismatch => PushOutcome.HashMismatch,
|
||||
ProjectPushOutcomes.NewerFormat => PushOutcome.NewerFormat,
|
||||
ProjectPushOutcomes.TooLarge => PushOutcome.TooLarge,
|
||||
_ => PushOutcome.Invalid,
|
||||
};
|
||||
|
||||
/// <summary>A push's answer as <see cref="ProjectService.PushAsync"/> gives it on this PC's archive.</summary>
|
||||
public static PushResult ToResult(ProjectPushResultDto dto)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(dto);
|
||||
return new PushResult(ParseOutcome(dto.Outcome), dto.Version is null ? null : ToVersion(dto.Version), dto.Head is null ? null : ToVersion(dto.Head));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The links a server sends with a sequence's frames (<see cref="ProjectFramesDto.Aliases"/>): its tombstones of reason
|
||||
/// <c>replaced</c> with their replacement.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<RemovedPhotoDto> Aliases(IEnumerable<FrameRemoval> removals) =>
|
||||
Sync.SyncTombstones.ToDto(removals.Where(r => r.Reason == RemovalReason.Replaced && !string.IsNullOrEmpty(r.ReplacementSha256)));
|
||||
|
||||
/// <summary>
|
||||
/// The frames a server sequence's project positions are placed on, as <see cref="ProjectService.FramesAsync"/> gives a
|
||||
/// sequence of this PC's archive: rows in index order and their photos, with the encodings its conversions joined.
|
||||
/// </summary>
|
||||
public static HomeFrames ToHomeFrames(ProjectFramesDto dto)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(dto);
|
||||
var frames = dto.Frames.Select(RenderNodeClient.ToFrame).OrderBy(f => f.Index).ToList();
|
||||
var aliases = (dto.Aliases ?? []).Where(a => !string.IsNullOrEmpty(a.Sha256) && !string.IsNullOrEmpty(a.Replacement))
|
||||
.Select(a => new PhotoAlias(a.Sha256, a.Replacement!)).ToList();
|
||||
return new HomeFrames(dto.FramesVersion, frames, HomePhotos.Of(frames, aliases));
|
||||
}
|
||||
}
|
||||
@@ -472,6 +472,98 @@ public sealed class ChronosClient : IDisposable
|
||||
public Task ReportNodeFailureAsync(Guid nodeId, Guid jobId, string error, CancellationToken ct = default) =>
|
||||
SendAsync(HttpMethod.Post, $"api/v1/nodes/{nodeId}/jobs/{jobId}/failed", new NodeJobFailure(error), ProtocolJson.Default.NodeJobFailure, ct);
|
||||
|
||||
// ---------------- Projects (owner review 2, R8; servers with the "projects" capability) ----------------
|
||||
|
||||
/// <summary>The live projects of a server sequence, Main first (made from the sequence's settings the first time they are read).</summary>
|
||||
public Task<List<ProjectDto>> ListProjectsAsync(Guid sequenceId, CancellationToken ct = default) =>
|
||||
GetAsync($"api/v1/sequences/{sequenceId}/projects", ProtocolJson.Default.ListProjectDto, ct);
|
||||
|
||||
public Task<ProjectDto> GetProjectAsync(Guid sequenceId, Guid projectId, CancellationToken ct = default) =>
|
||||
GetAsync($"api/v1/sequences/{sequenceId}/projects/{projectId}", ProtocolJson.Default.ProjectDto, ct);
|
||||
|
||||
/// <summary>
|
||||
/// A new project of the sequence, its first version copied from <see cref="CreateProjectRequest.FromVersionId"/> or from
|
||||
/// Main's head; a name refused is a 400 <c>invalid_name</c> or 409 <c>name_taken</c> <see cref="ChronosApiException"/>.
|
||||
/// </summary>
|
||||
public Task<ProjectDto> CreateProjectAsync(Guid sequenceId, CreateProjectRequest request, CancellationToken ct = default) =>
|
||||
SendAsync(HttpMethod.Post, $"api/v1/sequences/{sequenceId}/projects", request, ProtocolJson.Default.CreateProjectRequest, ProtocolJson.Default.ProjectDto, ct);
|
||||
|
||||
/// <summary>Gives a project another name; a name refused is a 400 <c>invalid_name</c> or 409 <c>name_taken</c> <see cref="ChronosApiException"/>.</summary>
|
||||
public Task<ProjectDto> RenameProjectAsync(Guid sequenceId, Guid projectId, string name, CancellationToken ct = default) =>
|
||||
SendAsync(HttpMethod.Patch, $"api/v1/sequences/{sequenceId}/projects/{projectId}", new RenameProjectRequest(name), ProtocolJson.Default.RenameProjectRequest,
|
||||
ProtocolJson.Default.ProjectDto, ct);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a project and its versions into the sequence's projects' trash (never erased); Main is refused (409
|
||||
/// <c>main_cannot_be_deleted</c>).
|
||||
/// </summary>
|
||||
public Task DeleteProjectAsync(Guid sequenceId, Guid projectId, CancellationToken ct = default) =>
|
||||
SendNoContentAsync(HttpMethod.Delete, $"api/v1/sequences/{sequenceId}/projects/{projectId}", ct);
|
||||
|
||||
/// <summary>The project's newest version.</summary>
|
||||
public Task<ProjectVersionDto> GetProjectHeadAsync(Guid sequenceId, Guid projectId, CancellationToken ct = default) =>
|
||||
GetAsync($"api/v1/sequences/{sequenceId}/projects/{projectId}/head", ProtocolJson.Default.ProjectVersionDto, ct);
|
||||
|
||||
/// <summary>
|
||||
/// One page of a project's versions: newest first, numbered below <paramref name="before"/> (from the head when null),
|
||||
/// or, with <paramref name="after"/>, oldest first numbered above it (what a workspace pulls). <paramref name="limit"/>:
|
||||
/// 1–200 (the server's default, 50 newest first and 100 oldest first, when null).
|
||||
/// </summary>
|
||||
public Task<ProjectVersionPageDto> ListProjectVersionsAsync(Guid sequenceId, Guid projectId, int? before = null, int? after = null, int? limit = null,
|
||||
CancellationToken ct = default) =>
|
||||
GetAsync($"api/v1/sequences/{sequenceId}/projects/{projectId}/versions?{N("before", before)}{N("after", after)}{N("limit", limit)}".TrimEnd('&', '?'),
|
||||
ProtocolJson.Default.ProjectVersionPageDto, ct);
|
||||
|
||||
/// <summary>A version of the project by id; null when the server has no such version of it.</summary>
|
||||
public async Task<ProjectVersionDto?> GetProjectVersionAsync(Guid sequenceId, Guid projectId, Guid versionId, CancellationToken ct = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await GetAsync($"api/v1/sequences/{sequenceId}/projects/{projectId}/versions/{versionId}", ProtocolJson.Default.ProjectVersionDto, ct)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.NotFound && ex.Error.Details == "version")
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pushes <paramref name="version"/>, made in a workspace from the project's head (<see cref="ProjectVersionDto.ParentId"/>):
|
||||
/// the server stores it as the new head, numbered head + 1 (201, <c>stored</c>), or answers it again for a repeated push
|
||||
/// (200, <c>already_stored</c>), or refuses it, storing nothing: 409 <c>diverged</c> with the head, 422
|
||||
/// <c>hash_mismatch</c>, <c>newer_format</c> or <c>invalid</c>. These come back as <see cref="ProjectPushResultDto"/>
|
||||
/// without throwing; any other failure (the sequence or project gone, no permission, the archive offline or the sequence
|
||||
/// busy: 503) throws <see cref="ChronosApiException"/>. Safe to repeat: the version's id makes a second push a no-op.
|
||||
/// </summary>
|
||||
public async Task<ProjectPushResultDto> PushProjectVersionAsync(Guid sequenceId, Guid projectId, ProjectVersionDto version, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(version);
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v1/sequences/{sequenceId}/projects/{projectId}/versions")
|
||||
{
|
||||
Content = JsonContent.Create(version, ProtocolJson.Default.ProjectVersionDto),
|
||||
};
|
||||
using var response = await SendRawAsync(request, ct).ConfigureAwait(false);
|
||||
if (response.StatusCode is HttpStatusCode.OK or HttpStatusCode.Created or HttpStatusCode.Conflict or HttpStatusCode.UnprocessableEntity)
|
||||
{
|
||||
var body = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (JsonSerializer.Deserialize(body, ProtocolJson.Default.ProjectPushResultDto) is { Outcome.Length: > 0 } result) return result;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
// Not a push answer (a proxy's page): the status says what failed.
|
||||
}
|
||||
}
|
||||
await EnsureSuccessAsync(response, ct).ConfigureAwait(false);
|
||||
throw new ChronosApiException(response.StatusCode, new ApiError("http_" + (int)response.StatusCode, response.ReasonPhrase ?? "error"));
|
||||
}
|
||||
|
||||
/// <summary>The sequence's frames and photo aliases as of one frames version: what its projects' positions are placed on.</summary>
|
||||
public Task<ProjectFramesDto> GetProjectFramesAsync(Guid sequenceId, CancellationToken ct = default) =>
|
||||
GetAsync($"api/v1/sequences/{sequenceId}/projects/frames", ProtocolJson.Default.ProjectFramesDto, ct);
|
||||
|
||||
// ---------------- Events ----------------
|
||||
|
||||
/// <summary>Server events over WebSocket until cancelled; reconnect is the caller's policy.</summary>
|
||||
@@ -606,4 +698,8 @@ public sealed class ChronosClient : IDisposable
|
||||
};
|
||||
|
||||
private static string Q(string name, string? value) => string.IsNullOrEmpty(value) ? string.Empty : "&" + name + "=" + Uri.EscapeDataString(value);
|
||||
|
||||
/// <summary>A number of the query (<c>name=value&</c>); empty when null.</summary>
|
||||
private static string N(string name, int? value) =>
|
||||
value is { } n ? name + "=" + n.ToString(System.Globalization.CultureInfo.InvariantCulture) + "&" : string.Empty;
|
||||
}
|
||||
@@ -23,5 +23,15 @@ public sealed record ServerInfo(
|
||||
ServerLimits Limits,
|
||||
IReadOnlyList<string>? Extensions = null);
|
||||
|
||||
/// <summary>Names of <see cref="ServerInfo.Capabilities"/> that clients act on.</summary>
|
||||
public static class ServerCapabilities
|
||||
{
|
||||
/// <summary>
|
||||
/// The server keeps its sequences' projects (owner review 2, R8): <c>/api/v1/sequences/{sid}/projects</c>, the
|
||||
/// <c>projectChanged</c> event, renders of a project version. A server without it knows only the sequence's settings.
|
||||
/// </summary>
|
||||
public const string Projects = "projects";
|
||||
}
|
||||
|
||||
/// <summary><c>GET /health</c>.</summary>
|
||||
public sealed record HealthStatus(string Status, string Version, IReadOnlyDictionary<string, string> Checks);
|
||||
@@ -4,7 +4,8 @@ namespace Chronos.Protocol;
|
||||
|
||||
/// <summary>
|
||||
/// Messages pushed on <c>WS /api/v1/events</c>. Each frame is one JSON object with a <c>type</c> discriminator:
|
||||
/// jobProgress, jobState, sequenceChanged, cardEvent, nodeState, scheduleChanged.
|
||||
/// jobProgress, jobState, sequenceChanged, cardEvent, nodeState, scheduleChanged, projectChanged. A client ignores the types it
|
||||
/// does not know (an older client those of a newer server).
|
||||
/// </summary>
|
||||
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
|
||||
[JsonDerivedType(typeof(JobProgressEvent), "jobProgress")]
|
||||
@@ -13,6 +14,7 @@ namespace Chronos.Protocol;
|
||||
[JsonDerivedType(typeof(CardEvent), "cardEvent")]
|
||||
[JsonDerivedType(typeof(NodeStateEvent), "nodeState")]
|
||||
[JsonDerivedType(typeof(ScheduleChangedEvent), "scheduleChanged")]
|
||||
[JsonDerivedType(typeof(ProjectChangedEvent), "projectChanged")]
|
||||
public abstract record ServerEvent;
|
||||
|
||||
public sealed record JobProgressEvent(Guid JobId, long Done, long Total, double? ItemsPerSecond, double? EtaSeconds) : ServerEvent;
|
||||
@@ -48,3 +50,12 @@ public sealed record NodeStateEvent(Guid NodeId, string ClientName, string State
|
||||
/// included, or its next run moved (owner review 2, R10): the admin panel's Scheduled tab reads the list again.
|
||||
/// </summary>
|
||||
public sealed record ScheduleChangedEvent(Guid ScheduleId, bool Deleted) : ServerEvent;
|
||||
|
||||
/// <summary>
|
||||
/// A project of a sequence changed (owner review 2, R8): <see cref="Change"/> is one of <see cref="ProjectChanges"/>
|
||||
/// (<c>created</c>, <c>renamed</c>, <c>deleted</c>, <c>pushed</c>), <see cref="HeadNumber"/> and <see cref="HeadVersionId"/>
|
||||
/// its head after the change. Sent for the API's own changes and when Main takes settings another path wrote (a
|
||||
/// <c>PATCH</c> of the sequence's settings, an analysis, the sync of a desktop's copy, a conversion); a workspace with that
|
||||
/// project checked out knows its home moved, and the admin panel reads the projects again.
|
||||
/// </summary>
|
||||
public sealed record ProjectChangedEvent(Guid SequenceId, Guid ProjectId, string Change, int HeadNumber, Guid? HeadVersionId) : ServerEvent;
|
||||
@@ -0,0 +1,157 @@
|
||||
namespace Chronos.Protocol;
|
||||
|
||||
/// <summary>
|
||||
/// A project of a server sequence (owner review 2, R8; <c>/api/v1/sequences/{sid}/projects</c>): a named set of render
|
||||
/// settings ("4K landscape", "Vertical social") with a linear history of versions. Every sequence has one Main project
|
||||
/// (<see cref="IsMain"/>, listed first), whose head is the sequence's own settings: an old client's <c>PATCH
|
||||
/// /sequences/{id}</c> of the settings becomes a version of it. <see cref="HeadKind"/>, <see cref="HeadAuthor"/>,
|
||||
/// <see cref="HeadClientName"/> and <see cref="HeadStoredAt"/> describe the head version: who changed the project last,
|
||||
/// and when. <see cref="CreatedBy"/> is the account that made the project (null for Main, made from the settings).
|
||||
/// </summary>
|
||||
public sealed record ProjectDto(
|
||||
Guid Id,
|
||||
Guid SequenceId,
|
||||
string Name,
|
||||
bool IsMain,
|
||||
Guid? HeadVersionId,
|
||||
int HeadNumber,
|
||||
string? CreatedBy,
|
||||
DateTimeOffset CreatedAt,
|
||||
DateTimeOffset UpdatedAt,
|
||||
string? HeadKind = null,
|
||||
string? HeadAuthor = null,
|
||||
string? HeadClientName = null,
|
||||
DateTimeOffset? HeadStoredAt = null);
|
||||
|
||||
/// <summary>
|
||||
/// One version of a project, immutable once stored (owner review 2, R8). <see cref="SettingsJson"/> is the portable form of
|
||||
/// the settings (every frame position parked by photo, canonical text) and <see cref="ContentHash"/> the lower-case hex
|
||||
/// SHA-256 of its UTF-8 bytes, which every side checks. <see cref="Kind"/> is <c>commit</c>, <c>merge</c>, <c>restore</c>,
|
||||
/// <c>imported</c> or <c>external</c> (Main only: settings written outside the projects). <see cref="SettingsFormat"/> is the
|
||||
/// settings' format (a newer one opens read-only), <see cref="FramesVersion"/> the frames version they were made against,
|
||||
/// <see cref="RestoredFrom"/> the version a restore brought back. <see cref="StoredAt"/> is null for a version a workspace
|
||||
/// has not pushed. A push (<c>POST …/versions</c>) sends the version as the workspace made it, its parent the head it knew:
|
||||
/// the server numbers it (head + 1), records the account that pushed it as its <see cref="Author"/> and stores it.
|
||||
/// </summary>
|
||||
public sealed record ProjectVersionDto(
|
||||
Guid Id,
|
||||
Guid ProjectId,
|
||||
Guid? ParentId,
|
||||
int Number,
|
||||
string Kind,
|
||||
string? Author,
|
||||
string? ClientName,
|
||||
string? Note,
|
||||
string SettingsJson,
|
||||
string ContentHash,
|
||||
int SettingsFormat,
|
||||
int FramesVersion,
|
||||
Guid? RestoredFrom,
|
||||
DateTimeOffset CreatedAt,
|
||||
DateTimeOffset? StoredAt);
|
||||
|
||||
/// <summary>
|
||||
/// <c>POST /api/v1/sequences/{sid}/projects</c>: a new project named <see cref="Name"/> (1–60 characters, unique among the
|
||||
/// sequence's projects whatever the case), whose first version (kind <c>imported</c>) holds the settings of
|
||||
/// <see cref="FromVersionId"/> (a version of any project of the sequence), or of Main's head.
|
||||
/// </summary>
|
||||
public sealed record CreateProjectRequest(string Name, Guid? FromVersionId = null);
|
||||
|
||||
/// <summary><c>PATCH /api/v1/sequences/{sid}/projects/{pid}</c>: the project's new name (1–60 characters, unique among the sequence's projects).</summary>
|
||||
public sealed record RenameProjectRequest(string Name);
|
||||
|
||||
/// <summary>
|
||||
/// <c>GET /api/v1/sequences/{sid}/projects/{pid}/versions</c>: one page of a project's versions, newest first (below
|
||||
/// <c>before</c>, from the head when absent) or oldest first with <c>after</c> (what a workspace pulls). <see cref="More"/>:
|
||||
/// versions follow this page in its order. <see cref="HeadNumber"/> is the project's head as the page was read.
|
||||
/// </summary>
|
||||
public sealed record ProjectVersionPageDto(IReadOnlyList<ProjectVersionDto> Items, int HeadNumber, bool More);
|
||||
|
||||
/// <summary>
|
||||
/// The answer to a push (<c>POST /api/v1/sequences/{sid}/projects/{pid}/versions</c>): <see cref="Outcome"/> (one of
|
||||
/// <see cref="ProjectPushOutcomes"/>), the version as stored (<c>stored</c>: 201, <c>already_stored</c>: 200, a push repeated
|
||||
/// after its answer was lost) and the project's head. A refusal stores nothing and carries the error's <see cref="Code"/>
|
||||
/// and its sentence in the request's language (<see cref="Message"/>), so the body also reads as an <see cref="ApiError"/>:
|
||||
/// 409 <c>project_diverged</c> (<c>diverged</c>: the head moved past the version's parent; pull the head, replay the version
|
||||
/// on it and push again), 422 <c>hash_mismatch</c> (the settings do not match the content hash), <c>newer_format</c>
|
||||
/// (settings of a newer format than the server reads), <c>invalid_version</c> (<c>invalid</c>: positions not parked by
|
||||
/// photo, or the id is another project's version).
|
||||
/// </summary>
|
||||
public sealed record ProjectPushResultDto(string Outcome, ProjectVersionDto? Version, ProjectVersionDto? Head, string? Code = null, string? Message = null);
|
||||
|
||||
/// <summary>
|
||||
/// <c>GET /api/v1/sequences/{sid}/projects/frames</c>: what a workspace places a server sequence's project positions on, as
|
||||
/// of one frames version: every frame in index order (as <c>GET /sequences/{id}/frames</c> gives them) and the links between
|
||||
/// encodings of one photo, the sequence's tombstones of reason <c>replaced</c> (a conversion's original and its replacement;
|
||||
/// a position parked on either finds the frame that holds the other). <see cref="Aliases"/> is null when there are none.
|
||||
/// </summary>
|
||||
public sealed record ProjectFramesDto(Guid SequenceId, int FramesVersion, IReadOnlyList<FrameDto> Frames, IReadOnlyList<RemovedPhotoDto>? Aliases = null);
|
||||
|
||||
/// <summary>What became of a pushed version (<see cref="ProjectPushResultDto.Outcome"/>).</summary>
|
||||
public static class ProjectPushOutcomes
|
||||
{
|
||||
/// <summary>Stored as the new head: its parent was the head (a fast-forward). 201.</summary>
|
||||
public const string Stored = "stored";
|
||||
|
||||
/// <summary>A version with that id and content is stored already (a repeated push): nothing changed. 200.</summary>
|
||||
public const string AlreadyStored = "already_stored";
|
||||
|
||||
/// <summary>The head moved past the version's parent. 409 <c>project_diverged</c>.</summary>
|
||||
public const string Diverged = "diverged";
|
||||
|
||||
/// <summary>The settings do not match the content hash. 422 <c>hash_mismatch</c>.</summary>
|
||||
public const string HashMismatch = "hash_mismatch";
|
||||
|
||||
/// <summary>Settings of a newer format than the server reads. 422 <c>newer_format</c>.</summary>
|
||||
public const string NewerFormat = "newer_format";
|
||||
|
||||
/// <summary>Not a version of this project (positions not parked by photo, or another project's id). 422 <c>invalid_version</c>.</summary>
|
||||
public const string Invalid = "invalid";
|
||||
|
||||
/// <summary>The version's file would be larger than a version may be (64 MiB). 422 <c>too_large</c>.</summary>
|
||||
public const string TooLarge = "too_large";
|
||||
}
|
||||
|
||||
/// <summary>Changes of a project a <see cref="ProjectChangedEvent"/> tells.</summary>
|
||||
public static class ProjectChanges
|
||||
{
|
||||
/// <summary>A project was made (Main too, the first time the sequence's projects are read).</summary>
|
||||
public const string Created = "created";
|
||||
|
||||
public const string Renamed = "renamed";
|
||||
|
||||
/// <summary>A project left the sequence (into its projects' trash).</summary>
|
||||
public const string Deleted = "deleted";
|
||||
|
||||
/// <summary>
|
||||
/// The project has a new head: a version pushed, settings written outside the projects taken as Main's external version
|
||||
/// (an older client, an analysis, the sync of a desktop's copy), a merge, or versions another program sharing the archive
|
||||
/// wrote.
|
||||
/// </summary>
|
||||
public const string Pushed = "pushed";
|
||||
}
|
||||
|
||||
/// <summary>Error codes of the project calls that clients act on (owner review 2, R8).</summary>
|
||||
public static class ProjectErrors
|
||||
{
|
||||
/// <summary>409 from a push: the head moved past the version's parent (the answer carries the head).</summary>
|
||||
public const string Diverged = "project_diverged";
|
||||
|
||||
/// <summary>422 from a push: the settings do not match their content hash.</summary>
|
||||
public const string HashMismatch = "hash_mismatch";
|
||||
|
||||
/// <summary>422 from a push: settings of a newer format than the server reads.</summary>
|
||||
public const string NewerFormat = "newer_format";
|
||||
|
||||
/// <summary>422 from a push: not a version of this project.</summary>
|
||||
public const string InvalidVersion = "invalid_version";
|
||||
|
||||
/// <summary>400: a project name is blank, longer than 60 characters or holds a control character.</summary>
|
||||
public const string InvalidName = "invalid_name";
|
||||
|
||||
/// <summary>409: another project of the sequence has that name (case does not count).</summary>
|
||||
public const string NameTaken = "name_taken";
|
||||
|
||||
/// <summary>409: the Main project holds the sequence's own settings and is never deleted.</summary>
|
||||
public const string MainCannotBeDeleted = "main_cannot_be_deleted";
|
||||
}
|
||||
@@ -91,4 +91,12 @@ namespace Chronos.Protocol;
|
||||
[JsonSerializable(typeof(ReorganizePlanDto))]
|
||||
[JsonSerializable(typeof(ConvertRequest))]
|
||||
[JsonSerializable(typeof(ConvertPlanDto))]
|
||||
[JsonSerializable(typeof(ProjectDto))]
|
||||
[JsonSerializable(typeof(List<ProjectDto>))]
|
||||
[JsonSerializable(typeof(ProjectVersionDto))]
|
||||
[JsonSerializable(typeof(CreateProjectRequest))]
|
||||
[JsonSerializable(typeof(RenameProjectRequest))]
|
||||
[JsonSerializable(typeof(ProjectVersionPageDto))]
|
||||
[JsonSerializable(typeof(ProjectPushResultDto))]
|
||||
[JsonSerializable(typeof(ProjectFramesDto))]
|
||||
public sealed partial class ProtocolJson : JsonSerializerContext;
|
||||
@@ -387,6 +387,13 @@ textarea.input.notes { height: auto; min-height: 64px; padding: 6px 10px; resize
|
||||
.frame-badge.hero { color: var(--sun); }
|
||||
.frame-badge.warn { top: auto; bottom: 4px; inset-inline: 4px auto; color: var(--warning); }
|
||||
.table.renders { grid-template-columns: minmax(100px, 1.4fr) max-content max-content max-content max-content max-content; }
|
||||
/* A sequence's projects (owner review 2, R8) and, under one opened, its versions. */
|
||||
.table.projects { grid-template-columns: minmax(120px, 1.4fr) max-content minmax(120px, 1.2fr) max-content; }
|
||||
.table.projects.fit-1 { grid-template-columns: minmax(120px, 1fr) max-content max-content; }
|
||||
.table.projects.fit-1 .col-changed { display: none; }
|
||||
.versions { display: grid; grid-template-columns: max-content max-content minmax(0, max-content) minmax(0, 1fr) max-content; gap: 6px 18px; align-items: center; padding: 10px 18px; background: var(--bg); border-top: 1px solid var(--stroke); }
|
||||
.versions .version { display: contents; }
|
||||
.versions .more { grid-column: 1 / -1; justify-self: start; }
|
||||
.table.trash { grid-template-columns: max-content minmax(140px, 2fr) max-content max-content minmax(60px, 1fr) max-content max-content; }
|
||||
.table.trash.fit-1 { grid-template-columns: max-content minmax(120px, 2fr) max-content max-content max-content max-content; }
|
||||
.table.trash.fit-1 .col-by { display: none; }
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
// The projects of a sequence on the panel (owner review 2, R8): each project's name, Main marked, its newest version and who
|
||||
// changed it last, its versions on demand, and Rename and Delete for the projects other than Main. Nothing here edits a
|
||||
// project's settings: the desktop does, in its workspace, and pushes its versions here. The list follows projectChanged.
|
||||
import { t, plural, applyStrings } from "../../i18n.js";
|
||||
import { escapeHtml, attr, formatNumber, formatDateTime, toast, errorText, confirmDialog, formDialog, emptyState, markInvalid, redraw, fitColumns } from "../../ui.js";
|
||||
|
||||
/** Versions read at a time when a project is opened, and with each Load more. */
|
||||
const VERSION_PAGE = 20;
|
||||
/** A project name, as the server takes it (ProjectService.MaxNameLength). */
|
||||
const MAX_NAME = 60;
|
||||
/** The name every sequence's Main project is made with; shown in the panel's language. */
|
||||
const MAIN_NAME = "Main";
|
||||
/** How long after a projectChanged the projects are read again; one read covers every change meanwhile. */
|
||||
const AFTER_CHANGE_MS = 500;
|
||||
/** Version kinds the panel names (ProjectVersionKinds). */
|
||||
const KINDS = { commit: "Commit", merge: "Merge", restore: "Restore", imported: "Imported", external: "External" };
|
||||
|
||||
/**
|
||||
* Draws the Projects section of sequence sequenceId into host (its count into meta) and keeps it current. Returns
|
||||
* onEvent(e), which the sequence page passes every server event to.
|
||||
*/
|
||||
export function projectsSection(host, meta, { api, sequenceId }) {
|
||||
const base = `/api/v1/sequences/${encodeURIComponent(sequenceId)}/projects`;
|
||||
let projects = [];
|
||||
/** The versions read of each project opened: { items, more }. */
|
||||
const versions = new Map();
|
||||
const expanded = new Set();
|
||||
let timer = null;
|
||||
let failed = null;
|
||||
|
||||
host.innerHTML = `
|
||||
<div class="table projects" data-levels="1" role="table" aria-labelledby="projects-title">
|
||||
<div class="thead" role="row"><span role="columnheader" data-i18n="Admin.Projects.Name"></span>
|
||||
<span role="columnheader" class="end" data-i18n="Admin.Projects.Head"></span>
|
||||
<span role="columnheader" class="col-changed" data-i18n="Admin.Projects.Changed"></span>
|
||||
<span role="columnheader"><span class="sr-only" data-i18n="Admin.Common.Actions"></span></span></div>
|
||||
<div role="rowgroup" id="project-rows"></div>
|
||||
</div>`;
|
||||
applyStrings(host);
|
||||
const table = host.querySelector(".table");
|
||||
const rows = host.querySelector("#project-rows");
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
projects = await api.get(base);
|
||||
failed = null;
|
||||
// A project deleted meanwhile (here or elsewhere) closes; the others' versions are read again.
|
||||
for (const id of expanded) {
|
||||
if (!projects.some((p) => p.id === id)) forget(id);
|
||||
}
|
||||
await Promise.all([...expanded].map((id) => loadVersions(id).catch(() => forget(id))));
|
||||
} catch (err) {
|
||||
projects = [];
|
||||
failed = errorText(err);
|
||||
}
|
||||
if (!host.isConnected) return;
|
||||
meta.textContent = failed ? "" : plural("Admin.Projects.Count", projects.length);
|
||||
draw();
|
||||
}
|
||||
|
||||
/** The newest versions of a project, or with more the next page after those shown. */
|
||||
async function loadVersions(id, more = false) {
|
||||
const known = versions.get(id);
|
||||
const before = more && known?.items.length ? `&before=${known.items[known.items.length - 1].number}` : "";
|
||||
const page = await api.get(`${base}/${encodeURIComponent(id)}/versions?limit=${VERSION_PAGE}${before}`);
|
||||
versions.set(id, { items: more && known ? known.items.concat(page.items) : page.items, more: page.more });
|
||||
}
|
||||
|
||||
function forget(id) {
|
||||
expanded.delete(id);
|
||||
versions.delete(id);
|
||||
}
|
||||
|
||||
function draw() {
|
||||
redraw(rows, () => {
|
||||
rows.innerHTML = failed
|
||||
? `<div class="row" role="row"><div class="wide empty" role="cell">${escapeHtml(failed)}</div></div>`
|
||||
: projects.map(rowHtml).join("") || `<div class="row" role="row"><div class="wide" role="cell">${emptyState("Admin.Projects.Empty")}</div></div>`;
|
||||
applyStrings(rows);
|
||||
});
|
||||
rows.querySelectorAll("[data-toggle]").forEach((b) => b.addEventListener("click", () => toggle(b.dataset.toggle)));
|
||||
rows.querySelectorAll("[data-act]").forEach((b) => b.addEventListener("click", () => act(b.dataset.act, find(b.closest("[data-key]").dataset.key))));
|
||||
rows.querySelectorAll("[data-more-versions]").forEach((b) => b.addEventListener("click", () => moreVersions(b.dataset.moreVersions)));
|
||||
fitColumns(table);
|
||||
}
|
||||
|
||||
function find(id) {
|
||||
return projects.find((p) => p.id === id);
|
||||
}
|
||||
|
||||
/** Main keeps the name it was made with in the panel's language; a Main renamed shows its name and the Main mark. */
|
||||
function projectName(p) {
|
||||
return p.isMain && p.name === MAIN_NAME ? t("Admin.Projects.Main") : p.name;
|
||||
}
|
||||
|
||||
function rowHtml(p) {
|
||||
const name = projectName(p);
|
||||
const mark = p.isMain && p.name !== MAIN_NAME
|
||||
? `<span class="pill" data-i18n="Admin.Projects.Main" data-i18n-title="Admin.Projects.Main.Tip"></span>` : "";
|
||||
const who = p.headAuthor ?? p.headClientName ?? "";
|
||||
const when = p.headStoredAt ?? p.updatedAt;
|
||||
const actions = p.isMain ? "" : `
|
||||
<button class="btn small" data-act="rename" data-i18n="Admin.Projects.Rename" data-i18n-title="Admin.Projects.Rename.Tip"></button>
|
||||
<button class="btn small destructive" data-act="delete" data-i18n="Admin.Projects.Delete" data-i18n-title="Admin.Projects.Delete.Tip"></button>`;
|
||||
return `
|
||||
<div class="row" role="row" data-key="${attr(p.id)}">
|
||||
<span class="name-cell" role="cell"><button class="link ellipsis" data-toggle="${attr(p.id)}" aria-expanded="${expanded.has(p.id)}"
|
||||
aria-controls="versions-${attr(p.id)}" dir="auto" title="${attr(p.isMain ? `${t("Admin.Projects.Main.Tip")}\n${t("Admin.Projects.Versions.Tip")}` : t("Admin.Projects.Versions.Tip"))}">${escapeHtml(name)}</button>${mark}</span>
|
||||
<span class="num end nowrap" role="cell" title="${attr(t("Admin.Projects.Head.Tip"))}">${escapeHtml(formatNumber(p.headNumber))}</span>
|
||||
<span class="col-changed ellipsis" role="cell" title="${attr([kindTip(p.headKind), p.headClientName].filter(Boolean).join("\n"))}"><span dir="auto">${escapeHtml(who)}</span>${who ? " · " : ""}<span class="muted nowrap">${escapeHtml(formatDateTime(when))}</span></span>
|
||||
<span class="row-actions" role="cell">${actions}</span>
|
||||
</div>
|
||||
${expanded.has(p.id) ? `<div class="row log-row" role="row"><div class="wide" role="cell" id="versions-${attr(p.id)}">${versionsHtml(p)}</div></div>` : ""}`;
|
||||
}
|
||||
|
||||
function versionsHtml(p) {
|
||||
const page = versions.get(p.id);
|
||||
if (!page) return "";
|
||||
if (page.items.length === 0) return emptyState("Admin.Projects.NoVersions");
|
||||
return `<div class="versions" role="list" aria-label="${attr(t("Admin.Projects.VersionsOf", projectName(p)))}">${page.items.map((v) => `
|
||||
<div class="version" role="listitem">
|
||||
<span class="num nowrap">${escapeHtml(t("Admin.Projects.Number", formatNumber(v.number)))}${v.id === p.headVersionId
|
||||
? ` <span class="pill" data-i18n="Admin.Projects.Current" data-i18n-title="Admin.Projects.Current.Tip"></span>` : ""}</span>
|
||||
<span class="nowrap" title="${attr(kindTip(v.kind))}">${escapeHtml(kindLabel(v.kind))}</span>
|
||||
<span class="ellipsis" dir="auto" title="${attr([v.author, v.clientName].filter(Boolean).join(" · "))}">${escapeHtml(v.author ?? v.clientName ?? "")}</span>
|
||||
<span class="ellipsis muted" dir="auto" title="${attr(v.note ?? "")}">${escapeHtml(v.note ?? "")}</span>
|
||||
<span class="muted nowrap num">${escapeHtml(formatDateTime(v.storedAt ?? v.createdAt))}</span>
|
||||
</div>`).join("")}
|
||||
${page.more ? `<button class="btn small more" data-more-versions="${attr(p.id)}" data-i18n="Admin.Common.More" data-i18n-title="Admin.Common.More.Tip"></button>` : ""}</div>`;
|
||||
}
|
||||
|
||||
function kindLabel(kind) {
|
||||
return KINDS[kind] ? t(`Admin.Projects.Kind.${KINDS[kind]}`) : String(kind ?? "");
|
||||
}
|
||||
|
||||
function kindTip(kind) {
|
||||
return KINDS[kind] ? t(`Admin.Projects.Kind.${KINDS[kind]}.Tip`) : "";
|
||||
}
|
||||
|
||||
async function toggle(id) {
|
||||
if (expanded.delete(id)) {
|
||||
versions.delete(id);
|
||||
draw();
|
||||
return;
|
||||
}
|
||||
expanded.add(id);
|
||||
try {
|
||||
await loadVersions(id);
|
||||
} catch (err) {
|
||||
forget(id);
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
draw();
|
||||
}
|
||||
|
||||
async function moreVersions(id) {
|
||||
try {
|
||||
await loadVersions(id, true);
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
return;
|
||||
}
|
||||
draw();
|
||||
}
|
||||
|
||||
async function act(action, project) {
|
||||
if (!project || project.isMain) return;
|
||||
if (action === "rename") return rename(project);
|
||||
if (action === "delete") return remove(project);
|
||||
}
|
||||
|
||||
async function rename(project) {
|
||||
const data = await formDialog({
|
||||
title: t("Admin.Projects.RenameTitle", project.name),
|
||||
fields: `<label><span data-i18n="Admin.Projects.NewName"></span><input class="input" name="name" dir="auto" maxlength="${MAX_NAME}" autocomplete="off"
|
||||
value="${attr(project.name)}" data-i18n-title="Admin.Projects.NewName.Tip"></label>`,
|
||||
confirm: t("Admin.Projects.RenameConfirm"),
|
||||
confirmTip: t("Admin.Projects.RenameConfirm.Tip"),
|
||||
cancel: t("Admin.Common.Cancel"),
|
||||
cancelTip: t("Admin.Common.Cancel.Tip"),
|
||||
check: (form) => {
|
||||
const name = form.elements.name.value.trim();
|
||||
if (name.length > 0 && name.length <= MAX_NAME) return true;
|
||||
markInvalid(form.elements.name, t("Admin.Projects.NameInvalid"));
|
||||
form.elements.name.focus();
|
||||
return false;
|
||||
},
|
||||
});
|
||||
const name = String(data?.get("name") ?? "").trim();
|
||||
if (!data || name === project.name) return;
|
||||
try {
|
||||
await api.patch(`${base}/${encodeURIComponent(project.id)}`, { name });
|
||||
toast(t("Admin.Projects.Renamed", name));
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
await load();
|
||||
}
|
||||
|
||||
/** Delete, after a confirmation that names the project: it and its versions move to the sequence's project trash. */
|
||||
async function remove(project) {
|
||||
const confirmed = await confirmDialog({
|
||||
title: t("Admin.Projects.DeleteTitle", project.name),
|
||||
body: t("Admin.Projects.DeleteBody", formatNumber(project.headNumber)),
|
||||
confirm: t("Admin.Projects.DeleteConfirm"),
|
||||
confirmTip: t("Admin.Projects.DeleteConfirm.Tip"),
|
||||
cancel: t("Admin.Common.Keep"),
|
||||
cancelTip: t("Admin.Common.Keep.Tip"),
|
||||
});
|
||||
if (!confirmed) return;
|
||||
try {
|
||||
await api.del(`${base}/${encodeURIComponent(project.id)}`);
|
||||
toast(t("Admin.Projects.Deleted", project.name));
|
||||
forget(project.id);
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
await load();
|
||||
}
|
||||
|
||||
load();
|
||||
return {
|
||||
onEvent(e) {
|
||||
if ((e.type === "projectChanged" && e.sequenceId === sequenceId) || e.type === "reconnected") {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
if (host.isConnected) load();
|
||||
}, AFTER_CHANGE_MS);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
// A sequence on the panel (owner review 2, R6): its title, tags, rating and notes, its facts, its photos in pages of 500
|
||||
// with lazy thumbnails, a photo viewer, deleting photos into the archive's trash, the cover, the originals and the renders
|
||||
// to download. Nothing here edits the video: its settings and its renders are the desktop editor's.
|
||||
// with lazy thumbnails, a photo viewer, deleting photos into the archive's trash, the cover, its projects (R8: listed,
|
||||
// renamed and deleted, projects.js), the originals and the renders to download. Nothing here edits the video: its settings,
|
||||
// its projects' versions and its renders are the desktop editor's.
|
||||
import { t, plural, applyStrings } from "../../i18n.js";
|
||||
import { escapeHtml, attr, formatBytes, formatNumber, formatDateTime, toast, errorText, confirmDialog, emptyState, escapeReverts, redraw } from "../../ui.js";
|
||||
import { icons } from "../../icons.js";
|
||||
import { captureText, cameraLabel, spanText, statusChips, lazyImage, watchImages, retryImages, Selection, selectableGrid, tagList, coverUrl, listed, JOB_ENDED } from "./shared.js";
|
||||
import { projectsSection } from "./projects.js";
|
||||
|
||||
const FRAME_PAGE = 500;
|
||||
/** Deleting more photos than this asks for their number to be typed. */
|
||||
@@ -80,11 +82,16 @@ export async function renderSequence(root, { api, setMeta, actions, onEvent }, i
|
||||
<div class="table-foot" id="frames-foot" hidden><span class="muted" id="frames-shown"></span><span class="spacer"></span>
|
||||
<button class="btn small" id="frames-more" data-i18n="Admin.Common.More" data-i18n-title="Admin.Sequence.MorePhotos.Tip"></button></div>
|
||||
</section>
|
||||
<section class="panel flush" aria-labelledby="projects-title">
|
||||
<div class="panel-head padded"><h2 id="projects-title" data-i18n="Admin.Projects.Title"></h2><span class="muted ellipsis" id="projects-meta"></span></div>
|
||||
<div id="projects"></div>
|
||||
</section>
|
||||
<section class="panel flush" aria-labelledby="renders-title">
|
||||
<div class="panel-head padded"><h2 id="renders-title" data-i18n="Admin.Sequence.Renders"></h2></div>
|
||||
<div id="renders"></div>
|
||||
</section>`;
|
||||
applyStrings(root);
|
||||
const projects = projectsSection(root.querySelector("#projects"), root.querySelector("#projects-meta"), { api, sequenceId: id });
|
||||
const form = root.querySelector("form");
|
||||
const framesHost = root.querySelector("#frames");
|
||||
escapeReverts(form);
|
||||
@@ -573,6 +580,7 @@ export async function renderSequence(root, { api, setMeta, actions, onEvent }, i
|
||||
}
|
||||
|
||||
onEvent((e) => {
|
||||
projects.onEvent(e);
|
||||
if (e.type === "jobState") {
|
||||
// The event names neither the job's kind nor its targets: any job that ended may have worked on this one. The timer
|
||||
// is not pushed back by the next one, so a busy queue still refreshes the page every AFTER_JOB_MS; a refresh that
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Processing;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Server.Jobs;
|
||||
using Chronos.Server.Security;
|
||||
@@ -11,8 +14,9 @@ using Chronos.Server.Storage;
|
||||
namespace Chronos.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// <c>/api/v1/jobs</c> (BRIEF §14.6): queue and history, creation (a render batch becomes one job per preset), cancel,
|
||||
/// pause, resume, retry and priority. Changes reach WebSocket clients as <c>jobState</c> and <c>jobProgress</c> events.
|
||||
/// <c>/api/v1/jobs</c> (BRIEF §14.6): queue and history, creation (a render batch becomes one job per preset; a render of a
|
||||
/// project version is checked against the sequence's projects, owner review 2, R8), cancel, pause, resume, retry and
|
||||
/// priority. Changes reach WebSocket clients as <c>jobState</c> and <c>jobProgress</c> events.
|
||||
/// </summary>
|
||||
public static class JobEndpoints
|
||||
{
|
||||
@@ -62,7 +66,7 @@ public static class JobEndpoints
|
||||
}
|
||||
|
||||
private static async Task<IResult> CreateAsync(HttpContext context, Caller caller, CreateJobRequest request, ChronosDatabase db, JobEngineHost host,
|
||||
ServerArchive archive, AuditLog audit)
|
||||
ServerArchive archive, AuditLog audit, [FromServices] ArchiveService? archives)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (!JobWire.TryParseKind(request.Kind, out var kind) || !(kind == JobKind.Render || SequenceKinds.Contains(kind) || ArchiveKinds.Contains(kind)))
|
||||
@@ -94,7 +98,11 @@ public static class JobEndpoints
|
||||
List<(string Label, string? Parameters)> queue = [(label, request.ParametersJson)];
|
||||
if (kind == JobKind.Render)
|
||||
{
|
||||
if (ParseRender(request.ParametersJson) is not { } render) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
|
||||
if (ParseRender(request.ParametersJson) is not { } parsed) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
|
||||
// A render of a project version (owner review 2, R8): of the sequence's projects, a project alone pinned to its head.
|
||||
var (render, missing) = await ProjectRenderAsync(db, archives, targets[0], parsed, ct);
|
||||
if (missing is not null) return ApiErrors.NotFound(context, missing);
|
||||
var parametersJson = render == parsed ? request.ParametersJson : JsonSerializer.Serialize(render, ProtocolJson.Default.RenderJobParameters);
|
||||
// A batch becomes one job per preset: a render node uploads one file per job (BRIEF §14.7).
|
||||
IReadOnlyList<RenderJobParameters> each;
|
||||
try
|
||||
@@ -107,7 +115,7 @@ public static class JobEndpoints
|
||||
return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
|
||||
}
|
||||
queue = each.Count == 1
|
||||
? [(ProcessingJobs.RenderLabel(label, render.PresetName), request.ParametersJson)]
|
||||
? [(ProcessingJobs.RenderLabel(label, render.PresetName), parametersJson)]
|
||||
: [.. each.Select(p => (ProcessingJobs.RenderLabel(label, p.PresetName), (string?)JsonSerializer.Serialize(p, ProtocolJson.Default.RenderJobParameters)))];
|
||||
}
|
||||
else if (request.ParametersJson is not null && !IsJsonObject(request.ParametersJson))
|
||||
@@ -133,6 +141,51 @@ public static class JobEndpoints
|
||||
return Results.Json(Dto.Job(first!), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The render of a project version (owner review 2, R8), checked against the sequence: <see cref="RenderJobParameters.ProjectVersionId"/>
|
||||
/// must be a version of one of its projects (that project, when <see cref="RenderJobParameters.ProjectId"/> names one too) and
|
||||
/// fills in the project; a live project alone, without settings of its own, renders its head as it is now, which the job then
|
||||
/// names, so the render records the version it rendered; embedded settings with a project render as they are, recording
|
||||
/// the project. A deleted project's versions still render (they are kept). Without either, the parameters as they are.
|
||||
/// Returns what is missing (<c>project</c>, <c>version</c>) instead of the parameters when they do not hold.
|
||||
/// </summary>
|
||||
internal static async Task<(RenderJobParameters Parameters, string? Missing)> ProjectRenderAsync(ChronosDatabase db, ArchiveService? archives, Guid sequenceId,
|
||||
RenderJobParameters render, CancellationToken ct)
|
||||
{
|
||||
if (render.ProjectId is null && render.ProjectVersionId is null) return (render, null);
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
ProjectVersion? version = null;
|
||||
if (render.ProjectVersionId is { } versionId && (version = await KnownVersionAsync(versionId)) is null) return (render, "version");
|
||||
var projectId = render.ProjectId ?? version!.ProjectId;
|
||||
if (await db.Projects.GetAsync(projectId, ct) is not { } project)
|
||||
{
|
||||
// A project the index does not know yet may be in the sequence's sidecars: listing reads them first.
|
||||
await projects.ListAsync(sequenceId, ct);
|
||||
project = await db.Projects.GetAsync(projectId, ct);
|
||||
}
|
||||
if (project is null || project.SequenceId != sequenceId) return (render, "project");
|
||||
if (version is not null) return version.ProjectId == project.Id ? (render with { ProjectId = project.Id }, null) : (render, "version");
|
||||
if (render.SettingsJson is not null) return (render with { ProjectId = project.Id }, null);
|
||||
try
|
||||
{
|
||||
// Caught up first: Main's head is the sequence's settings as they are now.
|
||||
var head = await projects.HeadAsync(project.Id, ct);
|
||||
return (render with { ProjectId = project.Id, ProjectVersionId = head.Id }, null);
|
||||
}
|
||||
catch (ProjectException)
|
||||
{
|
||||
// A deleted project has no head to render; its versions, named by id, still render (they are kept).
|
||||
return (render, project.DeletedAt is null ? "version" : "project");
|
||||
}
|
||||
|
||||
async Task<ProjectVersion?> KnownVersionAsync(Guid id)
|
||||
{
|
||||
if (await db.Projects.GetVersionAsync(id, ct) is { } known) return known;
|
||||
await projects.ListAsync(sequenceId, ct);
|
||||
return await db.Projects.GetVersionAsync(id, ct);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The oriented size of the sequence's first frame (what a preset without a fixed size renders at), else the job's size.</summary>
|
||||
private static async Task<(int Width, int Height)> SourceSizeAsync(ChronosDatabase db, Guid sequenceId, RenderJobParameters render, CancellationToken ct) =>
|
||||
(await db.Frames.ListBySequenceAsync(sequenceId, limit: 1, cancellationToken: ct)).FirstOrDefault() is { Width: > 0, Height: > 0 } frame
|
||||
|
||||
@@ -0,0 +1,364 @@
|
||||
using Microsoft.AspNetCore.Http.Metadata;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Server.Security;
|
||||
|
||||
namespace Chronos.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// <c>/api/v1/sequences/{sid}/projects</c> (owner review 2, R8): the server as the home of its sequences' projects, through
|
||||
/// the archive engine's <see cref="ProjectService"/> as a desktop's archive is (its sidecars first, then the index, under the
|
||||
/// sequence's lease). The live projects with Main first (made from the sequence's settings when first read), a new project
|
||||
/// from a version or from Main's head, rename, delete into the sequence's projects' trash (never Main), the head, the versions
|
||||
/// a page at a time and one by id, the push of a version made in a workspace (a fast-forward, a repeated push answered again,
|
||||
/// a divergence, a hash, format or portability refusal), and the frames a workspace places the positions on. Reading needs
|
||||
/// Read; making, renaming and pushing Edit; deleting Delete, and the sequence's owner, the project's maker or an admin.
|
||||
/// Every change is told to connected clients as <c>projectChanged</c> (the archive module, from what each call changed) and
|
||||
/// creations, renames and deletions are audited. Nothing here edits a video on the admin panel: it lists and manages.
|
||||
/// </summary>
|
||||
public static class ProjectEndpoints
|
||||
{
|
||||
/// <summary>
|
||||
/// The body a push may take: a version's portable settings name each exclusion and camera keyframe by photo (about 150
|
||||
/// bytes each), so the project of a large sequence outgrows the 1 MiB of other JSON bodies.
|
||||
/// </summary>
|
||||
public const long PushBodyLimit = 16L * 1024 * 1024;
|
||||
|
||||
/// <summary>Versions one page may hold.</summary>
|
||||
public const int MaxPage = 200;
|
||||
|
||||
private const int MaxNote = 2000;
|
||||
private const int MaxClientName = 200;
|
||||
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var projects = api.MapGroup("/sequences/{sid:guid}/projects").Requires(Permissions.Read);
|
||||
projects.MapGet("", ListAsync);
|
||||
projects.MapPost("", CreateAsync).Requires(Permissions.Edit);
|
||||
projects.MapGet("/frames", FramesAsync);
|
||||
projects.MapGet("/{pid:guid}", GetAsync);
|
||||
projects.MapPatch("/{pid:guid}", RenameAsync).Requires(Permissions.Edit);
|
||||
projects.MapDelete("/{pid:guid}", DeleteAsync).Requires(Permissions.Delete);
|
||||
projects.MapGet("/{pid:guid}/head", HeadAsync);
|
||||
projects.MapGet("/{pid:guid}/versions", VersionsAsync);
|
||||
projects.MapGet("/{pid:guid}/versions/{vid:guid}", VersionAsync);
|
||||
projects.MapPost("/{pid:guid}/versions", PushAsync).Requires(Permissions.Edit).WithMetadata(new PushBodyLimitMetadata());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The sequence's live projects, Main first, each with its head's kind, author, client and time. A sequence without a Main
|
||||
/// project gets it now, from its settings; while that cannot be done (the archive offline, the sequence busy, its folder
|
||||
/// elsewhere) the answer is 503 with the reason's code.
|
||||
/// </summary>
|
||||
private static async Task<IResult> ListAsync(HttpContext context, Guid sid, ChronosDatabase db, [FromServices] ArchiveService? archives) =>
|
||||
await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
if (await db.Sequences.GetAsync(sid, ct) is null) return ApiErrors.NotFound(context, "sequence");
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var list = await projects.ListAsync(sid, ct);
|
||||
if (!list.Any(p => p.IsMain))
|
||||
{
|
||||
await projects.MainAsync(sid, ct);
|
||||
list = await projects.ListAsync(sid, ct);
|
||||
}
|
||||
var items = new List<ProjectDto>(list.Count);
|
||||
foreach (var project in list) items.Add(ProjectWire.ToDto(project, await HeadOfAsync(projects, project, ct)));
|
||||
return Results.Json(items, ProtocolJson.Default.ListProjectDto);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// A new project (201): its first version, kind <c>imported</c>, copies <c>fromVersionId</c> (a version of any project of the
|
||||
/// sequence; 404 <c>not_found</c> <c>version</c> otherwise) or Main's head. 400 <c>invalid_name</c>, 409 <c>name_taken</c>.
|
||||
/// Audited <c>project_created</c>.
|
||||
/// </summary>
|
||||
private static async Task<IResult> CreateAsync(HttpContext context, Caller caller, Guid sid, CreateProjectRequest request, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives, AuditLog audit, IHostApplicationLifetime lifetime) =>
|
||||
await RunAsync(context, "version", async ct =>
|
||||
{
|
||||
if (await db.Sequences.GetAsync(sid, ct) is null) return ApiErrors.NotFound(context, "sequence");
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
// Once asked for, the project is made even if the client goes away meanwhile.
|
||||
var created = await projects.CreateAsync(sid, request.Name ?? string.Empty, request.FromVersionId, caller.User.Username, lifetime.ApplicationStopping);
|
||||
await audit.WriteAsync(caller, context, "project_created", "project", created.Id.ToString(), $"{sid} · {created.Name}");
|
||||
return Results.Json(ProjectWire.ToDto(created, await HeadOfAsync(projects, created, ct)), ProtocolJson.Default.ProjectDto,
|
||||
statusCode: StatusCodes.Status201Created);
|
||||
});
|
||||
|
||||
/// <summary>A live project of the sequence; 404 <c>not_found</c> <c>project</c> for one deleted or of another sequence.</summary>
|
||||
private static async Task<IResult> GetAsync(HttpContext context, Guid sid, Guid pid, ChronosDatabase db, [FromServices] ArchiveService? archives) =>
|
||||
await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
var project = known.Project!;
|
||||
if (project.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
return Results.Json(ProjectWire.ToDto(project, await HeadOfAsync(projects, project, ct)), ProtocolJson.Default.ProjectDto);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Gives a project another name (Main too: the desktop shows Main in its own language either way). 400 <c>invalid_name</c>,
|
||||
/// 409 <c>name_taken</c>. Audited <c>project_renamed</c> when the name changed.
|
||||
/// </summary>
|
||||
private static async Task<IResult> RenameAsync(HttpContext context, Caller caller, Guid sid, Guid pid, RenameProjectRequest request, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives, AuditLog audit, IHostApplicationLifetime lifetime) =>
|
||||
await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
var before = known.Project!;
|
||||
if (before.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
var renamed = await projects.RenameAsync(pid, request.Name ?? string.Empty, lifetime.ApplicationStopping);
|
||||
if (renamed.Name != before.Name)
|
||||
await audit.WriteAsync(caller, context, "project_renamed", "project", pid.ToString(), $"{sid} · {before.Name} → {renamed.Name}");
|
||||
return Results.Json(ProjectWire.ToDto(renamed, await HeadOfAsync(projects, renamed, ct)), ProtocolJson.Default.ProjectDto);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a project (204): its record says so and its folder, with every version, moves to the sequence's
|
||||
/// <c>.chronos/projects/.trash</c>, kept and never erased by Chronos. The Main project is refused (409
|
||||
/// <c>main_cannot_be_deleted</c>); so is another account's project of another account's sequence, for anyone but an admin
|
||||
/// (403). Audited <c>project_deleted</c>.
|
||||
/// </summary>
|
||||
private static async Task<IResult> DeleteAsync(HttpContext context, Caller caller, Guid sid, Guid pid, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives, AuditLog audit, IHostApplicationLifetime lifetime) =>
|
||||
await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
var project = known.Project!;
|
||||
if (project.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
if (project.IsMain) return ApiErrors.Conflict(context, ProjectErrors.MainCannotBeDeleted);
|
||||
if (!caller.IsAdmin && known.Sequence!.OwnerUserId != caller.Id && project.CreatedBy != caller.User.Username) return ApiErrors.Forbidden(context);
|
||||
await projects.DeleteAsync(pid, lifetime.ApplicationStopping);
|
||||
await audit.WriteAsync(caller, context, "project_deleted", "project", pid.ToString(), $"{sid} · {project.Name}");
|
||||
return Results.NoContent();
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// The newest version of a live project; 404 <c>not_found</c> <c>project</c> once it is deleted (its versions are still
|
||||
/// listed: a workspace's copy of it reads as gone, never as in step), <c>version</c> when it has none.
|
||||
/// </summary>
|
||||
private static async Task<IResult> HeadAsync(HttpContext context, Guid sid, Guid pid, ChronosDatabase db, [FromServices] ArchiveService? archives) =>
|
||||
await RunAsync(context, "version", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
if (known.Project!.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
return Results.Json(ProjectWire.ToDto(await projects.HeadAsync(pid, ct)), ProtocolJson.Default.ProjectVersionDto);
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// One page of the project's versions: newest first, numbered below <paramref name="before"/> (from the head when absent),
|
||||
/// or with <paramref name="after"/> oldest first, numbered above it. <paramref name="limit"/> 1–<see cref="MaxPage"/> (50 newest
|
||||
/// first, 100 oldest first). 400 <c>bad_request</c> (<c>after</c> with <c>before</c>, a number below 0).
|
||||
/// </summary>
|
||||
private static async Task<IResult> VersionsAsync(HttpContext context, Guid sid, Guid pid, int? before, int? after, int? limit, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives)
|
||||
{
|
||||
if (before is not null && after is not null) return ApiErrors.BadRequest(context, "bad_request", "after");
|
||||
if (before is < 0) return ApiErrors.BadRequest(context, "bad_request", "before");
|
||||
if (after is < 0) return ApiErrors.BadRequest(context, "bad_request", "after");
|
||||
var take = Math.Clamp(limit ?? (after is null ? 50 : 100), 1, MaxPage);
|
||||
return await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
var page = after is { } from
|
||||
? await projects.VersionsAfterAsync(pid, from, take + 1, ct)
|
||||
: await projects.HistoryAsync(pid, before, take + 1, ct);
|
||||
// The head as the page was read: the call above caught the project up first.
|
||||
var head = (await projects.GetAsync(pid, ct))?.HeadNumber ?? known.Project!.HeadNumber;
|
||||
return Results.Json(new ProjectVersionPageDto([.. page.Take(take).Select(ProjectWire.ToDto)], head, page.Count > take),
|
||||
ProtocolJson.Default.ProjectVersionPageDto);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>A version of the project by id; one the index does not know yet is looked for in the sequence's sidecars first.</summary>
|
||||
private static async Task<IResult> VersionAsync(HttpContext context, Guid sid, Guid pid, Guid vid, ChronosDatabase db, [FromServices] ArchiveService? archives) =>
|
||||
await RunAsync(context, "version", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
if (await projects.GetVersionAsync(vid, ct) is not { } version)
|
||||
{
|
||||
await projects.ListAsync(sid, ct);
|
||||
version = await projects.GetVersionAsync(vid, ct);
|
||||
}
|
||||
return version is not null && version.ProjectId == pid
|
||||
? Results.Json(ProjectWire.ToDto(version), ProtocolJson.Default.ProjectVersionDto)
|
||||
: ApiErrors.NotFound(context, "version");
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Stores a version made in a workspace from the project's head (<see cref="ProjectService.PushAsync"/>), recording the
|
||||
/// account that pushed it as its author: 201 <c>stored</c> (a fast-forward: numbered head + 1, Main's mirrored into the
|
||||
/// sequence's settings), 200 <c>already_stored</c> (the same id and content stored before: a push repeated after its
|
||||
/// answer was lost), 409 <c>project_diverged</c> (the head moved past its parent; the answer carries the head), 422
|
||||
/// <c>hash_mismatch</c>, <c>newer_format</c>, <c>invalid_version</c> or <c>too_large</c> (a version file over 64 MiB), each answered as a
|
||||
/// <see cref="ProjectPushResultDto"/> and logged. 400 <c>bad_request</c> (<c>id</c>, <c>settingsJson</c>, <c>contentHash</c>,
|
||||
/// <c>note</c>, <c>clientName</c>); 413 <c>too_large</c> above <see cref="PushBodyLimit"/>.
|
||||
/// </summary>
|
||||
private static async Task<IResult> PushAsync(HttpContext context, Caller caller, Guid sid, Guid pid, ProjectVersionDto request, ChronosDatabase db,
|
||||
[FromServices] ArchiveService? archives, ILoggerFactory loggers, IHostApplicationLifetime lifetime)
|
||||
{
|
||||
if (request is null) return ApiErrors.BadRequest(context, "bad_request");
|
||||
if (request.Id == Guid.Empty) return ApiErrors.BadRequest(context, "bad_request", "id");
|
||||
if (string.IsNullOrEmpty(request.SettingsJson)) return ApiErrors.BadRequest(context, "bad_request", "settingsJson");
|
||||
if (string.IsNullOrEmpty(request.ContentHash)) return ApiErrors.BadRequest(context, "bad_request", "contentHash");
|
||||
if (request.Note is { Length: > MaxNote }) return ApiErrors.BadRequest(context, "bad_request", "note");
|
||||
if (request.ClientName is { Length: > MaxClientName }) return ApiErrors.BadRequest(context, "bad_request", "clientName");
|
||||
return await RunAsync(context, "project", async ct =>
|
||||
{
|
||||
var projects = LibraryEndpoints.Engine(archives, db).Projects;
|
||||
var known = await KnownAsync(context, db, projects, sid, pid, ct);
|
||||
if (known.Refused is { } refused) return refused;
|
||||
if (known.Project!.DeletedAt is not null) return ApiErrors.NotFound(context, "project");
|
||||
var client = string.IsNullOrWhiteSpace(request.ClientName) ? caller.ClientName : request.ClientName.Trim();
|
||||
var version = ProjectWire.ToVersion(request) with
|
||||
{
|
||||
ProjectId = pid,
|
||||
Author = caller.User.Username,
|
||||
ClientName = client,
|
||||
Note = string.IsNullOrWhiteSpace(request.Note) ? null : request.Note.Trim(),
|
||||
CreatedAt = request.CreatedAt == default ? DateTimeOffset.UtcNow : request.CreatedAt,
|
||||
StoredAt = null,
|
||||
};
|
||||
// Once sent, the version is stored even if the client goes away meanwhile; a client that asks again is answered already_stored.
|
||||
var result = await projects.PushAsync(pid, version, lifetime.ApplicationStopping);
|
||||
var outcome = ProjectWire.Outcome(result.Outcome);
|
||||
loggers.CreateLogger("Chronos.Server.Projects").LogInformation(
|
||||
"Version {Version} of project {Project} ({Name}) of sequence {Sequence} pushed by {User} from {Client}: {Outcome}{Number}", request.Id, pid,
|
||||
known.Project.Name, sid, caller.User.Username, client ?? "-", outcome, result.Version is { } stored ? $" as number {stored.Number}" : string.Empty);
|
||||
var (status, code, message) = result.Outcome switch
|
||||
{
|
||||
PushOutcome.Stored => (StatusCodes.Status201Created, (string?)null, (string?)null),
|
||||
PushOutcome.AlreadyStored => (StatusCodes.Status200OK, null, null),
|
||||
PushOutcome.Diverged => (StatusCodes.Status409Conflict, ProjectErrors.Diverged, ApiErrors.Message(context, ProjectErrors.Diverged)),
|
||||
PushOutcome.HashMismatch => (StatusCodes.Status422UnprocessableEntity, ProjectErrors.HashMismatch, ApiErrors.Message(context, "version_hash_mismatch")),
|
||||
PushOutcome.NewerFormat => (StatusCodes.Status422UnprocessableEntity, ProjectErrors.NewerFormat, ApiErrors.Message(context, ProjectErrors.NewerFormat)),
|
||||
PushOutcome.TooLarge => (StatusCodes.Status422UnprocessableEntity, "too_large", ApiErrors.Message(context, "too_large")),
|
||||
_ => (StatusCodes.Status422UnprocessableEntity, ProjectErrors.InvalidVersion, ApiErrors.Message(context, ProjectErrors.InvalidVersion)),
|
||||
};
|
||||
return Results.Json(new ProjectPushResultDto(outcome, result.Version is { } v ? ProjectWire.ToDto(v) : null,
|
||||
result.Head is { } h ? ProjectWire.ToDto(h) : null, code, message), ProtocolJson.Default.ProjectPushResultDto, statusCode: status);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The sequence's frames in index order and the photo aliases of its conversions, as of one frames version
|
||||
/// (<see cref="SequenceEndpoints.SnapshotOfAsync"/>): what a workspace places a version's positions on.
|
||||
/// </summary>
|
||||
private static async Task<IResult> FramesAsync(HttpContext context, Guid sid, ChronosDatabase db)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await SequenceEndpoints.SnapshotOfAsync(db, sid, async (on, sequence, c) =>
|
||||
(Frames: await AllFramesAsync(on, sequence.Id, c), Removals: await on.FrameRemovals.ListBySequenceAsync(sequence.Id, c)), ct) is not { } snapshot)
|
||||
return ApiErrors.NotFound(context, "sequence");
|
||||
var (row, (frames, removals)) = snapshot;
|
||||
var aliases = ProjectWire.Aliases(removals);
|
||||
return Results.Json(new ProjectFramesDto(sid, row.FramesVersion, [.. frames.Select(Dto.Frame)], aliases.Count == 0 ? null : aliases),
|
||||
ProtocolJson.Default.ProjectFramesDto);
|
||||
}
|
||||
|
||||
/// <summary>Every frame of a sequence in index order, read a page at a time.</summary>
|
||||
internal static async Task<List<Frame>> AllFramesAsync(ChronosRepositories db, Guid sequenceId, CancellationToken cancellationToken)
|
||||
{
|
||||
const int pageSize = 1000;
|
||||
var frames = new List<Frame>();
|
||||
for (var after = -1; ;)
|
||||
{
|
||||
var page = await db.Frames.ListBySequenceAsync(sequenceId, after, pageSize, cancellationToken);
|
||||
frames.AddRange(page);
|
||||
if (page.Count < pageSize) return frames;
|
||||
after = page[^1].Index;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The sequence and a project of it (deleted ones too, whose versions are kept); one the index does not know yet (an index
|
||||
/// taken back from a backup, a project another program sharing the archive made) is looked for in the sequence's sidecars
|
||||
/// first. Otherwise the refusal: 404 <c>not_found</c> <c>sequence</c> or <c>project</c>.
|
||||
/// </summary>
|
||||
private static async Task<(Sequence? Sequence, SequenceProject? Project, IResult? Refused)> KnownAsync(HttpContext context, ChronosDatabase db,
|
||||
ProjectService projects, Guid sid, Guid pid, CancellationToken ct)
|
||||
{
|
||||
if (await db.Sequences.GetAsync(sid, ct) is not { } sequence) return (null, null, ApiErrors.NotFound(context, "sequence"));
|
||||
if (await projects.GetAsync(pid, ct) is not { } project)
|
||||
{
|
||||
await projects.ListAsync(sid, ct);
|
||||
project = await projects.GetAsync(pid, ct);
|
||||
}
|
||||
return project is not null && project.SequenceId == sid ? (sequence, project, null) : (sequence, null, ApiErrors.NotFound(context, "project"));
|
||||
}
|
||||
|
||||
private static async Task<ProjectVersion?> HeadOfAsync(ProjectService projects, SequenceProject project, CancellationToken ct) =>
|
||||
project.HeadVersionId is { } id ? await projects.GetVersionAsync(id, ct) : null;
|
||||
|
||||
/// <summary>
|
||||
/// Runs a project call with its refusals answered by their codes: 404 <c>not_found</c> (<c>sequence</c>, or
|
||||
/// <paramref name="notFound"/> for a project or version the engine does not know), 400 <c>invalid_name</c>, 409
|
||||
/// <c>name_taken</c>, <c>main_cannot_be_deleted</c>; 503 <c>archive_offline</c> (offline or read-only), <c>sequence_busy</c>
|
||||
/// (another writer held it past the lease wait), <c>folder_moved</c> (its folder is not where the index has it),
|
||||
/// <c>projects_unreadable</c> (a project record or version this Chronos cannot read now: nothing was written); 413
|
||||
/// <c>too_large</c> for a version file over 64 MiB; 409 <c>folder_in_use</c> when a project's file cannot be written.
|
||||
/// </summary>
|
||||
private static async Task<IResult> RunAsync(HttpContext context, string notFound, Func<CancellationToken, Task<IResult>> call)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await call(context.RequestAborted);
|
||||
}
|
||||
catch (ProjectException ex)
|
||||
{
|
||||
return ex.Reason switch
|
||||
{
|
||||
ProjectError.SequenceNotFound => ApiErrors.NotFound(context, "sequence"),
|
||||
ProjectError.InvalidName => ApiErrors.BadRequest(context, ProjectErrors.InvalidName),
|
||||
ProjectError.NameTaken => ApiErrors.Conflict(context, ProjectErrors.NameTaken),
|
||||
ProjectError.MainCannotBeDeleted => ApiErrors.Conflict(context, ProjectErrors.MainCannotBeDeleted),
|
||||
_ => ApiErrors.NotFound(context, notFound),
|
||||
};
|
||||
}
|
||||
catch (ArchiveOfflineException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
|
||||
}
|
||||
catch (LeaseUnavailableException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "sequence_busy");
|
||||
}
|
||||
catch (SequenceFolderMovedException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "folder_moved");
|
||||
}
|
||||
catch (ProjectsUnreadableException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "projects_unreadable");
|
||||
}
|
||||
catch (ProjectVersionTooLargeException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status413PayloadTooLarge, "too_large");
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return ApiErrors.Conflict(context, "folder_in_use");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The push route's body limit (<see cref="PushBodyLimit"/>), applied by routing as it selects the endpoint.</summary>
|
||||
private sealed class PushBodyLimitMetadata : IRequestSizeLimitMetadata
|
||||
{
|
||||
public long? MaxRequestBodySize => PushBodyLimit;
|
||||
}
|
||||
}
|
||||
@@ -129,7 +129,7 @@ public static class SequenceEndpoints
|
||||
}
|
||||
|
||||
private static async Task<IResult> UpdateAsync(HttpContext context, Caller caller, Guid id, UpdateSequenceRequest request, ChronosDatabase db,
|
||||
AuditLog audit, EventHub events)
|
||||
AuditLog audit, EventHub events, ProjectCatchUp projects)
|
||||
{
|
||||
if (request.Title is { Length: > MaxTitle }) return ApiErrors.BadRequest(context, "bad_request", "title");
|
||||
if (request.Notes is { Length: > MaxNotes }) return ApiErrors.BadRequest(context, "bad_request", "notes");
|
||||
@@ -166,6 +166,9 @@ public static class SequenceEndpoints
|
||||
var sequence = result.Sequence!;
|
||||
await audit.WriteAsync(caller, context, "sequence_updated", "sequence", id.ToString(), Changes(request));
|
||||
events.Publish(new SequenceChangedEvent(id, "settings", sequence.FramesVersion, sequence.SettingsVersion));
|
||||
// Settings written outside the projects (an older client, the sync of a desktop's copy) become Main's next version now,
|
||||
// so a workspace with Main checked out hears that its home moved (owner review 2, R8).
|
||||
if (request.SettingsJson is not null) projects.Touch(id);
|
||||
context.Response.Headers.ETag = ETag(sequence);
|
||||
return Results.Json(Dto.Sequence(sequence), ProtocolJson.Default.SequenceDto);
|
||||
}
|
||||
@@ -266,19 +269,28 @@ public static class SequenceEndpoints
|
||||
/// the sequence is gone.
|
||||
/// </summary>
|
||||
internal static async Task<(Sequence Sequence, List<ManifestFrame> Frames)?> SnapshotAsync(ChronosDatabase db, Guid id,
|
||||
Func<ChronosRepositories, Sequence, CancellationToken, Task<List<ManifestFrame>>> listFrames, CancellationToken cancellationToken)
|
||||
Func<ChronosRepositories, Sequence, CancellationToken, Task<List<ManifestFrame>>> listFrames, CancellationToken cancellationToken) =>
|
||||
await SnapshotOfAsync(db, id, listFrames, cancellationToken) is { } snapshot ? (snapshot.Sequence, snapshot.Value) : null;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="SnapshotAsync"/> for anything read from the sequence's frames (the frames a render node renders and the photos
|
||||
/// a project version is placed on, the frames and photo aliases of a project home): <paramref name="read"/> runs again while
|
||||
/// the frames version moved during it, then inside the transaction holding the sequence's row. Null when the sequence is gone.
|
||||
/// </summary>
|
||||
internal static async Task<(Sequence Sequence, T Value)?> SnapshotOfAsync<T>(ChronosDatabase db, Guid id,
|
||||
Func<ChronosRepositories, Sequence, CancellationToken, Task<T>> read, CancellationToken cancellationToken)
|
||||
{
|
||||
for (var attempt = 0; attempt < SnapshotAttempts; attempt++)
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, cancellationToken) is not { } sequence) return null;
|
||||
var frames = await listFrames(db, sequence, cancellationToken);
|
||||
var value = await read(db, sequence, cancellationToken);
|
||||
if (await db.Sequences.GetAsync(id, cancellationToken) is not { } after) return null;
|
||||
if (after.FramesVersion == sequence.FramesVersion) return (sequence, frames);
|
||||
if (after.FramesVersion == sequence.FramesVersion) return (sequence, value);
|
||||
}
|
||||
// Commits keep landing (an upload of earlier photos): the frames are listed while none can.
|
||||
await using var tx = await db.BeginAsync(cancellationToken);
|
||||
if (await tx.Sequences.GetForUpdateAsync(id, cancellationToken) is not { } locked) return null;
|
||||
return (locked, await listFrames(tx, locked, cancellationToken));
|
||||
return (locked, await read(tx, locked, cancellationToken));
|
||||
}
|
||||
|
||||
private static async Task<IResult> FramesAsync(HttpContext context, Guid id, string? cursor, int? limit, ChronosDatabase db)
|
||||
|
||||
@@ -104,7 +104,8 @@ public sealed class ServerState
|
||||
/// <summary>Engine and version of the database ("PostgreSQL 17.2", "SQLite 3.45.1").</summary>
|
||||
public string Database { get; init; } = string.Empty;
|
||||
|
||||
public List<string> Capabilities { get; } = ["uploads", "sync", "jobs", "render-nodes", "events"];
|
||||
/// <summary>What this server offers clients (<see cref="ServerInfo.Capabilities"/>; <see cref="ServerCapabilities"/> names those clients act on).</summary>
|
||||
public List<string> Capabilities { get; } = ["uploads", "sync", "jobs", "render-nodes", "events", ServerCapabilities.Projects];
|
||||
|
||||
public DateTimeOffset StartedAt { get; } = DateTimeOffset.UtcNow;
|
||||
}
|
||||
@@ -163,6 +163,9 @@ public static class ServerHost
|
||||
services.AddSingleton<UploadMaintenance>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<UploadMaintenance>());
|
||||
services.AddSingleton<ICleanupStep, UploadCleanupStep>();
|
||||
// Main takes settings written outside the projects soon after they are (owner review 2, R8).
|
||||
services.AddSingleton<ProjectCatchUp>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<ProjectCatchUp>());
|
||||
services.AddSingleton<DiscoveryBeaconService>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<DiscoveryBeaconService>());
|
||||
}
|
||||
@@ -181,6 +184,7 @@ public static class ServerHost
|
||||
AuthEndpoints.Map(api);
|
||||
UserEndpoints.Map(api);
|
||||
SequenceEndpoints.Map(api);
|
||||
ProjectEndpoints.Map(api);
|
||||
LibraryEndpoints.Map(api);
|
||||
FileEndpoints.Map(api);
|
||||
UploadEndpoints.Map(api);
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Data.Repositories;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Server.Api;
|
||||
@@ -20,7 +23,9 @@ namespace Chronos.Server.Jobs;
|
||||
/// render jobs they can encode, report progress, and upload the output in resumable chunks that become a render of
|
||||
/// the sequence. A claimed job lists every frame with its flags (a node renders none missing, corrupt or excluded) and
|
||||
/// the proxies the server asks for (<see cref="ServerProxies"/>), which the node sends from the originals it fetched.
|
||||
/// A node that stops heart-beating loses its jobs to the queue (<see cref="RenderNodeMonitor"/>).
|
||||
/// A node that stops heart-beating loses its jobs to the queue (<see cref="RenderNodeMonitor"/>). A job of a project version
|
||||
/// (owner review 2, R8) gets that version's settings placed on the frames it lists, and its render records the project and
|
||||
/// the version.
|
||||
/// </summary>
|
||||
public sealed class RenderNodeService(
|
||||
ChronosDatabase db,
|
||||
@@ -90,14 +95,62 @@ public sealed class RenderNodeService(
|
||||
continue;
|
||||
}
|
||||
await TouchAsync(node, cancellationToken);
|
||||
if (await SettingsAsync(parameters, sequence, cancellationToken) is not { } settings)
|
||||
{
|
||||
await FinishAsync(node, job, JobState.Failed, Loc.L("Errors.Render.ProjectVersion"), cancellationToken);
|
||||
continue;
|
||||
}
|
||||
var spec = ServerProxies.Spec(archive.Current);
|
||||
return new ClaimedJob(Dto.Job(job), sequence.Id, parameters.SettingsJson ?? sequence.SettingsJson ?? "{}", parameters.PresetJson,
|
||||
parameters.UseProxies, await ServerProxies.FramesAsync(db, archive.Current, sequence, spec, cancellationToken), spec);
|
||||
// The frames the node renders and the photos a project version's positions are placed on, of one frames version.
|
||||
var snapshot = await SequenceEndpoints.SnapshotOfAsync(db, sequence.Id, async (on, row, ct) => (
|
||||
Frames: await ServerProxies.FramesAsync(on, archive.Current, row, spec, ct),
|
||||
Photos: settings.Portable ? HomePhotos.Of(await ProjectEndpoints.AllFramesAsync(on, row.Id, ct), await HomePhotos.AliasesAsync(on, row.Id, ct)) : null),
|
||||
cancellationToken);
|
||||
if (snapshot is not { } taken)
|
||||
{
|
||||
await FinishAsync(node, job, JobState.Failed, "The sequence no longer exists.", cancellationToken);
|
||||
continue;
|
||||
}
|
||||
var (frames, photos) = taken.Value;
|
||||
return new ClaimedJob(Dto.Job(job), sequence.Id, photos is null ? settings.Json : Placed(settings.Json, photos), parameters.PresetJson,
|
||||
parameters.UseProxies, frames, spec);
|
||||
}
|
||||
await TouchAsync(node, cancellationToken);
|
||||
return (ClaimedJob?)null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The settings a node renders: the job's own (a project version's portable form among them, whose positions are placed on
|
||||
/// the frames the node gets), else those of the project version the job names (owner review 2, R8), else the sequence's.
|
||||
/// Null when the job names a version that is not one of the sequence's projects'.
|
||||
/// </summary>
|
||||
private async Task<(string Json, bool Portable)?> SettingsAsync(RenderJobParameters parameters, Sequence sequence, CancellationToken cancellationToken)
|
||||
{
|
||||
if (parameters.SettingsJson is { } embedded) return (embedded, Waits(embedded));
|
||||
if (parameters.ProjectVersionId is not { } versionId) return (sequence.SettingsJson ?? "{}", false);
|
||||
return await db.Projects.GetVersionAsync(versionId, cancellationToken) is { } version
|
||||
&& await db.Projects.GetAsync(version.ProjectId, cancellationToken) is { } project && project.SequenceId == sequence.Id
|
||||
? (version.SettingsJson, Waits(version.SettingsJson))
|
||||
: null;
|
||||
}
|
||||
|
||||
/// <summary>Settings with positions that wait for their photos (a project version's portable form, settings synced from another copy).</summary>
|
||||
private static bool Waits(string settingsJson) => settingsJson.Contains("pendingFrames", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>The settings with their waiting positions placed on <paramref name="photos"/>, as the server's own render does; others as they are.</summary>
|
||||
private static string Placed(string settingsJson, FramePhotos photos)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ProjectContent.Place(settingsJson, photos);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
// Not a project: the node says so as it reads it.
|
||||
return settingsJson;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Outcome<bool>> ProgressAsync(Caller caller, Guid nodeId, Guid jobId, NodeProgress progress, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome<bool>.Fail(404, "not_found", "node");
|
||||
@@ -176,10 +229,15 @@ public sealed class RenderNodeService(
|
||||
if (await uploads.StageVerifiedAsync(upload, cancellationToken) is not { } staged) return Outcome<RenderDto>.Fail(422, "hash_mismatch");
|
||||
var output = JsonSerializer.Deserialize(upload.RenderJson, ProtocolJson.Default.RenderResultRequest)!;
|
||||
var sequence = await db.Sequences.GetAsync(job.TargetIds[0], cancellationToken);
|
||||
// The render records the project and the version its job rendered (owner review 2, R8).
|
||||
var rendered = JobEndpoints.ParseRender(job.ParametersJson);
|
||||
Render? render;
|
||||
try
|
||||
{
|
||||
render = sequence is null ? null : await renders.CommitAsync(sequence, staged, output, node.ClientName, cancellationToken);
|
||||
render = sequence is null
|
||||
? null
|
||||
: await renders.CommitAsync(sequence, staged, output, node.ClientName, Guid.CreateVersion7(), rendered?.ProjectId, rendered?.ProjectVersionId,
|
||||
cancellationToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is LeaseUnavailableException or ArchiveOfflineException or FolderMovedException)
|
||||
{
|
||||
|
||||
@@ -14,13 +14,17 @@ namespace Chronos.Server.Storage;
|
||||
/// thumbnails, and its job handlers (scan, rebuild, verify, import, thumbnails, proxies, maintenance, the duplicates and
|
||||
/// orphans reports, index backups and clean-ups with the server's own steps, the <see cref="ICleanupStep"/>s in DI, Reorganize's
|
||||
/// <c>move</c> jobs and the <c>convert</c> jobs, which tell connected clients of the sequences they change or make, and check
|
||||
/// their space against the space volume's free bytes, <see cref="VolumeSpace.Report"/>); a scan
|
||||
/// their space against the space volume's free bytes, <see cref="VolumeSpace.Report"/>); the projects of its sequences, every
|
||||
/// change of them told to connected clients as <c>projectChanged</c> (owner review 2, R8); a scan
|
||||
/// queues thumbnails and proxies of what it indexes, as an upload does. Decoding is managed only (BRIEF §4.2). Recurring
|
||||
/// work (the periodic scan, the nightly backup of an SQLite index, clean-ups) is queued by the server's system schedules
|
||||
/// (<see cref="Jobs.ServerScheduleHost"/>, owner review 2, R10).
|
||||
/// </summary>
|
||||
public static class ArchiveModule
|
||||
{
|
||||
/// <summary>The change a conversion's Replace tells about a sequence (<c>sequenceChanged</c>).</summary>
|
||||
private const string ConvertedChange = "converted";
|
||||
|
||||
/// <summary>Name the server writes into the archive's lease files.</summary>
|
||||
public static string Owner { get; } = "server@" + Environment.MachineName;
|
||||
|
||||
@@ -40,6 +44,18 @@ public static class ArchiveModule
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, ct) is { } sequence)
|
||||
events.Publish(new SequenceChangedEvent(id, change, sequence.FramesVersion, sequence.SettingsVersion));
|
||||
// A conversion may leave settings Main has not taken yet (owner review 2, R8).
|
||||
if (change == ConvertedChange) sp.GetService<ProjectCatchUp>()?.Touch(id);
|
||||
},
|
||||
// Every change of a sequence's projects reaches connected clients (owner review 2, R8): a workspace with that
|
||||
// project checked out knows its home moved, the admin panel lists the projects again. Main's head written into the
|
||||
// sequence's settings is a settings change for the clients that read those (an older client, a desktop's sync).
|
||||
ProjectsChanged = async (changed, ct) =>
|
||||
{
|
||||
foreach (var change in changed.Changes)
|
||||
events.Publish(new ProjectChangedEvent(changed.SequenceId, change.ProjectId, change.Change, change.HeadNumber, change.HeadVersionId));
|
||||
if (changed.SettingsChanged && await db.Sequences.GetAsync(changed.SequenceId, ct) is { } sequence)
|
||||
events.Publish(new SequenceChangedEvent(sequence.Id, "settings", sequence.FramesVersion, sequence.SettingsVersion));
|
||||
},
|
||||
Decoder = DecoderFactory.CreateServer(),
|
||||
// Where a conversion writes (owner review 2, R7): the tighter of what the panel shows as the archive's free space
|
||||
|
||||
@@ -18,7 +18,8 @@ namespace Chronos.Server.Storage;
|
||||
/// handler takes the jobs whose every output a managed encoder produces (AVI MJPEG, image sequences, GIF, APNG); MP4
|
||||
/// jobs wait for render nodes until none can take them (<see cref="RenderNodeAvailability"/>), and then render here as
|
||||
/// MJPEG in AVI with a line in the job log (BRIEF §14.7). Renders recorded and Auto values changed here reach connected
|
||||
/// clients as <c>sequenceChanged</c> events (§14.3).
|
||||
/// clients as <c>sequenceChanged</c> events (§14.3); Auto values changed become Main's next version (owner review 2, R8). A
|
||||
/// render of a project version places that version's settings on the sequence's frames and records the project and the version.
|
||||
/// </summary>
|
||||
public static class ProcessingModule
|
||||
{
|
||||
@@ -48,6 +49,8 @@ public static class ProcessingModule
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, ct) is { } sequence)
|
||||
events.Publish(new SequenceChangedEvent(id, change, sequence.FramesVersion, sequence.SettingsVersion));
|
||||
// Auto values an analysis wrote into the settings become Main's next version now (owner review 2, R8).
|
||||
if (change == ProcessingOptions.SettingsChange) sp.GetService<ProjectCatchUp>()?.Touch(id);
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Channels;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Projects;
|
||||
|
||||
namespace Chronos.Server.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Brings a sequence's projects in step soon after its settings were written outside the projects (owner review 2, R8): an
|
||||
/// older client's or a desktop sync's <c>PATCH /sequences/{id}</c>, an analysis's Auto values, a conversion. Main takes them
|
||||
/// as an external version at once (<see cref="ProjectService.CatchUpAsync"/>), and connected clients hear
|
||||
/// <c>projectChanged</c> without waiting for the next project call, which would catch up all the same. Only a sequence that
|
||||
/// has its Main project already: Main is made when the projects are first read, never for every sequence an analysis or a
|
||||
/// sync touches. One sequence at a time in the background; a sequence asked for again while it waits is caught up once; one
|
||||
/// another writer holds is tried again a few times, then left to that next call. Nothing happens without the archive engine
|
||||
/// (a test host without it).
|
||||
/// </summary>
|
||||
public sealed class ProjectCatchUp(IServiceProvider services, ILogger<ProjectCatchUp> logger) : BackgroundService
|
||||
{
|
||||
/// <summary>The waits before each new try of a sequence another writer holds.</summary>
|
||||
private static readonly TimeSpan[] Retries = [TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(2)];
|
||||
|
||||
private readonly Channel<(Guid Id, int Attempt)> _queue = Channel.CreateUnbounded<(Guid Id, int Attempt)>(new UnboundedChannelOptions { SingleReader = true });
|
||||
private readonly ConcurrentDictionary<Guid, byte> _waiting = new();
|
||||
private CancellationToken _stopping;
|
||||
|
||||
/// <summary>Catches the projects of <paramref name="sequenceId"/> up soon (at once when nothing waits).</summary>
|
||||
public void Touch(Guid sequenceId) => Enqueue(sequenceId, 0);
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_stopping = stoppingToken;
|
||||
try
|
||||
{
|
||||
await foreach (var (id, attempt) in _queue.Reader.ReadAllAsync(stoppingToken))
|
||||
{
|
||||
_waiting.TryRemove(id, out _);
|
||||
await CatchUpAsync(id, attempt, stoppingToken);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
// The server stops: what waits is caught up by the next project call.
|
||||
}
|
||||
}
|
||||
|
||||
private void Enqueue(Guid id, int attempt)
|
||||
{
|
||||
if (_waiting.TryAdd(id, 0)) _queue.Writer.TryWrite((id, attempt));
|
||||
}
|
||||
|
||||
private async Task CatchUpAsync(Guid id, int attempt, CancellationToken cancellationToken)
|
||||
{
|
||||
if (services.GetService<ArchiveService>() is not { } archives) return;
|
||||
try
|
||||
{
|
||||
if (await services.GetRequiredService<ChronosDatabase>().Projects.GetMainAsync(id, cancellationToken) is null) return;
|
||||
await archives.Projects.CatchUpAsync(id, cancellationToken);
|
||||
}
|
||||
catch (LeaseUnavailableException) when (attempt < Retries.Length)
|
||||
{
|
||||
_ = RetryLaterAsync(id, attempt);
|
||||
}
|
||||
catch (Exception ex) when (ex is ProjectException or ArchiveOfflineException or SequenceFolderMovedException or LeaseUnavailableException)
|
||||
{
|
||||
// The sequence is gone, its archive offline or its folder elsewhere, or it stayed busy: the next project call catches up.
|
||||
logger.LogDebug("The projects of sequence {Sequence} are caught up later: {Reason}", id, ex.Message);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogWarning(ex, "The projects of sequence {Sequence} could not be caught up", id);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RetryLaterAsync(Guid id, int attempt)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(Retries[attempt], _stopping);
|
||||
Enqueue(id, attempt + 1);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Stopping.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,15 @@ public sealed class RenderStore(ChronosDatabase db, ServerArchive archive, TimeP
|
||||
/// <exception cref="FolderMovedException">The sequence is not in the folder the index has.</exception>
|
||||
public Task<Render?> CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, Guid id,
|
||||
CancellationToken cancellationToken) =>
|
||||
CommitAsync(sequence, stagedFile, output, producedBy, id, null, null, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// As above, recording the project and the version rendered (owner review 2, R8: a render node's output of a job that names
|
||||
/// them); null for a render of the sequence's own settings.
|
||||
/// </summary>
|
||||
/// <exception cref="FolderMovedException">The sequence is not in the folder the index has.</exception>
|
||||
public Task<Render?> CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, Guid id, Guid? projectId,
|
||||
Guid? projectVersionId, CancellationToken cancellationToken) =>
|
||||
_archives.UnderLeaseAsync(sequence, async row =>
|
||||
{
|
||||
if (row is null || string.IsNullOrEmpty(row.Folder) || ArchivePathGuard.Resolve(archive.Root, row.Folder.TrimEnd('/') + "/exports") is not { } exports)
|
||||
@@ -55,6 +64,8 @@ public sealed class RenderStore(ChronosDatabase db, ServerArchive archive, TimeP
|
||||
Bytes = output.Size,
|
||||
Created = time.GetUtcNow(),
|
||||
ProducedBy = producedBy,
|
||||
ProjectId = projectId,
|
||||
ProjectVersionId = projectVersionId,
|
||||
};
|
||||
await db.Renders.InsertAsync(render, cancellationToken);
|
||||
return (Render?)render;
|
||||
|
||||
Reference in new issue
Block a user