DashboardController.cs 11 KB

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