PluginsController.cs 7.4 KB

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