PluginService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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<List<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")]
  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")]
  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")]
  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")]
  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")]
  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()).ToList();
  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. .ToList();
  182. foreach (var plugin in result)
  183. {
  184. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && string.Equals(i.guid.Replace("-", string.Empty), plugin.Id.Replace("-", string.Empty), StringComparison.OrdinalIgnoreCase));
  185. if (pkg != null)
  186. {
  187. plugin.ImageUrl = pkg.thumbImage;
  188. }
  189. }
  190. if (requireAppStoreEnabled)
  191. {
  192. result = result
  193. .Where(plugin =>
  194. {
  195. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  196. return pkg != null && pkg.enableInAppStore;
  197. })
  198. .ToList();
  199. }
  200. }
  201. catch
  202. {
  203. //Logger.ErrorException("Error getting plugin list", ex);
  204. // Play it safe here
  205. if (requireAppStoreEnabled)
  206. {
  207. result = new List<PluginInfo>();
  208. }
  209. }
  210. return ToOptimizedSerializedResultUsingCache(result);
  211. }
  212. /// <summary>
  213. /// Gets the specified request.
  214. /// </summary>
  215. /// <param name="request">The request.</param>
  216. /// <returns>System.Object.</returns>
  217. public object Get(GetPluginConfiguration request)
  218. {
  219. var guid = new Guid(request.Id);
  220. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  221. return ToOptimizedResult(plugin.Configuration);
  222. }
  223. /// <summary>
  224. /// Gets the specified request.
  225. /// </summary>
  226. /// <param name="request">The request.</param>
  227. /// <returns>System.Object.</returns>
  228. public object Get(GetPluginSecurityInfo request)
  229. {
  230. var result = new PluginSecurityInfo
  231. {
  232. IsMBSupporter = _securityManager.IsMBSupporter,
  233. SupporterKey = _securityManager.SupporterKey
  234. };
  235. return ToOptimizedSerializedResultUsingCache(result);
  236. }
  237. /// <summary>
  238. /// Post app store sale
  239. /// </summary>
  240. /// <param name="request"></param>
  241. /// <returns></returns>
  242. public void Post(RegisterAppstoreSale request)
  243. {
  244. var task = _securityManager.RegisterAppStoreSale(request.Parameters);
  245. Task.WaitAll(task);
  246. }
  247. /// <summary>
  248. /// Posts the specified request.
  249. /// </summary>
  250. /// <param name="request">The request.</param>
  251. public void Post(UpdatePluginSecurityInfo request)
  252. {
  253. var info = request;
  254. _securityManager.SupporterKey = info.SupporterKey;
  255. }
  256. /// <summary>
  257. /// Posts the specified request.
  258. /// </summary>
  259. /// <param name="request">The request.</param>
  260. public void Post(UpdatePluginConfiguration request)
  261. {
  262. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  263. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  264. var id = new Guid(GetPathValue(1));
  265. var plugin = _appHost.Plugins.First(p => p.Id == id);
  266. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  267. plugin.UpdateConfiguration(configuration);
  268. }
  269. /// <summary>
  270. /// Deletes the specified request.
  271. /// </summary>
  272. /// <param name="request">The request.</param>
  273. public void Delete(UninstallPlugin request)
  274. {
  275. var guid = new Guid(request.Id);
  276. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  277. _installationManager.UninstallPlugin(plugin);
  278. }
  279. }
  280. }