PluginService.cs 8.2 KB

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