PluginsController.cs 14 KB

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