PluginsController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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.Api.Models.PluginDtos;
  11. using Jellyfin.Extensions.Json;
  12. using MediaBrowser.Common.Configuration;
  13. using MediaBrowser.Common.Plugins;
  14. using MediaBrowser.Common.Updates;
  15. using MediaBrowser.Model.Net;
  16. using MediaBrowser.Model.Plugins;
  17. using Microsoft.AspNetCore.Authorization;
  18. using Microsoft.AspNetCore.Http;
  19. using Microsoft.AspNetCore.Mvc;
  20. namespace Jellyfin.Api.Controllers
  21. {
  22. /// <summary>
  23. /// Plugins controller.
  24. /// </summary>
  25. [Authorize(Policy = Policies.DefaultAuthorization)]
  26. public class PluginsController : BaseJellyfinApiController
  27. {
  28. private readonly IInstallationManager _installationManager;
  29. private readonly IPluginManager _pluginManager;
  30. private readonly IConfigurationManager _config;
  31. private readonly JsonSerializerOptions _serializerOptions;
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="PluginsController"/> class.
  34. /// </summary>
  35. /// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
  36. /// <param name="pluginManager">Instance of the <see cref="IPluginManager"/> interface.</param>
  37. /// <param name="config">Instance of the <see cref="IConfigurationManager"/> interface.</param>
  38. public PluginsController(
  39. IInstallationManager installationManager,
  40. IPluginManager pluginManager,
  41. IConfigurationManager config)
  42. {
  43. _installationManager = installationManager;
  44. _pluginManager = pluginManager;
  45. _serializerOptions = JsonDefaults.Options;
  46. _config = config;
  47. }
  48. /// <summary>
  49. /// Get plugin security info.
  50. /// </summary>
  51. /// <response code="200">Plugin security info returned.</response>
  52. /// <returns>Plugin security info.</returns>
  53. [Obsolete("This endpoint should not be used.")]
  54. [HttpGet("SecurityInfo")]
  55. [ProducesResponseType(StatusCodes.Status200OK)]
  56. public static ActionResult<PluginSecurityInfo> GetPluginSecurityInfo()
  57. {
  58. return new PluginSecurityInfo
  59. {
  60. IsMbSupporter = true,
  61. SupporterKey = "IAmTotallyLegit"
  62. };
  63. }
  64. /// <summary>
  65. /// Gets registration status for a feature.
  66. /// </summary>
  67. /// <param name="name">Feature name.</param>
  68. /// <response code="200">Registration status returned.</response>
  69. /// <returns>Mb registration record.</returns>
  70. [Obsolete("This endpoint should not be used.")]
  71. [HttpPost("RegistrationRecords/{name}")]
  72. [ProducesResponseType(StatusCodes.Status200OK)]
  73. public static ActionResult<MBRegistrationRecord> GetRegistrationStatus([FromRoute, Required] string name)
  74. {
  75. return new MBRegistrationRecord
  76. {
  77. IsRegistered = true,
  78. RegChecked = true,
  79. TrialVersion = false,
  80. IsValid = true,
  81. RegError = false
  82. };
  83. }
  84. /// <summary>
  85. /// Gets registration status for a feature.
  86. /// </summary>
  87. /// <param name="name">Feature name.</param>
  88. /// <response code="501">Not implemented.</response>
  89. /// <returns>Not Implemented.</returns>
  90. /// <exception cref="NotImplementedException">This endpoint is not implemented.</exception>
  91. [Obsolete("Paid plugins are not supported")]
  92. [HttpGet("Registrations/{name}")]
  93. [ProducesResponseType(StatusCodes.Status501NotImplemented)]
  94. public static ActionResult GetRegistration([FromRoute, Required] string name)
  95. {
  96. // TODO Once we have proper apps and plugins and decide to break compatibility with paid plugins,
  97. // delete all these registration endpoints. They are only kept for compatibility.
  98. throw new NotImplementedException();
  99. }
  100. /// <summary>
  101. /// Gets a list of currently installed plugins.
  102. /// </summary>
  103. /// <response code="200">Installed plugins returned.</response>
  104. /// <returns>List of currently installed plugins.</returns>
  105. [HttpGet]
  106. [ProducesResponseType(StatusCodes.Status200OK)]
  107. public ActionResult<IEnumerable<PluginInfo>> GetPlugins()
  108. {
  109. return Ok(_pluginManager.Plugins
  110. .OrderBy(p => p.Name)
  111. .Select(p => p.GetPluginInfo()));
  112. }
  113. /// <summary>
  114. /// Enables a disabled plugin.
  115. /// </summary>
  116. /// <param name="pluginId">Plugin id.</param>
  117. /// <param name="version">Plugin version.</param>
  118. /// <response code="204">Plugin enabled.</response>
  119. /// <response code="404">Plugin not found.</response>
  120. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  121. [HttpPost("{pluginId}/{version}/Enable")]
  122. [Authorize(Policy = Policies.RequiresElevation)]
  123. [ProducesResponseType(StatusCodes.Status204NoContent)]
  124. [ProducesResponseType(StatusCodes.Status404NotFound)]
  125. public ActionResult EnablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  126. {
  127. var plugin = _pluginManager.GetPlugin(pluginId, version);
  128. if (plugin == null)
  129. {
  130. return NotFound();
  131. }
  132. _pluginManager.EnablePlugin(plugin);
  133. return NoContent();
  134. }
  135. /// <summary>
  136. /// Disable a plugin.
  137. /// </summary>
  138. /// <param name="pluginId">Plugin id.</param>
  139. /// <param name="version">Plugin version.</param>
  140. /// <response code="204">Plugin disabled.</response>
  141. /// <response code="404">Plugin not found.</response>
  142. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  143. [HttpPost("{pluginId}/{version}/Disable")]
  144. [Authorize(Policy = Policies.RequiresElevation)]
  145. [ProducesResponseType(StatusCodes.Status204NoContent)]
  146. [ProducesResponseType(StatusCodes.Status404NotFound)]
  147. public ActionResult DisablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  148. {
  149. var plugin = _pluginManager.GetPlugin(pluginId, version);
  150. if (plugin == null)
  151. {
  152. return NotFound();
  153. }
  154. _pluginManager.DisablePlugin(plugin);
  155. return NoContent();
  156. }
  157. /// <summary>
  158. /// Uninstalls a plugin by version.
  159. /// </summary>
  160. /// <param name="pluginId">Plugin id.</param>
  161. /// <param name="version">Plugin version.</param>
  162. /// <response code="204">Plugin uninstalled.</response>
  163. /// <response code="404">Plugin not found.</response>
  164. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  165. [HttpDelete("{pluginId}/{version}")]
  166. [Authorize(Policy = Policies.RequiresElevation)]
  167. [ProducesResponseType(StatusCodes.Status204NoContent)]
  168. [ProducesResponseType(StatusCodes.Status404NotFound)]
  169. public ActionResult UninstallPluginByVersion([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  170. {
  171. var plugin = _pluginManager.GetPlugin(pluginId, version);
  172. if (plugin == null)
  173. {
  174. return NotFound();
  175. }
  176. _installationManager.UninstallPlugin(plugin);
  177. return NoContent();
  178. }
  179. /// <summary>
  180. /// Uninstalls a plugin.
  181. /// </summary>
  182. /// <param name="pluginId">Plugin id.</param>
  183. /// <response code="204">Plugin uninstalled.</response>
  184. /// <response code="404">Plugin not found.</response>
  185. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  186. [HttpDelete("{pluginId}")]
  187. [Authorize(Policy = Policies.RequiresElevation)]
  188. [ProducesResponseType(StatusCodes.Status204NoContent)]
  189. [ProducesResponseType(StatusCodes.Status404NotFound)]
  190. [Obsolete("Please use the UninstallPluginByVersion API.")]
  191. public ActionResult UninstallPlugin([FromRoute, Required] Guid pluginId)
  192. {
  193. // If no version is given, return the current instance.
  194. var plugins = _pluginManager.Plugins.Where(p => p.Id.Equals(pluginId));
  195. // Select the un-instanced one first.
  196. var plugin = plugins.FirstOrDefault(p => p.Instance == null) ?? plugins.OrderBy(p => p.Manifest.Status).FirstOrDefault();
  197. if (plugin != null)
  198. {
  199. _installationManager.UninstallPlugin(plugin);
  200. return NoContent();
  201. }
  202. return NotFound();
  203. }
  204. /// <summary>
  205. /// Gets plugin configuration.
  206. /// </summary>
  207. /// <param name="pluginId">Plugin id.</param>
  208. /// <response code="200">Plugin configuration returned.</response>
  209. /// <response code="404">Plugin not found or plugin configuration not found.</response>
  210. /// <returns>Plugin configuration.</returns>
  211. [HttpGet("{pluginId}/Configuration")]
  212. [ProducesResponseType(StatusCodes.Status200OK)]
  213. [ProducesResponseType(StatusCodes.Status404NotFound)]
  214. public ActionResult<BasePluginConfiguration> GetPluginConfiguration([FromRoute, Required] Guid pluginId)
  215. {
  216. var plugin = _pluginManager.GetPlugin(pluginId);
  217. if (plugin?.Instance is IHasPluginConfiguration configPlugin)
  218. {
  219. return configPlugin.Configuration;
  220. }
  221. return NotFound();
  222. }
  223. /// <summary>
  224. /// Updates plugin configuration.
  225. /// </summary>
  226. /// <remarks>
  227. /// Accepts plugin configuration as JSON body.
  228. /// </remarks>
  229. /// <param name="pluginId">Plugin id.</param>
  230. /// <response code="204">Plugin configuration updated.</response>
  231. /// <response code="404">Plugin not found or plugin does not have configuration.</response>
  232. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  233. [HttpPost("{pluginId}/Configuration")]
  234. [ProducesResponseType(StatusCodes.Status204NoContent)]
  235. [ProducesResponseType(StatusCodes.Status404NotFound)]
  236. public async Task<ActionResult> UpdatePluginConfiguration([FromRoute, Required] Guid pluginId)
  237. {
  238. var plugin = _pluginManager.GetPlugin(pluginId);
  239. if (plugin?.Instance is not IHasPluginConfiguration configPlugin)
  240. {
  241. return NotFound();
  242. }
  243. var configuration = (BasePluginConfiguration?)await JsonSerializer.DeserializeAsync(Request.Body, configPlugin.ConfigurationType, _serializerOptions)
  244. .ConfigureAwait(false);
  245. if (configuration != null)
  246. {
  247. configPlugin.UpdateConfiguration(configuration);
  248. }
  249. return NoContent();
  250. }
  251. /// <summary>
  252. /// Gets a plugin's image.
  253. /// </summary>
  254. /// <param name="pluginId">Plugin id.</param>
  255. /// <param name="version">Plugin version.</param>
  256. /// <response code="200">Plugin image returned.</response>
  257. /// <returns>Plugin's image.</returns>
  258. [HttpGet("{pluginId}/{version}/Image")]
  259. [ProducesResponseType(StatusCodes.Status200OK)]
  260. [ProducesResponseType(StatusCodes.Status404NotFound)]
  261. [ProducesImageFile]
  262. [AllowAnonymous]
  263. public ActionResult GetPluginImage([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  264. {
  265. var plugin = _pluginManager.GetPlugin(pluginId, version);
  266. if (plugin == null)
  267. {
  268. return NotFound();
  269. }
  270. var imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath ?? string.Empty);
  271. if (plugin.Manifest.ImagePath == null || !System.IO.File.Exists(imagePath))
  272. {
  273. return NotFound();
  274. }
  275. imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath);
  276. return PhysicalFile(imagePath, MimeTypes.GetMimeType(imagePath));
  277. }
  278. /// <summary>
  279. /// Gets a plugin's manifest.
  280. /// </summary>
  281. /// <param name="pluginId">Plugin id.</param>
  282. /// <response code="204">Plugin manifest returned.</response>
  283. /// <response code="404">Plugin not found.</response>
  284. /// <returns>A <see cref="PluginManifest"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  285. [HttpPost("{pluginId}/Manifest")]
  286. [ProducesResponseType(StatusCodes.Status204NoContent)]
  287. [ProducesResponseType(StatusCodes.Status404NotFound)]
  288. public ActionResult<PluginManifest> GetPluginManifest([FromRoute, Required] Guid pluginId)
  289. {
  290. var plugin = _pluginManager.GetPlugin(pluginId);
  291. if (plugin != null)
  292. {
  293. return plugin.Manifest;
  294. }
  295. return NotFound();
  296. }
  297. /// <summary>
  298. /// Updates plugin security info.
  299. /// </summary>
  300. /// <param name="pluginSecurityInfo">Plugin security info.</param>
  301. /// <response code="204">Plugin security info updated.</response>
  302. /// <returns>An <see cref="NoContentResult"/>.</returns>
  303. [Obsolete("This endpoint should not be used.")]
  304. [HttpPost("SecurityInfo")]
  305. [Authorize(Policy = Policies.RequiresElevation)]
  306. [ProducesResponseType(StatusCodes.Status204NoContent)]
  307. public ActionResult UpdatePluginSecurityInfo([FromBody, Required] PluginSecurityInfo pluginSecurityInfo)
  308. {
  309. return NoContent();
  310. }
  311. }
  312. }