PluginService.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.Security;
  4. using MediaBrowser.Common.Updates;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Plugins;
  8. using MediaBrowser.Model.Serialization;
  9. using ServiceStack;
  10. using ServiceStack.Text.Controller;
  11. using ServiceStack.Web;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. namespace MediaBrowser.Api
  17. {
  18. /// <summary>
  19. /// Class Plugins
  20. /// </summary>
  21. [Route("/Plugins", "GET", Summary = "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", Summary = "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", Summary = "Gets a plugin's configuration")]
  42. public class GetPluginConfiguration
  43. {
  44. /// <summary>
  45. /// Gets or sets the id.
  46. /// </summary>
  47. /// <value>The id.</value>
  48. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  49. public Guid Id { get; set; }
  50. }
  51. /// <summary>
  52. /// Class UpdatePluginConfiguration
  53. /// </summary>
  54. [Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")]
  55. public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
  56. {
  57. /// <summary>
  58. /// Gets or sets the id.
  59. /// </summary>
  60. /// <value>The id.</value>
  61. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  62. public Guid Id { get; set; }
  63. /// <summary>
  64. /// The raw Http Request Input Stream
  65. /// </summary>
  66. /// <value>The request stream.</value>
  67. public Stream RequestStream { get; set; }
  68. }
  69. /// <summary>
  70. /// Class GetPluginSecurityInfo
  71. /// </summary>
  72. [Route("/Plugins/SecurityInfo", "GET", Summary = "Gets plugin registration information")]
  73. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  74. {
  75. }
  76. /// <summary>
  77. /// Class UpdatePluginSecurityInfo
  78. /// </summary>
  79. [Route("/Plugins/SecurityInfo", "POST", Summary = "Updates plugin registration information")]
  80. public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
  81. {
  82. }
  83. [Route("/Plugins/RegistrationRecords/{Name}", "GET", Summary = "Gets registration status for a feature")]
  84. public class GetRegistrationStatus
  85. {
  86. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  87. public string Name { get; set; }
  88. [ApiMember(Name = "Mb2Equivalent", Description = "Optional. The equivalent feature name in MB2", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  89. public string Mb2Equivalent { get; set; }
  90. }
  91. /// <summary>
  92. /// Class PluginsService
  93. /// </summary>
  94. [Authenticated]
  95. public class PluginService : BaseApiService
  96. {
  97. /// <summary>
  98. /// The _json serializer
  99. /// </summary>
  100. private readonly IJsonSerializer _jsonSerializer;
  101. /// <summary>
  102. /// The _app host
  103. /// </summary>
  104. private readonly IApplicationHost _appHost;
  105. private readonly ISecurityManager _securityManager;
  106. private readonly IInstallationManager _installationManager;
  107. /// <summary>
  108. /// Initializes a new instance of the <see cref="PluginService" /> class.
  109. /// </summary>
  110. /// <param name="jsonSerializer">The json serializer.</param>
  111. /// <param name="appHost">The app host.</param>
  112. /// <param name="securityManager">The security manager.</param>
  113. /// <param name="installationManager">The installation manager.</param>
  114. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  115. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager)
  116. : base()
  117. {
  118. if (jsonSerializer == null)
  119. {
  120. throw new ArgumentNullException("jsonSerializer");
  121. }
  122. _appHost = appHost;
  123. _securityManager = securityManager;
  124. _installationManager = installationManager;
  125. _jsonSerializer = jsonSerializer;
  126. }
  127. /// <summary>
  128. /// Gets the specified request.
  129. /// </summary>
  130. /// <param name="request">The request.</param>
  131. /// <returns>System.Object.</returns>
  132. public object Get(GetRegistrationStatus request)
  133. {
  134. var result = _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).Result;
  135. return ToOptimizedResult(result);
  136. }
  137. /// <summary>
  138. /// Gets the specified request.
  139. /// </summary>
  140. /// <param name="request">The request.</param>
  141. /// <returns>System.Object.</returns>
  142. public object Get(GetPlugins request)
  143. {
  144. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  145. return ToOptimizedSerializedResultUsingCache(result);
  146. }
  147. /// <summary>
  148. /// Gets the specified request.
  149. /// </summary>
  150. /// <param name="request">The request.</param>
  151. /// <returns>System.Object.</returns>
  152. public object Get(GetPluginConfiguration request)
  153. {
  154. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  155. var dateModified = plugin.ConfigurationDateLastModified;
  156. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  157. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  158. }
  159. /// <summary>
  160. /// Gets the specified request.
  161. /// </summary>
  162. /// <param name="request">The request.</param>
  163. /// <returns>System.Object.</returns>
  164. public object Get(GetPluginSecurityInfo request)
  165. {
  166. var result = new PluginSecurityInfo
  167. {
  168. IsMBSupporter = _securityManager.IsMBSupporter,
  169. SupporterKey = _securityManager.SupporterKey
  170. };
  171. return ToOptimizedSerializedResultUsingCache(result);
  172. }
  173. /// <summary>
  174. /// Posts the specified request.
  175. /// </summary>
  176. /// <param name="request">The request.</param>
  177. public void Post(UpdatePluginSecurityInfo request)
  178. {
  179. var info = request;
  180. _securityManager.SupporterKey = info.SupporterKey;
  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. }