PluginService.cs 8.7 KB

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