2
0

PluginService.cs 8.2 KB

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