PluginService.cs 9.4 KB

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