PluginService.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Common;
  6. using MediaBrowser.Common.Plugins;
  7. using MediaBrowser.Common.Updates;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Net;
  10. using MediaBrowser.Model.Plugins;
  11. using MediaBrowser.Model.Serialization;
  12. using MediaBrowser.Model.Services;
  13. using Microsoft.Extensions.Logging;
  14. namespace MediaBrowser.Api
  15. {
  16. /// <summary>
  17. /// Class Plugins
  18. /// </summary>
  19. [Route("/Plugins", "GET", Summary = "Gets a list of currently installed plugins")]
  20. [Authenticated]
  21. public class GetPlugins : IReturn<PluginInfo[]>
  22. {
  23. public bool? IsAppStoreEnabled { get; set; }
  24. }
  25. /// <summary>
  26. /// Class UninstallPlugin
  27. /// </summary>
  28. [Route("/Plugins/{Id}", "DELETE", Summary = "Uninstalls a plugin")]
  29. [Authenticated(Roles = "Admin")]
  30. public class UninstallPlugin : IReturnVoid
  31. {
  32. /// <summary>
  33. /// Gets or sets the id.
  34. /// </summary>
  35. /// <value>The id.</value>
  36. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  37. public string Id { get; set; }
  38. }
  39. /// <summary>
  40. /// Class GetPluginConfiguration
  41. /// </summary>
  42. [Route("/Plugins/{Id}/Configuration", "GET", Summary = "Gets a plugin's configuration")]
  43. [Authenticated]
  44. public class GetPluginConfiguration
  45. {
  46. /// <summary>
  47. /// Gets or sets the id.
  48. /// </summary>
  49. /// <value>The id.</value>
  50. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  51. public string Id { get; set; }
  52. }
  53. /// <summary>
  54. /// Class UpdatePluginConfiguration
  55. /// </summary>
  56. [Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")]
  57. [Authenticated]
  58. public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
  59. {
  60. /// <summary>
  61. /// Gets or sets the id.
  62. /// </summary>
  63. /// <value>The id.</value>
  64. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  65. public string Id { get; set; }
  66. /// <summary>
  67. /// The raw Http Request Input Stream
  68. /// </summary>
  69. /// <value>The request stream.</value>
  70. public Stream RequestStream { get; set; }
  71. }
  72. // TODO Once we have proper apps and plugins and decide to break compatibility with paid plugins,
  73. // delete all these registration endpoints. They are only kept for compatibility.
  74. [Route("/Registrations/{Name}", "GET", Summary = "Gets registration status for a feature", IsHidden = true)]
  75. [Authenticated]
  76. public class GetRegistration : IReturn<RegistrationInfo>
  77. {
  78. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  79. public string Name { get; set; }
  80. }
  81. /// <summary>
  82. /// Class GetPluginSecurityInfo
  83. /// </summary>
  84. [Route("/Plugins/SecurityInfo", "GET", Summary = "Gets plugin registration information", IsHidden = true)]
  85. [Authenticated]
  86. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  87. {
  88. }
  89. /// <summary>
  90. /// Class UpdatePluginSecurityInfo
  91. /// </summary>
  92. [Route("/Plugins/SecurityInfo", "POST", Summary = "Updates plugin registration information", IsHidden = true)]
  93. [Authenticated(Roles = "Admin")]
  94. public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
  95. {
  96. }
  97. [Route("/Plugins/RegistrationRecords/{Name}", "GET", Summary = "Gets registration status for a feature", IsHidden = true)]
  98. [Authenticated]
  99. public class GetRegistrationStatus
  100. {
  101. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  102. public string Name { get; set; }
  103. }
  104. // TODO these two classes are only kept for compability with paid plugins and should be removed
  105. public class RegistrationInfo
  106. {
  107. public string Name { get; set; }
  108. public DateTime ExpirationDate { get; set; }
  109. public bool IsTrial { get; set; }
  110. public bool IsRegistered { get; set; }
  111. }
  112. public class MBRegistrationRecord
  113. {
  114. public DateTime ExpirationDate { get; set; }
  115. public bool IsRegistered { get; set; }
  116. public bool RegChecked { get; set; }
  117. public bool RegError { get; set; }
  118. public bool TrialVersion { get; set; }
  119. public bool IsValid { get; set; }
  120. }
  121. public class PluginSecurityInfo
  122. {
  123. public string SupporterKey { get; set; }
  124. public bool IsMBSupporter { get; set; }
  125. }
  126. /// <summary>
  127. /// Class PluginsService
  128. /// </summary>
  129. public class PluginService : BaseApiService
  130. {
  131. /// <summary>
  132. /// The _json serializer
  133. /// </summary>
  134. private readonly IJsonSerializer _jsonSerializer;
  135. /// <summary>
  136. /// The _app host
  137. /// </summary>
  138. private readonly IApplicationHost _appHost;
  139. private readonly IInstallationManager _installationManager;
  140. public PluginService(
  141. ILogger<PluginService> logger,
  142. IServerConfigurationManager serverConfigurationManager,
  143. IHttpResultFactory httpResultFactory,
  144. IJsonSerializer jsonSerializer,
  145. IApplicationHost appHost,
  146. IInstallationManager installationManager)
  147. : base(logger, serverConfigurationManager, httpResultFactory)
  148. {
  149. _appHost = appHost;
  150. _installationManager = installationManager;
  151. _jsonSerializer = jsonSerializer;
  152. }
  153. /// <summary>
  154. /// Gets the specified request.
  155. /// </summary>
  156. /// <param name="request">The request.</param>
  157. /// <returns>System.Object.</returns>
  158. public object Get(GetRegistrationStatus request)
  159. {
  160. var record = new MBRegistrationRecord
  161. {
  162. IsRegistered = true,
  163. RegChecked = true,
  164. TrialVersion = false,
  165. IsValid = true,
  166. RegError = false
  167. };
  168. return ToOptimizedResult(record);
  169. }
  170. /// <summary>
  171. /// Gets the specified request.
  172. /// </summary>
  173. /// <param name="request">The request.</param>
  174. /// <returns>System.Object.</returns>
  175. public object Get(GetPlugins request)
  176. {
  177. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToArray();
  178. return ToOptimizedResult(result);
  179. }
  180. /// <summary>
  181. /// Gets the specified request.
  182. /// </summary>
  183. /// <param name="request">The request.</param>
  184. /// <returns>System.Object.</returns>
  185. public object Get(GetPluginConfiguration request)
  186. {
  187. var guid = new Guid(request.Id);
  188. var plugin = _appHost.Plugins.First(p => p.Id == guid) as IHasPluginConfiguration;
  189. return ToOptimizedResult(plugin.Configuration);
  190. }
  191. /// <summary>
  192. /// Gets the specified request.
  193. /// </summary>
  194. /// <param name="request">The request.</param>
  195. /// <returns>System.Object.</returns>
  196. public object Get(GetPluginSecurityInfo request)
  197. {
  198. var result = new PluginSecurityInfo
  199. {
  200. IsMBSupporter = true,
  201. SupporterKey = "IAmTotallyLegit"
  202. };
  203. return ToOptimizedResult(result);
  204. }
  205. /// <summary>
  206. /// Posts the specified request.
  207. /// </summary>
  208. /// <param name="request">The request.</param>
  209. public Task Post(UpdatePluginSecurityInfo request)
  210. {
  211. return Task.CompletedTask;
  212. }
  213. /// <summary>
  214. /// Posts the specified request.
  215. /// </summary>
  216. /// <param name="request">The request.</param>
  217. public async Task Post(UpdatePluginConfiguration request)
  218. {
  219. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  220. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  221. var id = Guid.Parse(GetPathValue(1));
  222. if (!(_appHost.Plugins.First(p => p.Id == id) is IHasPluginConfiguration plugin))
  223. {
  224. throw new FileNotFoundException();
  225. }
  226. var configuration = (await _jsonSerializer.DeserializeFromStreamAsync(request.RequestStream, plugin.ConfigurationType).ConfigureAwait(false)) as BasePluginConfiguration;
  227. plugin.UpdateConfiguration(configuration);
  228. }
  229. /// <summary>
  230. /// Deletes the specified request.
  231. /// </summary>
  232. /// <param name="request">The request.</param>
  233. public void Delete(UninstallPlugin request)
  234. {
  235. var guid = new Guid(request.Id);
  236. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  237. _installationManager.UninstallPlugin(plugin);
  238. }
  239. }
  240. }