aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Controllers/PluginsController.cs
diff options
context:
space:
mode:
authorRonan Charles-Lorel <roro.roronoa@gmail.com>2023-06-29 15:08:52 +0200
committerGitHub <noreply@github.com>2023-06-29 15:08:52 +0200
commite108183b138552013bbfd13c36937481228eb9e6 (patch)
tree8e374adf35d64b157ac88e5b84a25d186bd4ccf1 /Jellyfin.Api/Controllers/PluginsController.cs
parent31ac861b8560547c7e0c46513077abf76e6bc618 (diff)
parentb5bbb98175e0542d43c01f80c15e8dce04e58b53 (diff)
Merge branch 'jellyfin:master' into master
Diffstat (limited to 'Jellyfin.Api/Controllers/PluginsController.cs')
-rw-r--r--Jellyfin.Api/Controllers/PluginsController.cs412
1 files changed, 205 insertions, 207 deletions
diff --git a/Jellyfin.Api/Controllers/PluginsController.cs b/Jellyfin.Api/Controllers/PluginsController.cs
index b8a09990a..72ad14a28 100644
--- a/Jellyfin.Api/Controllers/PluginsController.cs
+++ b/Jellyfin.Api/Controllers/PluginsController.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
-using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text.Json;
@@ -17,250 +16,249 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
-namespace Jellyfin.Api.Controllers
+namespace Jellyfin.Api.Controllers;
+
+/// <summary>
+/// Plugins controller.
+/// </summary>
+[Authorize]
+public class PluginsController : BaseJellyfinApiController
{
+ private readonly IInstallationManager _installationManager;
+ private readonly IPluginManager _pluginManager;
+ private readonly JsonSerializerOptions _serializerOptions;
+
/// <summary>
- /// Plugins controller.
+ /// Initializes a new instance of the <see cref="PluginsController"/> class.
/// </summary>
- [Authorize(Policy = Policies.DefaultAuthorization)]
- public class PluginsController : BaseJellyfinApiController
+ /// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
+ /// <param name="pluginManager">Instance of the <see cref="IPluginManager"/> interface.</param>
+ public PluginsController(
+ IInstallationManager installationManager,
+ IPluginManager pluginManager)
{
- private readonly IInstallationManager _installationManager;
- private readonly IPluginManager _pluginManager;
- private readonly JsonSerializerOptions _serializerOptions;
+ _installationManager = installationManager;
+ _pluginManager = pluginManager;
+ _serializerOptions = JsonDefaults.Options;
+ }
- /// <summary>
- /// Initializes a new instance of the <see cref="PluginsController"/> class.
- /// </summary>
- /// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
- /// <param name="pluginManager">Instance of the <see cref="IPluginManager"/> interface.</param>
- public PluginsController(
- IInstallationManager installationManager,
- IPluginManager pluginManager)
+ /// <summary>
+ /// Gets a list of currently installed plugins.
+ /// </summary>
+ /// <response code="200">Installed plugins returned.</response>
+ /// <returns>List of currently installed plugins.</returns>
+ [HttpGet]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ public ActionResult<IEnumerable<PluginInfo>> GetPlugins()
+ {
+ return Ok(_pluginManager.Plugins
+ .OrderBy(p => p.Name)
+ .Select(p => p.GetPluginInfo()));
+ }
+
+ /// <summary>
+ /// Enables a disabled plugin.
+ /// </summary>
+ /// <param name="pluginId">Plugin id.</param>
+ /// <param name="version">Plugin version.</param>
+ /// <response code="204">Plugin enabled.</response>
+ /// <response code="404">Plugin not found.</response>
+ /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
+ [HttpPost("{pluginId}/{version}/Enable")]
+ [Authorize(Policy = Policies.RequiresElevation)]
+ [ProducesResponseType(StatusCodes.Status204NoContent)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public ActionResult EnablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
+ {
+ var plugin = _pluginManager.GetPlugin(pluginId, version);
+ if (plugin is null)
{
- _installationManager = installationManager;
- _pluginManager = pluginManager;
- _serializerOptions = JsonDefaults.Options;
+ return NotFound();
}
- /// <summary>
- /// Gets a list of currently installed plugins.
- /// </summary>
- /// <response code="200">Installed plugins returned.</response>
- /// <returns>List of currently installed plugins.</returns>
- [HttpGet]
- [ProducesResponseType(StatusCodes.Status200OK)]
- public ActionResult<IEnumerable<PluginInfo>> GetPlugins()
+ _pluginManager.EnablePlugin(plugin);
+ return NoContent();
+ }
+
+ /// <summary>
+ /// Disable a plugin.
+ /// </summary>
+ /// <param name="pluginId">Plugin id.</param>
+ /// <param name="version">Plugin version.</param>
+ /// <response code="204">Plugin disabled.</response>
+ /// <response code="404">Plugin not found.</response>
+ /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
+ [HttpPost("{pluginId}/{version}/Disable")]
+ [Authorize(Policy = Policies.RequiresElevation)]
+ [ProducesResponseType(StatusCodes.Status204NoContent)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public ActionResult DisablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
+ {
+ var plugin = _pluginManager.GetPlugin(pluginId, version);
+ if (plugin is null)
{
- return Ok(_pluginManager.Plugins
- .OrderBy(p => p.Name)
- .Select(p => p.GetPluginInfo()));
+ return NotFound();
}
- /// <summary>
- /// Enables a disabled plugin.
- /// </summary>
- /// <param name="pluginId">Plugin id.</param>
- /// <param name="version">Plugin version.</param>
- /// <response code="204">Plugin enabled.</response>
- /// <response code="404">Plugin not found.</response>
- /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
- [HttpPost("{pluginId}/{version}/Enable")]
- [Authorize(Policy = Policies.RequiresElevation)]
- [ProducesResponseType(StatusCodes.Status204NoContent)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- public ActionResult EnablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
- {
- var plugin = _pluginManager.GetPlugin(pluginId, version);
- if (plugin is null)
- {
- return NotFound();
- }
+ _pluginManager.DisablePlugin(plugin);
+ return NoContent();
+ }
- _pluginManager.EnablePlugin(plugin);
- return NoContent();
+ /// <summary>
+ /// Uninstalls a plugin by version.
+ /// </summary>
+ /// <param name="pluginId">Plugin id.</param>
+ /// <param name="version">Plugin version.</param>
+ /// <response code="204">Plugin uninstalled.</response>
+ /// <response code="404">Plugin not found.</response>
+ /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
+ [HttpDelete("{pluginId}/{version}")]
+ [Authorize(Policy = Policies.RequiresElevation)]
+ [ProducesResponseType(StatusCodes.Status204NoContent)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public ActionResult UninstallPluginByVersion([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
+ {
+ var plugin = _pluginManager.GetPlugin(pluginId, version);
+ if (plugin is null)
+ {
+ return NotFound();
}
- /// <summary>
- /// Disable a plugin.
- /// </summary>
- /// <param name="pluginId">Plugin id.</param>
- /// <param name="version">Plugin version.</param>
- /// <response code="204">Plugin disabled.</response>
- /// <response code="404">Plugin not found.</response>
- /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
- [HttpPost("{pluginId}/{version}/Disable")]
- [Authorize(Policy = Policies.RequiresElevation)]
- [ProducesResponseType(StatusCodes.Status204NoContent)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- public ActionResult DisablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
- {
- var plugin = _pluginManager.GetPlugin(pluginId, version);
- if (plugin is null)
- {
- return NotFound();
- }
+ _installationManager.UninstallPlugin(plugin);
+ return NoContent();
+ }
- _pluginManager.DisablePlugin(plugin);
- return NoContent();
- }
+ /// <summary>
+ /// Uninstalls a plugin.
+ /// </summary>
+ /// <param name="pluginId">Plugin id.</param>
+ /// <response code="204">Plugin uninstalled.</response>
+ /// <response code="404">Plugin not found.</response>
+ /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
+ [HttpDelete("{pluginId}")]
+ [Authorize(Policy = Policies.RequiresElevation)]
+ [ProducesResponseType(StatusCodes.Status204NoContent)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ [Obsolete("Please use the UninstallPluginByVersion API.")]
+ public ActionResult UninstallPlugin([FromRoute, Required] Guid pluginId)
+ {
+ // If no version is given, return the current instance.
+ var plugins = _pluginManager.Plugins.Where(p => p.Id.Equals(pluginId)).ToList();
- /// <summary>
- /// Uninstalls a plugin by version.
- /// </summary>
- /// <param name="pluginId">Plugin id.</param>
- /// <param name="version">Plugin version.</param>
- /// <response code="204">Plugin uninstalled.</response>
- /// <response code="404">Plugin not found.</response>
- /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
- [HttpDelete("{pluginId}/{version}")]
- [Authorize(Policy = Policies.RequiresElevation)]
- [ProducesResponseType(StatusCodes.Status204NoContent)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- public ActionResult UninstallPluginByVersion([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
- {
- var plugin = _pluginManager.GetPlugin(pluginId, version);
- if (plugin is null)
- {
- return NotFound();
- }
+ // Select the un-instanced one first.
+ var plugin = plugins.FirstOrDefault(p => p.Instance is null) ?? plugins.MinBy(p => p.Manifest.Status);
+ if (plugin is not null)
+ {
_installationManager.UninstallPlugin(plugin);
return NoContent();
}
- /// <summary>
- /// Uninstalls a plugin.
- /// </summary>
- /// <param name="pluginId">Plugin id.</param>
- /// <response code="204">Plugin uninstalled.</response>
- /// <response code="404">Plugin not found.</response>
- /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
- [HttpDelete("{pluginId}")]
- [Authorize(Policy = Policies.RequiresElevation)]
- [ProducesResponseType(StatusCodes.Status204NoContent)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- [Obsolete("Please use the UninstallPluginByVersion API.")]
- public ActionResult UninstallPlugin([FromRoute, Required] Guid pluginId)
- {
- // If no version is given, return the current instance.
- var plugins = _pluginManager.Plugins.Where(p => p.Id.Equals(pluginId)).ToList();
-
- // Select the un-instanced one first.
- var plugin = plugins.FirstOrDefault(p => p.Instance is null) ?? plugins.OrderBy(p => p.Manifest.Status).FirstOrDefault();
-
- if (plugin is not null)
- {
- _installationManager.UninstallPlugin(plugin);
- return NoContent();
- }
+ return NotFound();
+ }
- return NotFound();
+ /// <summary>
+ /// Gets plugin configuration.
+ /// </summary>
+ /// <param name="pluginId">Plugin id.</param>
+ /// <response code="200">Plugin configuration returned.</response>
+ /// <response code="404">Plugin not found or plugin configuration not found.</response>
+ /// <returns>Plugin configuration.</returns>
+ [HttpGet("{pluginId}/Configuration")]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public ActionResult<BasePluginConfiguration> GetPluginConfiguration([FromRoute, Required] Guid pluginId)
+ {
+ var plugin = _pluginManager.GetPlugin(pluginId);
+ if (plugin?.Instance is IHasPluginConfiguration configPlugin)
+ {
+ return configPlugin.Configuration;
}
- /// <summary>
- /// Gets plugin configuration.
- /// </summary>
- /// <param name="pluginId">Plugin id.</param>
- /// <response code="200">Plugin configuration returned.</response>
- /// <response code="404">Plugin not found or plugin configuration not found.</response>
- /// <returns>Plugin configuration.</returns>
- [HttpGet("{pluginId}/Configuration")]
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- public ActionResult<BasePluginConfiguration> GetPluginConfiguration([FromRoute, Required] Guid pluginId)
- {
- var plugin = _pluginManager.GetPlugin(pluginId);
- if (plugin?.Instance is IHasPluginConfiguration configPlugin)
- {
- return configPlugin.Configuration;
- }
+ return NotFound();
+ }
+ /// <summary>
+ /// Updates plugin configuration.
+ /// </summary>
+ /// <remarks>
+ /// Accepts plugin configuration as JSON body.
+ /// </remarks>
+ /// <param name="pluginId">Plugin id.</param>
+ /// <response code="204">Plugin configuration updated.</response>
+ /// <response code="404">Plugin not found or plugin does not have configuration.</response>
+ /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
+ [HttpPost("{pluginId}/Configuration")]
+ [ProducesResponseType(StatusCodes.Status204NoContent)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task<ActionResult> UpdatePluginConfiguration([FromRoute, Required] Guid pluginId)
+ {
+ var plugin = _pluginManager.GetPlugin(pluginId);
+ if (plugin?.Instance is not IHasPluginConfiguration configPlugin)
+ {
return NotFound();
}
- /// <summary>
- /// Updates plugin configuration.
- /// </summary>
- /// <remarks>
- /// Accepts plugin configuration as JSON body.
- /// </remarks>
- /// <param name="pluginId">Plugin id.</param>
- /// <response code="204">Plugin configuration updated.</response>
- /// <response code="404">Plugin not found or plugin does not have configuration.</response>
- /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
- [HttpPost("{pluginId}/Configuration")]
- [ProducesResponseType(StatusCodes.Status204NoContent)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- public async Task<ActionResult> UpdatePluginConfiguration([FromRoute, Required] Guid pluginId)
- {
- var plugin = _pluginManager.GetPlugin(pluginId);
- if (plugin?.Instance is not IHasPluginConfiguration configPlugin)
- {
- return NotFound();
- }
+ var configuration = (BasePluginConfiguration?)await JsonSerializer.DeserializeAsync(Request.Body, configPlugin.ConfigurationType, _serializerOptions)
+ .ConfigureAwait(false);
- var configuration = (BasePluginConfiguration?)await JsonSerializer.DeserializeAsync(Request.Body, configPlugin.ConfigurationType, _serializerOptions)
- .ConfigureAwait(false);
+ if (configuration is not null)
+ {
+ configPlugin.UpdateConfiguration(configuration);
+ }
- if (configuration is not null)
- {
- configPlugin.UpdateConfiguration(configuration);
- }
+ return NoContent();
+ }
- return NoContent();
+ /// <summary>
+ /// Gets a plugin's image.
+ /// </summary>
+ /// <param name="pluginId">Plugin id.</param>
+ /// <param name="version">Plugin version.</param>
+ /// <response code="200">Plugin image returned.</response>
+ /// <returns>Plugin's image.</returns>
+ [HttpGet("{pluginId}/{version}/Image")]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ [ProducesImageFile]
+ [AllowAnonymous]
+ public ActionResult GetPluginImage([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
+ {
+ var plugin = _pluginManager.GetPlugin(pluginId, version);
+ if (plugin is null)
+ {
+ return NotFound();
}
- /// <summary>
- /// Gets a plugin's image.
- /// </summary>
- /// <param name="pluginId">Plugin id.</param>
- /// <param name="version">Plugin version.</param>
- /// <response code="200">Plugin image returned.</response>
- /// <returns>Plugin's image.</returns>
- [HttpGet("{pluginId}/{version}/Image")]
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- [ProducesImageFile]
- [AllowAnonymous]
- public ActionResult GetPluginImage([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
+ var imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath ?? string.Empty);
+ if (plugin.Manifest.ImagePath is null || !System.IO.File.Exists(imagePath))
{
- var plugin = _pluginManager.GetPlugin(pluginId, version);
- if (plugin is null)
- {
- return NotFound();
- }
-
- var imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath ?? string.Empty);
- if (plugin.Manifest.ImagePath is null || !System.IO.File.Exists(imagePath))
- {
- return NotFound();
- }
-
- imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath);
- return PhysicalFile(imagePath, MimeTypes.GetMimeType(imagePath));
+ return NotFound();
}
- /// <summary>
- /// Gets a plugin's manifest.
- /// </summary>
- /// <param name="pluginId">Plugin id.</param>
- /// <response code="204">Plugin manifest returned.</response>
- /// <response code="404">Plugin not found.</response>
- /// <returns>A <see cref="PluginManifest"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
- [HttpPost("{pluginId}/Manifest")]
- [ProducesResponseType(StatusCodes.Status204NoContent)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- public ActionResult<PluginManifest> GetPluginManifest([FromRoute, Required] Guid pluginId)
- {
- var plugin = _pluginManager.GetPlugin(pluginId);
+ imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath);
+ return PhysicalFile(imagePath, MimeTypes.GetMimeType(imagePath));
+ }
- if (plugin is not null)
- {
- return plugin.Manifest;
- }
+ /// <summary>
+ /// Gets a plugin's manifest.
+ /// </summary>
+ /// <param name="pluginId">Plugin id.</param>
+ /// <response code="204">Plugin manifest returned.</response>
+ /// <response code="404">Plugin not found.</response>
+ /// <returns>A <see cref="PluginManifest"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
+ [HttpPost("{pluginId}/Manifest")]
+ [ProducesResponseType(StatusCodes.Status204NoContent)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public ActionResult<PluginManifest> GetPluginManifest([FromRoute, Required] Guid pluginId)
+ {
+ var plugin = _pluginManager.GetPlugin(pluginId);
- return NotFound();
+ if (plugin is not null)
+ {
+ return plugin.Manifest;
}
+
+ return NotFound();
}
}