jellyfin/Jellyfin.Api/Controllers/PackageController.cs

121 lines
4.9 KiB
C#
Raw Normal View History

2020-04-25 20:59:31 +02: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;
using MediaBrowser.Model.Updates;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Package Controller.
/// </summary>
[Route("Packages")]
2020-06-16 22:15:58 +02:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-04-25 20:59:31 +02:00
public class PackageController : BaseJellyfinApiController
{
private readonly IInstallationManager _installationManager;
/// <summary>
/// Initializes a new instance of the <see cref="PackageController"/> class.
/// </summary>
/// <param name="installationManager">Instance of <see cref="IInstallationManager"/>Installation Manager.</param>
public PackageController(IInstallationManager installationManager)
{
_installationManager = installationManager;
}
/// <summary>
/// Gets a package by name or assembly GUID.
2020-04-25 20:59:31 +02:00
/// </summary>
/// <param name="name">The name of the package.</param>
/// <param name="assemblyGuid">The GUID of the associated assembly.</param>
/// <response code="200">Package retrieved.</response>
/// <returns>A <see cref="PackageInfo"/> containing package information.</returns>
[HttpGet("/{name}")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<PackageInfo>> GetPackageInfo(
2020-06-27 18:50:44 +02:00
[FromRoute] [Required] string? name,
2020-04-25 20:59:31 +02:00
[FromQuery] string? assemblyGuid)
{
var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
2020-04-25 20:59:31 +02:00
var result = _installationManager.FilterPackages(
packages,
name,
string.IsNullOrEmpty(assemblyGuid) ? default : Guid.Parse(assemblyGuid)).FirstOrDefault();
return result;
2020-04-25 20:59:31 +02:00
}
/// <summary>
/// Gets available packages.
/// </summary>
/// <response code="200">Available packages returned.</response>
/// <returns>An <see cref="PackageInfo"/> containing available packages information.</returns>
2020-04-25 20:59:31 +02:00
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IEnumerable<PackageInfo>> GetPackages()
2020-04-25 20:59:31 +02:00
{
IEnumerable<PackageInfo> packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
return packages;
2020-04-25 20:59:31 +02:00
}
/// <summary>
/// Installs a package.
/// </summary>
/// <param name="name">Package name.</param>
/// <param name="assemblyGuid">GUID of the associated assembly.</param>
2020-04-25 20:59:31 +02:00
/// <param name="version">Optional version. Defaults to latest version.</param>
/// <response code="204">Package found.</response>
/// <response code="404">Package not found.</response>
/// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the package could not be found.</returns>
[HttpPost("/Installed/{name}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-04-25 20:59:31 +02:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
[Authorize(Policy = Policies.RequiresElevation)]
2020-04-25 20:59:31 +02:00
public async Task<ActionResult> InstallPackage(
2020-06-27 18:50:44 +02:00
[FromRoute] [Required] string? name,
[FromQuery] string? assemblyGuid,
[FromQuery] string? version)
2020-04-25 20:59:31 +02:00
{
var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
var package = _installationManager.GetCompatibleVersions(
packages,
name,
string.IsNullOrEmpty(assemblyGuid) ? Guid.Empty : Guid.Parse(assemblyGuid),
string.IsNullOrEmpty(version) ? null : Version.Parse(version)).FirstOrDefault();
if (package == null)
{
return NotFound();
}
await _installationManager.InstallPackage(package).ConfigureAwait(false);
return NoContent();
2020-04-25 20:59:31 +02:00
}
/// <summary>
/// Cancels a package installation.
/// </summary>
/// <param name="packageId">Installation Id.</param>
/// <response code="204">Installation cancelled.</response>
/// <returns>A <see cref="NoContentResult"/> on successfully cancelling a package installation.</returns>
[HttpDelete("/Installing/{packageId}")]
2020-04-25 20:59:31 +02:00
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-04-25 20:59:31 +02:00
public IActionResult CancelPackageInstallation(
[FromRoute] [Required] Guid packageId)
2020-04-25 20:59:31 +02:00
{
_installationManager.CancelInstallation(packageId);
return NoContent();
2020-04-25 20:59:31 +02:00
}
}
}