PluginService.cs 9.4 KB

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