2
0

PluginService.cs 9.4 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.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,
  143. IApplicationHost appHost,
  144. IInstallationManager installationManager,
  145. INetworkManager network,
  146. IDeviceManager deviceManager)
  147. : base()
  148. {
  149. if (jsonSerializer == null)
  150. {
  151. throw new ArgumentNullException(nameof(jsonSerializer));
  152. }
  153. _appHost = appHost;
  154. _installationManager = installationManager;
  155. _network = network;
  156. _deviceManager = deviceManager;
  157. _jsonSerializer = jsonSerializer;
  158. }
  159. /// <summary>
  160. /// Gets the specified request.
  161. /// </summary>
  162. /// <param name="request">The request.</param>
  163. /// <returns>System.Object.</returns>
  164. public object Get(GetRegistrationStatus request)
  165. {
  166. var record = new MBRegistrationRecord
  167. {
  168. IsRegistered = true,
  169. RegChecked = true,
  170. TrialVersion = false,
  171. IsValid = true,
  172. RegError = false
  173. };
  174. return ToOptimizedResult(record);
  175. }
  176. /// <summary>
  177. /// Gets the specified request.
  178. /// </summary>
  179. /// <param name="request">The request.</param>
  180. /// <returns>System.Object.</returns>
  181. public object Get(GetPlugins request)
  182. {
  183. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToArray();
  184. return ToOptimizedResult(result);
  185. }
  186. /// <summary>
  187. /// Gets the specified request.
  188. /// </summary>
  189. /// <param name="request">The request.</param>
  190. /// <returns>System.Object.</returns>
  191. public object Get(GetPluginConfiguration request)
  192. {
  193. var guid = new Guid(request.Id);
  194. var plugin = _appHost.Plugins.First(p => p.Id == guid) as IHasPluginConfiguration;
  195. return ToOptimizedResult(plugin.Configuration);
  196. }
  197. /// <summary>
  198. /// Gets the specified request.
  199. /// </summary>
  200. /// <param name="request">The request.</param>
  201. /// <returns>System.Object.</returns>
  202. public object Get(GetPluginSecurityInfo request)
  203. {
  204. var result = new PluginSecurityInfo
  205. {
  206. IsMBSupporter = true,
  207. SupporterKey = "IAmTotallyLegit"
  208. };
  209. return ToOptimizedResult(result);
  210. }
  211. /// <summary>
  212. /// Posts the specified request.
  213. /// </summary>
  214. /// <param name="request">The request.</param>
  215. public Task Post(UpdatePluginSecurityInfo request)
  216. {
  217. return Task.CompletedTask;
  218. }
  219. /// <summary>
  220. /// Posts the specified request.
  221. /// </summary>
  222. /// <param name="request">The request.</param>
  223. public async Task Post(UpdatePluginConfiguration request)
  224. {
  225. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  226. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  227. var id = new Guid(GetPathValue(1));
  228. var plugin = _appHost.Plugins.First(p => p.Id == id) as IHasPluginConfiguration;
  229. if (plugin == null)
  230. {
  231. throw new FileNotFoundException();
  232. }
  233. var configuration = (await _jsonSerializer.DeserializeFromStreamAsync(request.RequestStream, plugin.ConfigurationType).ConfigureAwait(false)) as BasePluginConfiguration;
  234. plugin.UpdateConfiguration(configuration);
  235. }
  236. /// <summary>
  237. /// Deletes the specified request.
  238. /// </summary>
  239. /// <param name="request">The request.</param>
  240. public void Delete(UninstallPlugin request)
  241. {
  242. var guid = new Guid(request.Id);
  243. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  244. _installationManager.UninstallPlugin(plugin);
  245. }
  246. }
  247. }