PluginsController.cs 7.5 KB

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