PluginsController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 MediaBrowser.Common.Configuration;
  12. using MediaBrowser.Common.Json;
  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);
  197. if (plugin == null)
  198. {
  199. // Then by the status.
  200. plugin = plugins.OrderBy(p => p.Manifest.Status).FirstOrDefault();
  201. }
  202. if (plugin != null)
  203. {
  204. _installationManager.UninstallPlugin(plugin);
  205. return NoContent();
  206. }
  207. return NotFound();
  208. }
  209. /// <summary>
  210. /// Gets plugin configuration.
  211. /// </summary>
  212. /// <param name="pluginId">Plugin id.</param>
  213. /// <response code="200">Plugin configuration returned.</response>
  214. /// <response code="404">Plugin not found or plugin configuration not found.</response>
  215. /// <returns>Plugin configuration.</returns>
  216. [HttpGet("{pluginId}/Configuration")]
  217. [ProducesResponseType(StatusCodes.Status200OK)]
  218. [ProducesResponseType(StatusCodes.Status404NotFound)]
  219. public ActionResult<BasePluginConfiguration> GetPluginConfiguration([FromRoute, Required] Guid pluginId)
  220. {
  221. var plugin = _pluginManager.GetPlugin(pluginId);
  222. if (plugin?.Instance is IHasPluginConfiguration configPlugin)
  223. {
  224. return configPlugin.Configuration;
  225. }
  226. return NotFound();
  227. }
  228. /// <summary>
  229. /// Updates plugin configuration.
  230. /// </summary>
  231. /// <remarks>
  232. /// Accepts plugin configuration as JSON body.
  233. /// </remarks>
  234. /// <param name="pluginId">Plugin id.</param>
  235. /// <response code="204">Plugin configuration updated.</response>
  236. /// <response code="404">Plugin not found or plugin does not have configuration.</response>
  237. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  238. [HttpPost("{pluginId}/Configuration")]
  239. [ProducesResponseType(StatusCodes.Status204NoContent)]
  240. [ProducesResponseType(StatusCodes.Status404NotFound)]
  241. public async Task<ActionResult> UpdatePluginConfiguration([FromRoute, Required] Guid pluginId)
  242. {
  243. var plugin = _pluginManager.GetPlugin(pluginId);
  244. if (plugin?.Instance is not IHasPluginConfiguration configPlugin)
  245. {
  246. return NotFound();
  247. }
  248. var configuration = (BasePluginConfiguration?)await JsonSerializer.DeserializeAsync(Request.Body, configPlugin.ConfigurationType, _serializerOptions)
  249. .ConfigureAwait(false);
  250. if (configuration != null)
  251. {
  252. configPlugin.UpdateConfiguration(configuration);
  253. }
  254. return NoContent();
  255. }
  256. /// <summary>
  257. /// Gets a plugin's image.
  258. /// </summary>
  259. /// <param name="pluginId">Plugin id.</param>
  260. /// <param name="version">Plugin version.</param>
  261. /// <response code="200">Plugin image returned.</response>
  262. /// <returns>Plugin's image.</returns>
  263. [HttpGet("{pluginId}/{version}/Image")]
  264. [ProducesResponseType(StatusCodes.Status200OK)]
  265. [ProducesResponseType(StatusCodes.Status404NotFound)]
  266. [ProducesImageFile]
  267. [AllowAnonymous]
  268. public ActionResult GetPluginImage([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  269. {
  270. var plugin = _pluginManager.GetPlugin(pluginId, version);
  271. if (plugin == null)
  272. {
  273. return NotFound();
  274. }
  275. var imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath ?? string.Empty);
  276. if (plugin.Manifest.ImagePath == null || !System.IO.File.Exists(imagePath))
  277. {
  278. return NotFound();
  279. }
  280. imagePath = Path.Combine(plugin.Path, plugin.Manifest.ImagePath);
  281. return PhysicalFile(imagePath, MimeTypes.GetMimeType(imagePath));
  282. }
  283. /// <summary>
  284. /// Gets a plugin's manifest.
  285. /// </summary>
  286. /// <param name="pluginId">Plugin id.</param>
  287. /// <response code="204">Plugin manifest returned.</response>
  288. /// <response code="404">Plugin not found.</response>
  289. /// <returns>A <see cref="PluginManifest"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  290. [HttpPost("{pluginId}/Manifest")]
  291. [ProducesResponseType(StatusCodes.Status204NoContent)]
  292. [ProducesResponseType(StatusCodes.Status404NotFound)]
  293. public ActionResult<PluginManifest> GetPluginManifest([FromRoute, Required] Guid pluginId)
  294. {
  295. var plugin = _pluginManager.GetPlugin(pluginId);
  296. if (plugin != null)
  297. {
  298. return plugin.Manifest;
  299. }
  300. return NotFound();
  301. }
  302. /// <summary>
  303. /// Updates plugin security info.
  304. /// </summary>
  305. /// <param name="pluginSecurityInfo">Plugin security info.</param>
  306. /// <response code="204">Plugin security info updated.</response>
  307. /// <returns>An <see cref="NoContentResult"/>.</returns>
  308. [Obsolete("This endpoint should not be used.")]
  309. [HttpPost("SecurityInfo")]
  310. [Authorize(Policy = Policies.RequiresElevation)]
  311. [ProducesResponseType(StatusCodes.Status204NoContent)]
  312. public ActionResult UpdatePluginSecurityInfo([FromBody, Required] PluginSecurityInfo pluginSecurityInfo)
  313. {
  314. return NoContent();
  315. }
  316. }
  317. }