2020-04-25 18:59:31 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Jellyfin.Api.Constants;
|
|
|
|
using MediaBrowser.Common.Updates;
|
2020-07-20 20:43:54 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2020-04-25 18:59:31 +00:00
|
|
|
using MediaBrowser.Model.Updates;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Package Controller.
|
|
|
|
/// </summary>
|
2020-08-04 18:48:53 +00:00
|
|
|
[Route("")]
|
2020-06-16 20:15:58 +00:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-04-25 18:59:31 +00:00
|
|
|
public class PackageController : BaseJellyfinApiController
|
|
|
|
{
|
|
|
|
private readonly IInstallationManager _installationManager;
|
2020-07-20 20:43:54 +00:00
|
|
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
2020-04-25 18:59:31 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="PackageController"/> class.
|
|
|
|
/// </summary>
|
2020-07-20 20:43:54 +00:00
|
|
|
/// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
|
|
|
|
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
|
|
|
public PackageController(IInstallationManager installationManager, IServerConfigurationManager serverConfigurationManager)
|
2020-04-25 18:59:31 +00:00
|
|
|
{
|
|
|
|
_installationManager = installationManager;
|
2020-07-20 20:43:54 +00:00
|
|
|
_serverConfigurationManager = serverConfigurationManager;
|
2020-04-25 18:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-05-19 17:13:42 +00:00
|
|
|
/// Gets a package by name or assembly GUID.
|
2020-04-25 18:59:31 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name of the package.</param>
|
2020-05-19 17:13:42 +00:00
|
|
|
/// <param name="assemblyGuid">The GUID of the associated assembly.</param>
|
2020-06-21 00:02:07 +00:00
|
|
|
/// <response code="200">Package retrieved.</response>
|
2020-05-01 14:17:40 +00:00
|
|
|
/// <returns>A <see cref="PackageInfo"/> containing package information.</returns>
|
2020-08-04 18:48:53 +00:00
|
|
|
[HttpGet("Packages/{name}")]
|
2020-06-21 00:02:07 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-04-25 20:41:56 +00:00
|
|
|
public async Task<ActionResult<PackageInfo>> GetPackageInfo(
|
2020-09-08 00:45:06 +00:00
|
|
|
[FromRoute, Required] string name,
|
2020-12-01 18:07:41 +00:00
|
|
|
[FromQuery] Guid? assemblyGuid)
|
2020-04-25 18:59:31 +00:00
|
|
|
{
|
2020-04-25 20:41:56 +00:00
|
|
|
var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
|
2020-04-25 18:59:31 +00:00
|
|
|
var result = _installationManager.FilterPackages(
|
2020-08-14 14:54:21 +00:00
|
|
|
packages,
|
|
|
|
name,
|
2020-12-01 18:07:41 +00:00
|
|
|
assemblyGuid ?? default)
|
2020-08-14 14:54:21 +00:00
|
|
|
.FirstOrDefault();
|
2020-04-25 18:59:31 +00:00
|
|
|
|
2020-11-13 16:04:31 +00:00
|
|
|
if (result == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
2020-04-25 20:32:49 +00:00
|
|
|
return result;
|
2020-04-25 18:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets available packages.
|
|
|
|
/// </summary>
|
2020-06-21 00:02:07 +00:00
|
|
|
/// <response code="200">Available packages returned.</response>
|
2020-05-01 14:17:40 +00:00
|
|
|
/// <returns>An <see cref="PackageInfo"/> containing available packages information.</returns>
|
2020-08-04 18:48:53 +00:00
|
|
|
[HttpGet("Packages")]
|
2020-06-21 00:02:07 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-04-25 20:41:56 +00:00
|
|
|
public async Task<IEnumerable<PackageInfo>> GetPackages()
|
2020-04-25 18:59:31 +00:00
|
|
|
{
|
|
|
|
IEnumerable<PackageInfo> packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
|
|
|
|
|
2020-04-25 20:41:56 +00:00
|
|
|
return packages;
|
2020-04-25 18:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Installs a package.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">Package name.</param>
|
2020-05-19 17:13:42 +00:00
|
|
|
/// <param name="assemblyGuid">GUID of the associated assembly.</param>
|
2020-04-25 18:59:31 +00:00
|
|
|
/// <param name="version">Optional version. Defaults to latest version.</param>
|
2020-08-19 02:22:15 +00:00
|
|
|
/// <param name="repositoryUrl">Optional. Specify the repository to install from.</param>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <response code="204">Package found.</response>
|
2020-05-01 14:17:40 +00:00
|
|
|
/// <response code="404">Package not found.</response>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the package could not be found.</returns>
|
2020-08-04 18:48:53 +00:00
|
|
|
[HttpPost("Packages/Installed/{name}")]
|
2020-06-12 10:38:13 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-04-25 18:59:31 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2020-04-25 20:41:56 +00:00
|
|
|
[Authorize(Policy = Policies.RequiresElevation)]
|
2020-04-25 18:59:31 +00:00
|
|
|
public async Task<ActionResult> InstallPackage(
|
2020-09-08 00:45:06 +00:00
|
|
|
[FromRoute, Required] string name,
|
2020-12-01 18:07:41 +00:00
|
|
|
[FromQuery] Guid? assemblyGuid,
|
2020-08-19 02:22:15 +00:00
|
|
|
[FromQuery] string? version,
|
|
|
|
[FromQuery] string? repositoryUrl)
|
2020-04-25 18:59:31 +00:00
|
|
|
{
|
|
|
|
var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
|
2020-08-19 02:22:15 +00:00
|
|
|
if (!string.IsNullOrEmpty(repositoryUrl))
|
|
|
|
{
|
2020-12-06 23:48:54 +00:00
|
|
|
packages = packages.Where(p => p.Versions.Any(q => q.RepositoryUrl.Equals(repositoryUrl, StringComparison.OrdinalIgnoreCase)))
|
2020-08-19 02:22:15 +00:00
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:59:31 +00:00
|
|
|
var package = _installationManager.GetCompatibleVersions(
|
|
|
|
packages,
|
|
|
|
name,
|
2020-12-01 18:07:41 +00:00
|
|
|
assemblyGuid ?? Guid.Empty,
|
2020-08-14 14:54:21 +00:00
|
|
|
specificVersion: string.IsNullOrEmpty(version) ? null : Version.Parse(version))
|
|
|
|
.FirstOrDefault();
|
2020-04-25 18:59:31 +00:00
|
|
|
|
|
|
|
if (package == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
await _installationManager.InstallPackage(package).ConfigureAwait(false);
|
|
|
|
|
2020-06-12 10:38:13 +00:00
|
|
|
return NoContent();
|
2020-04-25 18:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Cancels a package installation.
|
|
|
|
/// </summary>
|
2020-06-21 00:02:07 +00:00
|
|
|
/// <param name="packageId">Installation Id.</param>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <response code="204">Installation cancelled.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/> on successfully cancelling a package installation.</returns>
|
2020-08-04 18:48:53 +00:00
|
|
|
[HttpDelete("Packages/Installing/{packageId}")]
|
2020-04-25 18:59:31 +00:00
|
|
|
[Authorize(Policy = Policies.RequiresElevation)]
|
2020-06-12 10:38:13 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-07-20 20:43:54 +00:00
|
|
|
public ActionResult CancelPackageInstallation(
|
2020-09-06 15:07:27 +00:00
|
|
|
[FromRoute, Required] Guid packageId)
|
2020-04-25 18:59:31 +00:00
|
|
|
{
|
2020-06-21 00:02:07 +00:00
|
|
|
_installationManager.CancelInstallation(packageId);
|
2020-06-12 10:38:13 +00:00
|
|
|
return NoContent();
|
2020-04-25 18:59:31 +00:00
|
|
|
}
|
2020-07-20 20:43:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets all package repositories.
|
|
|
|
/// </summary>
|
|
|
|
/// <response code="200">Package repositories returned.</response>
|
|
|
|
/// <returns>An <see cref="OkResult"/> containing the list of package repositories.</returns>
|
2020-08-04 18:48:53 +00:00
|
|
|
[HttpGet("Repositories")]
|
2020-07-20 20:43:54 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public ActionResult<IEnumerable<RepositoryInfo>> GetRepositories()
|
|
|
|
{
|
|
|
|
return _serverConfigurationManager.Configuration.PluginRepositories;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the enabled and existing package repositories.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="repositoryInfos">The list of package repositories.</param>
|
|
|
|
/// <response code="204">Package repositories saved.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/>.</returns>
|
2020-11-12 09:13:56 +00:00
|
|
|
[HttpPost("Repositories")]
|
2022-04-08 15:23:30 +00:00
|
|
|
[Authorize(Policy = Policies.RequiresElevation)]
|
2020-07-20 20:43:54 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2021-01-24 22:02:56 +00:00
|
|
|
public ActionResult SetRepositories([FromBody, Required] List<RepositoryInfo> repositoryInfos)
|
2020-07-20 20:43:54 +00:00
|
|
|
{
|
|
|
|
_serverConfigurationManager.Configuration.PluginRepositories = repositoryInfos;
|
2020-11-12 09:13:56 +00:00
|
|
|
_serverConfigurationManager.SaveConfiguration();
|
2020-07-20 20:43:54 +00:00
|
|
|
return NoContent();
|
|
|
|
}
|
2020-04-25 18:59:31 +00:00
|
|
|
}
|
|
|
|
}
|