PluginService.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Common.Updates;
  4. using MediaBrowser.Controller.Devices;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Model.Plugins;
  7. using MediaBrowser.Model.Serialization;
  8. using System;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Model.Services;
  13. using MediaBrowser.Common.Plugins;
  14. using Microsoft.Extensions.Logging;
  15. namespace MediaBrowser.Api
  16. {
  17. /// <summary>
  18. /// Class Plugins
  19. /// </summary>
  20. [Route("/Plugins", "GET", Summary = "Gets a list of currently installed plugins")]
  21. [Authenticated]
  22. public class GetPlugins : IReturn<PluginInfo[]>
  23. {
  24. public bool? IsAppStoreEnabled { get; set; }
  25. }
  26. /// <summary>
  27. /// Class UninstallPlugin
  28. /// </summary>
  29. [Route("/Plugins/{Id}", "DELETE", Summary = "Uninstalls a plugin")]
  30. [Authenticated(Roles = "Admin")]
  31. public class UninstallPlugin : IReturnVoid
  32. {
  33. /// <summary>
  34. /// Gets or sets the id.
  35. /// </summary>
  36. /// <value>The id.</value>
  37. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  38. public string Id { get; set; }
  39. }
  40. /// <summary>
  41. /// Class GetPluginConfiguration
  42. /// </summary>
  43. [Route("/Plugins/{Id}/Configuration", "GET", Summary = "Gets a plugin's configuration")]
  44. [Authenticated]
  45. public class GetPluginConfiguration
  46. {
  47. /// <summary>
  48. /// Gets or sets the id.
  49. /// </summary>
  50. /// <value>The id.</value>
  51. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  52. public string Id { get; set; }
  53. }
  54. /// <summary>
  55. /// Class UpdatePluginConfiguration
  56. /// </summary>
  57. [Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")]
  58. [Authenticated]
  59. public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
  60. {
  61. /// <summary>
  62. /// Gets or sets the id.
  63. /// </summary>
  64. /// <value>The id.</value>
  65. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  66. public string Id { get; set; }
  67. /// <summary>
  68. /// The raw Http Request Input Stream
  69. /// </summary>
  70. /// <value>The request stream.</value>
  71. public Stream RequestStream { get; set; }
  72. }
  73. //TODO cvium delete this
  74. [Route("/Registrations/{Name}", "GET", Summary = "Gets registration status for a feature", IsHidden = true)]
  75. [Authenticated]
  76. public class GetRegistration : IReturn<RegistrationInfo>
  77. {
  78. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  79. public string Name { get; set; }
  80. }
  81. //TODO cvium delete this
  82. public class RegistrationInfo
  83. {
  84. public string Name { get; set; }
  85. public DateTime ExpirationDate { get; set; }
  86. public bool IsTrial { get; set; }
  87. public bool IsRegistered { get; set; }
  88. }
  89. /// <summary>
  90. /// Class PluginsService
  91. /// </summary>
  92. public class PluginService : BaseApiService
  93. {
  94. /// <summary>
  95. /// The _json serializer
  96. /// </summary>
  97. private readonly IJsonSerializer _jsonSerializer;
  98. /// <summary>
  99. /// The _app host
  100. /// </summary>
  101. private readonly IApplicationHost _appHost;
  102. private readonly IInstallationManager _installationManager;
  103. private readonly INetworkManager _network;
  104. private readonly IDeviceManager _deviceManager;
  105. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, IInstallationManager installationManager, INetworkManager network, IDeviceManager deviceManager)
  106. : base()
  107. {
  108. if (jsonSerializer == null)
  109. {
  110. throw new ArgumentNullException(nameof(jsonSerializer));
  111. }
  112. _appHost = appHost;
  113. _installationManager = installationManager;
  114. _network = network;
  115. _deviceManager = deviceManager;
  116. _jsonSerializer = jsonSerializer;
  117. }
  118. //TODO cvium delete this
  119. public async Task<object> Get(GetRegistration request)
  120. {
  121. var info = new RegistrationInfo
  122. {
  123. ExpirationDate = DateTime.Now.AddYears(100),
  124. IsRegistered = true,
  125. IsTrial = false,
  126. Name = request.Name
  127. };
  128. return ToOptimizedResult(info);
  129. }
  130. /// <summary>
  131. /// Gets the specified request.
  132. /// </summary>
  133. /// <param name="request">The request.</param>
  134. /// <returns>System.Object.</returns>
  135. public async Task<object> Get(GetPlugins request)
  136. {
  137. // TODO cvium
  138. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToArray();
  139. // var requireAppStoreEnabled = request.IsAppStoreEnabled.HasValue && request.IsAppStoreEnabled.Value;
  140. //
  141. // // Don't fail just on account of image url's
  142. // try
  143. // {
  144. // var packages = (await _installationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None));
  145. //
  146. // foreach (var plugin in result)
  147. // {
  148. // var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && string.Equals(i.guid.Replace("-", string.Empty), plugin.Id.Replace("-", string.Empty), StringComparison.OrdinalIgnoreCase));
  149. //
  150. // if (pkg != null)
  151. // {
  152. // plugin.ImageUrl = pkg.thumbImage;
  153. // }
  154. // }
  155. //
  156. // if (requireAppStoreEnabled)
  157. // {
  158. // result = result
  159. // .Where(plugin =>
  160. // {
  161. // var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  162. // return pkg != null && pkg.enableInAppStore;
  163. //
  164. // })
  165. // .ToArray();
  166. // }
  167. // }
  168. // catch (Exception ex)
  169. // {
  170. // Logger.LogError(ex, "Error getting plugin list");
  171. // // Play it safe here
  172. // if (requireAppStoreEnabled)
  173. // {
  174. // result = new PluginInfo[] { };
  175. // }
  176. // }
  177. return ToOptimizedResult(result);
  178. }
  179. /// <summary>
  180. /// Gets the specified request.
  181. /// </summary>
  182. /// <param name="request">The request.</param>
  183. /// <returns>System.Object.</returns>
  184. public object Get(GetPluginConfiguration request)
  185. {
  186. var guid = new Guid(request.Id);
  187. var plugin = _appHost.Plugins.First(p => p.Id == guid) as IHasPluginConfiguration;
  188. return ToOptimizedResult(plugin.Configuration);
  189. }
  190. /// <summary>
  191. /// Posts the specified request.
  192. /// </summary>
  193. /// <param name="request">The request.</param>
  194. public async Task Post(UpdatePluginConfiguration request)
  195. {
  196. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  197. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  198. var id = new Guid(GetPathValue(1));
  199. var plugin = _appHost.Plugins.First(p => p.Id == id) as IHasPluginConfiguration;
  200. if (plugin == null)
  201. {
  202. throw new FileNotFoundException();
  203. }
  204. var configuration = (await _jsonSerializer.DeserializeFromStreamAsync(request.RequestStream, plugin.ConfigurationType).ConfigureAwait(false)) as BasePluginConfiguration;
  205. plugin.UpdateConfiguration(configuration);
  206. }
  207. /// <summary>
  208. /// Deletes the specified request.
  209. /// </summary>
  210. /// <param name="request">The request.</param>
  211. public void Delete(UninstallPlugin request)
  212. {
  213. var guid = new Guid(request.Id);
  214. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  215. _installationManager.UninstallPlugin(plugin);
  216. }
  217. }
  218. }