#nullable enable
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
{
///
/// Package Controller.
///
[Route("Packages")]
[Authorize]
public class PackageController : BaseJellyfinApiController
{
private readonly IInstallationManager _installationManager;
///
/// Initializes a new instance of the class.
///
/// Instance of Installation Manager.
public PackageController(IInstallationManager installationManager)
{
_installationManager = installationManager;
}
///
/// Gets a package by name or assembly GUID.
///
/// The name of the package.
/// The GUID of the associated assembly.
/// A containing package information.
[HttpGet("/{Name}")]
[ProducesResponseType(typeof(PackageInfo), StatusCodes.Status200OK)]
public async Task> GetPackageInfo(
[FromRoute] [Required] string name,
[FromQuery] string? assemblyGuid)
{
var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
var result = _installationManager.FilterPackages(
packages,
name,
string.IsNullOrEmpty(assemblyGuid) ? default : Guid.Parse(assemblyGuid)).FirstOrDefault();
return result;
}
///
/// Gets available packages.
///
/// An containing available packages information.
[HttpGet]
[ProducesResponseType(typeof(PackageInfo[]), StatusCodes.Status200OK)]
public async Task> GetPackages()
{
IEnumerable packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
return packages;
}
///
/// Installs a package.
///
/// Package name.
/// GUID of the associated assembly.
/// Optional version. Defaults to latest version.
/// Package found.
/// Package not found.
/// A on success, or a if the package could not be found.
[HttpPost("/Installed/{Name}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[Authorize(Policy = Policies.RequiresElevation)]
public async Task InstallPackage(
[FromRoute] [Required] string name,
[FromQuery] string assemblyGuid,
[FromQuery] string version)
{
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();
}
///
/// Cancels a package installation.
///
/// Installation Id.
/// Installation cancelled.
/// A on successfully cancelling a package installation.
[HttpDelete("/Installing/{id}")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult CancelPackageInstallation(
[FromRoute] [Required] string id)
{
_installationManager.CancelInstallation(new Guid(id));
return NoContent();
}
}
}