PluginsController.cs 8.0 KB

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