2
0

PluginsController.cs 14 KB

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