PluginsController.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if (configuration != null)
  116. {
  117. plugin.UpdateConfiguration(configuration);
  118. }
  119. return NoContent();
  120. }
  121. /// <summary>
  122. /// Get plugin security info.
  123. /// </summary>
  124. /// <response code="200">Plugin security info returned.</response>
  125. /// <returns>Plugin security info.</returns>
  126. [Obsolete("This endpoint should not be used.")]
  127. [HttpGet("SecurityInfo")]
  128. [ProducesResponseType(StatusCodes.Status200OK)]
  129. public ActionResult<PluginSecurityInfo> GetPluginSecurityInfo()
  130. {
  131. return new PluginSecurityInfo
  132. {
  133. IsMbSupporter = true,
  134. SupporterKey = "IAmTotallyLegit"
  135. };
  136. }
  137. /// <summary>
  138. /// Updates plugin security info.
  139. /// </summary>
  140. /// <param name="pluginSecurityInfo">Plugin security info.</param>
  141. /// <response code="204">Plugin security info updated.</response>
  142. /// <returns>An <see cref="NoContentResult"/>.</returns>
  143. [Obsolete("This endpoint should not be used.")]
  144. [HttpPost("SecurityInfo")]
  145. [Authorize(Policy = Policies.RequiresElevation)]
  146. [ProducesResponseType(StatusCodes.Status204NoContent)]
  147. public ActionResult UpdatePluginSecurityInfo([FromBody, Required] PluginSecurityInfo pluginSecurityInfo)
  148. {
  149. return NoContent();
  150. }
  151. /// <summary>
  152. /// Gets registration status for a feature.
  153. /// </summary>
  154. /// <param name="name">Feature name.</param>
  155. /// <response code="200">Registration status returned.</response>
  156. /// <returns>Mb registration record.</returns>
  157. [Obsolete("This endpoint should not be used.")]
  158. [HttpPost("RegistrationRecords/{name}")]
  159. [ProducesResponseType(StatusCodes.Status200OK)]
  160. public ActionResult<MBRegistrationRecord> GetRegistrationStatus([FromRoute] string? name)
  161. {
  162. return new MBRegistrationRecord
  163. {
  164. IsRegistered = true,
  165. RegChecked = true,
  166. TrialVersion = false,
  167. IsValid = true,
  168. RegError = false
  169. };
  170. }
  171. /// <summary>
  172. /// Gets registration status for a feature.
  173. /// </summary>
  174. /// <param name="name">Feature name.</param>
  175. /// <response code="501">Not implemented.</response>
  176. /// <returns>Not Implemented.</returns>
  177. /// <exception cref="NotImplementedException">This endpoint is not implemented.</exception>
  178. [Obsolete("Paid plugins are not supported")]
  179. [HttpGet("Registrations/{name}")]
  180. [ProducesResponseType(StatusCodes.Status501NotImplemented)]
  181. public ActionResult GetRegistration([FromRoute] string? name)
  182. {
  183. // TODO Once we have proper apps and plugins and decide to break compatibility with paid plugins,
  184. // delete all these registration endpoints. They are only kept for compatibility.
  185. throw new NotImplementedException();
  186. }
  187. }
  188. }