PluginService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. public bool? IsAppStoreEnabled { get; set; }
  28. }
  29. /// <summary>
  30. /// Class UninstallPlugin
  31. /// </summary>
  32. [Route("/Plugins/{Id}", "DELETE", Summary = "Uninstalls a plugin")]
  33. [Authenticated(Roles = "Admin")]
  34. public class UninstallPlugin : IReturnVoid
  35. {
  36. /// <summary>
  37. /// Gets or sets the id.
  38. /// </summary>
  39. /// <value>The id.</value>
  40. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  41. public Guid Id { get; set; }
  42. }
  43. /// <summary>
  44. /// Class GetPluginConfiguration
  45. /// </summary>
  46. [Route("/Plugins/{Id}/Configuration", "GET", Summary = "Gets a plugin's configuration")]
  47. [Authenticated]
  48. public class GetPluginConfiguration
  49. {
  50. /// <summary>
  51. /// Gets or sets the id.
  52. /// </summary>
  53. /// <value>The id.</value>
  54. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  55. public Guid Id { get; set; }
  56. }
  57. /// <summary>
  58. /// Class UpdatePluginConfiguration
  59. /// </summary>
  60. [Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")]
  61. [Authenticated]
  62. public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
  63. {
  64. /// <summary>
  65. /// Gets or sets the id.
  66. /// </summary>
  67. /// <value>The id.</value>
  68. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  69. public Guid Id { get; set; }
  70. /// <summary>
  71. /// The raw Http Request Input Stream
  72. /// </summary>
  73. /// <value>The request stream.</value>
  74. public Stream RequestStream { get; set; }
  75. }
  76. /// <summary>
  77. /// Class GetPluginSecurityInfo
  78. /// </summary>
  79. [Route("/Plugins/SecurityInfo", "GET", Summary = "Gets plugin registration information")]
  80. [Authenticated]
  81. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  82. {
  83. }
  84. /// <summary>
  85. /// Class UpdatePluginSecurityInfo
  86. /// </summary>
  87. [Route("/Plugins/SecurityInfo", "POST", Summary = "Updates plugin registration information")]
  88. [Authenticated(Roles = "Admin")]
  89. public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
  90. {
  91. }
  92. [Route("/Plugins/RegistrationRecords/{Name}", "GET", Summary = "Gets registration status for a feature")]
  93. [Authenticated]
  94. public class GetRegistrationStatus
  95. {
  96. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  97. public string Name { get; set; }
  98. [ApiMember(Name = "Mb2Equivalent", Description = "Optional. The equivalent feature name in MB2", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  99. public string Mb2Equivalent { get; set; }
  100. }
  101. [Route("/Registrations/{Name}", "GET", Summary = "Gets registration status for a feature")]
  102. [Authenticated]
  103. public class GetRegistration : IReturn<RegistrationInfo>
  104. {
  105. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  106. public string Name { get; set; }
  107. }
  108. /// <summary>
  109. /// Class PluginsService
  110. /// </summary>
  111. public class PluginService : BaseApiService
  112. {
  113. /// <summary>
  114. /// The _json serializer
  115. /// </summary>
  116. private readonly IJsonSerializer _jsonSerializer;
  117. /// <summary>
  118. /// The _app host
  119. /// </summary>
  120. private readonly IApplicationHost _appHost;
  121. private readonly ISecurityManager _securityManager;
  122. private readonly IInstallationManager _installationManager;
  123. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager)
  124. : base()
  125. {
  126. if (jsonSerializer == null)
  127. {
  128. throw new ArgumentNullException("jsonSerializer");
  129. }
  130. _appHost = appHost;
  131. _securityManager = securityManager;
  132. _installationManager = installationManager;
  133. _jsonSerializer = jsonSerializer;
  134. }
  135. /// <summary>
  136. /// Gets the specified request.
  137. /// </summary>
  138. /// <param name="request">The request.</param>
  139. /// <returns>System.Object.</returns>
  140. public async Task<object> Get(GetRegistrationStatus request)
  141. {
  142. var result = await _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).ConfigureAwait(false);
  143. return ToOptimizedResult(result);
  144. }
  145. public async Task<object> Get(GetRegistration request)
  146. {
  147. var result = await _securityManager.GetRegistrationStatus(request.Name).ConfigureAwait(false);
  148. return ToOptimizedResult(new RegistrationInfo
  149. {
  150. ExpirationDate = result.ExpirationDate,
  151. IsRegistered = result.IsRegistered,
  152. IsTrial = result.TrialVersion,
  153. Name = request.Name
  154. });
  155. }
  156. /// <summary>
  157. /// Gets the specified request.
  158. /// </summary>
  159. /// <param name="request">The request.</param>
  160. /// <returns>System.Object.</returns>
  161. public async Task<object> Get(GetPlugins request)
  162. {
  163. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
  164. var requireAppStoreEnabled = request.IsAppStoreEnabled.HasValue && request.IsAppStoreEnabled.Value;
  165. // Don't fail just on account of image url's
  166. try
  167. {
  168. var packages = (await _installationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None))
  169. .ToList();
  170. foreach (var plugin in result)
  171. {
  172. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  173. if (pkg != null)
  174. {
  175. plugin.ImageUrl = pkg.thumbImage;
  176. }
  177. }
  178. if (requireAppStoreEnabled)
  179. {
  180. result = result
  181. .Where(plugin =>
  182. {
  183. var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  184. return pkg != null && pkg.enableInAppStore;
  185. })
  186. .ToList();
  187. }
  188. }
  189. catch
  190. {
  191. // Play it safe here
  192. if (requireAppStoreEnabled)
  193. {
  194. result = new List<PluginInfo>();
  195. }
  196. }
  197. return ToOptimizedSerializedResultUsingCache(result);
  198. }
  199. /// <summary>
  200. /// Gets the specified request.
  201. /// </summary>
  202. /// <param name="request">The request.</param>
  203. /// <returns>System.Object.</returns>
  204. public object Get(GetPluginConfiguration request)
  205. {
  206. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  207. var dateModified = plugin.ConfigurationDateLastModified;
  208. var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
  209. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
  210. }
  211. /// <summary>
  212. /// Gets the specified request.
  213. /// </summary>
  214. /// <param name="request">The request.</param>
  215. /// <returns>System.Object.</returns>
  216. public object Get(GetPluginSecurityInfo request)
  217. {
  218. var result = new PluginSecurityInfo
  219. {
  220. IsMBSupporter = _securityManager.IsMBSupporter,
  221. SupporterKey = _securityManager.SupporterKey
  222. };
  223. return ToOptimizedSerializedResultUsingCache(result);
  224. }
  225. /// <summary>
  226. /// Posts the specified request.
  227. /// </summary>
  228. /// <param name="request">The request.</param>
  229. public void Post(UpdatePluginSecurityInfo request)
  230. {
  231. var info = request;
  232. _securityManager.SupporterKey = info.SupporterKey;
  233. }
  234. /// <summary>
  235. /// Posts the specified request.
  236. /// </summary>
  237. /// <param name="request">The request.</param>
  238. public void Post(UpdatePluginConfiguration request)
  239. {
  240. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  241. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  242. var id = new Guid(GetPathValue(1));
  243. var plugin = _appHost.Plugins.First(p => p.Id == id);
  244. var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
  245. plugin.UpdateConfiguration(configuration);
  246. }
  247. /// <summary>
  248. /// Deletes the specified request.
  249. /// </summary>
  250. /// <param name="request">The request.</param>
  251. public void Delete(UninstallPlugin request)
  252. {
  253. var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
  254. _installationManager.UninstallPlugin(plugin);
  255. }
  256. }
  257. }