jRay had no requirement IDs, so nothing in this repo could be traced to and the CI gate had no denominator to read. The other two components had already moved to registers; this brings the plugin level with them. Adds docs/requirements.md with 46 permanent JR-nnn IDs, each carrying a parent requirement, priority, status and verification tier, plus a per-requirement verification plan. JR is flat rather than split by theme: the plugin is one deployable with one audience, and JRay-public-server already ships UR/DR, so a second repo using those prefixes would make UR-007 ambiguous across registers. Rewrites SPEC.md as requirements prose with Current:/Gap: on every one. It had drifted into a format-plus-API reference that documented schema_version 1 while owning a format whose v2 shape was specified only in the other two repos, said nothing about SR-002's scene-scoped semantics, and carried the manifest exchange as a "planned" aside while its configuration classes were already implemented. Plugin-side exchange obligations move here from the server's spec, where they were an ownership inversion. Adds JR-038..041 for PR-005, which had no software row in any repo -- it was held structurally by SR-004 and GR-005 both being prohibitions, and a goal preserved only by prohibitions is the kind that erodes unnoticed. jRay is the component that actually opens a socket. Tags 18 units with the requirements they satisfy. Tags name what the code satisfies, so FileTransformationRegistration is not tagged JR-021: that requirement is a prohibition and was still violated elsewhere when this was written. Vendors jray-project as a submodule for the system spec and shared gate. TRACES: JR-001, JR-004, JR-005, JR-007, JR-008, JR-009, JR-010, JR-011 TRACES: JR-012, JR-013, JR-014, JR-015, JR-016, JR-017, JR-018, JR-019 TRACES: JR-020, JR-024, JR-025, JR-036, JR-038 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
141 lines
4.1 KiB
C#
141 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Jellyfin.Plugin.JRay.Models;
|
|
using Jellyfin.Plugin.JRay.Services.Interfaces;
|
|
using MediaBrowser.Common.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.JRay.Services;
|
|
|
|
/// <summary>
|
|
/// Persists the prioritise/ignore rules to a single JSON file under the
|
|
/// plugin's configuration directory, and keeps an in-memory copy for fast
|
|
/// reads. All access is synchronised so the work-discovery API and the
|
|
/// config page can touch it concurrently.
|
|
/// </summary>
|
|
// TRACES: JR-016 | PR-003
|
|
public sealed class MediaPolicyStore : IMediaPolicyStore
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
|
{
|
|
WriteIndented = true,
|
|
Converters = { new JsonStringEnumConverter() }
|
|
};
|
|
|
|
private readonly IApplicationPaths _applicationPaths;
|
|
private readonly ILogger<MediaPolicyStore> _logger;
|
|
private readonly object _gate = new();
|
|
private List<MediaPolicyRule>? _rules;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MediaPolicyStore"/> class.
|
|
/// </summary>
|
|
/// <param name="applicationPaths">The Jellyfin application paths.</param>
|
|
/// <param name="logger">The logger.</param>
|
|
public MediaPolicyStore(IApplicationPaths applicationPaths, ILogger<MediaPolicyStore> logger)
|
|
{
|
|
_applicationPaths = applicationPaths;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IReadOnlyList<MediaPolicyRule> GetRules()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return EnsureLoaded().ToList();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void SetRule(MediaPolicyRule rule)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(rule);
|
|
|
|
lock (_gate)
|
|
{
|
|
var rules = EnsureLoaded();
|
|
rules.RemoveAll(r => r.Scope == rule.Scope && ValueEquals(r.Value, rule.Value));
|
|
rules.Add(rule);
|
|
Save(rules);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public bool RemoveRule(PolicyScope scope, string value)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
var rules = EnsureLoaded();
|
|
var removed = rules.RemoveAll(r => r.Scope == scope && ValueEquals(r.Value, value));
|
|
if (removed > 0)
|
|
{
|
|
Save(rules);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static bool ValueEquals(string a, string b)
|
|
{
|
|
// Genre names are matched case-insensitively; ids happen to be
|
|
// case-insensitive too (GUID strings), so a single comparison suffices.
|
|
return string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private List<MediaPolicyRule> EnsureLoaded()
|
|
{
|
|
if (_rules is not null)
|
|
{
|
|
return _rules;
|
|
}
|
|
|
|
var path = GetPath();
|
|
if (!File.Exists(path))
|
|
{
|
|
_rules = new List<MediaPolicyRule>();
|
|
return _rules;
|
|
}
|
|
|
|
try
|
|
{
|
|
using var stream = File.OpenRead(path);
|
|
_rules = JsonSerializer.Deserialize<List<MediaPolicyRule>>(stream, JsonOptions)
|
|
?? new List<MediaPolicyRule>();
|
|
}
|
|
catch (Exception ex) when (ex is IOException or JsonException)
|
|
{
|
|
_logger.LogWarning(ex, "JRay: failed to read media policy file {Path}; starting empty", path);
|
|
_rules = new List<MediaPolicyRule>();
|
|
}
|
|
|
|
return _rules;
|
|
}
|
|
|
|
private void Save(List<MediaPolicyRule> rules)
|
|
{
|
|
var path = GetPath();
|
|
var directory = Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Media policy path has no directory.");
|
|
Directory.CreateDirectory(directory);
|
|
|
|
var tempPath = path + ".tmp";
|
|
using (var stream = File.Create(tempPath))
|
|
{
|
|
JsonSerializer.Serialize(stream, rules, JsonOptions);
|
|
}
|
|
|
|
File.Move(tempPath, path, overwrite: true);
|
|
}
|
|
|
|
private string GetPath()
|
|
{
|
|
return Path.Combine(_applicationPaths.PluginConfigurationsPath, "JRay", "policy.json");
|
|
}
|
|
}
|