PluginService.cs 7.2 KB

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