jellyfin/Jellyfin.Api/Controllers/DashboardController.cs

148 lines
5.7 KiB
C#
Raw Normal View History

2020-11-21 06:26:03 -07:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using Jellyfin.Api.Attributes;
using Jellyfin.Api.Models;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Extensions;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Plugins;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
2020-06-21 09:00:04 -07:00
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// The dashboard controller.
/// </summary>
2020-08-04 11:48:53 -07:00
[Route("")]
public class DashboardController : BaseJellyfinApiController
{
2020-06-21 09:00:04 -07:00
private readonly ILogger<DashboardController> _logger;
private readonly IServerApplicationHost _appHost;
2020-12-06 16:48:54 -07:00
private readonly IPluginManager _pluginManager;
/// <summary>
/// Initializes a new instance of the <see cref="DashboardController"/> class.
/// </summary>
2020-06-21 09:00:04 -07:00
/// <param name="logger">Instance of <see cref="ILogger{DashboardController}"/> interface.</param>
/// <param name="appHost">Instance of <see cref="IServerApplicationHost"/> interface.</param>
2020-12-06 16:48:54 -07:00
/// <param name="pluginManager">Instance of <see cref="IPluginManager"/> interface.</param>
public DashboardController(
2020-06-21 09:00:04 -07:00
ILogger<DashboardController> logger,
2020-12-06 16:48:54 -07:00
IServerApplicationHost appHost,
IPluginManager pluginManager)
{
2020-06-21 09:00:04 -07:00
_logger = logger;
_appHost = appHost;
2020-12-06 16:48:54 -07:00
_pluginManager = pluginManager;
}
/// <summary>
/// Gets the configuration pages.
/// </summary>
/// <param name="enableInMainMenu">Whether to enable in the main menu.</param>
/// <param name="pageType">The <see cref="ConfigurationPageInfo"/>.</param>
/// <response code="200">ConfigurationPages returned.</response>
/// <response code="404">Server still loading.</response>
/// <returns>An <see cref="IEnumerable{ConfigurationPageInfo}"/> with infos about the plugins.</returns>
2020-08-04 11:48:53 -07:00
[HttpGet("web/ConfigurationPages")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-06-21 09:00:04 -07:00
public ActionResult<IEnumerable<ConfigurationPageInfo?>> GetConfigurationPages(
[FromQuery] bool? enableInMainMenu,
[FromQuery] ConfigurationPageType? pageType)
{
2021-01-12 15:10:23 -07:00
var configPages = _pluginManager.Plugins.SelectMany(GetConfigPages).ToList();
2020-06-21 03:35:06 -07:00
if (pageType.HasValue)
{
2020-06-21 09:00:04 -07:00
configPages = configPages.Where(p => p!.ConfigurationPageType == pageType).ToList();
}
if (enableInMainMenu.HasValue)
{
2020-06-21 09:00:04 -07:00
configPages = configPages.Where(p => p!.EnableInMainMenu == enableInMainMenu.Value).ToList();
}
return configPages;
}
/// <summary>
/// Gets a dashboard configuration page.
/// </summary>
/// <param name="name">The name of the page.</param>
/// <response code="200">ConfigurationPage returned.</response>
/// <response code="404">Plugin configuration page not found.</response>
/// <returns>The configuration page.</returns>
2020-08-04 11:48:53 -07:00
[HttpGet("web/ConfigurationPage")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesFile(MediaTypeNames.Text.Html, "application/x-javascript")]
2020-06-27 09:50:44 -07:00
public ActionResult GetDashboardConfigurationPage([FromQuery] string? name)
{
IPlugin? plugin = null;
Stream? stream = null;
var isJs = false;
var isTemplate = false;
2021-01-12 15:10:23 -07:00
var altPage = GetPluginPages().FirstOrDefault(p => string.Equals(p.Item1.Name, name, StringComparison.OrdinalIgnoreCase));
if (altPage != null)
{
2021-01-12 15:10:23 -07:00
plugin = altPage.Item2;
stream = plugin.GetType().Assembly.GetManifestResourceStream(altPage.Item1.EmbeddedResourcePath);
2021-01-12 15:10:23 -07:00
isJs = string.Equals(Path.GetExtension(altPage.Item1.EmbeddedResourcePath), ".js", StringComparison.OrdinalIgnoreCase);
isTemplate = altPage.Item1.EmbeddedResourcePath.EndsWith(".template.html", StringComparison.Ordinal);
}
if (plugin != null && stream != null)
{
if (isJs)
{
return File(stream, MimeTypes.GetMimeType("page.js"));
}
if (isTemplate)
{
return File(stream, MimeTypes.GetMimeType("page.html"));
}
return File(stream, MimeTypes.GetMimeType("page.html"));
}
return NotFound();
}
2020-12-06 16:48:54 -07:00
private IEnumerable<ConfigurationPageInfo> GetConfigPages(LocalPlugin plugin)
{
2020-12-06 16:48:54 -07:00
return GetPluginPages(plugin).Select(i => new ConfigurationPageInfo(plugin.Instance, i.Item1));
}
2020-12-06 16:48:54 -07:00
private IEnumerable<Tuple<PluginPageInfo, IPlugin>> GetPluginPages(LocalPlugin? plugin)
{
2020-12-06 16:48:54 -07:00
if (plugin?.Instance is not IHasWebPages hasWebPages)
{
return new List<Tuple<PluginPageInfo, IPlugin>>();
}
2020-12-06 16:48:54 -07:00
return hasWebPages.GetPages().Select(i => new Tuple<PluginPageInfo, IPlugin>(i, plugin.Instance));
}
private IEnumerable<Tuple<PluginPageInfo, IPlugin>> GetPluginPages()
{
2020-12-06 16:48:54 -07:00
return _pluginManager.Plugins.SelectMany(GetPluginPages);
}
}
}