PluginService.cs 8.2 KB

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