PluginService.cs 8.4 KB

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