PluginService.cs 8.5 KB

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