PluginsController.cs 8.3 KB

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