DashboardController.cs 11 KB

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