PluginService.cs 11 KB

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