#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.
/// Package info.
[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.
///
/// 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.
/// Status.
[HttpPost("/Installed/{Name}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[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 Ok();
}
///
/// Cancels a package installation.
///
/// Installation Id.
/// Status.
[HttpDelete("/Installing/{id}")]
[Authorize(Policy = Policies.RequiresElevation)]
public IActionResult CancelPackageInstallation(
[FromRoute] [Required] string id)
{
_installationManager.CancelInstallation(new Guid(id));
return Ok();
}
}
}