PluginService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.Registration;
  9. using MediaBrowser.Model.Serialization;
  10. using ServiceStack;
  11. using ServiceStack.Web;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  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. [Route("/Registrations/{Name}", "GET", Summary = "Gets registration status for a feature")]
  101. [Authenticated]
  102. public class GetRegistration : IReturn<RegistrationInfo>
  103. {
  104. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  105. public string Name { get; set; }
  106. }
  107. /// <summary>
  108. /// Class PluginsService
  109. /// </summary>
  110. public class PluginService : BaseApiService
  111. {
  112. /// <summary>
  113. /// The _json serializer
  114. /// </summary>
  115. private readonly IJsonSerializer _jsonSerializer;
  116. /// <summary>
  117. /// The _app host
  118. /// </summary>
  119. private readonly IApplicationHost _appHost;
  120. private readonly ISecurityManager _securityManager;
  121. private readonly IInstallationManager _installationManager;
  122. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager)
  123. : base()
  124. {
  125. if (jsonSerializer == null)
  126. {
  127. throw new ArgumentNullException("jsonSerializer");
  128. }
  129. _appHost = appHost;
  130. _securityManager = securityManager;
  131. _installationManager = installationManager;
  132. _jsonSerializer = jsonSerializer;
  133. }
  134. /// <summary>
  135. /// Gets the specified request.
  136. /// </summary>
  137. /// <param name="request">The request.</param>
  138. /// <returns>System.Object.</returns>
  139. public async Task<object> Get(GetRegistrationStatus request)
  140. {
  141. var result = await _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).ConfigureAwait(false);
  142. return ToOptimizedResult(result);
  143. }
  144. public async Task<object> Get(GetRegistration request)
  145. {
  146. var result = await _securityManager.GetRegistrationStatus(request.Name).ConfigureAwait(false);
  147. return ToOptimizedResult(new RegistrationInfo
  148. {
  149. ExpirationDate = result.ExpirationDate,
  150. IsRegistered = result.IsRegistered,
  151. IsTrial = result.TrialVersion,
  152. Name = request.Name
  153. });
  154. }
  155. /// <summary>
  156. /// Gets the specified request.
  157. /// </summary>
  158. /// <param name="request">The request.</param>
  159. /// <returns>System.Object.</returns>
  160. public async Task<object> Get(GetPlugins request)
  161. {
  162. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  163. // Don't fail just on account of image url's
  164. try
  165. {
  166. var packages = (await _installationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None))
  167. .ToList();
  168. foreach (var plugin in result)
  169. {
  170. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  171. if (pkg != null)
  172. {
  173. plugin.ImageUrl = pkg.thumbImage;
  174. }
  175. }
  176. }
  177. catch
  178. {
  179. }
  180. return ToOptimizedSerializedResultUsingCache(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(GetPluginConfiguration request)
  188. {
  189. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  190. var dateModified = plugin.ConfigurationDateLastModified;
  191. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  192. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  193. }
  194. /// <summary>
  195. /// Gets the specified request.
  196. /// </summary>
  197. /// <param name="request">The request.</param>
  198. /// <returns>System.Object.</returns>
  199. public object Get(GetPluginSecurityInfo request)
  200. {
  201. var result = new PluginSecurityInfo
  202. {
  203. IsMBSupporter = _securityManager.IsMBSupporter,
  204. SupporterKey = _securityManager.SupporterKey
  205. };
  206. return ToOptimizedSerializedResultUsingCache(result);
  207. }
  208. /// <summary>
  209. /// Posts the specified request.
  210. /// </summary>
  211. /// <param name="request">The request.</param>
  212. public void Post(UpdatePluginSecurityInfo request)
  213. {
  214. var info = request;
  215. _securityManager.SupporterKey = info.SupporterKey;
  216. }
  217. /// <summary>
  218. /// Posts the specified request.
  219. /// </summary>
  220. /// <param name="request">The request.</param>
  221. public void Post(UpdatePluginConfiguration request)
  222. {
  223. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  224. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  225. var id = new Guid(GetPathValue(1));
  226. var plugin = _appHost.Plugins.First(p => p.Id == id);
  227. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  228. plugin.UpdateConfiguration(configuration);
  229. }
  230. /// <summary>
  231. /// Deletes the specified request.
  232. /// </summary>
  233. /// <param name="request">The request.</param>
  234. public void Delete(UninstallPlugin request)
  235. {
  236. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  237. _installationManager.UninstallPlugin(plugin);
  238. }
  239. }
  240. }