PluginService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 : PluginSecurityInfo, IReturnVoid
  101. {
  102. }
  103. /// <summary>
  104. /// Class PluginsService
  105. /// </summary>
  106. public class PluginService : BaseRestService
  107. {
  108. /// <summary>
  109. /// The _json serializer
  110. /// </summary>
  111. private readonly IJsonSerializer _jsonSerializer;
  112. /// <summary>
  113. /// The _app host
  114. /// </summary>
  115. private readonly IApplicationHost _appHost;
  116. private readonly ISecurityManager _securityManager;
  117. /// <summary>
  118. /// Initializes a new instance of the <see cref="PluginService" /> class.
  119. /// </summary>
  120. /// <param name="jsonSerializer">The json serializer.</param>
  121. /// <param name="appHost">The app host.</param>
  122. /// <param name="securityManager">The security manager.</param>
  123. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  124. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager)
  125. : base()
  126. {
  127. if (jsonSerializer == null)
  128. {
  129. throw new ArgumentNullException("jsonSerializer");
  130. }
  131. _appHost = appHost;
  132. _securityManager = securityManager;
  133. _jsonSerializer = jsonSerializer;
  134. }
  135. /// <summary>
  136. /// Gets the specified request.
  137. /// </summary>
  138. /// <param name="request">The request.</param>
  139. /// <returns>System.Object.</returns>
  140. public object Get(GetPlugins request)
  141. {
  142. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  143. return ToOptimizedResult(result);
  144. }
  145. /// <summary>
  146. /// Gets the specified request.
  147. /// </summary>
  148. /// <param name="request">The request.</param>
  149. /// <returns>System.Object.</returns>
  150. public object Get(GetPluginAssembly request)
  151. {
  152. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  153. return ToStaticFileResult(plugin.AssemblyFilePath);
  154. }
  155. /// <summary>
  156. /// Gets the specified request.
  157. /// </summary>
  158. /// <param name="request">The request.</param>
  159. /// <returns>System.Object.</returns>
  160. public object Get(GetPluginConfiguration request)
  161. {
  162. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  163. var dateModified = plugin.ConfigurationDateLastModified;
  164. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  165. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  166. }
  167. /// <summary>
  168. /// Gets the specified request.
  169. /// </summary>
  170. /// <param name="request">The request.</param>
  171. /// <returns>System.Object.</returns>
  172. public object Get(GetPluginConfigurationFile request)
  173. {
  174. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  175. return ToStaticFileResult(plugin.ConfigurationFilePath);
  176. }
  177. /// <summary>
  178. /// Gets the specified request.
  179. /// </summary>
  180. /// <param name="request">The request.</param>
  181. /// <returns>System.Object.</returns>
  182. public object Get(GetPluginSecurityInfo request)
  183. {
  184. var result = new PluginSecurityInfo
  185. {
  186. IsMBSupporter = _securityManager.IsMBSupporter,
  187. SupporterKey = _securityManager.SupporterKey,
  188. LegacyKey = _securityManager.LegacyKey
  189. };
  190. return ToOptimizedResult(result);
  191. }
  192. /// <summary>
  193. /// Posts the specified request.
  194. /// </summary>
  195. /// <param name="request">The request.</param>
  196. public void Post(UpdatePluginSecurityInfo request)
  197. {
  198. var info = request;
  199. _securityManager.SupporterKey = info.SupporterKey;
  200. _securityManager.LegacyKey = info.LegacyKey;
  201. }
  202. /// <summary>
  203. /// Posts the specified request.
  204. /// </summary>
  205. /// <param name="request">The request.</param>
  206. public void Post(UpdatePluginConfiguration request)
  207. {
  208. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  209. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  210. var pathInfo = PathInfo.Parse(Request.PathInfo);
  211. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  212. var plugin = _appHost.Plugins.First(p => p.Id == id);
  213. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  214. plugin.UpdateConfiguration(configuration);
  215. }
  216. /// <summary>
  217. /// Deletes the specified request.
  218. /// </summary>
  219. /// <param name="request">The request.</param>
  220. public void Delete(UninstallPlugin request)
  221. {
  222. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  223. Kernel.Instance.InstallationManager.UninstallPlugin(plugin);
  224. }
  225. }
  226. }