Files
Jellyfin_Request_tvandmovies/Jellyfin.Plugin.Request/Hosting/WebNavInjector.cs
T
Michael SloanandCursor bf6cd4540f feat(repo): add request plugin with jellyfin catalog manifest
Ship the plugin sources, release zip, and SeerrFin-style manifest.json so Jellyfin can install from the Gitea raw repository URL.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-02 16:31:26 -04:00

125 lines
3.7 KiB
C#

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.Request.Hosting;
/// <summary>
/// Ensures jellyfin-web index.html loads the Request nav loader script.
/// </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 readonly IServerApplicationPaths _applicationPaths;
private readonly ILogger<WebNavInjector> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="WebNavInjector"/> class.
/// </summary>
/// <param name="applicationPaths">Server application paths.</param>
/// <param name="logger">Logger.</param>
public WebNavInjector(IServerApplicationPaths applicationPaths, ILogger<WebNavInjector> logger)
{
_applicationPaths = applicationPaths;
_logger = logger;
}
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
try
{
TryInject();
}
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.");
}
return Task.CompletedTask;
}
/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
private void TryInject()
{
var indexPath = ResolveIndexHtmlPath();
if (indexPath is null)
{
_logger.LogWarning("Could not locate jellyfin-web index.html for Request nav injection.");
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))
{
_logger.LogInformation("Request nav loader already present in {Path}", indexPath);
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;
}
File.WriteAllText(indexPath, updated);
_logger.LogInformation("Injected Request nav loader into {Path}", indexPath);
}
private string? ResolveIndexHtmlPath()
{
var webPath = _applicationPaths.WebPath;
if (!string.IsNullOrWhiteSpace(webPath))
{
var candidate = Path.Combine(webPath, "index.html");
if (File.Exists(candidate))
{
return candidate;
}
}
string[] fallbacks =
[
"/usr/share/jellyfin/web/index.html",
"/usr/lib/jellyfin/bin/jellyfin-web/index.html",
Path.Combine(AppContext.BaseDirectory, "jellyfin-web", "index.html")
];
foreach (var path in fallbacks)
{
if (File.Exists(path))
{
return path;
}
}
return null;
}
}