Files
Chronos/src/Chronos.Server/Storage/RenderUploads.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

119 lines
6.7 KiB
C#

using System.Globalization;
using System.Text.Json;
using Chronos.Data;
using Chronos.Data.Repositories;
using Chronos.Protocol;
using Chronos.Server.Api;
using Chronos.Server.Configuration;
using Chronos.Server.Events;
using Chronos.Server.Security;
using UploadState = Chronos.Data.Repositories.UploadState;
namespace Chronos.Server.Storage;
/// <summary>
/// Renders a client sends on request (BRIEF §14.5): chunked like frame uploads (same store, chunk endpoint and
/// retention), verified against their SHA-256 at completion and filed by <see cref="RenderStore"/> under the
/// sequence's exports with the client's render id, so both sides recognise the same render.
/// </summary>
public sealed class RenderUploads(
ChronosDatabase db,
ServerOptions options,
UploadService uploads,
UploadStore store,
RenderStore renders,
EventHub events,
TimeProvider time)
{
public async Task<Outcome<StartUploadResponse>> StartAsync(Caller caller, Guid sequenceId, RenderUploadRequest request, CancellationToken cancellationToken)
{
if (await db.Sequences.GetAsync(sequenceId, cancellationToken) is null) return Outcome<StartUploadResponse>.Fail(404, "not_found", "sequence");
if (UploadRules.SafeFileName(request.FileName) is not { } fileName) return Outcome<StartUploadResponse>.Fail(400, "invalid_file_name");
if (!UploadRules.IsSha256(request.Sha256)) return Outcome<StartUploadResponse>.Fail(400, "invalid_hash");
if (request.Size <= 0) return Outcome<StartUploadResponse>.Fail(400, "bad_request", "size");
if (request.Size > options.MaxUploadBytes)
return Outcome<StartUploadResponse>.Fail(413, "too_large", options.MaxUploadBytes.ToString(CultureInfo.InvariantCulture));
if (string.IsNullOrWhiteSpace(request.PresetName) || request.PresetName.Length > 200) return Outcome<StartUploadResponse>.Fail(400, "bad_request", "presetName");
if (request.RenderId == Guid.Empty) return Outcome<StartUploadResponse>.Fail(400, "bad_request", "renderId");
if (await db.Renders.GetAsync(request.RenderId, cancellationToken) is { } known)
{
return known.SequenceId == sequenceId
? new StartUploadResponse(null, options.ChunkSizeBytes, [], AlreadyExists: true, null)
: Outcome<StartUploadResponse>.Fail(409, "render_exists");
}
var sha = request.Sha256.ToLowerInvariant();
if (await db.Uploads.FindReceivingAsync(caller.Id, sha, cancellationToken) is { } open && open.SequenceId == sequenceId && open.Size == request.Size
&& Read(open)?.RenderId == request.RenderId && store.Exists(open.Id))
return Started(open);
if (UploadRules.FreeBytes(options.UploadsPath) is { } free && free < request.Size) return Outcome<StartUploadResponse>.Fail(507, "insufficient_storage");
var now = time.GetUtcNow();
var upload = await db.Uploads.CreateAsync(new Upload
{
Id = Guid.CreateVersion7(),
UserId = caller.Id,
SequenceId = sequenceId,
FileName = fileName,
Size = request.Size,
Sha256 = sha,
ChunkSize = options.ChunkSizeBytes,
State = UploadState.Receiving,
Created = now,
UpdatedAt = now,
RenderJson = JsonSerializer.Serialize(request with { FileName = fileName, Sha256 = sha }, ProtocolJson.Default.RenderUploadRequest),
}, cancellationToken);
store.Create(upload.Id, upload.Size);
return Started(upload);
}
/// <summary>Verifies a fully received render and files it; completing a committed one answers it again (a lost response).</summary>
public async Task<Outcome<RenderDto>> CompleteAsync(Caller caller, Guid sequenceId, Guid uploadId, CancellationToken cancellationToken)
{
var upload = await db.Uploads.GetAsync(uploadId, cancellationToken);
if (upload is null || upload.UserId != caller.Id || upload.SequenceId != sequenceId || upload.JobId is not null || Read(upload) is not { } output)
return Outcome<RenderDto>.Fail(404, "not_found", "upload");
if (upload.State == UploadState.Committed && await db.Renders.GetAsync(output.RenderId, cancellationToken) is { } done) return Dto.Render(done);
if (upload.State != UploadState.Receiving) return Outcome<RenderDto>.Fail(409, "upload_state");
if (!upload.IsComplete)
return Outcome<RenderDto>.Fail(409, "incomplete", (upload.ChunkCount - upload.ReceivedCount).ToString(CultureInfo.InvariantCulture));
if (!await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Receiving, UploadState.Committing, cancellationToken: cancellationToken))
return Outcome<RenderDto>.Fail(409, "upload_state");
if (await uploads.StageVerifiedAsync(upload, cancellationToken) is not { } staged) return Outcome<RenderDto>.Fail(422, "hash_mismatch");
var sequence = await db.Sequences.GetAsync(sequenceId, cancellationToken);
var result = new RenderResultRequest(output.FileName, output.Size, output.Sha256, output.PresetName, output.Codec, output.Width, output.Height, output.Fps,
output.DurationSeconds);
var render = sequence is null ? null : await renders.CommitAsync(sequence, staged, result, output.ProducedBy ?? caller.ClientName ?? caller.User.Username,
output.RenderId, cancellationToken);
if (render is null)
{
UploadStore.TryDelete(staged);
await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Failed, cancellationToken: CancellationToken.None);
store.Delete(upload.Id);
return Outcome<RenderDto>.Fail(409, "upload_state", "sequence");
}
await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Committed, cancellationToken: CancellationToken.None);
store.Delete(upload.Id);
if (await db.Sequences.GetAsync(sequenceId, CancellationToken.None) is { } updated)
events.Publish(new SequenceChangedEvent(updated.Id, "renders", updated.FramesVersion, updated.SettingsVersion));
return Dto.Render(render);
}
private static RenderUploadRequest? Read(Upload upload)
{
if (upload.RenderJson is not { } json) return null;
try
{
return JsonSerializer.Deserialize(json, ProtocolJson.Default.RenderUploadRequest) is { RenderId: var id } request && id != Guid.Empty ? request : null;
}
catch (JsonException)
{
return null;
}
}
private static StartUploadResponse Started(Upload upload) =>
new(upload.Id, upload.ChunkSize, UploadRules.ReceivedChunks(upload), AlreadyExists: false, FrameId: null);
}