PluginService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. [Authenticated]
  23. public class GetPlugins : IReturn<List<PluginInfo>>
  24. {
  25. }
  26. /// <summary>
  27. /// Class UninstallPlugin
  28. /// </summary>
  29. [Route("/Plugins/{Id}", "DELETE", Summary = "Uninstalls a plugin")]
  30. [Authenticated(Roles = "Admin")]
  31. public class UninstallPlugin : IReturnVoid
  32. {
  33. /// <summary>
  34. /// Gets or sets the id.
  35. /// </summary>
  36. /// <value>The id.</value>
  37. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  38. public Guid Id { get; set; }
  39. }
  40. /// <summary>
  41. /// Class GetPluginConfiguration
  42. /// </summary>
  43. [Route("/Plugins/{Id}/Configuration", "GET", Summary = "Gets a plugin's configuration")]
  44. [Authenticated]
  45. public class GetPluginConfiguration
  46. {
  47. /// <summary>
  48. /// Gets or sets the id.
  49. /// </summary>
  50. /// <value>The id.</value>
  51. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  52. public Guid Id { get; set; }
  53. }
  54. /// <summary>
  55. /// Class UpdatePluginConfiguration
  56. /// </summary>
  57. [Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")]
  58. [Authenticated]
  59. public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
  60. {
  61. /// <summary>
  62. /// Gets or sets the id.
  63. /// </summary>
  64. /// <value>The id.</value>
  65. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  66. public Guid Id { get; set; }
  67. /// <summary>
  68. /// The raw Http Request Input Stream
  69. /// </summary>
  70. /// <value>The request stream.</value>
  71. public Stream RequestStream { get; set; }
  72. }
  73. /// <summary>
  74. /// Class GetPluginSecurityInfo
  75. /// </summary>
  76. [Route("/Plugins/SecurityInfo", "GET", Summary = "Gets plugin registration information")]
  77. [Authenticated]
  78. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  79. {
  80. }
  81. /// <summary>
  82. /// Class UpdatePluginSecurityInfo
  83. /// </summary>
  84. [Route("/Plugins/SecurityInfo", "POST", Summary = "Updates plugin registration information")]
  85. [Authenticated(Roles = "Admin")]
  86. public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
  87. {
  88. }
  89. [Route("/Plugins/RegistrationRecords/{Name}", "GET", Summary = "Gets registration status for a feature")]
  90. [Authenticated]
  91. public class GetRegistrationStatus
  92. {
  93. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  94. public string Name { get; set; }
  95. [ApiMember(Name = "Mb2Equivalent", Description = "Optional. The equivalent feature name in MB2", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  96. public string Mb2Equivalent { get; set; }
  97. }
  98. /// <summary>
  99. /// Class PluginsService
  100. /// </summary>
  101. public class PluginService : BaseApiService
  102. {
  103. /// <summary>
  104. /// The _json serializer
  105. /// </summary>
  106. private readonly IJsonSerializer _jsonSerializer;
  107. /// <summary>
  108. /// The _app host
  109. /// </summary>
  110. private readonly IApplicationHost _appHost;
  111. private readonly ISecurityManager _securityManager;
  112. private readonly IInstallationManager _installationManager;
  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. };
  169. return ToOptimizedSerializedResultUsingCache(result);
  170. }
  171. /// <summary>
  172. /// Posts the specified request.
  173. /// </summary>
  174. /// <param name="request">The request.</param>
  175. public void Post(UpdatePluginSecurityInfo request)
  176. {
  177. var info = request;
  178. _securityManager.SupporterKey = info.SupporterKey;
  179. }
  180. /// <summary>
  181. /// Posts the specified request.
  182. /// </summary>
  183. /// <param name="request">The request.</param>
  184. public void Post(UpdatePluginConfiguration request)
  185. {
  186. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  187. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  188. var pathInfo = PathInfo.Parse(Request.PathInfo);
  189. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  190. var plugin = _appHost.Plugins.First(p => p.Id == id);
  191. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  192. plugin.UpdateConfiguration(configuration);
  193. }
  194. /// <summary>
  195. /// Deletes the specified request.
  196. /// </summary>
  197. /// <param name="request">The request.</param>
  198. public void Delete(UninstallPlugin request)
  199. {
  200. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  201. _installationManager.UninstallPlugin(plugin);
  202. }
  203. }
  204. }