jellyfin/Jellyfin.Api/Controllers/PluginsController.cs

352 lines
14 KiB
C#
Raw Normal View History

2020-11-21 06:26:03 -07:00
using System;
2020-06-09 08:57:01 -07:00
using System.Collections.Generic;
2020-08-06 07:17:45 -07:00
using System.ComponentModel.DataAnnotations;
2020-12-06 16:48:54 -07:00
using System.Globalization;
using System.IO;
2020-06-09 08:57:01 -07:00
using System.Linq;
2020-12-06 16:48:54 -07:00
using System.Net.Mime;
2020-06-09 08:57:01 -07:00
using System.Text.Json;
using System.Threading.Tasks;
2020-12-06 16:48:54 -07:00
using Jellyfin.Api.Attributes;
2020-06-09 08:57:01 -07:00
using Jellyfin.Api.Constants;
using Jellyfin.Api.Models.PluginDtos;
2020-12-06 16:48:54 -07:00
using MediaBrowser.Common.Configuration;
2020-07-12 11:11:59 -07:00
using MediaBrowser.Common.Json;
2020-06-09 08:57:01 -07:00
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Updates;
2020-12-06 16:48:54 -07:00
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Net;
2020-06-09 08:57:01 -07:00
using MediaBrowser.Model.Plugins;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
2020-06-09 08:57:01 -07:00
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Plugins controller.
/// </summary>
2020-06-22 06:44:11 -07:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-06-09 08:57:01 -07:00
public class PluginsController : BaseJellyfinApiController
{
private readonly IInstallationManager _installationManager;
2020-12-06 16:48:54 -07:00
private readonly IPluginManager _pluginManager;
private readonly IConfigurationManager _config;
private readonly JsonSerializerOptions _serializerOptions;
2020-07-14 11:20:24 -07:00
2020-06-09 08:57:01 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="PluginsController"/> class.
/// </summary>
/// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
2020-12-06 16:48:54 -07:00
/// <param name="pluginManager">Instance of the <see cref="IPluginManager"/> interface.</param>
/// <param name="config">Instance of the <see cref="IConfigurationManager"/> interface.</param>
2020-06-09 08:57:01 -07:00
public PluginsController(
2020-12-06 16:48:54 -07:00
IInstallationManager installationManager,
IPluginManager pluginManager,
IConfigurationManager config)
2020-06-09 08:57:01 -07:00
{
_installationManager = installationManager;
2020-12-06 16:48:54 -07:00
_pluginManager = pluginManager;
2020-12-16 14:40:52 -07:00
_serializerOptions = JsonDefaults.GetOptions();
2020-12-06 16:48:54 -07:00
_config = config;
}
/// <summary>
/// Get plugin security info.
/// </summary>
/// <response code="200">Plugin security info returned.</response>
/// <returns>Plugin security info.</returns>
[Obsolete("This endpoint should not be used.")]
[HttpGet("SecurityInfo")]
[ProducesResponseType(StatusCodes.Status200OK)]
public static ActionResult<PluginSecurityInfo> GetPluginSecurityInfo()
{
return new PluginSecurityInfo
{
IsMbSupporter = true,
SupporterKey = "IAmTotallyLegit"
};
}
/// <summary>
/// Gets registration status for a feature.
/// </summary>
/// <param name="name">Feature name.</param>
/// <response code="200">Registration status returned.</response>
/// <returns>Mb registration record.</returns>
[Obsolete("This endpoint should not be used.")]
[HttpPost("RegistrationRecords/{name}")]
[ProducesResponseType(StatusCodes.Status200OK)]
public static ActionResult<MBRegistrationRecord> GetRegistrationStatus([FromRoute, Required] string name)
{
return new MBRegistrationRecord
{
IsRegistered = true,
RegChecked = true,
TrialVersion = false,
IsValid = true,
RegError = false
};
}
/// <summary>
/// Gets registration status for a feature.
/// </summary>
/// <param name="name">Feature name.</param>
/// <response code="501">Not implemented.</response>
/// <returns>Not Implemented.</returns>
/// <exception cref="NotImplementedException">This endpoint is not implemented.</exception>
[Obsolete("Paid plugins are not supported")]
[HttpGet("Registrations/{name}")]
[ProducesResponseType(StatusCodes.Status501NotImplemented)]
public static ActionResult GetRegistration([FromRoute, Required] string name)
{
// TODO Once we have proper apps and plugins and decide to break compatibility with paid plugins,
// delete all these registration endpoints. They are only kept for compatibility.
throw new NotImplementedException();
2020-06-09 08:57:01 -07:00
}
/// <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)]
2020-06-24 13:43:28 -07:00
public ActionResult<IEnumerable<PluginInfo>> GetPlugins()
2020-06-09 08:57:01 -07:00
{
2020-12-06 16:48:54 -07:00
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>
2020-12-14 16:08:04 -07:00
[HttpPost("{pluginId}/{version}/Enable")]
2020-12-06 16:48:54 -07:00
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult EnablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
2020-12-06 16:48:54 -07:00
{
2020-12-15 13:27:42 -07:00
var plugin = _pluginManager.GetPlugin(pluginId, version);
if (plugin == null)
2020-12-06 16:48:54 -07:00
{
return NotFound();
}
2020-12-15 13:27:42 -07:00
_pluginManager.EnablePlugin(plugin);
2020-12-06 16:48:54 -07:00
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>
2020-12-14 16:08:04 -07:00
[HttpPost("{pluginId}/{version}/Disable")]
2020-12-06 16:48:54 -07:00
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult DisablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
2020-12-06 16:48:54 -07:00
{
2020-12-15 13:27:42 -07:00
var plugin = _pluginManager.GetPlugin(pluginId, version);
if (plugin == null)
2020-12-06 16:48:54 -07:00
{
return NotFound();
}
2020-12-15 13:27:42 -07:00
_pluginManager.DisablePlugin(plugin);
2020-12-06 16:48:54 -07:00
return NoContent();
2020-06-09 08:57:01 -07:00
}
/// <summary>
2020-12-15 02:29:51 -07:00
/// Uninstalls a plugin by version.
2020-06-09 08:57:01 -07:00
/// </summary>
/// <param name="pluginId">Plugin id.</param>
2020-12-06 16:48:54 -07:00
/// <param name="version">Plugin version.</param>
/// <response code="204">Plugin uninstalled.</response>
2020-06-09 08:57:01 -07:00
/// <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>
2020-12-14 16:08:04 -07:00
[HttpDelete("{pluginId}/{version}")]
2020-06-09 08:57:01 -07:00
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-12-15 02:29:51 -07:00
public ActionResult UninstallPluginByVersion([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
2020-06-09 08:57:01 -07:00
{
2020-12-15 13:27:42 -07:00
var plugin = _pluginManager.GetPlugin(pluginId, version);
if (plugin == null)
2020-06-09 08:57:01 -07:00
{
return NotFound();
}
_installationManager.UninstallPlugin(plugin);
return NoContent();
2020-06-09 08:57:01 -07:00
}
2020-12-15 02:29:51 -07:00
/// <summary>
/// Uninstalls a plugin.
/// </summary>
/// <param name="pluginId">Plugin id.</param>
/// <response code="204">Plugin uninstalled.</response>
/// <response code="404">Plugin not found.</response>
2020-12-23 09:28:50 -07:00
/// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
2020-12-15 02:29:51 -07:00
[HttpDelete("{pluginId}")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[Obsolete("Please use the UninstallPluginByVersion API.")]
2020-12-15 02:29:51 -07:00
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));
// Select the un-instanced one first.
2020-12-23 09:28:50 -07:00
var plugin = plugins.FirstOrDefault(p => p.Instance == null);
2020-12-15 02:29:51 -07:00
if (plugin == null)
{
// Then by the status.
plugin = plugins.OrderBy(p => p.Manifest.Status).FirstOrDefault();
}
2020-12-16 15:36:25 -07:00
if (plugin != null)
{
_installationManager.UninstallPlugin(plugin);
2020-12-16 15:36:25 -07:00
return NoContent();
}
return NotFound();
2020-12-15 02:29:51 -07:00
}
2020-06-09 08:57:01 -07:00
/// <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)]
2020-12-14 16:08:04 -07:00
public ActionResult<BasePluginConfiguration> GetPluginConfiguration([FromRoute, Required] Guid pluginId)
2020-06-09 08:57:01 -07:00
{
2020-12-15 13:27:42 -07:00
var plugin = _pluginManager.GetPlugin(pluginId);
if (plugin?.Instance is IHasPluginConfiguration configPlugin)
2020-06-09 08:57:01 -07:00
{
2020-12-06 16:48:54 -07:00
return configPlugin.Configuration;
2020-06-09 08:57:01 -07:00
}
2020-12-06 16:48:54 -07:00
return NotFound();
2020-06-09 08:57:01 -07:00
}
/// <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>
2020-12-23 09:28:50 -07:00
/// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
2020-06-09 08:57:01 -07:00
[HttpPost("{pluginId}/Configuration")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-12-14 16:08:04 -07:00
public async Task<ActionResult> UpdatePluginConfiguration([FromRoute, Required] Guid pluginId)
2020-06-09 08:57:01 -07:00
{
2020-12-15 13:27:42 -07:00
var plugin = _pluginManager.GetPlugin(pluginId);
if (plugin?.Instance is not IHasPluginConfiguration configPlugin)
2020-06-09 08:57:01 -07:00
{
return NotFound();
}
2020-12-06 16:48:54 -07:00
var configuration = (BasePluginConfiguration?)await JsonSerializer.DeserializeAsync(Request.Body, configPlugin.ConfigurationType, _serializerOptions)
2020-06-09 08:57:01 -07:00
.ConfigureAwait(false);
2020-08-25 06:33:58 -07:00
if (configuration != null)
{
2020-12-06 16:48:54 -07:00
configPlugin.UpdateConfiguration(configuration);
2020-08-25 06:33:58 -07:00
}
return NoContent();
2020-06-09 08:57:01 -07:00
}
/// <summary>
2020-12-06 16:48:54 -07:00
/// Gets a plugin's image.
2020-06-09 08:57:01 -07:00
/// </summary>
2020-12-06 16:48:54 -07:00
/// <param name="pluginId">Plugin id.</param>
/// <param name="version">Plugin version.</param>
/// <response code="200">Plugin image returned.</response>
/// <returns>Plugin's image.</returns>
2020-12-14 16:08:04 -07:00
[HttpGet("{pluginId}/{version}/Image")]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-12-06 16:48:54 -07:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesImageFile]
[AllowAnonymous]
2020-12-14 16:08:04 -07:00
public ActionResult GetPluginImage([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
2020-06-09 08:57:01 -07:00
{
2020-12-15 13:27:42 -07:00
var plugin = _pluginManager.GetPlugin(pluginId, version);
if (plugin == null)
2020-06-09 08:57:01 -07:00
{
2020-12-06 16:48:54 -07:00
return NotFound();
}
2020-12-18 13:52:44 -07:00
var imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath ?? string.Empty);
2020-12-06 16:48:54 -07:00
if (((ServerConfiguration)_config.CommonConfiguration).DisablePluginImages
2020-12-18 13:52:44 -07:00
|| plugin.Manifest.ImagePath == null
|| !System.IO.File.Exists(imagePath))
2020-12-06 16:48:54 -07:00
{
2020-12-16 15:36:25 -07:00
return NotFound();
2020-12-06 16:48:54 -07:00
}
2020-12-18 13:52:44 -07:00
imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath);
return PhysicalFile(imagePath, MimeTypes.GetMimeType(imagePath));
2020-06-09 08:57:01 -07:00
}
/// <summary>
2020-12-06 16:48:54 -07:00
/// Gets a plugin's manifest.
2020-06-09 08:57:01 -07:00
/// </summary>
2020-12-06 16:48:54 -07:00
/// <param name="pluginId">Plugin id.</param>
/// <response code="204">Plugin manifest returned.</response>
/// <response code="404">Plugin not found.</response>
2020-12-23 09:28:50 -07:00
/// <returns>A <see cref="PluginManifest"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
2020-12-06 16:48:54 -07:00
[HttpPost("{pluginId}/Manifest")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-12-14 16:08:04 -07:00
public ActionResult<PluginManifest> GetPluginManifest([FromRoute, Required] Guid pluginId)
2020-06-09 08:57:01 -07:00
{
2020-12-15 13:27:42 -07:00
var plugin = _pluginManager.GetPlugin(pluginId);
if (plugin != null)
2020-06-09 08:57:01 -07:00
{
2020-12-16 15:36:25 -07:00
return plugin.Manifest;
2020-12-06 16:48:54 -07:00
}
return NotFound();
2020-06-09 08:57:01 -07:00
}
/// <summary>
2020-12-06 16:48:54 -07:00
/// Updates plugin security info.
2020-06-09 08:57:01 -07:00
/// </summary>
2020-12-06 16:48:54 -07:00
/// <param name="pluginSecurityInfo">Plugin security info.</param>
/// <response code="204">Plugin security info updated.</response>
/// <returns>An <see cref="NoContentResult"/>.</returns>
[Obsolete("This endpoint should not be used.")]
[HttpPost("SecurityInfo")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult UpdatePluginSecurityInfo([FromBody, Required] PluginSecurityInfo pluginSecurityInfo)
2020-06-09 08:57:01 -07:00
{
2020-12-06 16:48:54 -07:00
return NoContent();
2020-06-09 08:57:01 -07:00
}
}
}