PluginsController.cs 14 KB

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