PluginService.cs 11 KB

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