Merge pull request #2983 from bruceCoelho/api-migration

PackageService to Jellyfin.API
This commit is contained in:
Patrick Barron 2020-06-11 22:52:35 -04:00 committed by GitHub
commit a6573e015f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 120 additions and 171 deletions

View File

@ -0,0 +1,120 @@
#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
{
/// <summary>
/// Package Controller.
/// </summary>
[Route("Packages")]
[Authorize]
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.
/// </summary>
/// <param name="name">The name of the package.</param>
/// <param name="assemblyGuid">The GUID of the associated assembly.</param>
/// <returns>A <see cref="PackageInfo"/> containing package information.</returns>
[HttpGet("/{Name}")]
[ProducesResponseType(typeof(PackageInfo), StatusCodes.Status200OK)]
public async Task<ActionResult<PackageInfo>> 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;
}
/// <summary>
/// Gets available packages.
/// </summary>
/// <returns>An <see cref="PackageInfo"/> containing available packages information.</returns>
[HttpGet]
[ProducesResponseType(typeof(PackageInfo[]), StatusCodes.Status200OK)]
public async Task<IEnumerable<PackageInfo>> GetPackages()
{
IEnumerable<PackageInfo> packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
return packages;
}
/// <summary>
/// Installs a package.
/// </summary>
/// <param name="name">Package name.</param>
/// <param name="assemblyGuid">GUID of the associated assembly.</param>
/// <param name="version">Optional version. Defaults to latest version.</param>
/// <response code="200">Package found.</response>
/// <response code="404">Package not found.</response>
/// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the package could not be found.</returns>
[HttpPost("/Installed/{Name}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[Authorize(Policy = Policies.RequiresElevation)]
public async Task<ActionResult> 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();
}
/// <summary>
/// Cancels a package installation.
/// </summary>
/// <param name="id">Installation Id.</param>
/// <response code="200">Installation cancelled.</response>
/// <returns>An <see cref="OkResult"/> on successfully cancelling a package installation.</returns>
[HttpDelete("/Installing/{id}")]
[Authorize(Policy = Policies.RequiresElevation)]
public IActionResult CancelPackageInstallation(
[FromRoute] [Required] string id)
{
_installationManager.CancelInstallation(new Guid(id));
return Ok();
}
}
}

View File

@ -1,171 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Updates;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Services;
using MediaBrowser.Model.Updates;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetPackage
/// </summary>
[Route("/Packages/{Name}", "GET", Summary = "Gets a package, by name or assembly guid")]
[Authenticated]
public class GetPackage : IReturn<PackageInfo>
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The name of the package", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Name { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "AssemblyGuid", Description = "The guid of the associated assembly", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string AssemblyGuid { get; set; }
}
/// <summary>
/// Class GetPackages
/// </summary>
[Route("/Packages", "GET", Summary = "Gets available packages")]
[Authenticated]
public class GetPackages : IReturn<PackageInfo[]>
{
}
/// <summary>
/// Class InstallPackage
/// </summary>
[Route("/Packages/Installed/{Name}", "POST", Summary = "Installs a package")]
[Authenticated(Roles = "Admin")]
public class InstallPackage : IReturnVoid
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "Package name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Name { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "AssemblyGuid", Description = "Guid of the associated assembly", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string AssemblyGuid { get; set; }
/// <summary>
/// Gets or sets the version.
/// </summary>
/// <value>The version.</value>
[ApiMember(Name = "Version", Description = "Optional version. Defaults to latest version.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Version { get; set; }
}
/// <summary>
/// Class CancelPackageInstallation
/// </summary>
[Route("/Packages/Installing/{Id}", "DELETE", Summary = "Cancels a package installation")]
[Authenticated(Roles = "Admin")]
public class CancelPackageInstallation : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Installation Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get; set; }
}
/// <summary>
/// Class PackageService
/// </summary>
public class PackageService : BaseApiService
{
private readonly IInstallationManager _installationManager;
public PackageService(
ILogger<PackageService> logger,
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
IInstallationManager installationManager)
: base(logger, serverConfigurationManager, httpResultFactory)
{
_installationManager = installationManager;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetPackage request)
{
var packages = _installationManager.GetAvailablePackages().GetAwaiter().GetResult();
var result = _installationManager.FilterPackages(
packages,
request.Name,
string.IsNullOrEmpty(request.AssemblyGuid) ? default : Guid.Parse(request.AssemblyGuid)).FirstOrDefault();
return ToOptimizedResult(result);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public async Task<object> Get(GetPackages request)
{
IEnumerable<PackageInfo> packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
return ToOptimizedResult(packages.ToArray());
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <exception cref="ResourceNotFoundException"></exception>
public async Task Post(InstallPackage request)
{
var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
var package = _installationManager.GetCompatibleVersions(
packages,
request.Name,
string.IsNullOrEmpty(request.AssemblyGuid) ? Guid.Empty : Guid.Parse(request.AssemblyGuid),
string.IsNullOrEmpty(request.Version) ? null : Version.Parse(request.Version)).FirstOrDefault();
if (package == null)
{
throw new ResourceNotFoundException(
string.Format(
CultureInfo.InvariantCulture,
"Package not found: {0}",
request.Name));
}
await _installationManager.InstallPackage(package);
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Delete(CancelPackageInstallation request)
{
_installationManager.CancelInstallation(new Guid(request.Id));
}
}
}