Files
Chronos/src/Chronos.Data/Videos/VideosFolder.cs
T
Alby96andClaude Opus 5.5 03b4d0ef60 feat(P2): videos library, verified video trash and copy job, render hashes and still jobs
Renders now record their SHA-256 and kind, so copies, deletes and restores can
be verified. The new videos library lists every render with paging and
filters, deletes into the archive trash after a dry run whose fingerprint must
still match (journalled, so a crash mid-move is settled on open), restores by id
refusing a tampered file, and a VideoCopy job copies renders hash-verified into
a videos folder outside the archive, never overwriting. A still job saves one
frame as an image through an interim Data-side path until P1's Core still API
lands. Batch exports keep the playback and routing uses the job's own export,
so a playback job needs settings format 3 and an older node never claims it.

Refs #21

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 02:27:37 +01:00

101 lines
4.8 KiB
C#

using System.Globalization;
namespace Chronos.Data.Videos;
/// <summary>
/// The videos folder (owner review 1, R9, decision 15): a flat folder outside every archive and the workspace that renders
/// are copied into, each under its own name or, when that is taken, "name (2)", "name (3)"… (the export naming rule). A
/// copy is written as <c>.incoming-&lt;render id&gt;.part</c> first and takes its name only once verified.
/// </summary>
public static class VideosFolder
{
/// <summary>The prefix of a copy being written; never a name a finished copy takes.</summary>
public const string PartPrefix = ".incoming-";
/// <summary>The suffix of a copy being written.</summary>
public const string PartSuffix = ".part";
/// <summary>
/// Why <paramref name="folder"/> cannot be the videos folder: <see cref="VideoErrors.FolderUnset"/> when blank,
/// <see cref="VideoErrors.FolderInvalid"/> when it is not an absolute path or lies inside (or is) one of
/// <paramref name="protectedRoots"/> (the archive roots, the workspace); null when it can.
/// </summary>
public static string? Problem(string? folder, IEnumerable<string?> protectedRoots)
{
ArgumentNullException.ThrowIfNull(protectedRoots);
if (string.IsNullOrWhiteSpace(folder)) return VideoErrors.FolderUnset;
string full;
try
{
if (!Path.IsPathFullyQualified(folder)) return VideoErrors.FolderInvalid;
full = Normalize(folder);
}
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
{
return VideoErrors.FolderInvalid;
}
foreach (var root in protectedRoots)
{
if (string.IsNullOrWhiteSpace(root)) continue;
if (IsInside(full, Normalize(root))) return VideoErrors.FolderInvalid;
}
return null;
}
/// <summary>Whether <paramref name="path"/> is <paramref name="root"/> or lies below it (both full and normalised).</summary>
internal static bool IsInside(string path, string root)
{
var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
if (string.Equals(path, root, comparison)) return true;
var prefix = root.EndsWith(Path.DirectorySeparatorChar) ? root : root + Path.DirectorySeparatorChar;
return path.StartsWith(prefix, comparison);
}
/// <summary>The full path without a trailing separator (a drive or file-system root keeps its own).</summary>
internal static string Normalize(string path)
{
var full = Path.GetFullPath(path);
var trimmed = full.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
return trimmed.Length == 0 || trimmed.EndsWith(':') ? full : trimmed;
}
/// <summary>
/// The path a copy named <paramref name="fileName"/> takes in <paramref name="folder"/>: the name itself when free, else
/// "name (2).ext", "name (3).ext"… (a folder's name, an image sequence, has no extension to keep).
/// </summary>
/// <param name="isFolder">The copy is an image sequence's folder: its whole name is the stem.</param>
public static string FreeName(string folder, string fileName, bool isFolder = false)
{
ArgumentException.ThrowIfNullOrWhiteSpace(folder);
ArgumentException.ThrowIfNullOrWhiteSpace(fileName);
var name = Path.GetFileName(fileName);
var (stem, extension) = isFolder ? (name, string.Empty) : (Path.GetFileNameWithoutExtension(name), Path.GetExtension(name));
for (var n = 1; ; n++)
{
var candidate = Path.Combine(folder, (n == 1 ? stem : string.Create(CultureInfo.InvariantCulture, $"{stem} ({n})")) + extension);
if (!File.Exists(candidate) && !Directory.Exists(candidate)) return candidate;
}
}
/// <summary>Where the copy of render <paramref name="renderId"/> is written in <paramref name="folder"/> until it is verified.</summary>
public static string PartPath(string folder, Guid renderId) => Path.Combine(folder, PartPrefix + renderId.ToString("N") + PartSuffix);
/// <summary>
/// The free bytes of the volume of <paramref name="folder"/>: what <paramref name="measure"/> says (a host's own measure,
/// tests), else the drive's; null when unknown.
/// </summary>
public static long? FreeBytes(string folder, Func<string, long?>? measure = null)
{
if (measure?.Invoke(folder) is { } measured) return measured;
try
{
var root = Path.GetPathRoot(Path.GetFullPath(folder));
return string.IsNullOrEmpty(root) ? null : new DriveInfo(root).AvailableFreeSpace;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException)
{
return null;
}
}
}