PluginService.cs 11 KB

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