PluginsController.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using Jellyfin.Api.Constants;
  8. using Jellyfin.Api.Models.PluginDtos;
  9. using MediaBrowser.Common;
  10. using MediaBrowser.Common.Json;
  11. using MediaBrowser.Common.Plugins;
  12. using MediaBrowser.Common.Updates;
  13. using MediaBrowser.Model.Plugins;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. namespace Jellyfin.Api.Controllers
  18. {
  19. /// <summary>
  20. /// Plugins controller.
  21. /// </summary>
  22. [Authorize(Policy = Policies.DefaultAuthorization)]
  23. public class PluginsController : BaseJellyfinApiController
  24. {
  25. private readonly IApplicationHost _appHost;
  26. private readonly IInstallationManager _installationManager;
  27. private readonly JsonSerializerOptions _serializerOptions = JsonDefaults.GetOptions();
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="PluginsController"/> class.
  30. /// </summary>
  31. /// <param name="appHost">Instance of the <see cref="IApplicationHost"/> interface.</param>
  32. /// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
  33. public PluginsController(
  34. IApplicationHost appHost,
  35. IInstallationManager installationManager)
  36. {
  37. _appHost = appHost;
  38. _installationManager = installationManager;
  39. }
  40. /// <summary>
  41. /// Gets a list of currently installed plugins.
  42. /// </summary>
  43. /// <response code="200">Installed plugins returned.</response>
  44. /// <returns>List of currently installed plugins.</returns>
  45. [HttpGet]
  46. [ProducesResponseType(StatusCodes.Status200OK)]
  47. public ActionResult<IEnumerable<PluginInfo>> GetPlugins()
  48. {
  49. return Ok(_appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()));
  50. }
  51. /// <summary>
  52. /// Uninstalls a plugin.
  53. /// </summary>
  54. /// <param name="pluginId">Plugin id.</param>
  55. /// <response code="204">Plugin uninstalled.</response>
  56. /// <response code="404">Plugin not found.</response>
  57. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the file could not be found.</returns>
  58. [HttpDelete("{pluginId}")]
  59. [Authorize(Policy = Policies.RequiresElevation)]
  60. [ProducesResponseType(StatusCodes.Status204NoContent)]
  61. [ProducesResponseType(StatusCodes.Status404NotFound)]
  62. public ActionResult UninstallPlugin([FromRoute] Guid pluginId)
  63. {
  64. var plugin = _appHost.Plugins.FirstOrDefault(p => p.Id == pluginId);
  65. if (plugin == null)
  66. {
  67. return NotFound();
  68. }
  69. _installationManager.UninstallPlugin(plugin);
  70. return NoContent();
  71. }
  72. /// <summary>
  73. /// Gets plugin configuration.
  74. /// </summary>
  75. /// <param name="pluginId">Plugin id.</param>
  76. /// <response code="200">Plugin configuration returned.</response>
  77. /// <response code="404">Plugin not found or plugin configuration not found.</response>
  78. /// <returns>Plugin configuration.</returns>
  79. [HttpGet("{pluginId}/Configuration")]
  80. [ProducesResponseType(StatusCodes.Status200OK)]
  81. [ProducesResponseType(StatusCodes.Status404NotFound)]
  82. public ActionResult<BasePluginConfiguration> GetPluginConfiguration([FromRoute] Guid pluginId)
  83. {
  84. if (!(_appHost.Plugins.FirstOrDefault(p => p.Id == pluginId) is IHasPluginConfiguration plugin))
  85. {
  86. return NotFound();
  87. }
  88. return plugin.Configuration;
  89. }
  90. /// <summary>
  91. /// Updates plugin configuration.
  92. /// </summary>
  93. /// <remarks>
  94. /// Accepts plugin configuration as JSON body.
  95. /// </remarks>
  96. /// <param name="pluginId">Plugin id.</param>
  97. /// <response code="204">Plugin configuration updated.</response>
  98. /// <response code="404">Plugin not found or plugin does not have configuration.</response>
  99. /// <returns>
  100. /// A <see cref="Task" /> that represents the asynchronous operation to update plugin configuration.
  101. /// The task result contains an <see cref="NoContentResult"/> indicating success, or <see cref="NotFoundResult"/>
  102. /// when plugin not found or plugin doesn't have configuration.
  103. /// </returns>
  104. [HttpPost("{pluginId}/Configuration")]
  105. [ProducesResponseType(StatusCodes.Status204NoContent)]
  106. [ProducesResponseType(StatusCodes.Status404NotFound)]
  107. public async Task<ActionResult> UpdatePluginConfiguration([FromRoute] Guid pluginId)
  108. {
  109. if (!(_appHost.Plugins.FirstOrDefault(p => p.Id == pluginId) is IHasPluginConfiguration plugin))
  110. {
  111. return NotFound();
  112. }
  113. var configuration = (BasePluginConfiguration)await JsonSerializer.DeserializeAsync(Request.Body, plugin.ConfigurationType, _serializerOptions)
  114. .ConfigureAwait(false);
  115. plugin.UpdateConfiguration(configuration);
  116. return NoContent();
  117. }
  118. /// <summary>
  119. /// Get plugin security info.
  120. /// </summary>
  121. /// <response code="200">Plugin security info returned.</response>
  122. /// <returns>Plugin security info.</returns>
  123. [Obsolete("This endpoint should not be used.")]
  124. [HttpGet("SecurityInfo")]
  125. [ProducesResponseType(StatusCodes.Status200OK)]
  126. public ActionResult<PluginSecurityInfo> GetPluginSecurityInfo()
  127. {
  128. return new PluginSecurityInfo
  129. {
  130. IsMbSupporter = true,
  131. SupporterKey = "IAmTotallyLegit"
  132. };
  133. }
  134. /// <summary>
  135. /// Updates plugin security info.
  136. /// </summary>
  137. /// <param name="pluginSecurityInfo">Plugin security info.</param>
  138. /// <response code="204">Plugin security info updated.</response>
  139. /// <returns>An <see cref="NoContentResult"/>.</returns>
  140. [Obsolete("This endpoint should not be used.")]
  141. [HttpPost("SecurityInfo")]
  142. [Authorize(Policy = Policies.RequiresElevation)]
  143. [ProducesResponseType(StatusCodes.Status204NoContent)]
  144. public ActionResult UpdatePluginSecurityInfo([FromBody, Required] PluginSecurityInfo pluginSecurityInfo)
  145. {
  146. return NoContent();
  147. }
  148. /// <summary>
  149. /// Gets registration status for a feature.
  150. /// </summary>
  151. /// <param name="name">Feature name.</param>
  152. /// <response code="200">Registration status returned.</response>
  153. /// <returns>Mb registration record.</returns>
  154. [Obsolete("This endpoint should not be used.")]
  155. [HttpPost("RegistrationRecords/{name}")]
  156. [ProducesResponseType(StatusCodes.Status200OK)]
  157. public ActionResult<MBRegistrationRecord> GetRegistrationStatus([FromRoute] string? name)
  158. {
  159. return new MBRegistrationRecord
  160. {
  161. IsRegistered = true,
  162. RegChecked = true,
  163. TrialVersion = false,
  164. IsValid = true,
  165. RegError = false
  166. };
  167. }
  168. /// <summary>
  169. /// Gets registration status for a feature.
  170. /// </summary>
  171. /// <param name="name">Feature name.</param>
  172. /// <response code="501">Not implemented.</response>
  173. /// <returns>Not Implemented.</returns>
  174. /// <exception cref="NotImplementedException">This endpoint is not implemented.</exception>
  175. [Obsolete("Paid plugins are not supported")]
  176. [HttpGet("Registrations/{name}")]
  177. [ProducesResponseType(StatusCodes.Status501NotImplemented)]
  178. public ActionResult GetRegistration([FromRoute] string? name)
  179. {
  180. // TODO Once we have proper apps and plugins and decide to break compatibility with paid plugins,
  181. // delete all these registration endpoints. They are only kept for compatibility.
  182. throw new NotImplementedException();
  183. }
  184. }
  185. }