DashboardController.cs 11 KB

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