diff --git a/deploy/docker/.env.example b/deploy/docker/.env.example index 9ef6444e..4e6e8df3 100644 --- a/deploy/docker/.env.example +++ b/deploy/docker/.env.example @@ -3,6 +3,8 @@ CHRONOS_DB_PASSWORD=choose-a-long-random-password # Folder of your archive, owned by PUID:PGID (an empty or root-owned folder is given to them at the first start) CHRONOS_ARCHIVE=/mnt/user/Timelapse CHRONOS_DATA=./data +# Folder that receives copies of renders (Copy to videos folder), outside the archive +CHRONOS_VIDEOS=./videos CHRONOS_PGDATA=./pgdata PUID=1000 PGID=1000 diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile index 274e3e73..7bed6e6a 100644 --- a/deploy/docker/Dockerfile +++ b/deploy/docker/Dockerfile @@ -37,7 +37,9 @@ ENV CHRONOS_ARCHIVE_PATH=/archive \ WORKDIR /app COPY --from=build /out/ ./ COPY deploy/docker/entrypoint.sh /usr/local/bin/chronos-entrypoint -RUN chmod 0755 /usr/local/bin/chronos-entrypoint && mkdir -p /archive /data +# /videos is where "Copy to videos folder" puts renders when CHRONOS_VIDEOS_PATH=/videos and a volume is mapped there; it +# is not a VOLUME, so an image run without one makes no anonymous volume that copies would vanish into. +RUN chmod 0755 /usr/local/bin/chronos-entrypoint && mkdir -p /archive /data /videos VOLUME ["/archive", "/data"] EXPOSE 8080 8443 HEALTHCHECK --interval=30s --timeout=8s --start-period=40s --retries=3 \ diff --git a/deploy/docker/docker-compose.yml b/deploy/docker/docker-compose.yml index e2224111..bacf4989 100644 --- a/deploy/docker/docker-compose.yml +++ b/deploy/docker/docker-compose.yml @@ -70,6 +70,10 @@ services: # The admin panel shows the archive volume's free space. An Unraid share (/mnt/user/) reports its primary # storage, often the cache: map the share's array view to /space (volumes below) and show that one instead. # CHRONOS_SPACE_PATH: /space + # Where "Copy to videos folder" (admin panel › Videos, the desktop's Videos section) copies renders, each checked by + # hash; the volume below. Set here, the admin panel can no longer change it; remove it (and the volume) to turn + # copying off. Chronos never deletes what it copied there. + CHRONOS_VIDEOS_PATH: /videos PUID: ${PUID:-1000} PGID: ${PGID:-1000} TZ: ${TZ:-UTC} @@ -80,6 +84,9 @@ services: # Optional, with CHRONOS_SPACE_PATH above: the same share seen on the array only, to show the array's free space. # - /mnt/user0/Timelapse:/space:ro - ${CHRONOS_DATA:-./data}:/data + # The videos folder (CHRONOS_VIDEOS_PATH above): copies of renders, flat, " (2)" on a name collision. Outside the + # archive: a folder inside it is refused. + - ${CHRONOS_VIDEOS:-./videos}:/videos postgres: image: postgres:17 diff --git a/deploy/docker/entrypoint.sh b/deploy/docker/entrypoint.sh index 0aea7c34..2bd812cd 100755 --- a/deploy/docker/entrypoint.sh +++ b/deploy/docker/entrypoint.sh @@ -9,6 +9,7 @@ PUID="${PUID:-1000}" PGID="${PGID:-1000}" DATA="${CHRONOS_DATA_PATH:-/data}" ARCHIVE="${CHRONOS_ARCHIVE_PATH:-/archive}" +VIDEOS="${CHRONOS_VIDEOS_PATH:-}" umask 002 # Help and the health probe touch neither folder. @@ -43,6 +44,24 @@ if [ "$(id -u)" = "0" ]; then exit 1 fi done + # The videos folder (optional) is treated as the archive is, except that a problem only warns: copying fails with its + # reason in the admin panel until it is fixed, and everything else runs. + if [ -n "$VIDEOS" ] && [ -d "$VIDEOS" ]; then + if command -v mountpoint >/dev/null 2>&1 && ! mountpoint -q "$VIDEOS"; then + echo "warning: $VIDEOS is not a mounted volume: copies to the videos folder stay inside the container and are lost when it is replaced; map a host folder to $VIDEOS" >&2 + fi + if ! as_user test -w "$VIDEOS"; then + if [ -z "$(ls -A "$VIDEOS")" ] || [ "$(stat -c %u "$VIDEOS")" = "0" ]; then + owner="$(stat -c %u:%g "$VIDEOS")" + if chown "$PUID:$PGID" "$VIDEOS" 2>/dev/null; then + echo "chronos: $VIDEOS belonged to $owner; it now belongs to $PUID:$PGID (the folder only, not its contents)" + fi + fi + if ! as_user test -w "$VIDEOS"; then + echo "warning: $VIDEOS is not writable by $PUID:$PGID; copies to the videos folder fail until that user can write it" >&2 + fi + fi + fi exec setpriv --reuid="$PUID" --regid="$PGID" --clear-groups -- dotnet /app/Chronos.Server.dll "$@" fi diff --git a/deploy/unraid/chronos.xml b/deploy/unraid/chronos.xml index fb51ec6f..c3f87bf8 100644 --- a/deploy/unraid/chronos.xml +++ b/deploy/unraid/chronos.xml @@ -27,6 +27,7 @@ /mnt/user/Timelapse /mnt/user0/Timelapse + /mnt/user/appdata/chronos admin @@ -39,6 +40,7 @@ auto /space + Chronos Server diff --git a/src/Chronos.Server/Api/Dto.cs b/src/Chronos.Server/Api/Dto.cs index 32a2a16c..dd6c37e9 100644 --- a/src/Chronos.Server/Api/Dto.cs +++ b/src/Chronos.Server/Api/Dto.cs @@ -24,7 +24,7 @@ public static class Dto f.FocalLength, f.Orientation, f.Width, f.Height, f.Format.ToString().ToLowerInvariant(), (long)f.Flags, f.PreviewWidth, f.PreviewHeight); public static RenderDto Render(Render r) => new(r.Id, r.PresetName, r.Codec, r.Width, r.Height, r.Fps, r.DurationSeconds, r.Bytes, r.Created, r.ProducedBy, - r.ProjectId, r.ProjectVersionId); + r.ProjectId, r.ProjectVersionId, r.Sha256, r.Kind); public static JobDto Job(Job j) => new( j.Id, JobWire.Kind(j.Kind), j.TargetIds, j.TargetLabel, JobWire.State(j.State), JobWire.Priority(j.Priority), j.ProgressDone, diff --git a/src/Chronos.Server/Api/SystemEndpoints.cs b/src/Chronos.Server/Api/SystemEndpoints.cs index 949e8e8e..3e484977 100644 --- a/src/Chronos.Server/Api/SystemEndpoints.cs +++ b/src/Chronos.Server/Api/SystemEndpoints.cs @@ -107,7 +107,7 @@ public sealed class ServerState public string Database { get; init; } = string.Empty; /// What this server offers clients (; names those clients act on). - public List Capabilities { get; } = ["uploads", "sync", "jobs", "render-nodes", "events", ServerCapabilities.Projects]; + public List Capabilities { get; } = ["uploads", "sync", "jobs", "render-nodes", "events", ServerCapabilities.Projects, ServerCapabilities.Videos]; public DateTimeOffset StartedAt { get; } = DateTimeOffset.UtcNow; } diff --git a/src/Chronos.Server/Api/TrashEndpoints.cs b/src/Chronos.Server/Api/TrashEndpoints.cs index 6f2ead2b..b1554fd3 100644 --- a/src/Chronos.Server/Api/TrashEndpoints.cs +++ b/src/Chronos.Server/Api/TrashEndpoints.cs @@ -5,6 +5,7 @@ using Chronos.Core.Model; using Chronos.Data; using Chronos.Data.Archiving; using Chronos.Data.Jobs; +using Chronos.Data.Videos; using Chronos.Protocol; using Chronos.Server.Events; using Chronos.Server.Jobs; @@ -15,7 +16,7 @@ namespace Chronos.Server.Api; /// /// /api/v1/archive/trash (owner review 2, R6; administrators of the archive): the entries of the archive's trash — -/// deleted sequences and photos deleted from sequences —, restoring one, and emptying it: a dry run that measures the +/// deleted sequences, photos deleted from sequences and deleted videos (owner review 1, R9) —, restoring one, and emptying it: a dry run that measures the /// entries and fingerprints them, then an empty-trash job that erases exactly those entries and refuses when the /// trash changed since (BRIEF §0.2 rule 8: the only erase of user files, always an explicit, confirmed action). /// @@ -29,8 +30,11 @@ public static partial class TrashEndpoints trash.MapPost("/empty", EmptyAsync); } - /// Whether has the shape of an entry name (a sequence id, a time, photos-): never a path. - public static bool IsEntryName(string? name) => name is not null && EntryName().IsMatch(name); + /// + /// Whether has the shape of an entry name (a sequence id, a time, photos-, or a videos entry + /// videos-<render id>-<ticks>, owner review 1, R9): never a path. + /// + public static bool IsEntryName(string? name) => name is not null && (EntryName().IsMatch(name) || VideoTrashNames.IsEntryName(name)); public static TrashEntryDto Entry(TrashEntry e) => new(e.Name, e.Kind.ToString().ToLowerInvariant(), e.SequenceId, e.Title, e.Folder, e.Frames, e.Files, e.Bytes, e.DeletedBy, e.DeletedAt, e.ConversionDeleted); @@ -55,9 +59,11 @@ public static partial class TrashEndpoints /// a sequence, frames for photos. /// private static async Task RestoreAsync(HttpContext context, Caller caller, string name, ChronosDatabase db, ServerArchive archive, - [FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit, EventHub events, IHostApplicationLifetime lifetime) + [FromServices] ArchiveService? archives, [FromServices] IVideoLibrary? videos, JobEngineHost host, AuditLog audit, EventHub events, + IHostApplicationLifetime lifetime) { if (!IsEntryName(name)) return ApiErrors.NotFound(context, "trash_entry"); + if (VideoTrashNames.IsEntryName(name)) return await RestoreVideoAsync(context, caller, name, db, archive, VideoEndpoints.Library(videos, archives, db), audit, events, lifetime); TrashRestoreResult result; try { @@ -107,6 +113,49 @@ public static partial class TrashEndpoints result.Kept.Count == 0 ? null : string.Join(" ", KeptPhoto.Sentences(result.Kept, culture))), ProtocolJson.Default.TrashRestoreDto); } + /// + /// Brings a deleted video back (owner review 1, R9, decision 18): the trashed file re-hashed against its record, moved back + /// into its sequence's folder (a free name when its own is taken) and its entry recorded again with the same render id. + /// 404 not_found (trash_entry); 409 sequence_gone (restore the sequence first), sequence_busy, + /// folder_in_use; 422 trash_hash_mismatch (it stays in the trash); 503 archive_offline. Publishes + /// sequenceChanged (renders). + /// + private static async Task RestoreVideoAsync(HttpContext context, Caller caller, string name, ChronosDatabase db, ServerArchive archive, + IVideoLibrary library, AuditLog audit, EventHub events, IHostApplicationLifetime lifetime) + { + if (!library.IsAvailable) return ApiErrors.NotFound(context, "trash_entry"); + Render render; + try + { + render = await library.RestoreAsync(archive.Id, name, lifetime.ApplicationStopping); + } + catch (DirectoryNotFoundException) + { + return ApiErrors.NotFound(context, "trash_entry"); + } + catch (VideoLibraryException ex) + { + return VideoEndpoints.Refused(context, ex); + } + catch (ArchiveOfflineException) + { + return Offline(context); + } + catch (LeaseUnavailableException) + { + return ApiErrors.Conflict(context, "sequence_busy"); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + return ApiErrors.Conflict(context, "folder_in_use"); + } + await audit.WriteAsync(caller, context, "trash_restored", "render", render.Id.ToString(), $"{name} · {Path.GetFileName(render.OutputPath.TrimEnd('/', '\\'))}"); + if (await db.Sequences.GetAsync(render.SequenceId, CancellationToken.None) is { } sequence) + events.Publish(new SequenceChangedEvent(sequence.Id, "renders", sequence.FramesVersion, sequence.SettingsVersion)); + return Results.Json(new TrashRestoreDto(name, TrashKind.Videos.ToString().ToLowerInvariant(), render.SequenceId, 1, [], NewSequence: false), + ProtocolJson.Default.TrashRestoreDto); + } + /// The code of why a photo stayed in the trash (). private static string ReasonCode(KeptReason reason) => reason switch { diff --git a/src/Chronos.Server/Api/VideoEndpoints.cs b/src/Chronos.Server/Api/VideoEndpoints.cs new file mode 100644 index 00000000..0086db40 --- /dev/null +++ b/src/Chronos.Server/Api/VideoEndpoints.cs @@ -0,0 +1,219 @@ +using System.Globalization; +using Microsoft.AspNetCore.Mvc; +using Chronos.Core.Archiving; +using Chronos.Core.Model; +using Chronos.Data; +using Chronos.Data.Archiving; +using Chronos.Data.Archiving.Jobs; +using Chronos.Data.Jobs; +using Chronos.Data.Videos; +using Chronos.Protocol; +using Chronos.Server.Configuration; +using Chronos.Server.Events; +using Chronos.Server.Jobs; +using Chronos.Server.Security; +using Chronos.Server.Storage; + +namespace Chronos.Server.Api; + +/// +/// /api/v1/videos (owner review 1, R9; capability ): every render of the +/// server in one list, read by anyone who may read; deleting renders into the archive's trash (Delete), always a dry run +/// first whose fingerprint the delete itself must give back, and restored through /archive/trash/{name}/restore; +/// and copying them, hash-verified, into the server's videos folder as one video-copy job (Edit). The files +/// themselves are GET /api/v1/renders/{id}/file. Deletes and copies are audited (videos_deleted, +/// videos_copied). The panel manages and never edits (owner review 2, R6): nothing here changes a project or starts a render. +/// +public static class VideoEndpoints +{ + /// The most renders one delete or copy names. + public const int MaxIds = VideoQuery.MaxLimit; + + /// Renders an audit entry names by file, the rest counted. + private const int AuditNames = 5; + + public static void Map(RouteGroupBuilder api) + { + var videos = api.MapGroup("/videos").Requires(Permissions.Read); + videos.MapGet("", ListAsync); + videos.MapGet("/{id:guid}", GetAsync); + videos.MapPost("/delete", DeleteAsync).Requires(Permissions.Delete); + videos.MapPost("/copy", CopyAsync).Requires(Permissions.Edit); + } + + /// The videos library behind these endpoints: the host's, or that of an archive engine of its own (a test host that replaces the archive module). + internal static IVideoLibrary Library(IVideoLibrary? videos, ArchiveService? archives, ChronosDatabase db) => + videos ?? LibraryEndpoints.Engine(archives, db).Videos; + + public static VideoDto Video(VideoItem item) + { + ArgumentNullException.ThrowIfNull(item); + var r = item.Render; + return new VideoDto(r.Id, r.SequenceId, r.PresetName, r.Codec, r.Width, r.Height, r.Fps, r.DurationSeconds, r.Bytes, r.Created, r.ProducedBy, + r.ProjectId, r.ProjectVersionId, r.Sha256, item.Kind, item.SequenceTitle, item.SequenceFirstCapture, item.ProjectName, item.ProjectVersionNumber, + item.State, [.. item.Copies.Select(c => new VideoCopyDto(c.Path, c.CopiedAt))], item.FileName); + } + + /// + /// One page of videos: q (sequence title, preset or file name), kind (video, still, + /// sequence), codec, year, sequence, sort (), cursor, + /// limit (1–500). 400 invalid_filter (details: the filter). + /// + private static async Task ListAsync(HttpContext context, string? q, string? kind, string? codec, int? year, Guid? sequence, string? sort, + string? cursor, int? limit, ChronosDatabase db, [FromServices] ArchiveService? archives, [FromServices] IVideoLibrary? videos) + { + kind = Blank(kind)?.ToLowerInvariant(); + sort = Blank(sort)?.ToLowerInvariant(); + if (kind is not null and not (RenderKind.Video or RenderKind.Still or RenderKind.Sequence)) return ApiErrors.BadRequest(context, "invalid_filter", "kind"); + if (sort is not null && !VideoSorts.All.Contains(sort)) return ApiErrors.BadRequest(context, "invalid_filter", "sort"); + if (year is < 1 or > 9999) return ApiErrors.BadRequest(context, "invalid_filter", "year"); + var query = VideoQuery.From(new VideoQueryDto(Blank(q), kind, Blank(codec), year, sequence, sort, Blank(cursor), limit)); + VideoPage page; + try + { + page = await Library(videos, archives, db).ListAsync(query, context.RequestAborted); + } + catch (Exception ex) when (ex is ArgumentException or FormatException) + { + // A cursor of another listing, or one this server never gave. + return ApiErrors.BadRequest(context, "invalid_cursor"); + } + return Results.Json(new VideoPageDto([.. page.Items.Select(Video)], page.Next, page.Total), ProtocolJson.Default.VideoPageDto); + } + + private static async Task GetAsync(HttpContext context, Guid id, ChronosDatabase db, [FromServices] ArchiveService? archives, + [FromServices] IVideoLibrary? videos) => + await Library(videos, archives, db).GetAsync(id, context.RequestAborted) is { } item + ? Results.Json(Video(item), ProtocolJson.Default.VideoDto) + : ApiErrors.NotFound(context, "render"); + + /// + /// With dryRun the plan of moving ids into the archive's trash: the renders that go (one whose file is + /// missing loses only its entry), their bytes, those left where they are (busy, not_found) and the plan's + /// fingerprint; nothing moves. Otherwise the request carries that fingerprint and the renders move as planned, each + /// into a trash entry (); copies in the videos folder are never touched. + /// 400 bad_request (ids, fingerprint); 409 plan_changed (nothing moved), video_busy, + /// sequence_busy, folder_in_use; 503 archive_offline. Publishes sequenceChanged + /// (renders) for every sequence that lost a render. + /// + private static async Task DeleteAsync(HttpContext context, Caller caller, VideoDeleteRequest request, ChronosDatabase db, + [FromServices] ArchiveService? archives, [FromServices] IVideoLibrary? videos, AuditLog audit, EventHub events, IHostApplicationLifetime lifetime) + { + if (Ids(request.Ids) is not { } ids) return ApiErrors.BadRequest(context, "bad_request", "ids"); + var library = Library(videos, archives, db); + if (!library.IsAvailable) return Unavailable(context); + try + { + if (request.DryRun) + { + var plan = await library.PlanDeleteAsync(ids, context.RequestAborted); + return Results.Json(new VideoDeletePlanDto([.. plan.Items.Select(Item)], plan.Bytes, [.. plan.Blocked.Select(Blocked)], plan.Fingerprint, DryRun: true), + ProtocolJson.Default.VideoDeletePlanDto); + } + if (string.IsNullOrWhiteSpace(request.Fingerprint)) return ApiErrors.BadRequest(context, "bad_request", "fingerprint"); + // Files move into the trash: the delete runs to the end even if the client disconnects meanwhile. + var result = await library.DeleteAsync(ids, request.Fingerprint, caller.User.Username, lifetime.ApplicationStopping); + var moved = result.Deleted.Where(i => !i.Missing).Sum(i => i.Render.Bytes); + if (result.Deleted.Count > 0) + { + await audit.WriteAsync(caller, context, "videos_deleted", "videos", result.Deleted.Count == 1 ? result.Deleted[0].Render.Id.ToString() : null, + Summary(result.Deleted.Count, moved, result.Deleted.Select(i => i.FileName))); + } + foreach (var sequenceId in result.Deleted.Select(i => i.Render.SequenceId).Distinct()) + { + if (await db.Sequences.GetAsync(sequenceId, CancellationToken.None) is { } sequence) + events.Publish(new SequenceChangedEvent(sequence.Id, "renders", sequence.FramesVersion, sequence.SettingsVersion)); + } + return Results.Json(new VideoDeletePlanDto([.. result.Deleted.Select(Item)], moved, [.. result.Blocked.Select(Blocked)], request.Fingerprint, + DryRun: false, result.TrashEntries), ProtocolJson.Default.VideoDeletePlanDto); + } + catch (VideoLibraryException ex) + { + return Refused(context, ex); + } + catch (ArchiveOfflineException) + { + return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline"); + } + catch (LeaseUnavailableException) + { + return ApiErrors.Conflict(context, "sequence_busy"); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + // A file is open elsewhere: nothing of it moved. + return ApiErrors.Conflict(context, "folder_in_use"); + } + } + + /// + /// Queues one video-copy job copying ids into the server's videos folder (), + /// each copy hash-verified before it takes its name (a free one, " (2)" on a collision); the renders are never touched. + /// 202 with the job. 400 bad_request (ids); 404 not_found (render, details); 409 + /// videos_folder_unset, job_unavailable; 422 videos_folder_invalid (inside the archive or the data + /// folder); 507 videos_no_space (the folder's volume cannot take the renders' bytes). + /// + private static async Task CopyAsync(HttpContext context, Caller caller, VideoCopyRequest request, ChronosDatabase db, ServerOptions options, + [FromServices] ArchiveService? archives, [FromServices] IVideoLibrary? videos, JobEngineHost host, VolumeSpace volumes, AuditLog audit) + { + var ct = context.RequestAborted; + if (Ids(request.Ids) is not { } ids) return ApiErrors.BadRequest(context, "bad_request", "ids"); + var folder = Blank(options.VideosPath); + var library = Library(videos, archives, db); + if (folder is null) return ApiErrors.Conflict(context, VideoErrors.FolderUnset); + if (!ServerSettingsStore.IsVideosFolder(folder, options) || (library.FolderProblem(folder) is { } problem && problem != VideoErrors.FolderUnset)) + return Refused(context, VideoErrors.FolderInvalid); + var renders = new List(ids.Count); + foreach (var id in ids) + { + if (await db.Renders.GetAsync(id, ct) is not { } render) return ApiErrors.NotFound(context, "render"); + renders.Add(render); + } + if (!library.IsAvailable || !host.CanRun(JobKind.VideoCopy)) return Unavailable(context); + if (volumes.Measure(folder) is { } volume && volume.FreeBytes < renders.Sum(r => r.Bytes)) return Refused(context, VideoErrors.NoSpace); + ArchiveJobRequest job; + try + { + job = library.CopyJob(new VideoCopyParameters(ids, folder, FromServer: false, caller.User.Username)); + } + catch (VideoLibraryException ex) + { + return Refused(context, ex); + } + var queued = await host.Engine.EnqueueAsync(job, caller.Id, ct); + await audit.WriteAsync(caller, context, "videos_copied", "job", queued.Id.ToString(), + Summary(renders.Count, renders.Sum(r => r.Bytes), renders.Select(r => Path.GetFileName(r.OutputPath.TrimEnd('/', '\\'))))); + return Results.Json(new VideoCopyResultDto(queued.Id), ProtocolJson.Default.VideoCopyResultDto, statusCode: StatusCodes.Status202Accepted); + } + + /// The answer of a library refusal: its code with its status. + internal static IResult Refused(HttpContext context, VideoLibraryException ex) => Refused(context, ex.Code); + + private static IResult Refused(HttpContext context, string code) => ApiErrors.Result(context, code switch + { + VideoErrors.FolderInvalid or VideoErrors.HashMismatch => StatusCodes.Status422UnprocessableEntity, + VideoErrors.NoSpace => StatusCodes.Status507InsufficientStorage, + _ => StatusCodes.Status409Conflict, + }, code); + + /// This server's archive engine has no videos library (a build before it): nothing can be deleted or copied. + private static IResult Unavailable(HttpContext context) => ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(JobKind.VideoCopy)); + + /// The ids of a request, each once in order; null when there are none, too many, or an empty one. + private static List? Ids(IReadOnlyList? ids) + { + if (ids is null || ids.Count is 0 or > MaxIds || ids.Contains(Guid.Empty)) return null; + return [.. ids.Distinct()]; + } + + private static VideoDeleteItemDto Item(VideoItem item) => + new(item.Render.Id, item.Render.SequenceId, item.SequenceTitle, item.FileName, item.Missing ? 0 : item.Render.Bytes, item.Missing); + + private static VideoBlockedDto Blocked(VideoBlocked blocked) => new(blocked.RenderId, blocked.Reason); + + /// An audit entry's details: how many, their bytes, and the first file names. + private static string Summary(int count, long bytes, IEnumerable names) => + string.Create(CultureInfo.InvariantCulture, $"{count} · {bytes} · {string.Join(", ", names.Take(AuditNames))}"); + + private static string? Blank(string? text) => string.IsNullOrWhiteSpace(text) ? null : text.Trim(); +} diff --git a/src/Chronos.Server/Configuration/ServerOptions.cs b/src/Chronos.Server/Configuration/ServerOptions.cs index 9fe3a340..23c1bbbc 100644 --- a/src/Chronos.Server/Configuration/ServerOptions.cs +++ b/src/Chronos.Server/Configuration/ServerOptions.cs @@ -40,6 +40,7 @@ public sealed class ServerOptions ("CHRONOS_SERVER_NAME", nameof(ServerName), (o, v) => o.ServerName = v), ("CHRONOS_ARCHIVE_PATH", nameof(ArchivePath), (o, v) => o.ArchivePath = v), ("CHRONOS_SPACE_PATH", nameof(SpacePath), (o, v) => o.SpacePath = v), + ("CHRONOS_VIDEOS_PATH", nameof(VideosPath), (o, v) => o.VideosPath = v), ("CHRONOS_ADMIN_USER", nameof(AdminUser), (o, v) => o.AdminUser = v), ("CHRONOS_ADMIN_PASSWORD", nameof(AdminPassword), (o, v) => o.AdminPassword = v), ("CHRONOS_DB", nameof(Database), (o, v) => o.ApplyDatabase(v)), @@ -106,6 +107,14 @@ public sealed class ServerOptions /// public string? SpacePath { get; set; } + /// + /// The folder "Copy to videos folder" copies renders into, hash-verified, flat, " (2)" on a collision (owner review 1, + /// R9; key VideosPath, variable CHRONOS_VIDEOS_PATH, which locks it). Docker: the /videos volume. + /// Unset by default, which turns copying off (videos_folder_unset); an absolute folder outside the archive and + /// the data folder (). Chronos never deletes what it copied there. + /// + public string? VideosPath { get; set; } + public string DataPath { get; set; } = OperatingSystem.IsWindows() ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Chronos Server") : "/data"; public string? AdminUser { get; set; } public string? AdminPassword { get; set; } @@ -394,6 +403,8 @@ public sealed class ServerOptions { throw new InvalidOperationException($"CHRONOS_TRUSTED_PROXIES must list IP addresses (such as 172.30.0.10) or networks in CIDR form: {ex.Message}", ex); } + if (!string.IsNullOrWhiteSpace(VideosPath) && !Path.IsPathFullyQualified(VideosPath.Trim())) + throw new InvalidOperationException("CHRONOS_VIDEOS_PATH must be an absolute folder, such as /videos."); if (ChunkSizeBytes is < 64 * 1024 or > 64 * 1024 * 1024) throw new InvalidOperationException("The chunk size must be between 64 KiB and 64 MiB."); if (MaxUploadBytes < 1 || TokenLifetimeDays < 1 || JobHistoryDays < 1 || UploadRetentionHours < 1) diff --git a/src/Chronos.Server/Configuration/ServerSettingsStore.cs b/src/Chronos.Server/Configuration/ServerSettingsStore.cs index 494c16d9..e1c56ef4 100644 --- a/src/Chronos.Server/Configuration/ServerSettingsStore.cs +++ b/src/Chronos.Server/Configuration/ServerSettingsStore.cs @@ -14,7 +14,8 @@ namespace Chronos.Server.Configuration; /// The admin panel's view of the configuration (BRIEF §14.8 Settings). Saving writes /data/server.json (only the /// edited keys; secrets and environment values are never written) and applies what can change while running, the /// parallelism and the priority queue included (owner review 1), and the log settings through -/// (owner review 2, R11), and the public ports clients use (owner review 3); the listening ports, TLS and chunk size wait for +/// (owner review 2, R11), the public ports clients use (owner review 3) and the videos folder (owner review 1, R9; checked by +/// ); the listening ports, TLS and chunk size wait for /// a restart. In a container the listening ports are locked: the template maps them. Job history is what the housekeeping system /// schedule is reset to (owner review 2, R10): the schedule's own value applies. The audit retention kept here is what the /// audit clean-up schedule is reset to; the endpoint shows that schedule's age limit in its place and puts a new one there @@ -27,9 +28,11 @@ public sealed class ServerSettingsStore(ServerOptions options, ServerLogging log [ JobKind.Render, JobKind.Import, JobKind.Scan, JobKind.Rebuild, JobKind.Verify, JobKind.LightAnalyse, JobKind.DeepAnalyse, JobKind.Thumbs, JobKind.Proxies, JobKind.Maintenance, JobKind.Duplicates, JobKind.Orphans, JobKind.Adopt, JobKind.Move, JobKind.Backup, JobKind.Cleanup, - JobKind.EmptyTrash, JobKind.Convert, + JobKind.EmptyTrash, JobKind.Convert, JobKind.VideoCopy, ]; + private static readonly StringComparison PathComparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + private readonly Lock _gate = new(); private ServerSettingsDto? _saved; @@ -52,8 +55,9 @@ public sealed class ServerSettingsStore(ServerOptions options, ServerLogging log Logs = request.Logs ?? before.Logs, PublicHttpPort = Public(request.PublicHttpPort, before.PublicHttpPort), PublicHttpsPort = Public(request.PublicHttpsPort, before.PublicHttpsPort), + VideosPath = Videos(request.VideosPath, before.VideosPath), }; - if (Validate(request, before) is { } invalid) return ("invalid_setting", invalid); + if (Validate(request, before, options) is { } invalid) return ("invalid_setting", invalid); lock (_gate) { var current = _saved ?? Snapshot(options, restartRequired: false); @@ -100,6 +104,8 @@ public sealed class ServerSettingsStore(ServerOptions options, ServerLogging log // Read by LAN discovery and the admin redirect at every use: they apply at once. options.PublicHttpPort = request.PublicHttpPort; options.PublicHttpsPort = request.PublicHttpsPort; + // Read by every copy request: it applies at once. + options.VideosPath = request.VideosPath; logging.Apply(options); var restart = request.HttpPort != options.HttpPort || request.HttpsPort != options.HttpsPort || !string.Equals(request.TlsMode, options.TlsMode.ToString(), StringComparison.OrdinalIgnoreCase) @@ -123,7 +129,41 @@ public sealed class ServerSettingsStore(ServerOptions options, ServerLogging log private static ServerSettingsDto Snapshot(ServerOptions o, bool restartRequired) => new( o.ServerName, o.HttpPort, o.HttpsPort, o.TlsMode.ToString().ToLowerInvariant(), o.TlsCert, o.TlsKey, o.Language, o.Parallelism, o.PriorityQueue, Priorities(PriorityQueue(o)), o.Discovery, o.MaxUploadBytes, o.ChunkSizeBytes, o.JobHistoryDays, o.TokenLifetimeDays, o.UploadRetentionHours, - Logs(o), LockedSettings(o), LockedBy(o), restartRequired, ProxyWarning: false, o.PublicHttpPort, o.PublicHttpsPort, o.InContainer); + Logs(o), LockedSettings(o), LockedBy(o), restartRequired, ProxyWarning: false, o.PublicHttpPort, o.PublicHttpsPort, o.InContainer, + string.IsNullOrWhiteSpace(o.VideosPath) ? null : o.VideosPath.Trim()); + + /// + /// Whether may be the videos folder (owner review 1, R9): an absolute folder that is neither the + /// archive nor the data folder nor inside either, so a copy never lands among the files the server indexes or keeps. + /// + public static bool IsVideosFolder(string folder, ServerOptions options) + { + ArgumentNullException.ThrowIfNull(options); + if (string.IsNullOrWhiteSpace(folder) || folder.Contains('\0', StringComparison.Ordinal) || !Path.IsPathFullyQualified(folder.Trim())) return false; + string full; + try + { + full = Path.TrimEndingDirectorySeparator(Path.GetFullPath(folder.Trim())); + } + catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException) + { + return false; + } + foreach (var root in new[] { options.ArchivePath, options.DataPath }) + { + var rootFull = Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)); + if (string.Equals(full, rootFull, PathComparison) || full.StartsWith(rootFull + Path.DirectorySeparatorChar, PathComparison)) return false; + } + return true; + } + + /// The videos folder as a request sets it: null keeps , an empty text clears it. + private static string? Videos(string? requested, string? current) => requested switch + { + null => current, + _ when string.IsNullOrWhiteSpace(requested) => null, + _ => requested.Trim(), + }; /// The listening ports, which a container's template maps (owner review 3). private static readonly string[] ContainerPorts = [nameof(ServerOptions.HttpPort), nameof(ServerOptions.HttpsPort)]; @@ -177,8 +217,10 @@ public sealed class ServerSettingsStore(ServerOptions options, ServerLogging log /// The first setting of out of range, or null. Listening ports are checked only when they change, /// so a value the panel cannot change (a container's, the test server's 0) never blocks saving the others. /// - private static string? Validate(ServerSettingsDto r, ServerSettingsDto current) + private static string? Validate(ServerSettingsDto r, ServerSettingsDto current, ServerOptions options) { + // Checked when it changes, so a folder an environment variable sets never blocks saving the others. + if (r.VideosPath is { } videos && videos != current.VideosPath && !IsVideosFolder(videos, options)) return nameof(ServerOptions.VideosPath); if (string.IsNullOrWhiteSpace(r.ServerName) || r.ServerName.Trim().Length > 60) return nameof(ServerOptions.ServerName); var portsChanged = r.HttpPort != current.HttpPort || r.HttpsPort != current.HttpsPort; if (portsChanged && r.HttpPort is < 1 or > 65535) return nameof(ServerOptions.HttpPort); @@ -244,6 +286,8 @@ public sealed class ServerSettingsStore(ServerOptions options, ServerLogging log Add(nameof(ServerOptions.LogRotation), Rotation(a.Logs.Rotation), Rotation(b.Logs.Rotation), v => JsonValue.Create(v.ToString())); Add(nameof(ServerOptions.AuditRetentionDays), a.Logs.AuditRetentionDays, b.Logs.AuditRetentionDays, v => JsonValue.Create(v)); Add(nameof(ServerOptions.DiagnosticsLogMb), a.Logs.DiagnosticsLogMb, b.Logs.DiagnosticsLogMb, v => JsonValue.Create(v)); + // A cleared videos folder is stored as null, so server.json no longer sets one. + Add(nameof(ServerOptions.VideosPath), a.VideosPath, b.VideosPath, v => v is null ? null : JsonValue.Create(v)); return changes; } } diff --git a/src/Chronos.Server/Hosting/ServerCommandLine.cs b/src/Chronos.Server/Hosting/ServerCommandLine.cs index bf1746c1..a91d85bb 100644 --- a/src/Chronos.Server/Hosting/ServerCommandLine.cs +++ b/src/Chronos.Server/Hosting/ServerCommandLine.cs @@ -39,6 +39,8 @@ public sealed record ServerCommandLine(ServerCommand Command, IReadOnlyList) mapped read-only to /space + CHRONOS_VIDEOS_PATH folder "Copy to videos folder" copies renders into, hash-verified (default: none, copying + off); in a container the /videos volume CHRONOS_PARALLELISM photos the running job processes at once (jobs run one at a time; default: half the cores); CHRONOS_WORKERS, its former name, still sets it CHRONOS_PRIORITY_QUEUE a job of a higher level pauses the running one, which resumes afterwards (true | false) diff --git a/src/Chronos.Server/Hosting/ServerHost.cs b/src/Chronos.Server/Hosting/ServerHost.cs index 6eb6075e..dd4d2ea6 100644 --- a/src/Chronos.Server/Hosting/ServerHost.cs +++ b/src/Chronos.Server/Hosting/ServerHost.cs @@ -187,6 +187,7 @@ public static class ServerHost ProjectEndpoints.Map(api); LibraryEndpoints.Map(api); FileEndpoints.Map(api); + VideoEndpoints.Map(api); UploadEndpoints.Map(api); JobEndpoints.Map(api); ScheduleEndpoints.Map(api); diff --git a/src/Chronos.Server/Storage/FileResponses.cs b/src/Chronos.Server/Storage/FileResponses.cs index 12c3dae6..e983cc81 100644 --- a/src/Chronos.Server/Storage/FileResponses.cs +++ b/src/Chronos.Server/Storage/FileResponses.cs @@ -11,6 +11,7 @@ public static class FileResponses [".jpg"] = "image/jpeg", [".jpeg"] = "image/jpeg", [".png"] = "image/png", + [".apng"] = "image/apng", [".gif"] = "image/gif", [".tif"] = "image/tiff", [".tiff"] = "image/tiff", @@ -22,6 +23,9 @@ public static class FileResponses [".mp4"] = "video/mp4", [".mov"] = "video/quicktime", [".avi"] = "video/x-msvideo", + // The admin panel's player (owner review 1, R9) plays what a browser's