PluginService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Common.Serialization;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Plugins;
  7. using ServiceStack.ServiceHost;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel.Composition;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. using ServiceStack.Text.Controller;
  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. [Restrict(VisibleLocalhostOnly = true)]
  94. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  95. {
  96. }
  97. /// <summary>
  98. /// Class UpdatePluginSecurityInfo
  99. /// </summary>
  100. [Route("/Plugins/SecurityInfo", "GET")]
  101. public class UpdatePluginSecurityInfo : IReturnVoid, IRequiresRequestStream
  102. {
  103. /// <summary>
  104. /// The raw Http Request Input Stream
  105. /// </summary>
  106. /// <value>The request stream.</value>
  107. public Stream RequestStream { get; set; }
  108. }
  109. /// <summary>
  110. /// Class PluginsService
  111. /// </summary>
  112. [Export(typeof(IRestfulService))]
  113. public class PluginService : BaseRestService
  114. {
  115. /// <summary>
  116. /// Gets the specified request.
  117. /// </summary>
  118. /// <param name="request">The request.</param>
  119. /// <returns>System.Object.</returns>
  120. public object Get(GetPlugins request)
  121. {
  122. var result = Kernel.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  123. return ToOptimizedResult(result);
  124. }
  125. /// <summary>
  126. /// Gets the specified request.
  127. /// </summary>
  128. /// <param name="request">The request.</param>
  129. /// <returns>System.Object.</returns>
  130. public object Get(GetPluginAssembly request)
  131. {
  132. var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
  133. return ToStaticFileResult(plugin.AssemblyFilePath);
  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(GetPluginConfiguration request)
  141. {
  142. var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
  143. var dateModified = plugin.ConfigurationDateLastModified;
  144. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  145. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  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(GetPluginConfigurationFile request)
  153. {
  154. var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
  155. return ToStaticFileResult(plugin.ConfigurationFilePath);
  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(GetPluginSecurityInfo request)
  163. {
  164. var kernel = (Kernel)Kernel;
  165. var result = new PluginSecurityInfo
  166. {
  167. IsMBSupporter = kernel.PluginSecurityManager.IsMBSupporter,
  168. SupporterKey = kernel.PluginSecurityManager.SupporterKey,
  169. LegacyKey = kernel.PluginSecurityManager.LegacyKey
  170. };
  171. return ToOptimizedResult(result);
  172. }
  173. /// <summary>
  174. /// Posts the specified request.
  175. /// </summary>
  176. /// <param name="request">The request.</param>
  177. public void Post(UpdatePluginSecurityInfo request)
  178. {
  179. var kernel = (Kernel)Kernel;
  180. var info = JsonSerializer.DeserializeFromStream<PluginSecurityInfo>(request.RequestStream);
  181. kernel.PluginSecurityManager.SupporterKey = info.SupporterKey;
  182. kernel.PluginSecurityManager.LegacyKey = info.LegacyKey;
  183. }
  184. /// <summary>
  185. /// Posts the specified request.
  186. /// </summary>
  187. /// <param name="request">The request.</param>
  188. public void Post(UpdatePluginConfiguration request)
  189. {
  190. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  191. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  192. var pathInfo = PathInfo.Parse(Request.PathInfo);
  193. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  194. var plugin = Kernel.Plugins.First(p => p.Id == id);
  195. var configuration = JsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  196. plugin.UpdateConfiguration(configuration);
  197. }
  198. /// <summary>
  199. /// Deletes the specified request.
  200. /// </summary>
  201. /// <param name="request">The request.</param>
  202. public void Delete(UninstallPlugin request)
  203. {
  204. var kernel = (Kernel)Kernel;
  205. var plugin = kernel.Plugins.First(p => p.Id == request.Id);
  206. kernel.InstallationManager.UninstallPlugin(plugin);
  207. }
  208. }
  209. }