DashboardController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.IO;
  5. using System.Linq;
  6. using Jellyfin.Api.Models;
  7. using MediaBrowser.Common.Plugins;
  8. using MediaBrowser.Controller;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Extensions;
  11. using MediaBrowser.Controller.Plugins;
  12. using MediaBrowser.Model.Net;
  13. using MediaBrowser.Model.Plugins;
  14. using Microsoft.AspNetCore.Http;
  15. using Microsoft.AspNetCore.Mvc;
  16. using Microsoft.Extensions.Configuration;
  17. namespace Jellyfin.Api.Controllers
  18. {
  19. /// <summary>
  20. /// The dashboard controller.
  21. /// </summary>
  22. public class DashboardController : BaseJellyfinApiController
  23. {
  24. private readonly IServerApplicationHost _appHost;
  25. private readonly IConfiguration _appConfig;
  26. private readonly IServerConfigurationManager _serverConfigurationManager;
  27. private readonly IResourceFileManager _resourceFileManager;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="DashboardController"/> class.
  30. /// </summary>
  31. /// <param name="appHost">Instance of <see cref="IServerApplicationHost"/> interface.</param>
  32. /// <param name="appConfig">Instance of <see cref="IConfiguration"/> interface.</param>
  33. /// <param name="resourceFileManager">Instance of <see cref="IResourceFileManager"/> interface.</param>
  34. /// <param name="serverConfigurationManager">Instance of <see cref="IServerConfigurationManager"/> interface.</param>
  35. public DashboardController(
  36. IServerApplicationHost appHost,
  37. IConfiguration appConfig,
  38. IResourceFileManager resourceFileManager,
  39. IServerConfigurationManager serverConfigurationManager)
  40. {
  41. _appHost = appHost;
  42. _appConfig = appConfig;
  43. _resourceFileManager = resourceFileManager;
  44. _serverConfigurationManager = serverConfigurationManager;
  45. }
  46. /// <summary>
  47. /// Gets the path of the directory containing the static web interface content, or null if the server is not
  48. /// hosting the web client.
  49. /// </summary>
  50. private string? WebClientUiPath => GetWebClientUiPath(_appConfig, _serverConfigurationManager);
  51. /// <summary>
  52. /// Gets the configuration pages.
  53. /// </summary>
  54. /// <param name="enableInMainMenu">Whether to enable in the main menu.</param>
  55. /// <param name="pageType">The <see cref="ConfigurationPageInfo"/>.</param>
  56. /// <response code="200">ConfigurationPages returned.</response>
  57. /// <response code="404">Server still loading.</response>
  58. /// <returns>An <see cref="IEnumerable{ConfigurationPageInfo}"/> with infos about the plugins.</returns>
  59. [HttpGet("/web/ConfigurationPages")]
  60. [ProducesResponseType(StatusCodes.Status200OK)]
  61. [ProducesResponseType(StatusCodes.Status404NotFound)]
  62. public ActionResult<IEnumerable<ConfigurationPageInfo>> GetConfigurationPages(
  63. [FromQuery] bool? enableInMainMenu,
  64. [FromQuery] ConfigurationPageType? pageType)
  65. {
  66. const string unavailableMessage = "The server is still loading. Please try again momentarily.";
  67. var pages = _appHost.GetExports<IPluginConfigurationPage>().ToList();
  68. if (pages == null)
  69. {
  70. return NotFound(unavailableMessage);
  71. }
  72. // Don't allow a failing plugin to fail them all
  73. var configPages = pages.Select(p =>
  74. {
  75. return new ConfigurationPageInfo(p);
  76. })
  77. .Where(i => i != null)
  78. .ToList();
  79. configPages.AddRange(_appHost.Plugins.SelectMany(GetConfigPages));
  80. if (pageType.HasValue)
  81. {
  82. configPages = configPages.Where(p => p.ConfigurationPageType == pageType).ToList();
  83. }
  84. if (enableInMainMenu.HasValue)
  85. {
  86. configPages = configPages.Where(p => p.EnableInMainMenu == enableInMainMenu.Value).ToList();
  87. }
  88. return configPages;
  89. }
  90. /// <summary>
  91. /// Gets a dashboard configuration page.
  92. /// </summary>
  93. /// <param name="name">The name of the page.</param>
  94. /// <response code="200">ConfigurationPage returned.</response>
  95. /// <response code="404">Plugin configuration page not found.</response>
  96. /// <returns>The configuration page.</returns>
  97. [HttpGet("/web/ConfigurationPage")]
  98. [ProducesResponseType(StatusCodes.Status200OK)]
  99. [ProducesResponseType(StatusCodes.Status404NotFound)]
  100. public ActionResult GetDashboardConfigurationPage([FromQuery] string name)
  101. {
  102. IPlugin? plugin = null;
  103. Stream? stream = null;
  104. var isJs = false;
  105. var isTemplate = false;
  106. var page = _appHost.GetExports<IPluginConfigurationPage>().FirstOrDefault(p => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase));
  107. if (page != null)
  108. {
  109. plugin = page.Plugin;
  110. stream = page.GetHtmlStream();
  111. }
  112. if (plugin == null)
  113. {
  114. var altPage = GetPluginPages().FirstOrDefault(p => string.Equals(p.Item1.Name, name, StringComparison.OrdinalIgnoreCase));
  115. if (altPage != null)
  116. {
  117. plugin = altPage.Item2;
  118. stream = plugin.GetType().Assembly.GetManifestResourceStream(altPage.Item1.EmbeddedResourcePath);
  119. isJs = string.Equals(Path.GetExtension(altPage.Item1.EmbeddedResourcePath), ".js", StringComparison.OrdinalIgnoreCase);
  120. isTemplate = altPage.Item1.EmbeddedResourcePath.EndsWith(".template.html", StringComparison.Ordinal);
  121. }
  122. }
  123. if (plugin != null && stream != null)
  124. {
  125. if (isJs)
  126. {
  127. return File(stream, MimeTypes.GetMimeType("page.js"));
  128. }
  129. if (isTemplate)
  130. {
  131. return File(stream, MimeTypes.GetMimeType("page.html"));
  132. }
  133. return File(stream, MimeTypes.GetMimeType("page.html"));
  134. }
  135. return NotFound();
  136. }
  137. /// <summary>
  138. /// Gets the robots.txt.
  139. /// </summary>
  140. /// <response code="200">Robots.txt returned.</response>
  141. /// <returns>The robots.txt.</returns>
  142. [HttpGet("/robots.txt")]
  143. [ProducesResponseType(StatusCodes.Status200OK)]
  144. [ApiExplorerSettings(IgnoreApi = true)]
  145. public ActionResult GetRobotsTxt()
  146. {
  147. return GetWebClientResource("robots.txt", string.Empty);
  148. }
  149. /// <summary>
  150. /// Gets a resource from the web client.
  151. /// </summary>
  152. /// <param name="resourceName">The resource name.</param>
  153. /// <param name="v">The v.</param>
  154. /// <response code="200">Web client returned.</response>
  155. /// <response code="404">Server does not host a web client.</response>
  156. /// <returns>The resource.</returns>
  157. [HttpGet("/web/{*resourceName}")]
  158. [ApiExplorerSettings(IgnoreApi = true)]
  159. [ProducesResponseType(StatusCodes.Status200OK)]
  160. [ProducesResponseType(StatusCodes.Status404NotFound)]
  161. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "v", Justification = "Imported from ServiceStack")]
  162. public ActionResult GetWebClientResource(
  163. [FromRoute] string resourceName,
  164. [FromQuery] string? v)
  165. {
  166. if (!_appConfig.HostWebClient() || WebClientUiPath == null)
  167. {
  168. return NotFound("Server does not host a web client.");
  169. }
  170. var path = resourceName;
  171. var basePath = WebClientUiPath;
  172. // Bounce them to the startup wizard if it hasn't been completed yet
  173. if (!_serverConfigurationManager.Configuration.IsStartupWizardCompleted
  174. && !Request.Path.Value.Contains("wizard", StringComparison.OrdinalIgnoreCase)
  175. && Request.Path.Value.Contains("index", StringComparison.OrdinalIgnoreCase))
  176. {
  177. return Redirect("index.html?start=wizard#!/wizardstart.html");
  178. }
  179. var stream = new FileStream(_resourceFileManager.GetResourcePath(basePath, path), FileMode.Open, FileAccess.Read);
  180. return File(stream, MimeTypes.GetMimeType(path));
  181. }
  182. /// <summary>
  183. /// Gets the favicon.
  184. /// </summary>
  185. /// <response code="200">Favicon.ico returned.</response>
  186. /// <returns>The favicon.</returns>
  187. [HttpGet("/favicon.ico")]
  188. [ProducesResponseType(StatusCodes.Status200OK)]
  189. [ApiExplorerSettings(IgnoreApi = true)]
  190. public ActionResult GetFavIcon()
  191. {
  192. return GetWebClientResource("favicon.ico", string.Empty);
  193. }
  194. /// <summary>
  195. /// Gets the path of the directory containing the static web interface content.
  196. /// </summary>
  197. /// <param name="appConfig">The app configuration.</param>
  198. /// <param name="serverConfigManager">The server configuration manager.</param>
  199. /// <returns>The directory path, or null if the server is not hosting the web client.</returns>
  200. public static string? GetWebClientUiPath(IConfiguration appConfig, IServerConfigurationManager serverConfigManager)
  201. {
  202. if (!appConfig.HostWebClient())
  203. {
  204. return null;
  205. }
  206. if (!string.IsNullOrEmpty(serverConfigManager.Configuration.DashboardSourcePath))
  207. {
  208. return serverConfigManager.Configuration.DashboardSourcePath;
  209. }
  210. return serverConfigManager.ApplicationPaths.WebPath;
  211. }
  212. private IEnumerable<ConfigurationPageInfo> GetConfigPages(IPlugin plugin)
  213. {
  214. return GetPluginPages(plugin).Select(i => new ConfigurationPageInfo(plugin, i.Item1));
  215. }
  216. private IEnumerable<Tuple<PluginPageInfo, IPlugin>> GetPluginPages(IPlugin plugin)
  217. {
  218. if (!(plugin is IHasWebPages))
  219. {
  220. return new List<Tuple<PluginPageInfo, IPlugin>>();
  221. }
  222. return (plugin as IHasWebPages)!.GetPages().Select(i => new Tuple<PluginPageInfo, IPlugin>(i, plugin));
  223. }
  224. private IEnumerable<Tuple<PluginPageInfo, IPlugin>> GetPluginPages()
  225. {
  226. return _appHost.Plugins.SelectMany(GetPluginPages);
  227. }
  228. }
  229. }