PluginsController.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using Jellyfin.Api.Attributes;
  9. using Jellyfin.Api.Constants;
  10. using Jellyfin.Extensions.Json;
  11. using MediaBrowser.Common.Api;
  12. using MediaBrowser.Common.Plugins;
  13. using MediaBrowser.Common.Updates;
  14. using MediaBrowser.Model.Net;
  15. using MediaBrowser.Model.Plugins;
  16. using Microsoft.AspNetCore.Authorization;
  17. using Microsoft.AspNetCore.Http;
  18. using Microsoft.AspNetCore.Mvc;
  19. namespace Jellyfin.Api.Controllers;
  20. /// <summary>
  21. /// Plugins controller.
  22. /// </summary>
  23. [Authorize(Policy = Policies.RequiresElevation)]
  24. public class PluginsController : BaseJellyfinApiController
  25. {
  26. private readonly IInstallationManager _installationManager;
  27. private readonly IPluginManager _pluginManager;
  28. private readonly JsonSerializerOptions _serializerOptions;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="PluginsController"/> class.
  31. /// </summary>
  32. /// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
  33. /// <param name="pluginManager">Instance of the <see cref="IPluginManager"/> interface.</param>
  34. public PluginsController(
  35. IInstallationManager installationManager,
  36. IPluginManager pluginManager)
  37. {
  38. _installationManager = installationManager;
  39. _pluginManager = pluginManager;
  40. _serializerOptions = JsonDefaults.Options;
  41. }
  42. /// <summary>
  43. /// Gets a list of currently installed plugins.
  44. /// </summary>
  45. /// <response code="200">Installed plugins returned.</response>
  46. /// <returns>List of currently installed plugins.</returns>
  47. [HttpGet]
  48. [ProducesResponseType(StatusCodes.Status200OK)]
  49. public ActionResult<IEnumerable<PluginInfo>> GetPlugins()
  50. {
  51. return Ok(_pluginManager.Plugins
  52. .OrderBy(p => p.Name)
  53. .Select(p => p.GetPluginInfo()));
  54. }
  55. /// <summary>
  56. /// Enables a disabled plugin.
  57. /// </summary>
  58. /// <param name="pluginId">Plugin id.</param>
  59. /// <param name="version">Plugin version.</param>
  60. /// <response code="204">Plugin enabled.</response>
  61. /// <response code="404">Plugin not found.</response>
  62. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  63. [HttpPost("{pluginId}/{version}/Enable")]
  64. [ProducesResponseType(StatusCodes.Status204NoContent)]
  65. [ProducesResponseType(StatusCodes.Status404NotFound)]
  66. public ActionResult EnablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  67. {
  68. var plugin = _pluginManager.GetPlugin(pluginId, version);
  69. if (plugin is null)
  70. {
  71. return NotFound();
  72. }
  73. _pluginManager.EnablePlugin(plugin);
  74. return NoContent();
  75. }
  76. /// <summary>
  77. /// Disable a plugin.
  78. /// </summary>
  79. /// <param name="pluginId">Plugin id.</param>
  80. /// <param name="version">Plugin version.</param>
  81. /// <response code="204">Plugin disabled.</response>
  82. /// <response code="404">Plugin not found.</response>
  83. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  84. [HttpPost("{pluginId}/{version}/Disable")]
  85. [ProducesResponseType(StatusCodes.Status204NoContent)]
  86. [ProducesResponseType(StatusCodes.Status404NotFound)]
  87. public ActionResult DisablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  88. {
  89. var plugin = _pluginManager.GetPlugin(pluginId, version);
  90. if (plugin is null)
  91. {
  92. return NotFound();
  93. }
  94. _pluginManager.DisablePlugin(plugin);
  95. return NoContent();
  96. }
  97. /// <summary>
  98. /// Uninstalls a plugin by version.
  99. /// </summary>
  100. /// <param name="pluginId">Plugin id.</param>
  101. /// <param name="version">Plugin version.</param>
  102. /// <response code="204">Plugin uninstalled.</response>
  103. /// <response code="404">Plugin not found.</response>
  104. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  105. [HttpDelete("{pluginId}/{version}")]
  106. [ProducesResponseType(StatusCodes.Status204NoContent)]
  107. [ProducesResponseType(StatusCodes.Status404NotFound)]
  108. public ActionResult UninstallPluginByVersion([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  109. {
  110. var plugin = _pluginManager.GetPlugin(pluginId, version);
  111. if (plugin is null)
  112. {
  113. return NotFound();
  114. }
  115. _installationManager.UninstallPlugin(plugin);
  116. return NoContent();
  117. }
  118. /// <summary>
  119. /// Uninstalls a plugin.
  120. /// </summary>
  121. /// <param name="pluginId">Plugin id.</param>
  122. /// <response code="204">Plugin uninstalled.</response>
  123. /// <response code="404">Plugin not found.</response>
  124. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  125. [HttpDelete("{pluginId}")]
  126. [ProducesResponseType(StatusCodes.Status204NoContent)]
  127. [ProducesResponseType(StatusCodes.Status404NotFound)]
  128. [Obsolete("Please use the UninstallPluginByVersion API.")]
  129. public ActionResult UninstallPlugin([FromRoute, Required] Guid pluginId)
  130. {
  131. // If no version is given, return the current instance.
  132. var plugins = _pluginManager.Plugins.Where(p => p.Id.Equals(pluginId)).ToList();
  133. // Select the un-instanced one first.
  134. var plugin = plugins.FirstOrDefault(p => p.Instance is null) ?? plugins.MinBy(p => p.Manifest.Status);
  135. if (plugin is not null)
  136. {
  137. _installationManager.UninstallPlugin(plugin);
  138. return NoContent();
  139. }
  140. return NotFound();
  141. }
  142. /// <summary>
  143. /// Gets plugin configuration.
  144. /// </summary>
  145. /// <param name="pluginId">Plugin id.</param>
  146. /// <response code="200">Plugin configuration returned.</response>
  147. /// <response code="404">Plugin not found or plugin configuration not found.</response>
  148. /// <returns>Plugin configuration.</returns>
  149. [HttpGet("{pluginId}/Configuration")]
  150. [ProducesResponseType(StatusCodes.Status200OK)]
  151. [ProducesResponseType(StatusCodes.Status404NotFound)]
  152. public ActionResult<BasePluginConfiguration> GetPluginConfiguration([FromRoute, Required] Guid pluginId)
  153. {
  154. var plugin = _pluginManager.GetPlugin(pluginId);
  155. if (plugin?.Instance is IHasPluginConfiguration configPlugin)
  156. {
  157. return configPlugin.Configuration;
  158. }
  159. return NotFound();
  160. }
  161. /// <summary>
  162. /// Updates plugin configuration.
  163. /// </summary>
  164. /// <remarks>
  165. /// Accepts plugin configuration as JSON body.
  166. /// </remarks>
  167. /// <param name="pluginId">Plugin id.</param>
  168. /// <response code="204">Plugin configuration updated.</response>
  169. /// <response code="404">Plugin not found or plugin does not have configuration.</response>
  170. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  171. [HttpPost("{pluginId}/Configuration")]
  172. [ProducesResponseType(StatusCodes.Status204NoContent)]
  173. [ProducesResponseType(StatusCodes.Status404NotFound)]
  174. public async Task<ActionResult> UpdatePluginConfiguration([FromRoute, Required] Guid pluginId)
  175. {
  176. var plugin = _pluginManager.GetPlugin(pluginId);
  177. if (plugin?.Instance is not IHasPluginConfiguration configPlugin)
  178. {
  179. return NotFound();
  180. }
  181. var configuration = (BasePluginConfiguration?)await JsonSerializer.DeserializeAsync(Request.Body, configPlugin.ConfigurationType, _serializerOptions)
  182. .ConfigureAwait(false);
  183. if (configuration is not null)
  184. {
  185. configPlugin.UpdateConfiguration(configuration);
  186. }
  187. return NoContent();
  188. }
  189. /// <summary>
  190. /// Gets a plugin's image.
  191. /// </summary>
  192. /// <param name="pluginId">Plugin id.</param>
  193. /// <param name="version">Plugin version.</param>
  194. /// <response code="200">Plugin image returned.</response>
  195. /// <returns>Plugin's image.</returns>
  196. [HttpGet("{pluginId}/{version}/Image")]
  197. [ProducesResponseType(StatusCodes.Status200OK)]
  198. [ProducesResponseType(StatusCodes.Status404NotFound)]
  199. [ProducesImageFile]
  200. [AllowAnonymous]
  201. public ActionResult GetPluginImage([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  202. {
  203. var plugin = _pluginManager.GetPlugin(pluginId, version);
  204. if (plugin is null)
  205. {
  206. return NotFound();
  207. }
  208. var imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath ?? string.Empty);
  209. if (plugin.Manifest.ImagePath is null || !System.IO.File.Exists(imagePath))
  210. {
  211. return NotFound();
  212. }
  213. Response.Headers.ContentDisposition = "attachment";
  214. imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath);
  215. return PhysicalFile(imagePath, MimeTypes.GetMimeType(imagePath));
  216. }
  217. /// <summary>
  218. /// Gets a plugin's manifest.
  219. /// </summary>
  220. /// <param name="pluginId">Plugin id.</param>
  221. /// <response code="204">Plugin manifest returned.</response>
  222. /// <response code="404">Plugin not found.</response>
  223. /// <returns>A <see cref="PluginManifest"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  224. [HttpPost("{pluginId}/Manifest")]
  225. [ProducesResponseType(StatusCodes.Status204NoContent)]
  226. [ProducesResponseType(StatusCodes.Status404NotFound)]
  227. public ActionResult<PluginManifest> GetPluginManifest([FromRoute, Required] Guid pluginId)
  228. {
  229. var plugin = _pluginManager.GetPlugin(pluginId);
  230. if (plugin is not null)
  231. {
  232. return plugin.Manifest;
  233. }
  234. return NotFound();
  235. }
  236. }