PluginsController.cs 14 KB

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