PluginService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Common;
  6. using MediaBrowser.Common.Net;
  7. using MediaBrowser.Common.Plugins;
  8. using MediaBrowser.Common.Updates;
  9. using MediaBrowser.Controller.Devices;
  10. using MediaBrowser.Controller.Net;
  11. using MediaBrowser.Model.Plugins;
  12. using MediaBrowser.Model.Serialization;
  13. using MediaBrowser.Model.Services;
  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. private readonly INetworkManager _network;
  141. private readonly IDeviceManager _deviceManager;
  142. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, IInstallationManager installationManager, INetworkManager network, IDeviceManager deviceManager)
  143. : base()
  144. {
  145. if (jsonSerializer == null)
  146. {
  147. throw new ArgumentNullException(nameof(jsonSerializer));
  148. }
  149. _appHost = appHost;
  150. _installationManager = installationManager;
  151. _network = network;
  152. _deviceManager = deviceManager;
  153. _jsonSerializer = jsonSerializer;
  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(GetRegistrationStatus request)
  161. {
  162. var record = new MBRegistrationRecord
  163. {
  164. IsRegistered = true,
  165. RegChecked = true,
  166. TrialVersion = false,
  167. IsValid = true,
  168. RegError = false
  169. };
  170. return ToOptimizedResult(record);
  171. }
  172. //TODO this function is only kept for compatibility and should be removed once paid plugins break
  173. public async Task<object> Get(GetRegistration request)
  174. {
  175. var info = new RegistrationInfo
  176. {
  177. ExpirationDate = DateTime.Now.AddYears(100),
  178. IsRegistered = true,
  179. IsTrial = false,
  180. Name = request.Name
  181. };
  182. return ToOptimizedResult(info);
  183. }
  184. /// <summary>
  185. /// Gets the specified request.
  186. /// </summary>
  187. /// <param name="request">The request.</param>
  188. /// <returns>System.Object.</returns>
  189. public async Task<object> Get(GetPlugins request)
  190. {
  191. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToArray();
  192. return ToOptimizedResult(result);
  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(GetPluginConfiguration request)
  200. {
  201. var guid = new Guid(request.Id);
  202. var plugin = _appHost.Plugins.First(p => p.Id == guid) as IHasPluginConfiguration;
  203. return ToOptimizedResult(plugin.Configuration);
  204. }
  205. /// <summary>
  206. /// Gets the specified request.
  207. /// </summary>
  208. /// <param name="request">The request.</param>
  209. /// <returns>System.Object.</returns>
  210. public async Task<object> Get(GetPluginSecurityInfo request)
  211. {
  212. var result = new PluginSecurityInfo
  213. {
  214. IsMBSupporter = true,
  215. SupporterKey = "IAmTotallyLegit"
  216. };
  217. return ToOptimizedResult(result);
  218. }
  219. /// <summary>
  220. /// Posts the specified request.
  221. /// </summary>
  222. /// <param name="request">The request.</param>
  223. public Task Post(UpdatePluginSecurityInfo request)
  224. {
  225. return Task.CompletedTask;
  226. }
  227. /// <summary>
  228. /// Posts the specified request.
  229. /// </summary>
  230. /// <param name="request">The request.</param>
  231. public async Task Post(UpdatePluginConfiguration request)
  232. {
  233. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  234. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  235. var id = new Guid(GetPathValue(1));
  236. var plugin = _appHost.Plugins.First(p => p.Id == id) as IHasPluginConfiguration;
  237. if (plugin == null)
  238. {
  239. throw new FileNotFoundException();
  240. }
  241. var configuration = (await _jsonSerializer.DeserializeFromStreamAsync(request.RequestStream, plugin.ConfigurationType).ConfigureAwait(false)) as BasePluginConfiguration;
  242. plugin.UpdateConfiguration(configuration);
  243. }
  244. /// <summary>
  245. /// Deletes the specified request.
  246. /// </summary>
  247. /// <param name="request">The request.</param>
  248. public void Delete(UninstallPlugin request)
  249. {
  250. var guid = new Guid(request.Id);
  251. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  252. _installationManager.UninstallPlugin(plugin);
  253. }
  254. }
  255. }