PackageController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Api.Constants;
  7. using MediaBrowser.Common.Updates;
  8. using MediaBrowser.Model.Updates;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. namespace Jellyfin.Api.Controllers
  13. {
  14. /// <summary>
  15. /// Package Controller.
  16. /// </summary>
  17. [Route("Packages")]
  18. [Authorize(Policy = Policies.DefaultAuthorization)]
  19. public class PackageController : BaseJellyfinApiController
  20. {
  21. private readonly IInstallationManager _installationManager;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="PackageController"/> class.
  24. /// </summary>
  25. /// <param name="installationManager">Instance of <see cref="IInstallationManager"/>Installation Manager.</param>
  26. public PackageController(IInstallationManager installationManager)
  27. {
  28. _installationManager = installationManager;
  29. }
  30. /// <summary>
  31. /// Gets a package by name or assembly GUID.
  32. /// </summary>
  33. /// <param name="name">The name of the package.</param>
  34. /// <param name="assemblyGuid">The GUID of the associated assembly.</param>
  35. /// <response code="200">Package retrieved.</response>
  36. /// <returns>A <see cref="PackageInfo"/> containing package information.</returns>
  37. [HttpGet("/{name}")]
  38. [ProducesResponseType(StatusCodes.Status200OK)]
  39. public async Task<ActionResult<PackageInfo>> GetPackageInfo(
  40. [FromRoute] [Required] string name,
  41. [FromQuery] string? assemblyGuid)
  42. {
  43. var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
  44. var result = _installationManager.FilterPackages(
  45. packages,
  46. name,
  47. string.IsNullOrEmpty(assemblyGuid) ? default : Guid.Parse(assemblyGuid)).FirstOrDefault();
  48. return result;
  49. }
  50. /// <summary>
  51. /// Gets available packages.
  52. /// </summary>
  53. /// <response code="200">Available packages returned.</response>
  54. /// <returns>An <see cref="PackageInfo"/> containing available packages information.</returns>
  55. [HttpGet]
  56. [ProducesResponseType(StatusCodes.Status200OK)]
  57. public async Task<IEnumerable<PackageInfo>> GetPackages()
  58. {
  59. IEnumerable<PackageInfo> packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
  60. return packages;
  61. }
  62. /// <summary>
  63. /// Installs a package.
  64. /// </summary>
  65. /// <param name="name">Package name.</param>
  66. /// <param name="assemblyGuid">GUID of the associated assembly.</param>
  67. /// <param name="version">Optional version. Defaults to latest version.</param>
  68. /// <response code="204">Package found.</response>
  69. /// <response code="404">Package not found.</response>
  70. /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the package could not be found.</returns>
  71. [HttpPost("/Installed/{name}")]
  72. [ProducesResponseType(StatusCodes.Status204NoContent)]
  73. [ProducesResponseType(StatusCodes.Status404NotFound)]
  74. [Authorize(Policy = Policies.RequiresElevation)]
  75. public async Task<ActionResult> InstallPackage(
  76. [FromRoute] [Required] string name,
  77. [FromQuery] string assemblyGuid,
  78. [FromQuery] string version)
  79. {
  80. var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
  81. var package = _installationManager.GetCompatibleVersions(
  82. packages,
  83. name,
  84. string.IsNullOrEmpty(assemblyGuid) ? Guid.Empty : Guid.Parse(assemblyGuid),
  85. string.IsNullOrEmpty(version) ? null : Version.Parse(version)).FirstOrDefault();
  86. if (package == null)
  87. {
  88. return NotFound();
  89. }
  90. await _installationManager.InstallPackage(package).ConfigureAwait(false);
  91. return NoContent();
  92. }
  93. /// <summary>
  94. /// Cancels a package installation.
  95. /// </summary>
  96. /// <param name="packageId">Installation Id.</param>
  97. /// <response code="204">Installation cancelled.</response>
  98. /// <returns>A <see cref="NoContentResult"/> on successfully cancelling a package installation.</returns>
  99. [HttpDelete("/Installing/{packageId}")]
  100. [Authorize(Policy = Policies.RequiresElevation)]
  101. [ProducesResponseType(StatusCodes.Status204NoContent)]
  102. public IActionResult CancelPackageInstallation(
  103. [FromRoute] [Required] Guid packageId)
  104. {
  105. _installationManager.CancelInstallation(packageId);
  106. return NoContent();
  107. }
  108. }
  109. }