using MediaBrowser.Common; using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Updates; 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 { /// /// Class GetPackage /// [Route("/Packages/{Name}", "GET")] [Api(("Gets a package, by name"))] public class GetPackage : IReturn { /// /// Gets or sets the name. /// /// The name. [ApiMember(Name = "Name", Description = "The name of the package", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Name { get; set; } } /// /// Class GetPackages /// [Route("/Packages", "GET")] [Api(("Gets available packages"))] public class GetPackages : IReturn> { /// /// Gets or sets the name. /// /// The name. [ApiMember(Name = "PackageType", Description = "Optional package type filter (System/UserInstalled)", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public PackageType? PackageType { get; set; } [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; } } /// /// Class GetPackageVersionUpdates /// [Route("/Packages/Updates", "GET")] [Api(("Gets available package updates for currently installed packages"))] public class GetPackageVersionUpdates : IReturn> { /// /// Gets or sets the name. /// /// The name. [ApiMember(Name = "PackageType", Description = "Package type filter (System/UserInstalled)", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")] public PackageType PackageType { get; set; } } /// /// Class InstallPackage /// [Route("/Packages/Installed/{Name}", "POST")] [Api(("Installs a package"))] public class InstallPackage : IReturnVoid { /// /// Gets or sets the name. /// /// The name. [ApiMember(Name = "Name", Description = "Package name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] public string Name { get; set; } /// /// Gets or sets the version. /// /// The version. [ApiMember(Name = "Version", Description = "Optional version. Defaults to latest version.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")] public string Version { get; set; } /// /// Gets or sets the update class. /// /// The update class. [ApiMember(Name = "UpdateClass", Description = "Optional update class (Dev, Beta, Release). Defaults to Release.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")] public PackageVersionClass UpdateClass { get; set; } } /// /// Class CancelPackageInstallation /// [Route("/Packages/Installing/{Id}", "DELETE")] [Api(("Cancels a package installation"))] public class CancelPackageInstallation : IReturnVoid { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Id", Description = "Installation Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] public Guid Id { get; set; } } /// /// Class PackageService /// public class PackageService : BaseApiService { private readonly IInstallationManager _installationManager; private readonly IApplicationHost _appHost; public PackageService(IInstallationManager installationManager, IApplicationHost appHost) { _installationManager = installationManager; _appHost = appHost; } /// /// Gets the specified request. /// /// The request. /// System.Object. /// Unsupported PackageType public object Get(GetPackageVersionUpdates request) { var result = new List(); if (request.PackageType == PackageType.UserInstalled || request.PackageType == PackageType.All) { result.AddRange(_installationManager.GetAvailablePluginUpdates(false, CancellationToken.None).Result.ToList()); } else if (request.PackageType == PackageType.System || request.PackageType == PackageType.All) { var updateCheckResult = _appHost.CheckForApplicationUpdate(CancellationToken.None, new Progress()).Result; if (updateCheckResult.IsUpdateAvailable) { result.Add(updateCheckResult.Package); } } return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPackage request) { var packages = _installationManager.GetAvailablePackages(CancellationToken.None, applicationVersion: _appHost.ApplicationVersion).Result; var result = packages.FirstOrDefault(p => p.name.Equals(request.Name, StringComparison.OrdinalIgnoreCase)); return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPackages request) { var packages = _installationManager.GetAvailablePackages(CancellationToken.None, request.PackageType, _appHost.ApplicationVersion).Result; if (!string.IsNullOrEmpty(request.TargetSystems)) { 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); } return ToOptimizedResult(packages.ToList()); } /// /// Posts the specified request. /// /// The request. /// 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; if (package == null) { throw new ResourceNotFoundException(string.Format("Package not found: {0}", request.Name)); } Task.Run(() => _installationManager.InstallPackage(package, new Progress(), CancellationToken.None)); } /// /// Deletes the specified request. /// /// The request. public void Delete(CancelPackageInstallation request) { var info = _installationManager.CurrentInstallations.FirstOrDefault(i => i.Item1.Id == request.Id); if (info != null) { info.Item2.Cancel(); } } } }