PluginService.cs 8.3 KB

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