PluginService.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System.Threading;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Common.Extensions;
  4. using MediaBrowser.Common.Security;
  5. using MediaBrowser.Common.Updates;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Plugins;
  9. using MediaBrowser.Model.Serialization;
  10. using ServiceStack;
  11. using ServiceStack.Text.Controller;
  12. using ServiceStack.Web;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Api
  19. {
  20. /// <summary>
  21. /// Class Plugins
  22. /// </summary>
  23. [Route("/Plugins", "GET", Summary = "Gets a list of currently installed plugins")]
  24. [Authenticated]
  25. public class GetPlugins : IReturn<List<PluginInfo>>
  26. {
  27. }
  28. /// <summary>
  29. /// Class UninstallPlugin
  30. /// </summary>
  31. [Route("/Plugins/{Id}", "DELETE", Summary = "Uninstalls a plugin")]
  32. [Authenticated(Roles = "Admin")]
  33. public class UninstallPlugin : IReturnVoid
  34. {
  35. /// <summary>
  36. /// Gets or sets the id.
  37. /// </summary>
  38. /// <value>The id.</value>
  39. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  40. public Guid Id { get; set; }
  41. }
  42. /// <summary>
  43. /// Class GetPluginConfiguration
  44. /// </summary>
  45. [Route("/Plugins/{Id}/Configuration", "GET", Summary = "Gets a plugin's configuration")]
  46. [Authenticated]
  47. public class GetPluginConfiguration
  48. {
  49. /// <summary>
  50. /// Gets or sets the id.
  51. /// </summary>
  52. /// <value>The id.</value>
  53. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  54. public Guid Id { get; set; }
  55. }
  56. /// <summary>
  57. /// Class UpdatePluginConfiguration
  58. /// </summary>
  59. [Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")]
  60. [Authenticated]
  61. public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
  62. {
  63. /// <summary>
  64. /// Gets or sets the id.
  65. /// </summary>
  66. /// <value>The id.</value>
  67. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  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 GetPluginSecurityInfo
  77. /// </summary>
  78. [Route("/Plugins/SecurityInfo", "GET", Summary = "Gets plugin registration information")]
  79. [Authenticated]
  80. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  81. {
  82. }
  83. /// <summary>
  84. /// Class UpdatePluginSecurityInfo
  85. /// </summary>
  86. [Route("/Plugins/SecurityInfo", "POST", Summary = "Updates plugin registration information")]
  87. [Authenticated(Roles = "Admin")]
  88. public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
  89. {
  90. }
  91. [Route("/Plugins/RegistrationRecords/{Name}", "GET", Summary = "Gets registration status for a feature")]
  92. [Authenticated]
  93. public class GetRegistrationStatus
  94. {
  95. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  96. public string Name { get; set; }
  97. [ApiMember(Name = "Mb2Equivalent", Description = "Optional. The equivalent feature name in MB2", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  98. public string Mb2Equivalent { get; set; }
  99. }
  100. /// <summary>
  101. /// Class PluginsService
  102. /// </summary>
  103. public class PluginService : BaseApiService
  104. {
  105. /// <summary>
  106. /// The _json serializer
  107. /// </summary>
  108. private readonly IJsonSerializer _jsonSerializer;
  109. /// <summary>
  110. /// The _app host
  111. /// </summary>
  112. private readonly IApplicationHost _appHost;
  113. private readonly ISecurityManager _securityManager;
  114. private readonly IInstallationManager _installationManager;
  115. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager)
  116. : base()
  117. {
  118. if (jsonSerializer == null)
  119. {
  120. throw new ArgumentNullException("jsonSerializer");
  121. }
  122. _appHost = appHost;
  123. _securityManager = securityManager;
  124. _installationManager = installationManager;
  125. _jsonSerializer = jsonSerializer;
  126. }
  127. /// <summary>
  128. /// Gets the specified request.
  129. /// </summary>
  130. /// <param name="request">The request.</param>
  131. /// <returns>System.Object.</returns>
  132. public object Get(GetRegistrationStatus request)
  133. {
  134. var result = _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).Result;
  135. return ToOptimizedResult(result);
  136. }
  137. /// <summary>
  138. /// Gets the specified request.
  139. /// </summary>
  140. /// <param name="request">The request.</param>
  141. /// <returns>System.Object.</returns>
  142. public async Task<object> Get(GetPlugins request)
  143. {
  144. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  145. // Don't fail just on account of image url's
  146. try
  147. {
  148. var packages = (await _installationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None))
  149. .ToList();
  150. foreach (var plugin in result)
  151. {
  152. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  153. if (pkg != null)
  154. {
  155. plugin.ImageUrl = pkg.thumbImage;
  156. }
  157. }
  158. }
  159. catch
  160. {
  161. }
  162. return ToOptimizedSerializedResultUsingCache(result);
  163. }
  164. /// <summary>
  165. /// Gets the specified request.
  166. /// </summary>
  167. /// <param name="request">The request.</param>
  168. /// <returns>System.Object.</returns>
  169. public object Get(GetPluginConfiguration request)
  170. {
  171. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  172. var dateModified = plugin.ConfigurationDateLastModified;
  173. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  174. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  175. }
  176. /// <summary>
  177. /// Gets the specified request.
  178. /// </summary>
  179. /// <param name="request">The request.</param>
  180. /// <returns>System.Object.</returns>
  181. public object Get(GetPluginSecurityInfo request)
  182. {
  183. var result = new PluginSecurityInfo
  184. {
  185. IsMBSupporter = _securityManager.IsMBSupporter,
  186. SupporterKey = _securityManager.SupporterKey
  187. };
  188. return ToOptimizedSerializedResultUsingCache(result);
  189. }
  190. /// <summary>
  191. /// Posts the specified request.
  192. /// </summary>
  193. /// <param name="request">The request.</param>
  194. public void Post(UpdatePluginSecurityInfo request)
  195. {
  196. var info = request;
  197. _securityManager.SupporterKey = info.SupporterKey;
  198. }
  199. /// <summary>
  200. /// Posts the specified request.
  201. /// </summary>
  202. /// <param name="request">The request.</param>
  203. public void Post(UpdatePluginConfiguration request)
  204. {
  205. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  206. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  207. var pathInfo = PathInfo.Parse(Request.PathInfo);
  208. var id = new Guid(pathInfo.GetArgumentValue<string>(1));
  209. var plugin = _appHost.Plugins.First(p => p.Id == id);
  210. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  211. plugin.UpdateConfiguration(configuration);
  212. }
  213. /// <summary>
  214. /// Deletes the specified request.
  215. /// </summary>
  216. /// <param name="request">The request.</param>
  217. public void Delete(UninstallPlugin request)
  218. {
  219. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  220. _installationManager.UninstallPlugin(plugin);
  221. }
  222. }
  223. }