PluginService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 = "Store", Description = "Store Name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  115. public string Store { get; set; }
  116. [ApiMember(Name = "Application", Description = "Application id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  117. public string Application { get; set; }
  118. [ApiMember(Name = "Product", Description = "Product id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  119. public string Product { get; set; }
  120. [ApiMember(Name = "Type", Description = "Type of product (Product or Subscription)", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  121. public string Type { get; set; }
  122. [ApiMember(Name = "StoreId", Description = "Store User Id (if needed)", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  123. public string StoreId { get; set; }
  124. [ApiMember(Name = "StoreToken", Description = "Unique ID for this purchase in the store", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  125. public string StoreToken { get; set; }
  126. [ApiMember(Name = "Feature", Description = "Emby Feature Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  127. public string Feature { get; set; }
  128. [ApiMember(Name = "Email", Description = "Email address for purchase", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  129. public string Email { get; set; }
  130. [ApiMember(Name = "Amount", Description = "String representation of price (can have currency sign)", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  131. public string Amount { get; set; }
  132. }
  133. /// <summary>
  134. /// Class PluginsService
  135. /// </summary>
  136. public class PluginService : BaseApiService
  137. {
  138. /// <summary>
  139. /// The _json serializer
  140. /// </summary>
  141. private readonly IJsonSerializer _jsonSerializer;
  142. /// <summary>
  143. /// The _app host
  144. /// </summary>
  145. private readonly IApplicationHost _appHost;
  146. private readonly ISecurityManager _securityManager;
  147. private readonly IInstallationManager _installationManager;
  148. private readonly INetworkManager _network;
  149. private readonly IDeviceManager _deviceManager;
  150. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager, INetworkManager network, IDeviceManager deviceManager)
  151. : base()
  152. {
  153. if (jsonSerializer == null)
  154. {
  155. throw new ArgumentNullException("jsonSerializer");
  156. }
  157. _appHost = appHost;
  158. _securityManager = securityManager;
  159. _installationManager = installationManager;
  160. _network = network;
  161. _deviceManager = deviceManager;
  162. _jsonSerializer = jsonSerializer;
  163. }
  164. /// <summary>
  165. /// Gets the specified request.
  166. /// </summary>
  167. /// <param name="request">The request.</param>
  168. /// <returns>System.Object.</returns>
  169. public async Task<object> Get(GetRegistrationStatus request)
  170. {
  171. var result = await _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).ConfigureAwait(false);
  172. return ToOptimizedResult(result);
  173. }
  174. public async Task<object> Get(GetRegistration request)
  175. {
  176. var result = await _securityManager.GetRegistrationStatus(request.Name).ConfigureAwait(false);
  177. var info = new RegistrationInfo
  178. {
  179. ExpirationDate = result.ExpirationDate,
  180. IsRegistered = result.IsRegistered,
  181. IsTrial = result.TrialVersion,
  182. Name = request.Name
  183. };
  184. return ToOptimizedResult(info);
  185. }
  186. /// <summary>
  187. /// Gets the specified request.
  188. /// </summary>
  189. /// <param name="request">The request.</param>
  190. /// <returns>System.Object.</returns>
  191. public async Task<object> Get(GetPlugins request)
  192. {
  193. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  194. var requireAppStoreEnabled = request.IsAppStoreEnabled.HasValue && request.IsAppStoreEnabled.Value;
  195. // Don't fail just on account of image url's
  196. try
  197. {
  198. var packages = (await _installationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None))
  199. .ToList();
  200. foreach (var plugin in result)
  201. {
  202. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  203. if (pkg != null)
  204. {
  205. plugin.ImageUrl = pkg.thumbImage;
  206. }
  207. }
  208. if (requireAppStoreEnabled)
  209. {
  210. result = result
  211. .Where(plugin =>
  212. {
  213. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  214. return pkg != null && pkg.enableInAppStore;
  215. })
  216. .ToList();
  217. }
  218. }
  219. catch
  220. {
  221. // Play it safe here
  222. if (requireAppStoreEnabled)
  223. {
  224. result = new List<PluginInfo>();
  225. }
  226. }
  227. return ToOptimizedSerializedResultUsingCache(result);
  228. }
  229. /// <summary>
  230. /// Gets the specified request.
  231. /// </summary>
  232. /// <param name="request">The request.</param>
  233. /// <returns>System.Object.</returns>
  234. public object Get(GetPluginConfiguration request)
  235. {
  236. var guid = new Guid(request.Id);
  237. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  238. var dateModified = plugin.ConfigurationDateLastModified;
  239. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  240. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  241. }
  242. /// <summary>
  243. /// Gets the specified request.
  244. /// </summary>
  245. /// <param name="request">The request.</param>
  246. /// <returns>System.Object.</returns>
  247. public object Get(GetPluginSecurityInfo request)
  248. {
  249. var result = new PluginSecurityInfo
  250. {
  251. IsMBSupporter = _securityManager.IsMBSupporter,
  252. SupporterKey = _securityManager.SupporterKey
  253. };
  254. return ToOptimizedSerializedResultUsingCache(result);
  255. }
  256. /// <summary>
  257. /// Post app store sale
  258. /// </summary>
  259. /// <param name="request"></param>
  260. /// <returns></returns>
  261. public async Task Post(RegisterAppstoreSale request)
  262. {
  263. await _securityManager.RegisterAppStoreSale(request.Store, request.Application, request.Product, request.Feature, request.Type, request.StoreId, request.StoreToken, request.Email, request.Amount);
  264. }
  265. /// <summary>
  266. /// Posts the specified request.
  267. /// </summary>
  268. /// <param name="request">The request.</param>
  269. public void Post(UpdatePluginSecurityInfo request)
  270. {
  271. var info = request;
  272. _securityManager.SupporterKey = info.SupporterKey;
  273. }
  274. /// <summary>
  275. /// Posts the specified request.
  276. /// </summary>
  277. /// <param name="request">The request.</param>
  278. public void Post(UpdatePluginConfiguration request)
  279. {
  280. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  281. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  282. var id = new Guid(GetPathValue(1));
  283. var plugin = _appHost.Plugins.First(p => p.Id == id);
  284. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  285. plugin.UpdateConfiguration(configuration);
  286. }
  287. /// <summary>
  288. /// Deletes the specified request.
  289. /// </summary>
  290. /// <param name="request">The request.</param>
  291. public void Delete(UninstallPlugin request)
  292. {
  293. var guid = new Guid(request.Id);
  294. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  295. _installationManager.UninstallPlugin(plugin);
  296. }
  297. }
  298. }