PluginsController.cs 8.0 KB

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