feat: add enable/disable toggle for the plugin and update configuration handling
This commit is contained in:
@@ -21,6 +21,7 @@ namespace Jellyfin.Plugin.Seasonals;
|
||||
public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
||||
{
|
||||
private readonly ScriptInjector _scriptInjector;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Plugin"/> class.
|
||||
@@ -32,10 +33,45 @@ public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
||||
: base(applicationPaths, xmlSerializer)
|
||||
{
|
||||
Instance = this;
|
||||
_loggerFactory = loggerFactory;
|
||||
_scriptInjector = new ScriptInjector(applicationPaths, loggerFactory.CreateLogger<ScriptInjector>());
|
||||
if (!_scriptInjector.Inject())
|
||||
|
||||
if (Configuration.IsEnabled)
|
||||
{
|
||||
TryRegisterFallback(loggerFactory.CreateLogger("FileTransformationFallback"));
|
||||
if (!_scriptInjector.Inject())
|
||||
{
|
||||
TryRegisterFallback(loggerFactory.CreateLogger("FileTransformationFallback"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_scriptInjector.Remove()) {
|
||||
TryRemoveFallback(loggerFactory.CreateLogger("FileTransformationFallback"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void UpdateConfiguration(BasePluginConfiguration configuration)
|
||||
{
|
||||
var oldConfig = Configuration;
|
||||
base.UpdateConfiguration(configuration);
|
||||
|
||||
if (Configuration.IsEnabled != oldConfig.IsEnabled)
|
||||
{
|
||||
if (Configuration.IsEnabled)
|
||||
{
|
||||
if (!_scriptInjector.Inject())
|
||||
{
|
||||
TryRegisterFallback(_loggerFactory.CreateLogger("FileTransformationFallback"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_scriptInjector.Remove()) {
|
||||
TryRemoveFallback(_loggerFactory.CreateLogger("FileTransformationFallback"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,10 +104,24 @@ public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
||||
try
|
||||
{
|
||||
var html = originalContents;
|
||||
const string inject = "<script src=\"/Seasonals/Resources/seasonals.js\"></script>\n<body";
|
||||
|
||||
const string scriptTag = "<script src=\"/Seasonals/Resources/seasonals.js\" defer></script>";
|
||||
// MARK: I need to remember to remove legacy script tag support in future versions...
|
||||
const string legacyScriptTag = "<script src=\"/Seasonals/Resources/seasonals.js\"></script>";
|
||||
|
||||
if (Instance?.Configuration.IsEnabled != true)
|
||||
{
|
||||
// Remove script if present
|
||||
if (html.Contains("seasonals.js", StringComparison.Ordinal))
|
||||
{
|
||||
return html.Replace(scriptTag, "", StringComparison.OrdinalIgnoreCase)
|
||||
.Replace(legacyScriptTag, "", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
return html;
|
||||
}
|
||||
|
||||
if (!html.Contains("seasonals.js", StringComparison.Ordinal))
|
||||
{
|
||||
var inject = $"{scriptTag}\n<body";
|
||||
return html.Replace("<body", inject, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
@@ -133,6 +183,44 @@ public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
||||
}
|
||||
}
|
||||
|
||||
private void TryRemoveFallback(ILogger logger)
|
||||
{
|
||||
try
|
||||
{
|
||||
var assembly = AssemblyLoadContext.All
|
||||
.SelectMany(x => x.Assemblies)
|
||||
.FirstOrDefault(x => x.FullName?.Contains(".FileTransformation") ?? false);
|
||||
|
||||
if (assembly == null)
|
||||
{
|
||||
logger.LogWarning("FileTransformation plugin not found. Fallback removal skipped.");
|
||||
return;
|
||||
}
|
||||
|
||||
var type = assembly.GetType("Jellyfin.Plugin.FileTransformation.PluginInterface");
|
||||
if (type == null)
|
||||
{
|
||||
logger.LogWarning("Jellyfin.Plugin.FileTransformation.PluginInterface not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
var method = type.GetMethod("RemoveTransformation");
|
||||
if (method == null)
|
||||
{
|
||||
logger.LogWarning("RemoveTransformation method not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
Guid pluginId = Id is Guid g ? g : Guid.Parse(Id.ToString());
|
||||
method.Invoke(null, new object[] { pluginId });
|
||||
logger.LogInformation("Successfully unregistered fallback transformation via FileTransformation plugin.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error attempting to unregister fallback transformation.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<PluginPageInfo> GetPages()
|
||||
{
|
||||
@@ -142,7 +230,7 @@ public class SeasonalsPlugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
||||
{
|
||||
Name = Name,
|
||||
EnableInMainMenu = true,
|
||||
MenuIcon = "snowflake",
|
||||
MenuIcon = "ac_unit",
|
||||
EmbeddedResourcePath = string.Format(CultureInfo.InvariantCulture, "{0}.Configuration.configPage.html", GetType().Namespace)
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user