jellyfin/MediaBrowser.Api/PackageService.cs

257 lines
11 KiB
C#
Raw Normal View History

2013-03-04 05:43:06 +00:00
using MediaBrowser.Common;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Updates;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Updates;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2017-06-23 16:04:45 +00:00
using MediaBrowser.Common.Progress;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
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; }
/// <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>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
2013-03-08 19:14:09 +00:00
[ApiMember(Name = "PackageType", Description = "Optional package type filter (System/UserInstalled)", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2016-10-08 05:57:38 +00:00
public string PackageType { get; set; }
2018-09-12 17:26:21 +00:00
[ApiMember(Name = "TargetSystems", Description = "Optional. Filter by target system type. Allows multiple, comma delimited.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
2013-04-03 18:46:58 +00:00
public string TargetSystems { get; set; }
2013-08-26 19:15:44 +00:00
[ApiMember(Name = "IsPremium", Description = "Optional. Filter by premium status", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? IsPremium { get; set; }
2014-06-07 21:06:01 +00:00
[ApiMember(Name = "IsAdult", Description = "Optional. Filter by package that contain adult content.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? IsAdult { get; set; }
2015-05-16 19:09:02 +00:00
public bool? IsAppStoreEnabled { get; set; }
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Class GetPackageVersionUpdates
/// </summary>
2014-03-23 20:07:02 +00:00
[Route("/Packages/Updates", "GET", Summary = "Gets available package updates for currently installed packages")]
2014-11-30 19:01:33 +00:00
[Authenticated(Roles = "Admin")]
2017-08-19 19:43:35 +00:00
public class GetPackageVersionUpdates : IReturn<PackageVersionInfo[]>
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
2013-03-08 19:14:09 +00:00
[ApiMember(Name = "PackageType", Description = "Package type filter (System/UserInstalled)", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
2016-10-08 05:57:38 +00:00
public string PackageType { get; set; }
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; }
/// <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>
/// Gets or sets the update class.
/// </summary>
/// <value>The update class.</value>
2013-03-08 19:14:09 +00:00
[ApiMember(Name = "UpdateClass", Description = "Optional update class (Dev, Beta, Release). Defaults to Release.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
2013-02-21 01:33:05 +00:00
public PackageVersionClass UpdateClass { 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
{
private readonly IInstallationManager _installationManager;
private readonly IApplicationHost _appHost;
public PackageService(IInstallationManager installationManager, IApplicationHost appHost)
{
_installationManager = installationManager;
_appHost = appHost;
}
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>
2018-09-12 17:26:21 +00:00
public async Task<object> Get(GetPackageVersionUpdates request)
2013-02-21 01:33:05 +00:00
{
2017-08-19 19:43:35 +00:00
PackageVersionInfo[] result = null;
2013-02-21 01:33:05 +00:00
2016-10-08 05:57:38 +00:00
if (string.Equals(request.PackageType, "UserInstalled", StringComparison.OrdinalIgnoreCase) || string.Equals(request.PackageType, "All", StringComparison.OrdinalIgnoreCase))
2013-02-21 01:33:05 +00:00
{
2018-09-12 17:26:21 +00:00
result = (await _installationManager.GetAvailablePluginUpdates(_appHost.ApplicationVersion, false, CancellationToken.None).ConfigureAwait(false)).ToArray();
2013-02-21 01:33:05 +00:00
}
2017-08-19 19:43:35 +00:00
else if (string.Equals(request.PackageType, "System", StringComparison.OrdinalIgnoreCase) ||
string.Equals(request.PackageType, "All", StringComparison.OrdinalIgnoreCase))
2013-02-21 01:33:05 +00:00
{
2018-09-12 17:26:21 +00:00
var updateCheckResult = await _appHost
.CheckForApplicationUpdate(CancellationToken.None, new SimpleProgress<double>()).ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
2013-02-22 04:23:06 +00:00
if (updateCheckResult.IsUpdateAvailable)
2013-02-21 01:33:05 +00:00
{
2018-09-12 17:26:21 +00:00
result = new PackageVersionInfo[] { updateCheckResult.Package };
2013-02-21 01:33:05 +00:00
}
}
2017-08-19 19:43:35 +00:00
return ToOptimizedResult(result ?? new PackageVersionInfo[] { });
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)
{
var packages = _installationManager.GetAvailablePackages(CancellationToken.None, applicationVersion: _appHost.ApplicationVersion).Result;
2013-02-21 01:33:05 +00:00
2017-08-19 19:43:35 +00:00
var result = packages.FirstOrDefault(p => string.Equals(p.guid, request.AssemblyGuid ?? "none", StringComparison.OrdinalIgnoreCase))
?? packages.FirstOrDefault(p => p.name.Equals(request.Name, StringComparison.OrdinalIgnoreCase));
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
{
2017-08-19 19:43:35 +00:00
IEnumerable<PackageInfo> packages = await _installationManager.GetAvailablePackages(CancellationToken.None, false, request.PackageType, _appHost.ApplicationVersion).ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
2013-04-03 18:46:58 +00:00
if (!string.IsNullOrEmpty(request.TargetSystems))
{
2013-04-03 18:46:58 +00:00
var apps = request.TargetSystems.Split(',').Select(i => (PackageTargetSystem)Enum.Parse(typeof(PackageTargetSystem), i, true));
packages = packages.Where(p => apps.Contains(p.targetSystem));
}
if (request.IsPremium.HasValue)
{
packages = packages.Where(p => p.isPremium == request.IsPremium.Value);
}
2014-06-07 21:06:01 +00:00
if (request.IsAdult.HasValue)
{
packages = packages.Where(p => p.adult == request.IsAdult.Value);
}
2015-05-16 19:09:02 +00:00
if (request.IsAppStoreEnabled.HasValue)
{
packages = packages.Where(p => p.enableInAppStore == request.IsAppStoreEnabled.Value);
}
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
{
var package = string.IsNullOrEmpty(request.Version) ?
2018-09-12 17:26:21 +00:00
await _installationManager.GetLatestCompatibleVersion(request.Name, request.AssemblyGuid, _appHost.ApplicationVersion, request.UpdateClass).ConfigureAwait(false) :
await _installationManager.GetPackage(request.Name, request.AssemblyGuid, request.UpdateClass, Version.Parse(request.Version)).ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
if (package == null)
{
throw new ResourceNotFoundException(string.Format("Package not found: {0}", request.Name));
}
2017-06-23 16:04:45 +00:00
Task.Run(() => _installationManager.InstallPackage(package, true, new SimpleProgress<double>(), CancellationToken.None));
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)
{
2018-09-12 17:26:21 +00:00
var info = _installationManager.CurrentInstallations.FirstOrDefault(i => i.Item1.Id.Equals(request.Id));
2013-02-21 01:33:05 +00:00
if (info != null)
{
info.Item2.Cancel();
}
}
}
}