fix(nav): inject request tab via file transformation
Register with IAmParadox File Transformation so the Request tab works when jellyfin-web is read-only (Docker/Unraid). Keep disk patch as fallback. Publish 1.0.0.2. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4531603d08
commit
57798d30e8
11 files changed
+175
-56
No files matched your search
@@ -0,0 +1,12 @@
|
||||
namespace Jellyfin.Plugin.Request.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Payload shape expected by the File Transformation callback.
|
||||
/// </summary>
|
||||
public sealed class IndexHtmlPatchPayload
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the current file contents.
|
||||
/// </summary>
|
||||
public string Contents { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -1,24 +1,31 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Jellyfin.Plugin.Request.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Ensures jellyfin-web index.html loads the Request nav loader script.
|
||||
/// Injects the Request nav loader into jellyfin-web.
|
||||
/// Prefers File Transformation (safe on Docker/Unraid); falls back to patching index.html on disk.
|
||||
/// </summary>
|
||||
public sealed class WebNavInjector : IHostedService
|
||||
{
|
||||
private const string LoaderSrc = "/Requests/Assets/nav-loader.js";
|
||||
private const string MarkerStart = "<!-- jellyfin-plugin-request-nav-start -->";
|
||||
private const string MarkerEnd = "<!-- jellyfin-plugin-request-nav-end -->";
|
||||
private static readonly Guid TransformationId = Guid.Parse("11c08950-c4bb-484d-89ff-205b3724416b");
|
||||
|
||||
private readonly IServerApplicationPaths _applicationPaths;
|
||||
private readonly ILogger<WebNavInjector> _logger;
|
||||
private CancellationTokenSource? _retryCts;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WebNavInjector"/> class.
|
||||
@@ -34,27 +41,127 @@ public sealed class WebNavInjector : IHostedService
|
||||
/// <inheritdoc />
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (TryRegisterFileTransformation())
|
||||
{
|
||||
_logger.LogInformation("Request nav loader registered with File Transformation for index.html.");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
_logger.LogWarning(
|
||||
"File Transformation plugin was not found. Install it from https://www.iamparadox.dev/jellyfin/plugins/manifest.json for reliable Request tab injection. Falling back to disk patch.");
|
||||
|
||||
try
|
||||
{
|
||||
TryInject();
|
||||
TryPatchIndexHtml();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
ex,
|
||||
"Could not inject Request nav loader into jellyfin-web. Use scripts/install-web-loader.ps1 or .sh after ensuring index.html is writable.");
|
||||
_logger.LogWarning(ex, "Could not patch jellyfin-web index.html on disk.");
|
||||
}
|
||||
|
||||
_retryCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
_ = RetryRegisterAsync(_retryCts.Token);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_retryCts?.Cancel();
|
||||
_retryCts?.Dispose();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void TryInject()
|
||||
/// <summary>
|
||||
/// File Transformation callback: injects the nav-loader script tag into index.html contents.
|
||||
/// </summary>
|
||||
/// <param name="payload">Current file contents.</param>
|
||||
/// <returns>Transformed HTML.</returns>
|
||||
public static string InjectNavLoader(IndexHtmlPatchPayload payload)
|
||||
{
|
||||
var html = payload?.Contents ?? string.Empty;
|
||||
if (html.Contains(LoaderSrc, StringComparison.Ordinal)
|
||||
|| html.Contains(MarkerStart, StringComparison.Ordinal))
|
||||
{
|
||||
return html;
|
||||
}
|
||||
|
||||
var block = $"{MarkerStart}\n<script src=\"{LoaderSrc}\" defer></script>\n{MarkerEnd}\n";
|
||||
var bodyIndex = html.LastIndexOf("</body>", StringComparison.OrdinalIgnoreCase);
|
||||
return bodyIndex >= 0
|
||||
? html.Insert(bodyIndex, block)
|
||||
: html + "\n" + block;
|
||||
}
|
||||
|
||||
private async Task RetryRegisterAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
for (var attempt = 1; attempt <= 10; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(3 * attempt), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (TryRegisterFileTransformation())
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Request nav loader registered with File Transformation on retry {Attempt}.",
|
||||
attempt);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogWarning(
|
||||
"File Transformation still unavailable after retries. Use scripts/install-web-loader.sh|.ps1 if the Request tab is missing.");
|
||||
}
|
||||
|
||||
private bool TryRegisterFileTransformation()
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly? fileTransformationAssembly = AssemblyLoadContext.All
|
||||
.SelectMany(context => context.Assemblies)
|
||||
.FirstOrDefault(assembly => assembly.FullName?.Contains(".FileTransformation", StringComparison.OrdinalIgnoreCase) == true);
|
||||
|
||||
if (fileTransformationAssembly is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Type? pluginInterfaceType = fileTransformationAssembly.GetType("Jellyfin.Plugin.FileTransformation.PluginInterface");
|
||||
MethodInfo? registerMethod = pluginInterfaceType?.GetMethod(
|
||||
"RegisterTransformation",
|
||||
BindingFlags.Public | BindingFlags.Static);
|
||||
|
||||
if (registerMethod is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
JObject payload = new()
|
||||
{
|
||||
["id"] = TransformationId,
|
||||
["fileNamePattern"] = @"index\.html",
|
||||
["callbackAssembly"] = typeof(WebNavInjector).Assembly.FullName,
|
||||
["callbackClass"] = typeof(WebNavInjector).FullName,
|
||||
["callbackMethod"] = nameof(InjectNavLoader)
|
||||
};
|
||||
|
||||
registerMethod.Invoke(null, [payload]);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "File Transformation registration failed.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void TryPatchIndexHtml()
|
||||
{
|
||||
var indexPath = ResolveIndexHtmlPath();
|
||||
if (indexPath is null)
|
||||
@@ -63,12 +170,6 @@ public sealed class WebNavInjector : IHostedService
|
||||
return;
|
||||
}
|
||||
|
||||
if (!File.Exists(indexPath))
|
||||
{
|
||||
_logger.LogWarning("jellyfin-web index.html was not found at {Path}", indexPath);
|
||||
return;
|
||||
}
|
||||
|
||||
var html = File.ReadAllText(indexPath);
|
||||
if (html.Contains(LoaderSrc, StringComparison.Ordinal))
|
||||
{
|
||||
@@ -76,18 +177,7 @@ public sealed class WebNavInjector : IHostedService
|
||||
return;
|
||||
}
|
||||
|
||||
var block = $"{MarkerStart}{Environment.NewLine}<script src=\"{LoaderSrc}\" defer></script>{Environment.NewLine}{MarkerEnd}";
|
||||
string updated;
|
||||
var bodyIndex = html.LastIndexOf("</body>", StringComparison.OrdinalIgnoreCase);
|
||||
if (bodyIndex >= 0)
|
||||
{
|
||||
updated = html.Insert(bodyIndex, block + Environment.NewLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
updated = html + Environment.NewLine + block + Environment.NewLine;
|
||||
}
|
||||
|
||||
var updated = InjectNavLoader(new IndexHtmlPatchPayload { Contents = html });
|
||||
File.WriteAllText(indexPath, updated);
|
||||
_logger.LogInformation("Injected Request nav loader into {Path}", indexPath);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,10 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<ExcludeAssets>runtime</ExcludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<ExcludeAssets>runtime</ExcludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in new issue
Block a user