PluginService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Common.Updates;
  4. using MediaBrowser.Controller.Devices;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Model.Plugins;
  7. using MediaBrowser.Model.Serialization;
  8. using System;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Model.Services;
  13. using MediaBrowser.Common.Plugins;
  14. using Microsoft.Extensions.Logging;
  15. namespace MediaBrowser.Api
  16. {
  17. /// <summary>
  18. /// Class Plugins
  19. /// </summary>
  20. [Route("/Plugins", "GET", Summary = "Gets a list of currently installed plugins")]
  21. [Authenticated]
  22. public class GetPlugins : IReturn<PluginInfo[]>
  23. {
  24. public bool? IsAppStoreEnabled { get; set; }
  25. }
  26. /// <summary>
  27. /// Class UninstallPlugin
  28. /// </summary>
  29. [Route("/Plugins/{Id}", "DELETE", Summary = "Uninstalls a plugin")]
  30. [Authenticated(Roles = "Admin")]
  31. public class UninstallPlugin : IReturnVoid
  32. {
  33. /// <summary>
  34. /// Gets or sets the id.
  35. /// </summary>
  36. /// <value>The id.</value>
  37. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  38. public string Id { get; set; }
  39. }
  40. /// <summary>
  41. /// Class GetPluginConfiguration
  42. /// </summary>
  43. [Route("/Plugins/{Id}/Configuration", "GET", Summary = "Gets a plugin's configuration")]
  44. [Authenticated]
  45. public class GetPluginConfiguration
  46. {
  47. /// <summary>
  48. /// Gets or sets the id.
  49. /// </summary>
  50. /// <value>The id.</value>
  51. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  52. public string Id { get; set; }
  53. }
  54. /// <summary>
  55. /// Class UpdatePluginConfiguration
  56. /// </summary>
  57. [Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")]
  58. [Authenticated]
  59. public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
  60. {
  61. /// <summary>
  62. /// Gets or sets the id.
  63. /// </summary>
  64. /// <value>The id.</value>
  65. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  66. public string Id { get; set; }
  67. /// <summary>
  68. /// The raw Http Request Input Stream
  69. /// </summary>
  70. /// <value>The request stream.</value>
  71. public Stream RequestStream { get; set; }
  72. }
  73. //TODO Once we have proper apps and plugins and decide to break compatibility with paid plugins,
  74. // delete all these registration endpoints. They are only kept for compatibility.
  75. [Route("/Registrations/{Name}", "GET", Summary = "Gets registration status for a feature", IsHidden = true)]
  76. [Authenticated]
  77. public class GetRegistration : IReturn<RegistrationInfo>
  78. {
  79. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  80. public string Name { get; set; }
  81. }
  82. /// <summary>
  83. /// Class GetPluginSecurityInfo
  84. /// </summary>
  85. [Route("/Plugins/SecurityInfo", "GET", Summary = "Gets plugin registration information", IsHidden = true)]
  86. [Authenticated]
  87. public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
  88. {
  89. }
  90. /// <summary>
  91. /// Class UpdatePluginSecurityInfo
  92. /// </summary>
  93. [Route("/Plugins/SecurityInfo", "POST", Summary = "Updates plugin registration information", IsHidden = true)]
  94. [Authenticated(Roles = "Admin")]
  95. public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
  96. {
  97. }
  98. [Route("/Plugins/RegistrationRecords/{Name}", "GET", Summary = "Gets registration status for a feature", IsHidden = true)]
  99. [Authenticated]
  100. public class GetRegistrationStatus
  101. {
  102. [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  103. public string Name { get; set; }
  104. }
  105. // TODO these two classes are only kept for compability with paid plugins and should be removed
  106. public class RegistrationInfo
  107. {
  108. public string Name { get; set; }
  109. public DateTime ExpirationDate { get; set; }
  110. public bool IsTrial { get; set; }
  111. public bool IsRegistered { get; set; }
  112. }
  113. public class MBRegistrationRecord
  114. {
  115. public DateTime ExpirationDate { get; set; }
  116. public bool IsRegistered { get; set; }
  117. public bool RegChecked { get; set; }
  118. public bool RegError { get; set; }
  119. public bool TrialVersion { get; set; }
  120. public bool IsValid { get; set; }
  121. }
  122. public class PluginSecurityInfo
  123. {
  124. public string SupporterKey { get; set; }
  125. public bool IsMBSupporter { get; set; }
  126. }
  127. /// <summary>
  128. /// Class PluginsService
  129. /// </summary>
  130. public class PluginService : BaseApiService
  131. {
  132. /// <summary>
  133. /// The _json serializer
  134. /// </summary>
  135. private readonly IJsonSerializer _jsonSerializer;
  136. /// <summary>
  137. /// The _app host
  138. /// </summary>
  139. private readonly IApplicationHost _appHost;
  140. private readonly IInstallationManager _installationManager;
  141. private readonly INetworkManager _network;
  142. private readonly IDeviceManager _deviceManager;
  143. public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, IInstallationManager installationManager, INetworkManager network, IDeviceManager deviceManager)
  144. : base()
  145. {
  146. if (jsonSerializer == null)
  147. {
  148. throw new ArgumentNullException(nameof(jsonSerializer));
  149. }
  150. _appHost = appHost;
  151. _installationManager = installationManager;
  152. _network = network;
  153. _deviceManager = deviceManager;
  154. _jsonSerializer = jsonSerializer;
  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(GetRegistrationStatus request)
  162. {
  163. var record = new MBRegistrationRecord
  164. {
  165. IsRegistered = true,
  166. RegChecked = true,
  167. TrialVersion = false,
  168. IsValid = true,
  169. RegError = false
  170. };
  171. return ToOptimizedResult(record);
  172. }
  173. //TODO this function is only kept for compatibility and should be removed once paid plugins break
  174. public async Task<object> Get(GetRegistration request)
  175. {
  176. var info = new RegistrationInfo
  177. {
  178. ExpirationDate = DateTime.Now.AddYears(100),
  179. IsRegistered = true,
  180. IsTrial = false,
  181. Name = request.Name
  182. };
  183. return ToOptimizedResult(info);
  184. }
  185. /// <summary>
  186. /// Gets the specified request.
  187. /// </summary>
  188. /// <param name="request">The request.</param>
  189. /// <returns>System.Object.</returns>
  190. public async Task<object> Get(GetPlugins request)
  191. {
  192. // TODO This code can be reused for a proper Jellyfin plugin store (maybe). Remove/reuse when decided.
  193. var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToArray();
  194. // var requireAppStoreEnabled = request.IsAppStoreEnabled.HasValue && request.IsAppStoreEnabled.Value;
  195. //
  196. // // Don't fail just on account of image url's
  197. // try
  198. // {
  199. // var packages = (await _installationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None));
  200. //
  201. // foreach (var plugin in result)
  202. // {
  203. // var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && string.Equals(i.guid.Replace("-", string.Empty), plugin.Id.Replace("-", string.Empty), StringComparison.OrdinalIgnoreCase));
  204. //
  205. // if (pkg != null)
  206. // {
  207. // plugin.ImageUrl = pkg.thumbImage;
  208. // }
  209. // }
  210. //
  211. // if (requireAppStoreEnabled)
  212. // {
  213. // result = result
  214. // .Where(plugin =>
  215. // {
  216. // var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
  217. // return pkg != null && pkg.enableInAppStore;
  218. //
  219. // })
  220. // .ToArray();
  221. // }
  222. // }
  223. // catch (Exception ex)
  224. // {
  225. // Logger.LogError(ex, "Error getting plugin list");
  226. // // Play it safe here
  227. // if (requireAppStoreEnabled)
  228. // {
  229. // result = new PluginInfo[] { };
  230. // }
  231. // }
  232. return ToOptimizedResult(result);
  233. }
  234. /// <summary>
  235. /// Gets the specified request.
  236. /// </summary>
  237. /// <param name="request">The request.</param>
  238. /// <returns>System.Object.</returns>
  239. public object Get(GetPluginConfiguration request)
  240. {
  241. var guid = new Guid(request.Id);
  242. var plugin = _appHost.Plugins.First(p => p.Id == guid) as IHasPluginConfiguration;
  243. return ToOptimizedResult(plugin.Configuration);
  244. }
  245. /// <summary>
  246. /// Gets the specified request.
  247. /// </summary>
  248. /// <param name="request">The request.</param>
  249. /// <returns>System.Object.</returns>
  250. public async Task<object> Get(GetPluginSecurityInfo request)
  251. {
  252. var result = new PluginSecurityInfo
  253. {
  254. IsMBSupporter = true,
  255. SupporterKey = "IAmTotallyLegit"
  256. };
  257. return ToOptimizedResult(result);
  258. }
  259. /// <summary>
  260. /// Posts the specified request.
  261. /// </summary>
  262. /// <param name="request">The request.</param>
  263. public Task Post(UpdatePluginSecurityInfo request)
  264. {
  265. return Task.CompletedTask;
  266. }
  267. /// <summary>
  268. /// Posts the specified request.
  269. /// </summary>
  270. /// <param name="request">The request.</param>
  271. public async Task Post(UpdatePluginConfiguration request)
  272. {
  273. // We need to parse this manually because we told service stack not to with IRequiresRequestStream
  274. // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs
  275. var id = new Guid(GetPathValue(1));
  276. var plugin = _appHost.Plugins.First(p => p.Id == id) as IHasPluginConfiguration;
  277. if (plugin == null)
  278. {
  279. throw new FileNotFoundException();
  280. }
  281. var configuration = (await _jsonSerializer.DeserializeFromStreamAsync(request.RequestStream, plugin.ConfigurationType).ConfigureAwait(false)) as BasePluginConfiguration;
  282. plugin.UpdateConfiguration(configuration);
  283. }
  284. /// <summary>
  285. /// Deletes the specified request.
  286. /// </summary>
  287. /// <param name="request">The request.</param>
  288. public void Delete(UninstallPlugin request)
  289. {
  290. var guid = new Guid(request.Id);
  291. var plugin = _appHost.Plugins.First(p => p.Id == guid);
  292. _installationManager.UninstallPlugin(plugin);
  293. }
  294. }
  295. }