Files
jellyfin-herald/Jellyfin.Plugin.Herald/Injection/FileTransformationRegistrar.cs
T
JoshiCodes 9c89852e97 fix: register the index.html transformation under the shared pattern
File Transformation only runs the callbacks whose pattern string equals
the requested path, so the anchored pattern was skipped whenever another
plugin had registered the plain one. Script chunks that match the pattern
as a regex are returned unchanged.
2026-11-13 22:34:31 +01:00

92 lines
3.9 KiB
C#

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";
// Has to be the literal "index.html" every other plugin registers. File Transformation groups
// callbacks by pattern string and, for a request, only runs the group whose key equals the path;
// a differently spelled pattern is skipped as soon as another plugin uses the plain one.
// The pattern is also used as a regex and matches some script chunks, which the transformer ignores.
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);
}
}