PluginService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. [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. /// <param name="installationManager">The installation manager.</param>
  149. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  150. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager)
  151. : base()
  152. {
  153. if (jsonSerializer == null)
  154. {
  155. throw new ArgumentNullException("jsonSerializer");
  156. }
  157. _appHost = appHost;
  158. _securityManager = securityManager;
  159. _installationManager = installationManager;
  160. _jsonSerializer = jsonSerializer;
  161. }
  162. /// <summary>
  163. /// Gets the specified request.
  164. /// </summary>
  165. /// <param name="request">The request.</param>
  166. /// <returns>System.Object.</returns>
  167. public object Get(GetRegistrationStatus request)
  168. {
  169. var result = _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).Result;
  170. return ToOptimizedResult(result);
  171. }
  172. /// <summary>
  173. /// Gets the specified request.
  174. /// </summary>
  175. /// <param name="request">The request.</param>
  176. /// <returns>System.Object.</returns>
  177. public object Get(GetPlugins request)
  178. {
  179. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  180. return ToOptimizedResult(result);
  181. }
  182. /// <summary>
  183. /// Gets the specified request.
  184. /// </summary>
  185. /// <param name="request">The request.</param>
  186. /// <returns>System.Object.</returns>
  187. public object Get(GetPluginAssembly request)
  188. {
  189. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  190. return ResultFactory.GetStaticFileResult(RequestContext, plugin.AssemblyFilePath);
  191. }
  192. /// <summary>
  193. /// Gets the specified request.
  194. /// </summary>
  195. /// <param name="request">The request.</param>
  196. /// <returns>System.Object.</returns>
  197. public object Get(GetPluginConfiguration request)
  198. {
  199. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  200. var dateModified = plugin.ConfigurationDateLastModified;
  201. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  202. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  203. }
  204. /// <summary>
  205. /// Gets the specified request.
  206. /// </summary>
  207. /// <param name="request">The request.</param>
  208. /// <returns>System.Object.</returns>
  209. public object Get(GetPluginConfigurationFile request)
  210. {
  211. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  212. return ResultFactory.GetStaticFileResult(RequestContext, plugin.ConfigurationFilePath);
  213. }
  214. /// <summary>
  215. /// Gets the specified request.
  216. /// </summary>
  217. /// <param name="request">The request.</param>
  218. /// <returns>System.Object.</returns>
  219. public object Get(GetPluginSecurityInfo request)
  220. {
  221. var result = new PluginSecurityInfo
  222. {
  223. IsMBSupporter = _securityManager.IsMBSupporter,
  224. SupporterKey = _securityManager.SupporterKey,
  225. LegacyKey = _securityManager.LegacyKey
  226. };
  227. return ToOptimizedResult(result);
  228. }
  229. /// <summary>
  230. /// Posts the specified request.
  231. /// </summary>
  232. /// <param name="request">The request.</param>
  233. public void Post(UpdatePluginSecurityInfo request)
  234. {
  235. var info = request;
  236. _securityManager.SupporterKey = info.SupporterKey;
  237. _securityManager.LegacyKey = info.LegacyKey;
  238. }
  239. /// <summary>
  240. /// Posts the specified request.
  241. /// </summary>
  242. /// <param name="request">The request.</param>
  243. public void Post(UpdatePluginConfiguration request)
  244. {
  245. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  246. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  247. var pathInfo = PathInfo.Parse(RequestContext.PathInfo);
  248. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  249. var plugin = _appHost.Plugins.First(p => p.Id == id);
  250. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  251. plugin.UpdateConfiguration(configuration);
  252. }
  253. /// <summary>
  254. /// Deletes the specified request.
  255. /// </summary>
  256. /// <param name="request">The request.</param>
  257. public void Delete(UninstallPlugin request)
  258. {
  259. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  260. _installationManager.UninstallPlugin(plugin);
  261. }
  262. }
  263. }