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