using System.Text.Json;
using System.Text.Json.Nodes;
using Chronos.Core.IO;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Jobs;
using Chronos.Protocol;
using Chronos.Server.Hosting;
using LogRotation = Chronos.Core.Diagnostics.LogRotation;
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), 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
/// (SchedulerHost.AuditRetentionAsync, SchedulerHost.ApplyAuditRetentionAsync).
///
public sealed class ServerSettingsStore(ServerOptions options, ServerLogging logging)
{
/// The kinds of job the server runs, in the order the admin panel lists their levels (downloads and uploads are a client's).
public static IReadOnlyList Kinds { get; } =
[
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.VideoCopy,
];
private static readonly StringComparison PathComparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
private readonly Lock _gate = new();
private ServerSettingsDto? _saved;
public ServerSettingsDto Current()
{
lock (_gate) return _saved ?? Snapshot(options, restartRequired: false);
}
/// The priority queue as the settings have it now (, ).
public static JobPriorityQueue PriorityQueue(ServerOptions options) => JobPriorityQueue.From(options.PriorityQueue, options.JobPriorities);
/// Validates and saves; returns the error code and the offending setting, or null on success.
public (string Code, string Setting)? Save(ServerSettingsDto request)
{
var before = Current();
// A panel without the log group keeps the log settings as they are, and a client without the public ports keeps
// them too (owner review 3: null keeps a public port, 0 clears it).
request = request with
{
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, options) is { } invalid) return ("invalid_setting", invalid);
lock (_gate)
{
var current = _saved ?? Snapshot(options, restartRequired: false);
var changes = Changes(current, request);
var fixedSettings = LockedSettings(options);
if (changes.FirstOrDefault(c => fixedSettings.Contains(c.Name)) is { Name: { } locked }) return ("setting_locked", locked);
// In a container the listening ports are the template's: ports an earlier version saved are dropped at the first
// save, so the start stops warning about them (owner review 3).
var dropPorts = options.InContainer && options.IgnoredSavedPorts;
if (changes.Count == 0 && !dropPorts) return null;
var file = File.Exists(options.SettingsFile) ? JsonNode.Parse(File.ReadAllText(options.SettingsFile)) as JsonObject ?? [] : [];
foreach (var (name, value) in changes) file[name] = value;
// The former name of the parallelism (jobs run at once before owner review 1) would set it again at the next start.
if (changes.Any(c => c.Name == nameof(ServerOptions.Parallelism))) file.Remove("Workers");
if (options.InContainer)
{
file.Remove(nameof(ServerOptions.HttpPort));
file.Remove(nameof(ServerOptions.HttpsPort));
}
Directory.CreateDirectory(options.DataPath);
AtomicFile.WriteAllText(options.SettingsFile, file.ToJsonString(new JsonSerializerOptions { WriteIndented = true }));
if (dropPorts) options.IgnoredSavedPorts = false;
options.ServerName = request.ServerName.Trim();
options.Discovery = request.Discovery;
options.Language = request.Language;
options.MaxUploadBytes = request.MaxUploadBytes;
options.TokenLifetimeDays = request.TokenLifetimeDays;
options.UploadRetentionHours = request.UploadRetentionHours;
options.JobHistoryDays = request.JobHistoryDays;
options.Parallelism = request.Parallelism;
options.PriorityQueue = request.PriorityQueue;
options.JobPriorities = JobPriorityQueue.Changed(Levels(request.JobPriorities));
var logs = request.Logs;
options.LogLevel = Level(logs.FileLevel);
options.ConsoleLogLevel = Level(logs.ConsoleLevel);
options.LogRetentionDays = logs.RetentionDays;
options.LogMaxFileMb = logs.MaxFileMb;
options.LogMaxTotalMb = logs.MaxTotalMb;
options.LogRotation = Rotation(logs.Rotation);
options.AuditRetentionDays = logs.AuditRetentionDays;
options.DiagnosticsLogMb = logs.DiagnosticsLogMb;
// 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)
|| request.TlsCert != options.TlsCert || request.TlsKey != options.TlsKey
|| request.ChunkSizeBytes != options.ChunkSizeBytes;
_saved = request with
{
ServerName = request.ServerName.Trim(),
JobPriorities = Priorities(PriorityQueue(options)),
Logs = Logs(options),
Locked = fixedSettings,
LockedBy = LockedBy(options),
RestartRequired = restart,
ProxyWarning = false,
InContainer = options.InContainer,
};
return null;
}
}
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,
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)];
/// Settings the panel cannot change: those environment variables fix and, in a container, its listening ports.
private static List LockedSettings(ServerOptions o) =>
[.. o.Locked.Concat(o.InContainer ? ContainerPorts : []).Distinct(StringComparer.Ordinal)];
///
/// The variable that fixes every locked setting (for one a test locks directly, the variable that would); a container's
/// port that no variable of its own sets is fixed by .
///
private static Dictionary LockedBy(ServerOptions o) => LockedSettings(o).ToDictionary(s => s,
s => o.LockedBy.TryGetValue(s, out var variable) ? variable
: o.InContainer && ContainerPorts.Contains(s) ? ServerOptions.ContainerVariable
: ServerOptions.VariableOf(s) ?? s, StringComparer.Ordinal);
/// A public port as a request sets it: null keeps , 0 clears it (the listening port applies).
private static int? Public(int? requested, int? current) => requested switch
{
null => current,
0 => null,
_ => requested,
};
private static LogSettingsDto Logs(ServerOptions o) => new(ServerLogging.Wire(o.LogLevel), ServerLogging.Wire(o.ConsoleLogLevel), o.LogRetentionDays,
o.LogMaxFileMb, o.LogMaxTotalMb, ServerLogging.Wire(o.LogRotation), o.AuditRetentionDays, o.DiagnosticsLogMb);
/// A level has checked.
private static LogLevel Level(string wire) => ServerLogging.TryParseLevel(wire, out var level) ? level : LogLevel.Information;
/// A rotation has checked.
private static LogRotation Rotation(string wire) => ServerLogging.TryParseRotation(wire, out var rotation) ? rotation : LogRotation.Daily;
/// The level of every kind the server runs in .
private static List Priorities(JobPriorityQueue queue) =>
[.. Kinds.Select(k => new JobPriorityDto(JobWire.Kind(k), JobPriorityQueue.Wire(queue.LevelOf(k))))];
/// The levels a request sets; has checked their names.
private static Dictionary Levels(IReadOnlyList? priorities)
{
var levels = new Dictionary();
foreach (var p in priorities ?? [])
{
if (JobWire.TryParseKind(p.Kind, out var kind) && JobPriorityQueue.TryParse(p.Level, out var level)) levels[kind] = level;
}
return levels;
}
///
/// 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, 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);
if (portsChanged && (r.HttpsPort is < 1 or > 65535 || r.HttpsPort == r.HttpPort)) return nameof(ServerOptions.HttpsPort);
if (r.PublicHttpPort is < 1 or > 65535) return nameof(ServerOptions.PublicHttpPort);
if (r.PublicHttpsPort is < 1 or > 65535) return nameof(ServerOptions.PublicHttpsPort);
if (!Enum.TryParse(r.TlsMode, ignoreCase: true, out var tls) || !Enum.IsDefined(tls)) return nameof(ServerOptions.TlsMode);
if (tls == TlsMode.Pem && (string.IsNullOrWhiteSpace(r.TlsCert) || !File.Exists(r.TlsCert))) return nameof(ServerOptions.TlsCert);
if (tls == TlsMode.Pem && (string.IsNullOrWhiteSpace(r.TlsKey) || !File.Exists(r.TlsKey))) return nameof(ServerOptions.TlsKey);
if (!Languages.Shipped.Contains(r.Language, StringComparer.OrdinalIgnoreCase)) return nameof(ServerOptions.Language);
if (r.Parallelism is < 1 or > ServerOptions.MaxParallelism) return nameof(ServerOptions.Parallelism);
if ((r.JobPriorities ?? []).Any(p => !JobWire.TryParseKind(p.Kind, out var kind) || !Kinds.Contains(kind) || !JobPriorityQueue.TryParse(p.Level, out _)))
return nameof(ServerOptions.JobPriorities);
if (r.MaxUploadBytes < 1024 * 1024) return nameof(ServerOptions.MaxUploadBytes);
if (r.ChunkSizeBytes is < 64 * 1024 or > 64 * 1024 * 1024) return nameof(ServerOptions.ChunkSizeBytes);
if (r.JobHistoryDays is < 1 or > 3650) return nameof(ServerOptions.JobHistoryDays);
if (r.TokenLifetimeDays is < 1 or > 365) return nameof(ServerOptions.TokenLifetimeDays);
if (r.UploadRetentionHours is < 1 or > 8760) return nameof(ServerOptions.UploadRetentionHours);
var logs = r.Logs;
if (!ServerLogging.TryParseLevel(logs.FileLevel, out _)) return nameof(ServerOptions.LogLevel);
if (!ServerLogging.TryParseLevel(logs.ConsoleLevel, out _)) return nameof(ServerOptions.ConsoleLogLevel);
if (!ServerLogging.TryParseRotation(logs.Rotation, out _)) return nameof(ServerOptions.LogRotation);
return ServerLogging.Problem(logs.RetentionDays, logs.MaxFileMb, logs.MaxTotalMb, logs.AuditRetentionDays, logs.DiagnosticsLogMb);
}
/// Settings whose value differs, as server.json keys and JSON values.
private static List<(string Name, JsonNode? Value)> Changes(ServerSettingsDto a, ServerSettingsDto b)
{
var changes = new List<(string, JsonNode?)>();
void Add(string name, T before, T after, Func json)
{
if (!EqualityComparer.Default.Equals(before, after)) changes.Add((name, json(after)));
}
Add(nameof(ServerOptions.ServerName), a.ServerName, b.ServerName.Trim(), v => JsonValue.Create(v));
Add(nameof(ServerOptions.HttpPort), a.HttpPort, b.HttpPort, v => JsonValue.Create(v));
Add(nameof(ServerOptions.HttpsPort), a.HttpsPort, b.HttpsPort, v => JsonValue.Create(v));
// A cleared public port is stored as null, so server.json no longer sets it.
Add(nameof(ServerOptions.PublicHttpPort), a.PublicHttpPort, b.PublicHttpPort, v => v is { } port ? JsonValue.Create(port) : null);
Add(nameof(ServerOptions.PublicHttpsPort), a.PublicHttpsPort, b.PublicHttpsPort, v => v is { } port ? JsonValue.Create(port) : null);
Add(nameof(ServerOptions.TlsMode), a.TlsMode.ToLowerInvariant(), b.TlsMode.ToLowerInvariant(), v => JsonValue.Create(v));
Add(nameof(ServerOptions.TlsCert), a.TlsCert, b.TlsCert, v => v is null ? null : JsonValue.Create(v));
Add(nameof(ServerOptions.TlsKey), a.TlsKey, b.TlsKey, v => v is null ? null : JsonValue.Create(v));
Add(nameof(ServerOptions.Language), a.Language, b.Language, v => JsonValue.Create(v));
Add(nameof(ServerOptions.Parallelism), a.Parallelism, b.Parallelism, v => JsonValue.Create(v));
Add(nameof(ServerOptions.PriorityQueue), a.PriorityQueue, b.PriorityQueue, v => JsonValue.Create(v));
// Only levels that differ from the defaults are stored, as the desktop does.
var before = JobPriorityQueue.Changed(Levels(a.JobPriorities));
var after = JobPriorityQueue.Changed(Levels(b.JobPriorities));
if (!before.OrderBy(l => l.Key).SequenceEqual(after.OrderBy(l => l.Key)))
changes.Add((nameof(ServerOptions.JobPriorities), new JsonObject(after.Select(l => KeyValuePair.Create(l.Key, (JsonNode?)JsonValue.Create(l.Value))))));
Add(nameof(ServerOptions.Discovery), a.Discovery, b.Discovery, v => JsonValue.Create(v));
Add(nameof(ServerOptions.MaxUploadBytes), a.MaxUploadBytes, b.MaxUploadBytes, v => JsonValue.Create(v));
Add(nameof(ServerOptions.ChunkSizeBytes), a.ChunkSizeBytes, b.ChunkSizeBytes, v => JsonValue.Create(v));
Add(nameof(ServerOptions.JobHistoryDays), a.JobHistoryDays, b.JobHistoryDays, v => JsonValue.Create(v));
Add(nameof(ServerOptions.TokenLifetimeDays), a.TokenLifetimeDays, b.TokenLifetimeDays, v => JsonValue.Create(v));
Add(nameof(ServerOptions.UploadRetentionHours), a.UploadRetentionHours, b.UploadRetentionHours, v => JsonValue.Create(v));
// Levels and the rotation are stored by name, as server.json reads them ("Debug", "Hourly").
Add(nameof(ServerOptions.LogLevel), Level(a.Logs.FileLevel), Level(b.Logs.FileLevel), v => JsonValue.Create(v.ToString()));
Add(nameof(ServerOptions.ConsoleLogLevel), Level(a.Logs.ConsoleLevel), Level(b.Logs.ConsoleLevel), v => JsonValue.Create(v.ToString()));
Add(nameof(ServerOptions.LogRetentionDays), a.Logs.RetentionDays, b.Logs.RetentionDays, v => JsonValue.Create(v));
Add(nameof(ServerOptions.LogMaxFileMb), a.Logs.MaxFileMb, b.Logs.MaxFileMb, v => JsonValue.Create(v));
Add(nameof(ServerOptions.LogMaxTotalMb), a.Logs.MaxTotalMb, b.Logs.MaxTotalMb, v => JsonValue.Create(v));
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;
}
}