Add JSON config, DB-backed library options, and docs

- Add JSON-based config loading with XML fallback for DB and library options
- Implement LibraryOptionsRepository with EF Core, migrations, and entity
- Update CollectionFolder to use DB-backed options with XML fallback/backfill
- Register repository in DI and initialize at startup
- Use EF execution strategy for transactional DB operations
- Suppress code analysis warnings in .csproj and test files
- Add DATABASE_MIGRATION.md, LIBRARY_OPTIONS_DB_DESIGN.md, and WEBSOCKET_AUTHENTICATION.md
- Add database.json.example and improve migration docs
- Add tests for JSON config loader and update test naming warnings
This commit is contained in:
2026-04-30 12:25:24 -04:00
parent bad167656a
commit e81c127514
80 changed files with 2039 additions and 140 deletions
@@ -10,6 +10,7 @@ namespace Emby.Server.Implementations.AppBase
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Events;
@@ -306,6 +307,32 @@ namespace Emby.Server.Implementations.AppBase
private object LoadConfiguration(string path, Type configurationType)
{
// Try JSON first, then fallback to XML for backward compatibility
var jsonPath = Path.ChangeExtension(path, ".json");
// Try loading from .json if it exists
if (File.Exists(jsonPath))
{
try
{
var method = typeof(ConfigurationHelper).GetMethod(
nameof(ConfigurationHelper.GetJsonConfiguration),
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
if (method is not null)
{
var genericMethod = method.MakeGenericMethod(configurationType);
return genericMethod.Invoke(null, new object[] { jsonPath })
?? throw new InvalidOperationException("JSON deserialization returned null.");
}
}
catch (Exception ex)
{
Logger.LogError(ex, "Error loading JSON configuration file: {Path}", jsonPath);
}
}
// Fall back to XML for backward compatibility
try
{
if (File.Exists(path))
@@ -6,6 +6,8 @@ namespace Emby.Server.Implementations.AppBase
{
using System;
using System.IO;
using System.Text.Json;
using Jellyfin.Extensions.Json;
using MediaBrowser.Model.Serialization;
/// <summary>
@@ -54,10 +56,54 @@ namespace Emby.Server.Implementations.AppBase
Directory.CreateDirectory(directory);
// Save it after load in case we got new items
#pragma warning disable IDE0063 // Use simple 'using' statement
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
fs.Write(newBytes);
}
#pragma warning restore IDE0063 // Use simple 'using' statement
}
return configuration;
}
/// <summary>
/// Reads a JSON configuration file from the file system using System.Text.Json.
/// It will immediately re-serialize and save if new serialization data is available due to property changes.
/// </summary>
/// <typeparam name="T">The type of configuration to deserialize.</typeparam>
/// <param name="path">The path to the JSON configuration file.</param>
/// <returns>The deserialized configuration object.</returns>
public static T GetJsonConfiguration<T>(string path)
where T : class, new()
{
T configuration;
byte[]? buffer = null;
// Use try/catch to avoid the extra file system lookup using File.Exists
try
{
buffer = File.ReadAllBytes(path);
configuration = JsonSerializer.Deserialize<T>(buffer, JsonDefaults.Options) ?? new T();
}
catch (Exception)
{
// If file doesn't exist or deserialization fails, create a new instance with defaults
configuration = new T();
}
// Re-serialize to ensure proper formatting and any new defaults are captured
byte[] newBytes = JsonSerializer.SerializeToUtf8Bytes(configuration, JsonDefaults.Options);
// If the file didn't exist before, or if something has changed, re-save
if (buffer is null || !newBytes.AsSpan().SequenceEqual(buffer))
{
var directory = Path.GetDirectoryName(path) ?? throw new ArgumentException($"Provided path ({path}) is not valid.", nameof(path));
Directory.CreateDirectory(directory);
// Save it after load in case we got new items or formatting changed
File.WriteAllBytes(path, newBytes);
}
return configuration;