jellyfin/MediaBrowser.Api/PackageService.cs

224 lines
8.6 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;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Updates;
using ServiceStack.ServiceHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetPackage
/// </summary>
[Route("/Packages/{Name}", "GET")]
[Api(("Gets a package, by name"))]
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>
/// Class GetPackages
/// </summary>
[Route("/Packages", "GET")]
[Api(("Gets available packages"))]
2013-02-21 01:33:05 +00:00
public class GetPackages : IReturn<List<PackageInfo>>
{
/// <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")]
2013-02-21 01:33:05 +00:00
public PackageType? PackageType { get; set; }
2013-04-03 18:46:58 +00:00
[ApiMember(Name = "TargetSystems", Description = "Optional. Filter by target system type. Allows multiple, comma delimited.", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET", AllowMultiple = true)]
public string TargetSystems { get; set; }
[ApiMember(Name = "IsPremium", Description = "Optiona. Filter by premium status", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? IsPremium { get; set; }
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Class GetPackageVersionUpdates
/// </summary>
[Route("/Packages/Updates", "GET")]
[Api(("Gets available package updates for currently installed packages"))]
2013-02-21 01:33:05 +00:00
public class GetPackageVersionUpdates : IReturn<List<PackageVersionInfo>>
{
/// <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")]
2013-02-21 01:33:05 +00:00
public PackageType PackageType { get; set; }
}
/// <summary>
/// Class InstallPackage
/// </summary>
[Route("/Packages/Installed/{Name}", "POST")]
[Api(("Installs a package"))]
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 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>
[Route("/Packages/Installing/{Id}", "DELETE")]
[Api(("Cancels a package installation"))]
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")]
2013-02-21 01:33:05 +00:00
public Guid Id { get; set; }
}
/// <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>
/// <exception cref="System.ArgumentException">Unsupported PackageType</exception>
public object Get(GetPackageVersionUpdates request)
{
var result = new List<PackageVersionInfo>();
if (request.PackageType == PackageType.UserInstalled || request.PackageType == PackageType.All)
{
2013-08-16 18:59:37 +00:00
result.AddRange(_installationManager.GetAvailablePluginUpdates(false, CancellationToken.None).Result.ToList());
2013-02-21 01:33:05 +00:00
}
else if (request.PackageType == PackageType.System || request.PackageType == PackageType.All)
{
2013-04-10 15:32:09 +00:00
var updateCheckResult = _appHost.CheckForApplicationUpdate(CancellationToken.None, new Progress<double>()).Result;
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
{
2013-02-28 19:29:17 +00:00
result.Add(updateCheckResult.Package);
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>
public object Get(GetPackage request)
{
var packages = _installationManager.GetAvailablePackages(CancellationToken.None, applicationVersion: _appHost.ApplicationVersion).Result;
2013-02-21 01:33:05 +00:00
var result = packages.FirstOrDefault(p => p.name.Equals(request.Name, StringComparison.OrdinalIgnoreCase));
return ToOptimizedResult(result);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetPackages request)
{
var packages = _installationManager.GetAvailablePackages(CancellationToken.None, request.PackageType, _appHost.ApplicationVersion).Result;
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);
}
2013-02-21 01:33:05 +00:00
return ToOptimizedResult(packages.ToList());
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <exception cref="ResourceNotFoundException"></exception>
public void Post(InstallPackage request)
{
var package = string.IsNullOrEmpty(request.Version) ?
_installationManager.GetLatestCompatibleVersion(request.Name, request.UpdateClass).Result :
_installationManager.GetPackage(request.Name, request.UpdateClass, Version.Parse(request.Version)).Result;
2013-02-21 01:33:05 +00:00
if (package == null)
{
throw new ResourceNotFoundException(string.Format("Package not found: {0}", request.Name));
}
2013-04-10 15:32:09 +00:00
Task.Run(() => _installationManager.InstallPackage(package, new Progress<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)
{
var info = _installationManager.CurrentInstallations.FirstOrDefault(i => i.Item1.Id == request.Id);
2013-02-21 01:33:05 +00:00
if (info != null)
{
info.Item2.Cancel();
}
}
}
}