PluginService.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.Security;
  4. using MediaBrowser.Controller.Updates;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Plugins;
  7. using MediaBrowser.Model.Serialization;
  8. using ServiceStack.ServiceHost;
  9. using ServiceStack.Text.Controller;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. namespace MediaBrowser.Api
  15. {
  16. /// <summary>
  17. /// Class Plugins
  18. /// </summary>
  19. [Route("/Plugins", "GET")]
  20. [Api(("Gets a list of currently installed plugins"))]
  21. public class GetPlugins : IReturn<List<PluginInfo>>
  22. {
  23. }
  24. /// <summary>
  25. /// Class UninstallPlugin
  26. /// </summary>
  27. [Route("/Plugins/{Id}", "DELETE")]
  28. [Api(("Uninstalls a plugin"))]
  29. public class UninstallPlugin : IReturnVoid
  30. {
  31. /// <summary>
  32. /// Gets or sets the id.
  33. /// </summary>
  34. /// <value>The id.</value>
  35. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  36. public Guid Id { get; set; }
  37. }
  38. /// <summary>
  39. /// Class GetPluginConfiguration
  40. /// </summary>
  41. [Route("/Plugins/{Id}/Configuration", "GET")]
  42. [Api(("Gets a plugin's configuration"))]
  43. public class GetPluginConfiguration
  44. {
  45. /// <summary>
  46. /// Gets or sets the id.
  47. /// </summary>
  48. /// <value>The id.</value>
  49. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  50. public Guid Id { get; set; }
  51. }
  52. /// <summary>
  53. /// Class UpdatePluginConfiguration
  54. /// </summary>
  55. [Route("/Plugins/{Id}/Configuration", "POST")]
  56. [Api(("Updates a plugin's configuration"))]
  57. public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
  58. {
  59. /// <summary>
  60. /// Gets or sets the id.
  61. /// </summary>
  62. /// <value>The id.</value>
  63. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  64. public Guid Id { get; set; }
  65. /// <summary>
  66. /// The raw Http Request Input Stream
  67. /// </summary>
  68. /// <value>The request stream.</value>
  69. public Stream RequestStream { get; set; }
  70. }
  71. /// <summary>
  72. /// Class GetPluginSecurityInfo
  73. /// </summary>
  74. [Route("/Plugins/SecurityInfo", "GET")]
  75. [Api(("Gets plugin registration information"))]
  76. [Restrict(VisibilityTo = EndpointAttributes.None)]
  77. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  78. {
  79. }
  80. /// <summary>
  81. /// Class UpdatePluginSecurityInfo
  82. /// </summary>
  83. [Route("/Plugins/SecurityInfo", "POST")]
  84. [Api("Updates plugin registration information")]
  85. [Restrict(VisibilityTo = EndpointAttributes.None)]
  86. public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
  87. {
  88. }
  89. [Route("/Plugins/RegistrationRecords/{Name}", "GET")]
  90. [Api("Gets registration status for a feature")]
  91. [Restrict(VisibilityTo = EndpointAttributes.None)]
  92. public class GetRegistrationStatus
  93. {
  94. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  95. public string Name { get; set; }
  96. [ApiMember(Name = "Mb2Equivalent", Description = "Optional. The equivalent feature name in MB2", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  97. public string Mb2Equivalent { get; set; }
  98. }
  99. /// <summary>
  100. /// Class PluginsService
  101. /// </summary>
  102. public class PluginService : BaseApiService
  103. {
  104. /// <summary>
  105. /// The _json serializer
  106. /// </summary>
  107. private readonly IJsonSerializer _jsonSerializer;
  108. /// <summary>
  109. /// The _app host
  110. /// </summary>
  111. private readonly IApplicationHost _appHost;
  112. private readonly ISecurityManager _securityManager;
  113. private readonly IInstallationManager _installationManager;
  114. /// <summary>
  115. /// Initializes a new instance of the <see cref="PluginService" /> class.
  116. /// </summary>
  117. /// <param name="jsonSerializer">The json serializer.</param>
  118. /// <param name="appHost">The app host.</param>
  119. /// <param name="securityManager">The security manager.</param>
  120. /// <param name="installationManager">The installation manager.</param>
  121. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  122. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager)
  123. : base()
  124. {
  125. if (jsonSerializer == null)
  126. {
  127. throw new ArgumentNullException("jsonSerializer");
  128. }
  129. _appHost = appHost;
  130. _securityManager = securityManager;
  131. _installationManager = installationManager;
  132. _jsonSerializer = jsonSerializer;
  133. }
  134. /// <summary>
  135. /// Gets the specified request.
  136. /// </summary>
  137. /// <param name="request">The request.</param>
  138. /// <returns>System.Object.</returns>
  139. public object Get(GetRegistrationStatus request)
  140. {
  141. var result = _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).Result;
  142. return ToOptimizedResult(result);
  143. }
  144. /// <summary>
  145. /// Gets the specified request.
  146. /// </summary>
  147. /// <param name="request">The request.</param>
  148. /// <returns>System.Object.</returns>
  149. public object Get(GetPlugins request)
  150. {
  151. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  152. return ToOptimizedResult(result);
  153. }
  154. /// <summary>
  155. /// Gets the specified request.
  156. /// </summary>
  157. /// <param name="request">The request.</param>
  158. /// <returns>System.Object.</returns>
  159. public object Get(GetPluginConfiguration request)
  160. {
  161. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  162. var dateModified = plugin.ConfigurationDateLastModified;
  163. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  164. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  165. }
  166. /// <summary>
  167. /// Gets the specified request.
  168. /// </summary>
  169. /// <param name="request">The request.</param>
  170. /// <returns>System.Object.</returns>
  171. public object Get(GetPluginSecurityInfo request)
  172. {
  173. var result = new PluginSecurityInfo
  174. {
  175. IsMBSupporter = _securityManager.IsMBSupporter,
  176. SupporterKey = _securityManager.SupporterKey,
  177. LegacyKey = _securityManager.LegacyKey
  178. };
  179. return ToOptimizedResult(result);
  180. }
  181. /// <summary>
  182. /// Posts the specified request.
  183. /// </summary>
  184. /// <param name="request">The request.</param>
  185. public void Post(UpdatePluginSecurityInfo request)
  186. {
  187. var info = request;
  188. _securityManager.SupporterKey = info.SupporterKey;
  189. _securityManager.LegacyKey = info.LegacyKey;
  190. }
  191. /// <summary>
  192. /// Posts the specified request.
  193. /// </summary>
  194. /// <param name="request">The request.</param>
  195. public void Post(UpdatePluginConfiguration request)
  196. {
  197. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  198. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  199. var pathInfo = PathInfo.Parse(RequestContext.PathInfo);
  200. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  201. var plugin = _appHost.Plugins.First(p => p.Id == id);
  202. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  203. plugin.UpdateConfiguration(configuration);
  204. }
  205. /// <summary>
  206. /// Deletes the specified request.
  207. /// </summary>
  208. /// <param name="request">The request.</param>
  209. public void Delete(UninstallPlugin request)
  210. {
  211. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  212. _installationManager.UninstallPlugin(plugin);
  213. }
  214. }
  215. }