PluginService.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. [Restrict(VisibleLocalhostOnly = true)]
  92. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  93. {
  94. }
  95. /// <summary>
  96. /// Class UpdatePluginSecurityInfo
  97. /// </summary>
  98. [Route("/Plugins/SecurityInfo", "GET")]
  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. /// Initializes a new instance of the <see cref="PluginService" /> class.
  118. /// </summary>
  119. /// <param name="jsonSerializer">The json serializer.</param>
  120. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  121. public PluginService(IJsonSerializer jsonSerializer)
  122. : base()
  123. {
  124. if (jsonSerializer == null)
  125. {
  126. throw new ArgumentNullException("jsonSerializer");
  127. }
  128. _jsonSerializer = jsonSerializer;
  129. }
  130. /// <summary>
  131. /// Gets the specified request.
  132. /// </summary>
  133. /// <param name="request">The request.</param>
  134. /// <returns>System.Object.</returns>
  135. public object Get(GetPlugins request)
  136. {
  137. var result = Kernel.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  138. return ToOptimizedResult(result);
  139. }
  140. /// <summary>
  141. /// Gets the specified request.
  142. /// </summary>
  143. /// <param name="request">The request.</param>
  144. /// <returns>System.Object.</returns>
  145. public object Get(GetPluginAssembly request)
  146. {
  147. var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
  148. return ToStaticFileResult(plugin.AssemblyFilePath);
  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(GetPluginConfiguration request)
  156. {
  157. var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
  158. var dateModified = plugin.ConfigurationDateLastModified;
  159. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  160. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  161. }
  162. /// <summary>
  163. /// Gets the specified request.
  164. /// </summary>
  165. /// <param name="request">The request.</param>
  166. /// <returns>System.Object.</returns>
  167. public object Get(GetPluginConfigurationFile request)
  168. {
  169. var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
  170. return ToStaticFileResult(plugin.ConfigurationFilePath);
  171. }
  172. /// <summary>
  173. /// Gets the specified request.
  174. /// </summary>
  175. /// <param name="request">The request.</param>
  176. /// <returns>System.Object.</returns>
  177. public object Get(GetPluginSecurityInfo request)
  178. {
  179. var kernel = (Kernel)Kernel;
  180. var result = new PluginSecurityInfo
  181. {
  182. IsMBSupporter = kernel.SecurityManager.IsMBSupporter,
  183. SupporterKey = kernel.SecurityManager.SupporterKey,
  184. LegacyKey = kernel.SecurityManager.LegacyKey
  185. };
  186. return ToOptimizedResult(result);
  187. }
  188. /// <summary>
  189. /// Posts the specified request.
  190. /// </summary>
  191. /// <param name="request">The request.</param>
  192. public void Post(UpdatePluginSecurityInfo request)
  193. {
  194. var kernel = (Kernel)Kernel;
  195. var info = _jsonSerializer.DeserializeFromStream<PluginSecurityInfo>(request.RequestStream);
  196. kernel.SecurityManager.SupporterKey = info.SupporterKey;
  197. kernel.SecurityManager.LegacyKey = info.LegacyKey;
  198. }
  199. /// <summary>
  200. /// Posts the specified request.
  201. /// </summary>
  202. /// <param name="request">The request.</param>
  203. public void Post(UpdatePluginConfiguration request)
  204. {
  205. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  206. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  207. var pathInfo = PathInfo.Parse(Request.PathInfo);
  208. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  209. var plugin = Kernel.Plugins.First(p => p.Id == id);
  210. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  211. plugin.UpdateConfiguration(configuration);
  212. }
  213. /// <summary>
  214. /// Deletes the specified request.
  215. /// </summary>
  216. /// <param name="request">The request.</param>
  217. public void Delete(UninstallPlugin request)
  218. {
  219. var kernel = (Kernel)Kernel;
  220. var plugin = kernel.Plugins.First(p => p.Id == request.Id);
  221. kernel.InstallationManager.UninstallPlugin(plugin);
  222. }
  223. }
  224. }