using System.Globalization;
namespace Chronos.Data.Videos;
///
/// 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 .incoming-<render id>.part first and takes its name only once verified.
///
public static class VideosFolder
{
/// The prefix of a copy being written; never a name a finished copy takes.
public const string PartPrefix = ".incoming-";
/// The suffix of a copy being written.
public const string PartSuffix = ".part";
///
/// Why cannot be the videos folder: when blank,
/// when it is not an absolute path or lies inside (or is) one of
/// (the archive roots, the workspace); null when it can.
///
public static string? Problem(string? folder, IEnumerable 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;
}
/// Whether is or lies below it (both full and normalised).
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);
}
/// The full path without a trailing separator (a drive or file-system root keeps its own).
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;
}
///
/// The path a copy named takes in : the name itself when free, else
/// "name (2).ext", "name (3).ext"… (a folder's name, an image sequence, has no extension to keep).
///
/// The copy is an image sequence's folder: its whole name is the stem.
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;
}
}
/// Where the copy of render is written in until it is verified.
public static string PartPath(string folder, Guid renderId) => Path.Combine(folder, PartPrefix + renderId.ToString("N") + PartSuffix);
///
/// The free bytes of the volume of : what says (a host's own measure,
/// tests), else the drive's; null when unknown.
///
public static long? FreeBytes(string folder, Func? 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;
}
}
}