PluginService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Common.Security;
  4. using MediaBrowser.Common.Updates;
  5. using MediaBrowser.Controller.Devices;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Plugins;
  9. using MediaBrowser.Model.Registration;
  10. using MediaBrowser.Model.Serialization;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using MediaBrowser.Model.Services;
  18. namespace MediaBrowser.Api
  19. {
  20. /// <summary>
  21. /// Class Plugins
  22. /// </summary>
  23. [Route("/Plugins", "GET", Summary = "Gets a list of currently installed plugins")]
  24. [Authenticated]
  25. public class GetPlugins : IReturn<PluginInfo[]>
  26. {
  27. public bool? IsAppStoreEnabled { get; set; }
  28. }
  29. /// <summary>
  30. /// Class UninstallPlugin
  31. /// </summary>
  32. [Route("/Plugins/{Id}", "DELETE", Summary = "Uninstalls a plugin")]
  33. [Authenticated(Roles = "Admin")]
  34. public class UninstallPlugin : IReturnVoid
  35. {
  36. /// <summary>
  37. /// Gets or sets the id.
  38. /// </summary>
  39. /// <value>The id.</value>
  40. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  41. public string Id { get; set; }
  42. }
  43. /// <summary>
  44. /// Class GetPluginConfiguration
  45. /// </summary>
  46. [Route("/Plugins/{Id}/Configuration", "GET", Summary = "Gets a plugin's configuration")]
  47. [Authenticated]
  48. public class GetPluginConfiguration
  49. {
  50. /// <summary>
  51. /// Gets or sets the id.
  52. /// </summary>
  53. /// <value>The id.</value>
  54. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  55. public string Id { get; set; }
  56. }
  57. /// <summary>
  58. /// Class UpdatePluginConfiguration
  59. /// </summary>
  60. [Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")]
  61. [Authenticated]
  62. public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
  63. {
  64. /// <summary>
  65. /// Gets or sets the id.
  66. /// </summary>
  67. /// <value>The id.</value>
  68. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  69. public string Id { get; set; }
  70. /// <summary>
  71. /// The raw Http Request Input Stream
  72. /// </summary>
  73. /// <value>The request stream.</value>
  74. public Stream RequestStream { get; set; }
  75. }
  76. /// <summary>
  77. /// Class GetPluginSecurityInfo
  78. /// </summary>
  79. [Route("/Plugins/SecurityInfo", "GET", Summary = "Gets plugin registration information", IsHidden = true)]
  80. [Authenticated]
  81. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  82. {
  83. }
  84. /// <summary>
  85. /// Class UpdatePluginSecurityInfo
  86. /// </summary>
  87. [Route("/Plugins/SecurityInfo", "POST", Summary = "Updates plugin registration information", IsHidden = true)]
  88. [Authenticated(Roles = "Admin")]
  89. public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
  90. {
  91. }
  92. [Route("/Plugins/RegistrationRecords/{Name}", "GET", Summary = "Gets registration status for a feature", IsHidden = true)]
  93. [Authenticated]
  94. public class GetRegistrationStatus
  95. {
  96. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  97. public string Name { get; set; }
  98. [ApiMember(Name = "Mb2Equivalent", Description = "Optional. The equivalent feature name in MB2", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  99. public string Mb2Equivalent { get; set; }
  100. }
  101. [Route("/Registrations/{Name}", "GET", Summary = "Gets registration status for a feature", IsHidden = true)]
  102. [Authenticated]
  103. public class GetRegistration : IReturn<RegistrationInfo>
  104. {
  105. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  106. public string Name { get; set; }
  107. }
  108. [Route("/Appstore/Register", "POST", Summary = "Registers an appstore sale", IsHidden = true)]
  109. [Authenticated]
  110. public class RegisterAppstoreSale
  111. {
  112. [ApiMember(Name = "Parameters", Description = "Java representation of parameters to pass through to admin server", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  113. public string Parameters { get; set; }
  114. }
  115. /// <summary>
  116. /// Class PluginsService
  117. /// </summary>
  118. public class PluginService : BaseApiService
  119. {
  120. /// <summary>
  121. /// The _json serializer
  122. /// </summary>
  123. private readonly IJsonSerializer _jsonSerializer;
  124. /// <summary>
  125. /// The _app host
  126. /// </summary>
  127. private readonly IApplicationHost _appHost;
  128. private readonly ISecurityManager _securityManager;
  129. private readonly IInstallationManager _installationManager;
  130. private readonly INetworkManager _network;
  131. private readonly IDeviceManager _deviceManager;
  132. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager, INetworkManager network, IDeviceManager deviceManager)
  133. : base()
  134. {
  135. if (jsonSerializer == null)
  136. {
  137. throw new ArgumentNullException("jsonSerializer");
  138. }
  139. _appHost = appHost;
  140. _securityManager = securityManager;
  141. _installationManager = installationManager;
  142. _network = network;
  143. _deviceManager = deviceManager;
  144. _jsonSerializer = jsonSerializer;
  145. }
  146. /// <summary>
  147. /// Gets the specified request.
  148. /// </summary>
  149. /// <param name="request">The request.</param>
  150. /// <returns>System.Object.</returns>
  151. public async Task<object> Get(GetRegistrationStatus request)
  152. {
  153. var result = await _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).ConfigureAwait(false);
  154. return ToOptimizedResult(result);
  155. }
  156. public async Task<object> Get(GetRegistration request)
  157. {
  158. var result = await _securityManager.GetRegistrationStatus(request.Name).ConfigureAwait(false);
  159. var info = new RegistrationInfo
  160. {
  161. ExpirationDate = result.ExpirationDate,
  162. IsRegistered = result.IsRegistered,
  163. IsTrial = result.TrialVersion,
  164. Name = request.Name
  165. };
  166. return ToOptimizedResult(info);
  167. }
  168. /// <summary>
  169. /// Gets the specified request.
  170. /// </summary>
  171. /// <param name="request">The request.</param>
  172. /// <returns>System.Object.</returns>
  173. public async Task<object> Get(GetPlugins request)
  174. {
  175. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToArray();
  176. var requireAppStoreEnabled = request.IsAppStoreEnabled.HasValue && request.IsAppStoreEnabled.Value;
  177. // Don't fail just on account of image url's
  178. try
  179. {
  180. var packages = (await _installationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None));
  181. foreach (var plugin in result)
  182. {
  183. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && string.Equals(i.guid.Replace("-", string.Empty), plugin.Id.Replace("-", string.Empty), StringComparison.OrdinalIgnoreCase));
  184. if (pkg != null)
  185. {
  186. plugin.ImageUrl = pkg.thumbImage;
  187. }
  188. }
  189. if (requireAppStoreEnabled)
  190. {
  191. result = result
  192. .Where(plugin =>
  193. {
  194. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  195. return pkg != null && pkg.enableInAppStore;
  196. })
  197. .ToArray();
  198. }
  199. }
  200. catch
  201. {
  202. //Logger.ErrorException("Error getting plugin list", ex);
  203. // Play it safe here
  204. if (requireAppStoreEnabled)
  205. {
  206. result = new PluginInfo[] { };
  207. }
  208. }
  209. return ToOptimizedSerializedResultUsingCache(result);
  210. }
  211. /// <summary>
  212. /// Gets the specified request.
  213. /// </summary>
  214. /// <param name="request">The request.</param>
  215. /// <returns>System.Object.</returns>
  216. public object Get(GetPluginConfiguration request)
  217. {
  218. var guid = new Guid(request.Id);
  219. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  220. return ToOptimizedResult(plugin.Configuration);
  221. }
  222. /// <summary>
  223. /// Gets the specified request.
  224. /// </summary>
  225. /// <param name="request">The request.</param>
  226. /// <returns>System.Object.</returns>
  227. public object Get(GetPluginSecurityInfo request)
  228. {
  229. var result = new PluginSecurityInfo
  230. {
  231. IsMBSupporter = _securityManager.IsMBSupporter,
  232. SupporterKey = _securityManager.SupporterKey
  233. };
  234. return ToOptimizedSerializedResultUsingCache(result);
  235. }
  236. /// <summary>
  237. /// Post app store sale
  238. /// </summary>
  239. /// <param name="request"></param>
  240. /// <returns></returns>
  241. public void Post(RegisterAppstoreSale request)
  242. {
  243. var task = _securityManager.RegisterAppStoreSale(request.Parameters);
  244. Task.WaitAll(task);
  245. }
  246. /// <summary>
  247. /// Posts the specified request.
  248. /// </summary>
  249. /// <param name="request">The request.</param>
  250. public void Post(UpdatePluginSecurityInfo request)
  251. {
  252. var info = request;
  253. _securityManager.SupporterKey = info.SupporterKey;
  254. }
  255. /// <summary>
  256. /// Posts the specified request.
  257. /// </summary>
  258. /// <param name="request">The request.</param>
  259. public void Post(UpdatePluginConfiguration request)
  260. {
  261. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  262. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  263. var id = new Guid(GetPathValue(1));
  264. var plugin = _appHost.Plugins.First(p => p.Id == id);
  265. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  266. plugin.UpdateConfiguration(configuration);
  267. }
  268. /// <summary>
  269. /// Deletes the specified request.
  270. /// </summary>
  271. /// <param name="request">The request.</param>
  272. public void Delete(UninstallPlugin request)
  273. {
  274. var guid = new Guid(request.Id);
  275. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  276. _installationManager.UninstallPlugin(plugin);
  277. }
  278. }
  279. }