PluginService.cs 7.9 KB

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