PluginService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.Serialization;
  10. using System;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using MediaBrowser.Model.Services;
  16. using MediaBrowser.Common.Plugins;
  17. using Microsoft.Extensions.Logging;
  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. }
  99. [Route("/Registrations/{Name}", "GET", Summary = "Gets registration status for a feature", IsHidden = true)]
  100. [Authenticated]
  101. public class GetRegistration : IReturn<RegistrationInfo>
  102. {
  103. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  104. public string Name { get; set; }
  105. }
  106. public class RegistrationInfo
  107. {
  108. public string Name { get; set; }
  109. public DateTime ExpirationDate { get; set; }
  110. public bool IsTrial { get; set; }
  111. public bool IsRegistered { get; set; }
  112. }
  113. [Route("/Appstore/Register", "POST", Summary = "Registers an appstore sale", IsHidden = true)]
  114. [Authenticated]
  115. public class RegisterAppstoreSale
  116. {
  117. [ApiMember(Name = "Parameters", Description = "Java representation of parameters to pass through to admin server", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  118. public string Parameters { get; set; }
  119. }
  120. /// <summary>
  121. /// Class PluginsService
  122. /// </summary>
  123. public class PluginService : BaseApiService
  124. {
  125. /// <summary>
  126. /// The _json serializer
  127. /// </summary>
  128. private readonly IJsonSerializer _jsonSerializer;
  129. /// <summary>
  130. /// The _app host
  131. /// </summary>
  132. private readonly IApplicationHost _appHost;
  133. private readonly ISecurityManager _securityManager;
  134. private readonly IInstallationManager _installationManager;
  135. private readonly INetworkManager _network;
  136. private readonly IDeviceManager _deviceManager;
  137. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager, INetworkManager network, IDeviceManager deviceManager)
  138. : base()
  139. {
  140. if (jsonSerializer == null)
  141. {
  142. throw new ArgumentNullException("jsonSerializer");
  143. }
  144. _appHost = appHost;
  145. _securityManager = securityManager;
  146. _installationManager = installationManager;
  147. _network = network;
  148. _deviceManager = deviceManager;
  149. _jsonSerializer = jsonSerializer;
  150. }
  151. /// <summary>
  152. /// Gets the specified request.
  153. /// </summary>
  154. /// <param name="request">The request.</param>
  155. /// <returns>System.Object.</returns>
  156. public async Task<object> Get(GetRegistrationStatus request)
  157. {
  158. var result = await _securityManager.GetRegistrationStatus(request.Name).ConfigureAwait(false);
  159. return ToOptimizedResult(result);
  160. }
  161. public async Task<object> Get(GetRegistration request)
  162. {
  163. var result = await _securityManager.GetRegistrationStatus(request.Name).ConfigureAwait(false);
  164. var info = new RegistrationInfo
  165. {
  166. ExpirationDate = result.ExpirationDate,
  167. IsRegistered = result.IsRegistered,
  168. IsTrial = result.TrialVersion,
  169. Name = request.Name
  170. };
  171. return ToOptimizedResult(info);
  172. }
  173. /// <summary>
  174. /// Gets the specified request.
  175. /// </summary>
  176. /// <param name="request">The request.</param>
  177. /// <returns>System.Object.</returns>
  178. public async Task<object> Get(GetPlugins request)
  179. {
  180. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToArray();
  181. var requireAppStoreEnabled = request.IsAppStoreEnabled.HasValue && request.IsAppStoreEnabled.Value;
  182. // Don't fail just on account of image url's
  183. try
  184. {
  185. var packages = (await _installationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None));
  186. foreach (var plugin in result)
  187. {
  188. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && string.Equals(i.guid.Replace("-", string.Empty), plugin.Id.Replace("-", string.Empty), StringComparison.OrdinalIgnoreCase));
  189. if (pkg != null)
  190. {
  191. plugin.ImageUrl = pkg.thumbImage;
  192. }
  193. }
  194. if (requireAppStoreEnabled)
  195. {
  196. result = result
  197. .Where(plugin =>
  198. {
  199. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  200. return pkg != null && pkg.enableInAppStore;
  201. })
  202. .ToArray();
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. Logger.LogError(ex, "Error getting plugin list");
  208. // Play it safe here
  209. if (requireAppStoreEnabled)
  210. {
  211. result = new PluginInfo[] { };
  212. }
  213. }
  214. return ToOptimizedResult(result);
  215. }
  216. /// <summary>
  217. /// Gets the specified request.
  218. /// </summary>
  219. /// <param name="request">The request.</param>
  220. /// <returns>System.Object.</returns>
  221. public object Get(GetPluginConfiguration request)
  222. {
  223. var guid = new Guid(request.Id);
  224. var plugin = _appHost.Plugins.First(p => p.Id == guid) as IHasPluginConfiguration;
  225. return ToOptimizedResult(plugin.Configuration);
  226. }
  227. /// <summary>
  228. /// Gets the specified request.
  229. /// </summary>
  230. /// <param name="request">The request.</param>
  231. /// <returns>System.Object.</returns>
  232. public async Task<object> Get(GetPluginSecurityInfo request)
  233. {
  234. var result = new PluginSecurityInfo
  235. {
  236. IsMBSupporter = await _securityManager.IsSupporter().ConfigureAwait(false),
  237. SupporterKey = _securityManager.SupporterKey
  238. };
  239. return ToOptimizedResult(result);
  240. }
  241. /// <summary>
  242. /// Post app store sale
  243. /// </summary>
  244. /// <param name="request"></param>
  245. /// <returns></returns>
  246. public Task Post(RegisterAppstoreSale request)
  247. {
  248. return _securityManager.RegisterAppStoreSale(request.Parameters);
  249. }
  250. /// <summary>
  251. /// Posts the specified request.
  252. /// </summary>
  253. /// <param name="request">The request.</param>
  254. public Task Post(UpdatePluginSecurityInfo request)
  255. {
  256. return _securityManager.UpdateSupporterKey(request.SupporterKey);
  257. }
  258. /// <summary>
  259. /// Posts the specified request.
  260. /// </summary>
  261. /// <param name="request">The request.</param>
  262. public async Task Post(UpdatePluginConfiguration request)
  263. {
  264. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  265. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  266. var id = new Guid(GetPathValue(1));
  267. var plugin = _appHost.Plugins.First(p => p.Id == id) as IHasPluginConfiguration;
  268. if (plugin == null)
  269. {
  270. throw new FileNotFoundException();
  271. }
  272. var configuration = (await _jsonSerializer.DeserializeFromStreamAsync(request.RequestStream, plugin.ConfigurationType).ConfigureAwait(false)) as BasePluginConfiguration;
  273. plugin.UpdateConfiguration(configuration);
  274. }
  275. /// <summary>
  276. /// Deletes the specified request.
  277. /// </summary>
  278. /// <param name="request">The request.</param>
  279. public void Delete(UninstallPlugin request)
  280. {
  281. var guid = new Guid(request.Id);
  282. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  283. _installationManager.UninstallPlugin(plugin);
  284. }
  285. }
  286. }