2019-01-13 20:01:16 +00:00
|
|
|
using System;
|
2013-02-21 01:33:05 +00:00
|
|
|
using System.Collections.Generic;
|
2019-10-08 18:51:11 +00:00
|
|
|
using System.Globalization;
|
2013-02-21 01:33:05 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
using MediaBrowser.Common.Updates;
|
2019-11-17 22:05:39 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Controller.Net;
|
2016-10-25 19:02:04 +00:00
|
|
|
using MediaBrowser.Model.Services;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Model.Updates;
|
2019-11-17 22:05:39 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class GetPackage
|
|
|
|
/// </summary>
|
2014-03-23 20:07:02 +00:00
|
|
|
[Route("/Packages/{Name}", "GET", Summary = "Gets a package, by name or assembly guid")]
|
2014-11-30 19:01:33 +00:00
|
|
|
[Authenticated]
|
2013-02-21 01:33:05 +00:00
|
|
|
public class GetPackage : IReturn<PackageInfo>
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the name.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name.</value>
|
2013-03-08 19:14:09 +00:00
|
|
|
[ApiMember(Name = "Name", Description = "The name of the package", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
2013-02-21 01:33:05 +00:00
|
|
|
public string Name { get; set; }
|
2013-11-04 18:16:47 +00:00
|
|
|
|
|
|
|
/// <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; }
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class GetPackages
|
|
|
|
/// </summary>
|
2014-03-23 20:07:02 +00:00
|
|
|
[Route("/Packages", "GET", Summary = "Gets available packages")]
|
2014-11-30 19:01:33 +00:00
|
|
|
[Authenticated]
|
2017-08-19 19:43:35 +00:00
|
|
|
public class GetPackages : IReturn<PackageInfo[]>
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class InstallPackage
|
|
|
|
/// </summary>
|
2014-03-23 20:07:02 +00:00
|
|
|
[Route("/Packages/Installed/{Name}", "POST", Summary = "Installs a package")]
|
2014-11-30 19:01:33 +00:00
|
|
|
[Authenticated(Roles = "Admin")]
|
2013-02-21 01:33:05 +00:00
|
|
|
public class InstallPackage : IReturnVoid
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the name.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name.</value>
|
2013-03-08 19:14:09 +00:00
|
|
|
[ApiMember(Name = "Name", Description = "Package name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
2013-02-21 01:33:05 +00:00
|
|
|
public string Name { get; set; }
|
|
|
|
|
2013-11-04 18:16:47 +00:00
|
|
|
/// <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; }
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the version.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The version.</value>
|
2013-03-08 19:14:09 +00:00
|
|
|
[ApiMember(Name = "Version", Description = "Optional version. Defaults to latest version.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
|
2013-02-21 01:33:05 +00:00
|
|
|
public string Version { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class CancelPackageInstallation
|
|
|
|
/// </summary>
|
2014-03-23 20:07:02 +00:00
|
|
|
[Route("/Packages/Installing/{Id}", "DELETE", Summary = "Cancels a package installation")]
|
2014-11-30 19:01:33 +00:00
|
|
|
[Authenticated(Roles = "Admin")]
|
2013-02-21 01:33:05 +00:00
|
|
|
public class CancelPackageInstallation : IReturnVoid
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the id.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The id.</value>
|
2013-03-08 19:14:09 +00:00
|
|
|
[ApiMember(Name = "Id", Description = "Installation Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
|
2014-05-08 20:09:53 +00:00
|
|
|
public string Id { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class PackageService
|
|
|
|
/// </summary>
|
2013-03-16 05:52:33 +00:00
|
|
|
public class PackageService : BaseApiService
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2013-03-05 04:25:27 +00:00
|
|
|
private readonly IInstallationManager _installationManager;
|
|
|
|
|
2019-11-17 22:05:39 +00:00
|
|
|
public PackageService(
|
|
|
|
ILogger<PackageService> logger,
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IHttpResultFactory httpResultFactory,
|
|
|
|
IInstallationManager installationManager)
|
|
|
|
: base(logger, serverConfigurationManager, httpResultFactory)
|
2013-03-05 04:25:27 +00:00
|
|
|
{
|
|
|
|
_installationManager = installationManager;
|
|
|
|
}
|
2013-02-22 04:23:06 +00:00
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
public object Get(GetPackage request)
|
|
|
|
{
|
2019-12-06 21:06:16 +00:00
|
|
|
var packages = _installationManager.GetAvailablePackages().GetAwaiter().GetResult();
|
|
|
|
var result = _installationManager.FilterPackages(
|
|
|
|
packages,
|
|
|
|
request.Name,
|
|
|
|
string.IsNullOrEmpty(request.AssemblyGuid) ? default : Guid.Parse(request.AssemblyGuid)).FirstOrDefault();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2015-01-10 05:53:35 +00:00
|
|
|
public async Task<object> Get(GetPackages request)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2019-10-08 18:51:11 +00:00
|
|
|
IEnumerable<PackageInfo> packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
return ToOptimizedResult(packages.ToArray());
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Posts the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <exception cref="ResourceNotFoundException"></exception>
|
2018-09-12 17:26:21 +00:00
|
|
|
public async Task Post(InstallPackage request)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2019-10-08 18:51:11 +00:00
|
|
|
var packages = await _installationManager.GetAvailablePackages().ConfigureAwait(false);
|
|
|
|
var package = _installationManager.GetCompatibleVersions(
|
|
|
|
packages,
|
|
|
|
request.Name,
|
2019-12-06 21:06:16 +00:00
|
|
|
string.IsNullOrEmpty(request.AssemblyGuid) ? Guid.Empty : Guid.Parse(request.AssemblyGuid),
|
2020-04-11 10:33:36 +00:00
|
|
|
string.IsNullOrEmpty(request.Version) ? null : Version.Parse(request.Version)).FirstOrDefault();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
if (package == null)
|
|
|
|
{
|
2019-10-08 18:51:11 +00:00
|
|
|
throw new ResourceNotFoundException(
|
|
|
|
string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"Package not found: {0}",
|
|
|
|
request.Name));
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 13:57:36 +00:00
|
|
|
await _installationManager.InstallPackage(package);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deletes the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
public void Delete(CancelPackageInstallation request)
|
|
|
|
{
|
2019-06-14 16:38:14 +00:00
|
|
|
_installationManager.CancelInstallation(new Guid(request.Id));
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|