using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Net; using MediaBrowser.Controller; using MediaBrowser.Model.Updates; using ServiceStack.ServiceHost; using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Api { /// /// Class GetPackage /// [Route("/Packages/{Name}", "GET")] public class GetPackage : IReturn { /// /// Gets or sets the name. /// /// The name. public string Name { get; set; } } /// /// Class GetPackages /// [Route("/Packages", "GET")] public class GetPackages : IReturn> { /// /// Gets or sets the name. /// /// The name. public PackageType? PackageType { get; set; } } /// /// Class GetPackageVersionUpdates /// [Route("/Packages/Updates", "GET")] public class GetPackageVersionUpdates : IReturn> { /// /// Gets or sets the name. /// /// The name. public PackageType PackageType { get; set; } } /// /// Class InstallPackage /// [Route("/Packages/Installed/{Name}", "POST")] public class InstallPackage : IReturnVoid { /// /// Gets or sets the name. /// /// The name. public string Name { get; set; } /// /// Gets or sets the version. /// /// The version. public string Version { get; set; } /// /// Gets or sets the update class. /// /// The update class. public PackageVersionClass UpdateClass { get; set; } } /// /// Class CancelPackageInstallation /// [Route("/Packages/Installing/{Id}", "DELETE")] public class CancelPackageInstallation : IReturnVoid { /// /// Gets or sets the id. /// /// The id. public Guid Id { get; set; } } /// /// Class PackageService /// [Export(typeof(IRestfulService))] public class PackageService : BaseRestService { /// /// Gets or sets the application host. /// /// The application host. public IApplicationHost ApplicationHost { get; set; } /// /// Gets the specified request. /// /// The request. /// System.Object. /// Unsupported PackageType public object Get(GetPackageVersionUpdates request) { var kernel = (Kernel)Kernel; var result = new List(); if (request.PackageType == PackageType.UserInstalled || request.PackageType == PackageType.All) { result.AddRange(kernel.InstallationManager.GetAvailablePluginUpdates(false, CancellationToken.None).Result.ToList()); } else if (request.PackageType == PackageType.System || request.PackageType == PackageType.All) { var updateCheckResult = ApplicationHost.CheckForApplicationUpdate(CancellationToken.None, new Progress { }).Result; if (updateCheckResult.IsUpdateAvailable) { result.Add(new PackageVersionInfo { versionStr = updateCheckResult.AvailableVersion.ToString() }); } } return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPackage request) { var kernel = (Kernel)Kernel; var packages = kernel.InstallationManager.GetAvailablePackages(CancellationToken.None, applicationVersion: kernel.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 kernel = (Kernel)Kernel; var packages = kernel.InstallationManager.GetAvailablePackages(CancellationToken.None, request.PackageType, kernel.ApplicationVersion).Result; return ToOptimizedResult(packages.ToList()); } /// /// Posts the specified request. /// /// The request. /// public void Post(InstallPackage request) { var kernel = (Kernel)Kernel; var package = string.IsNullOrEmpty(request.Version) ? kernel.InstallationManager.GetLatestCompatibleVersion(request.Name, request.UpdateClass).Result : kernel.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(() => kernel.InstallationManager.InstallPackage(package, new Progress { }, CancellationToken.None)); } /// /// Deletes the specified request. /// /// The request. public void Delete(CancelPackageInstallation request) { var kernel = (Kernel)Kernel; var info = kernel.InstallationManager.CurrentInstallations.FirstOrDefault(i => i.Item1.Id == request.Id); if (info != null) { info.Item2.Cancel(); } } } }