PluginService.cs 10 KB

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