PluginsController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.GetCamelCaseOptions();
  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. if (!_pluginManager.TryGetPlugin(pluginId, version, out var plugin))
  131. {
  132. return NotFound();
  133. }
  134. _pluginManager.EnablePlugin(plugin!);
  135. return NoContent();
  136. }
  137. /// <summary>
  138. /// Disable a plugin.
  139. /// </summary>
  140. /// <param name="pluginId">Plugin id.</param>
  141. /// <param name="version">Plugin version.</param>
  142. /// <response code="204">Plugin disabled.</response>
  143. /// <response code="404">Plugin not found.</response>
  144. /// <returns>An <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the plugin could not be found.</returns>
  145. [HttpPost("{pluginId}/{version}/Disable")]
  146. [Authorize(Policy = Policies.RequiresElevation)]
  147. [ProducesResponseType(StatusCodes.Status204NoContent)]
  148. [ProducesResponseType(StatusCodes.Status404NotFound)]
  149. public ActionResult DisablePlugin([FromRoute, Required] Guid pluginId, [FromRoute, Required] Version version)
  150. {
  151. if (!_pluginManager.TryGetPlugin(pluginId, version, out var plugin))
  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. if (!_pluginManager.TryGetPlugin(pluginId, version, out var plugin))
  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 file 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. _installationManager.UninstallPlugin(plugin!);
  203. return NoContent();
  204. }
  205. /// <summary>
  206. /// Gets plugin configuration.
  207. /// </summary>
  208. /// <param name="pluginId">Plugin id.</param>
  209. /// <response code="200">Plugin configuration returned.</response>
  210. /// <response code="404">Plugin not found or plugin configuration not found.</response>
  211. /// <returns>Plugin configuration.</returns>
  212. [HttpGet("{pluginId}/Configuration")]
  213. [ProducesResponseType(StatusCodes.Status200OK)]
  214. [ProducesResponseType(StatusCodes.Status404NotFound)]
  215. [ProducesFile(MediaTypeNames.Application.Json)]
  216. public ActionResult<BasePluginConfiguration> GetPluginConfiguration([FromRoute, Required] Guid pluginId)
  217. {
  218. if (_pluginManager.TryGetPlugin(pluginId, null, out var plugin)
  219. && plugin!.Instance is IHasPluginConfiguration configPlugin)
  220. {
  221. return configPlugin.Configuration;
  222. }
  223. return NotFound();
  224. }
  225. /// <summary>
  226. /// Updates plugin configuration.
  227. /// </summary>
  228. /// <remarks>
  229. /// Accepts plugin configuration as JSON body.
  230. /// </remarks>
  231. /// <param name="pluginId">Plugin id.</param>
  232. /// <response code="204">Plugin configuration updated.</response>
  233. /// <response code="404">Plugin not found or plugin does not have configuration.</response>
  234. /// <returns>
  235. /// A <see cref="Task" /> that represents the asynchronous operation to update plugin configuration.
  236. /// The task result contains an <see cref="NoContentResult"/> indicating success, or <see cref="NotFoundResult"/>
  237. /// when plugin not found or plugin doesn't have configuration.
  238. /// </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. if (!_pluginManager.TryGetPlugin(pluginId, null, out var plugin)
  245. || 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. if (!_pluginManager.TryGetPlugin(pluginId, version, out var plugin))
  272. {
  273. return NotFound();
  274. }
  275. var imgPath = Path.Combine(plugin!.Path, plugin!.Manifest.ImageUrl ?? string.Empty);
  276. if (((ServerConfiguration)_config.CommonConfiguration).DisablePluginImages
  277. || plugin!.Manifest.ImageUrl == null
  278. || !System.IO.File.Exists(imgPath))
  279. {
  280. // Use a blank image.
  281. var type = GetType();
  282. var stream = type.Assembly.GetManifestResourceStream(type.Namespace + ".Plugins.blank.png");
  283. return File(stream, "image/png");
  284. }
  285. imgPath = Path.Combine(plugin.Path, plugin.Manifest.ImageUrl);
  286. return PhysicalFile(imgPath, MimeTypes.GetMimeType(imgPath));
  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>
  295. /// A <see cref="Task" /> that represents the asynchronous operation to get the plugin's manifest.
  296. /// The task result contains an <see cref="NoContentResult"/> indicating success, or <see cref="NotFoundResult"/>
  297. /// when plugin not found.
  298. /// </returns>
  299. [HttpPost("{pluginId}/Manifest")]
  300. [ProducesResponseType(StatusCodes.Status204NoContent)]
  301. [ProducesResponseType(StatusCodes.Status404NotFound)]
  302. [ProducesFile(MediaTypeNames.Application.Json)]
  303. public ActionResult<PluginManifest> GetPluginManifest([FromRoute, Required] Guid pluginId)
  304. {
  305. if (_pluginManager.TryGetPlugin(pluginId, null, out var plugin))
  306. {
  307. return Ok(plugin!.Manifest);
  308. }
  309. return NotFound();
  310. }
  311. /// <summary>
  312. /// Updates plugin security info.
  313. /// </summary>
  314. /// <param name="pluginSecurityInfo">Plugin security info.</param>
  315. /// <response code="204">Plugin security info updated.</response>
  316. /// <returns>An <see cref="NoContentResult"/>.</returns>
  317. [Obsolete("This endpoint should not be used.")]
  318. [HttpPost("SecurityInfo")]
  319. [Authorize(Policy = Policies.RequiresElevation)]
  320. [ProducesResponseType(StatusCodes.Status204NoContent)]
  321. public ActionResult UpdatePluginSecurityInfo([FromBody, Required] PluginSecurityInfo pluginSecurityInfo)
  322. {
  323. return NoContent();
  324. }
  325. }
  326. }