The server side of the videos library (owner review 1, R9): one list of every render, a delete into the archive's trash that is always a dry run first and must give the plan's fingerprint back, and a hash-verified copy into a videos folder as one video-copy job, so the panel and the desktop manage renders without editing anything. - /api/v1/videos: list (filters, sort, paging), get, delete (Delete, audited videos_deleted) and copy (Edit, audited videos_copied; 409 videos_folder_unset, 422 videos_folder_invalid, 507 videos_no_space). - The "videos" capability; format 3 comes from ProjectSettings.CurrentVersion. - Trash routes accept videos-<id>-<ticks> entries; restore goes through the videos library (hash re-check, same render id). - Render DTOs carry sha256 and kind; sent renders and node results record the verified hash and the kind. - VideosPath setting (CHRONOS_VIDEOS_PATH locks it): absolute, outside the archive and the data folder; video-copy gets a priority level. - Docker: /videos volume in compose with CHRONOS_VIDEOS_PATH, folder made in the image (not a VOLUME), entrypoint checks it; Unraid template rows. - .webm, .m4v and .apng served with their media types for the panel player. Refs #21 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
171 lines
10 KiB
C#
171 lines
10 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using Chronos.Core.Archiving;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data;
|
|
using Chronos.Data.Archiving;
|
|
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.
|
|
/// A render a workspace made (phase 5c) names its project and version: both are checked against the sequence when the upload
|
|
/// starts and again when it completes (422 <c>project_mismatch</c>: the project is not one of the sequence's, or the version
|
|
/// not one of the project's) and recorded with the render; a version alone names its project. Without them a render is filed
|
|
/// as before, recording neither.
|
|
/// </summary>
|
|
/// <param name="archives">The archive engine whose projects are read when the index does not know one yet; one of its own when none is registered.</param>
|
|
public sealed class RenderUploads(
|
|
ChronosDatabase db,
|
|
ServerOptions options,
|
|
UploadService uploads,
|
|
UploadStore store,
|
|
RenderStore renders,
|
|
EventHub events,
|
|
VolumeSpace volumes,
|
|
TimeProvider time,
|
|
ArchiveService? archives = null)
|
|
{
|
|
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");
|
|
}
|
|
// Checked before a byte is sent, and again at completion.
|
|
if (await ProjectOfAsync(sequenceId, request, cancellationToken) is null) return Outcome<StartUploadResponse>.Fail(422, "project_mismatch");
|
|
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 (volumes.Measure(options.UploadsPath) is { } uploadVolume && uploadVolume.FreeBytes < 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));
|
|
// Asked again: the upload may have been started before the project was deleted from the index, or by an earlier server.
|
|
if (await ProjectOfAsync(sequenceId, output, cancellationToken) is not { } provenance) return Outcome<RenderDto>.Fail(422, "project_mismatch");
|
|
if (!uploads.ArchiveHasRoom(upload)) return Outcome<RenderDto>.Fail(507, "insufficient_storage");
|
|
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);
|
|
// The hash just verified and the kind (a still, owner review 1, R9) are recorded with the render.
|
|
var result = new RenderResultRequest(output.FileName, output.Size, output.Sha256, output.PresetName, output.Codec, output.Width, output.Height, output.Fps,
|
|
output.DurationSeconds, output.Kind);
|
|
Render? render;
|
|
try
|
|
{
|
|
render = sequence is null ? null : await renders.CommitAsync(sequence, staged, result, output.ProducedBy ?? caller.ClientName ?? caller.User.Username,
|
|
output.RenderId, provenance.ProjectId, provenance.ProjectVersionId, cancellationToken);
|
|
}
|
|
catch (Exception ex) when (ex is LeaseUnavailableException or ArchiveOfflineException or FolderMovedException)
|
|
{
|
|
return await uploads.ReceiveAgainAsync<RenderDto>(upload, staged, ex);
|
|
}
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The project and version <paramref name="request"/> names, checked against sequence <paramref name="sequenceId"/>: the
|
|
/// project one of its own (deleted ones included: their versions are kept), the version one of that project's (a version
|
|
/// alone names its project). One the index does not know yet may be in the sequence's sidecars: listing reads them first.
|
|
/// Both null when the request names neither; null when they do not hold.
|
|
/// </summary>
|
|
private async Task<(Guid? ProjectId, Guid? ProjectVersionId)?> ProjectOfAsync(Guid sequenceId, RenderUploadRequest request, CancellationToken cancellationToken)
|
|
{
|
|
if (request.ProjectId is null && request.ProjectVersionId is null) return (null, null);
|
|
var (project, version) = await FindAsync();
|
|
if (project is null || (request.ProjectVersionId is not null && version is null))
|
|
{
|
|
await LibraryEndpoints.Engine(archives, db).Projects.ListAsync(sequenceId, cancellationToken);
|
|
(project, version) = await FindAsync();
|
|
}
|
|
if (project is null || project.SequenceId != sequenceId || (version is not null && version.ProjectId != project.Id)) return null;
|
|
return (project.Id, version?.Id);
|
|
|
|
async Task<(SequenceProject? Project, ProjectVersion? Version)> FindAsync()
|
|
{
|
|
var known = request.ProjectVersionId is { } versionId ? await db.Projects.GetVersionAsync(versionId, cancellationToken) : null;
|
|
if (request.ProjectVersionId is not null && known is null) return (null, null);
|
|
var projectId = request.ProjectId ?? known!.ProjectId;
|
|
return (await db.Projects.GetAsync(projectId, cancellationToken), known);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|