feat: inject client script through File Transformation

This commit is contained in:
2026-11-13 18:10:41 +01:00
parent fe94b0dc08
commit 3a0b7ae0e5
3 changed files with 157 additions and 0 deletions
@@ -0,0 +1,89 @@
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Herald.Injection;
/// <summary>
/// Registers the index.html transformation with the File Transformation plugin at startup.
/// That plugin lives in its own load context, so it is only ever touched through reflection.
/// </summary>
public class FileTransformationRegistrar : IHostedService
{
private const string InterfaceTypeName = "Jellyfin.Plugin.FileTransformation.PluginInterface";
// File Transformation treats this as a regular expression. Unanchored, "index.html" would also
// match script chunks such as "session-login-index-html.<hash>.chunk.js".
private const string IndexHtmlPattern = @"^index\.html$";
private static readonly Guid _transformationId = Guid.Parse("bf6fabc9-721c-46d9-9e13-3cc415a9ae1b");
private readonly ILogger<FileTransformationRegistrar> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="FileTransformationRegistrar"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
public FileTransformationRegistrar(ILogger<FileTransformationRegistrar> logger)
{
_logger = logger;
}
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
try
{
Register();
}
catch (Exception ex)
{
_logger.LogError(ex, "Registering with the File Transformation plugin failed. Notices will not pop up until the client script is added manually (see the Herald README)");
}
return Task.CompletedTask;
}
/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
private void Register()
{
var assembly = AssemblyLoadContext.All
.SelectMany(context => context.Assemblies)
.FirstOrDefault(a => a.FullName?.Contains(".FileTransformation", StringComparison.Ordinal) ?? false);
if (assembly is null)
{
_logger.LogWarning("The File Transformation plugin is not installed, so the Herald client script is not injected into the web client and notices will not pop up. Install File Transformation, or add the script manually (see the Herald README)");
return;
}
var register = assembly.GetType(InterfaceTypeName)?.GetMethod("RegisterTransformation", BindingFlags.Public | BindingFlags.Static);
var payloadType = register?.GetParameters().FirstOrDefault()?.ParameterType;
// The payload is a Newtonsoft JObject from File Transformation's own context; build it there.
var parse = payloadType?.GetMethod("Parse", BindingFlags.Public | BindingFlags.Static, [typeof(string)]);
if (register is null || parse is null)
{
_logger.LogWarning("The installed File Transformation plugin ({Assembly}) does not expose the expected registration API. The Herald client script is not injected", assembly.FullName);
return;
}
var json = JsonSerializer.Serialize(new
{
id = _transformationId,
fileNamePattern = IndexHtmlPattern,
callbackAssembly = typeof(IndexHtmlTransformer).Assembly.FullName,
callbackClass = typeof(IndexHtmlTransformer).FullName,
callbackMethod = nameof(IndexHtmlTransformer.Transform)
});
register.Invoke(null, [parse.Invoke(null, [json])]);
_logger.LogInformation("Registered the Herald client script with File Transformation {Version}", assembly.GetName().Version);
}
}