| | | 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.Api; |
| | | 8 | | using MediaBrowser.Common.Updates; |
| | | 9 | | using MediaBrowser.Controller.Configuration; |
| | | 10 | | using MediaBrowser.Model.Updates; |
| | | 11 | | using Microsoft.AspNetCore.Authorization; |
| | | 12 | | using Microsoft.AspNetCore.Http; |
| | | 13 | | using Microsoft.AspNetCore.Mvc; |
| | | 14 | | |
| | | 15 | | namespace Jellyfin.Api.Controllers; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Package Controller. |
| | | 19 | | /// </summary> |
| | | 20 | | [Route("")] |
| | | 21 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 22 | | public class PackageController : BaseJellyfinApiController |
| | | 23 | | { |
| | | 24 | | private readonly IInstallationManager _installationManager; |
| | | 25 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="PackageController"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param> |
| | | 31 | | /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</p |
| | 0 | 32 | | public PackageController(IInstallationManager installationManager, IServerConfigurationManager serverConfigurationMa |
| | | 33 | | { |
| | 0 | 34 | | _installationManager = installationManager; |
| | 0 | 35 | | _serverConfigurationManager = serverConfigurationManager; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets a package by name or assembly GUID. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="name">The name of the package.</param> |
| | | 42 | | /// <param name="assemblyGuid">The GUID of the associated assembly.</param> |
| | | 43 | | /// <response code="200">Package retrieved.</response> |
| | | 44 | | /// <returns>A <see cref="PackageInfo"/> containing package information.</returns> |
| | | 45 | | [HttpGet("Packages/{name}")] |
| | | 46 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 47 | | public async Task<ActionResult<PackageInfo>> GetPackageInfo( |
| | | 48 | | [FromRoute, Required] string name, |
| | | 49 | | [FromQuery] Guid? assemblyGuid) |
| | | 50 | | { |
| | | 51 | | var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false); |
| | | 52 | | var result = _installationManager.FilterPackages( |
| | | 53 | | packages, |
| | | 54 | | name, |
| | | 55 | | assemblyGuid ?? default) |
| | | 56 | | .FirstOrDefault(); |
| | | 57 | | |
| | | 58 | | if (result is null) |
| | | 59 | | { |
| | | 60 | | return NotFound(); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | return result; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets available packages. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <response code="200">Available packages returned.</response> |
| | | 70 | | /// <returns>An <see cref="PackageInfo"/> containing available packages information.</returns> |
| | | 71 | | [HttpGet("Packages")] |
| | | 72 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 73 | | public async Task<IEnumerable<PackageInfo>> GetPackages() |
| | | 74 | | { |
| | | 75 | | IEnumerable<PackageInfo> packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false); |
| | | 76 | | |
| | | 77 | | return packages; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Installs a package. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="name">Package name.</param> |
| | | 84 | | /// <param name="assemblyGuid">GUID of the associated assembly.</param> |
| | | 85 | | /// <param name="version">Optional version. Defaults to latest version.</param> |
| | | 86 | | /// <param name="repositoryUrl">Optional. Specify the repository to install from.</param> |
| | | 87 | | /// <response code="204">Package found.</response> |
| | | 88 | | /// <response code="404">Package not found.</response> |
| | | 89 | | /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the package could not |
| | | 90 | | [HttpPost("Packages/Installed/{name}")] |
| | | 91 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 92 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 93 | | public async Task<ActionResult> InstallPackage( |
| | | 94 | | [FromRoute, Required] string name, |
| | | 95 | | [FromQuery] Guid? assemblyGuid, |
| | | 96 | | [FromQuery] string? version, |
| | | 97 | | [FromQuery] string? repositoryUrl) |
| | | 98 | | { |
| | | 99 | | var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false); |
| | | 100 | | if (!string.IsNullOrEmpty(repositoryUrl)) |
| | | 101 | | { |
| | | 102 | | packages = packages.Where(p => p.Versions.Any(q => q.RepositoryUrl.Equals(repositoryUrl, StringComparison.Or |
| | | 103 | | .ToList(); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | var package = _installationManager.GetCompatibleVersions( |
| | | 107 | | packages, |
| | | 108 | | name, |
| | | 109 | | assemblyGuid ?? Guid.Empty, |
| | | 110 | | specificVersion: string.IsNullOrEmpty(version) ? null : Version.Parse(version)) |
| | | 111 | | .FirstOrDefault(); |
| | | 112 | | |
| | | 113 | | if (package is null) |
| | | 114 | | { |
| | | 115 | | return NotFound(); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | await _installationManager.InstallPackage(package).ConfigureAwait(false); |
| | | 119 | | |
| | | 120 | | return NoContent(); |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Cancels a package installation. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <param name="packageId">Installation Id.</param> |
| | | 127 | | /// <response code="204">Installation cancelled.</response> |
| | | 128 | | /// <returns>A <see cref="NoContentResult"/> on successfully cancelling a package installation.</returns> |
| | | 129 | | [HttpDelete("Packages/Installing/{packageId}")] |
| | | 130 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 131 | | public ActionResult CancelPackageInstallation( |
| | | 132 | | [FromRoute, Required] Guid packageId) |
| | | 133 | | { |
| | 0 | 134 | | _installationManager.CancelInstallation(packageId); |
| | 0 | 135 | | return NoContent(); |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets all package repositories. |
| | | 140 | | /// </summary> |
| | | 141 | | /// <response code="200">Package repositories returned.</response> |
| | | 142 | | /// <returns>An <see cref="OkResult"/> containing the list of package repositories.</returns> |
| | | 143 | | [HttpGet("Repositories")] |
| | | 144 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 145 | | public ActionResult<IEnumerable<RepositoryInfo>> GetRepositories() |
| | | 146 | | { |
| | 0 | 147 | | return Ok(_serverConfigurationManager.Configuration.PluginRepositories.AsEnumerable()); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Sets the enabled and existing package repositories. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name="repositoryInfos">The list of package repositories.</param> |
| | | 154 | | /// <response code="204">Package repositories saved.</response> |
| | | 155 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 156 | | [HttpPost("Repositories")] |
| | | 157 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 158 | | public ActionResult SetRepositories([FromBody, Required] RepositoryInfo[] repositoryInfos) |
| | | 159 | | { |
| | 0 | 160 | | _serverConfigurationManager.Configuration.PluginRepositories = repositoryInfos; |
| | 0 | 161 | | _serverConfigurationManager.SaveConfiguration(); |
| | 0 | 162 | | return NoContent(); |
| | | 163 | | } |
| | | 164 | | } |