jellyfin-server/Jellyfin.Api/Controllers/DashboardController.cs

116 lines
4.2 KiB
C#
Raw Normal View History

2020-11-21 13:26:03 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using Jellyfin.Api.Attributes;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Models;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Plugins;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2020-06-21 16:00:04 +00:00
using Microsoft.Extensions.Logging;
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Controllers;
/// <summary>
/// The dashboard controller.
/// </summary>
[Route("")]
public class DashboardController : BaseJellyfinApiController
{
2023-01-31 11:18:10 +00:00
private readonly ILogger<DashboardController> _logger;
private readonly IPluginManager _pluginManager;
/// <summary>
2023-01-31 11:18:10 +00:00
/// Initializes a new instance of the <see cref="DashboardController"/> class.
/// </summary>
2023-01-31 11:18:10 +00:00
/// <param name="logger">Instance of <see cref="ILogger{DashboardController}"/> interface.</param>
/// <param name="pluginManager">Instance of <see cref="IPluginManager"/> interface.</param>
public DashboardController(
ILogger<DashboardController> logger,
IPluginManager pluginManager)
{
2023-01-31 11:18:10 +00:00
_logger = logger;
_pluginManager = pluginManager;
}
2023-01-31 11:18:10 +00:00
/// <summary>
/// Gets the configuration pages.
/// </summary>
/// <param name="enableInMainMenu">Whether to enable in the main menu.</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>
[HttpGet("web/ConfigurationPages")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[Authorize(Policy = Policies.DefaultAuthorization)]
public ActionResult<IEnumerable<ConfigurationPageInfo>> GetConfigurationPages(
[FromQuery] bool? enableInMainMenu)
{
var configPages = _pluginManager.Plugins.SelectMany(GetConfigPages).ToList();
2023-01-31 11:18:10 +00:00
if (enableInMainMenu.HasValue)
{
2023-01-31 11:18:10 +00:00
configPages = configPages.Where(p => p.EnableInMainMenu == enableInMainMenu.Value).ToList();
}
2023-01-31 11:18:10 +00:00
return configPages;
}
2023-01-31 11:18:10 +00:00
/// <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>
[HttpGet("web/ConfigurationPage")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesFile(MediaTypeNames.Text.Html, "application/x-javascript")]
public ActionResult GetDashboardConfigurationPage([FromQuery] string? name)
{
var altPage = GetPluginPages().FirstOrDefault(p => string.Equals(p.Item1.Name, name, StringComparison.OrdinalIgnoreCase));
if (altPage is null)
{
return NotFound();
}
2023-01-31 11:18:10 +00:00
IPlugin plugin = altPage.Item2;
string resourcePath = altPage.Item1.EmbeddedResourcePath;
Stream? stream = plugin.GetType().Assembly.GetManifestResourceStream(resourcePath);
if (stream is null)
{
2023-01-31 11:18:10 +00:00
_logger.LogError("Failed to get resource {Resource} from plugin {Plugin}", resourcePath, plugin.Name);
return NotFound();
}
2023-01-31 11:18:10 +00:00
return File(stream, MimeTypes.GetMimeType(resourcePath));
}
2023-01-31 11:18:10 +00:00
private IEnumerable<ConfigurationPageInfo> GetConfigPages(LocalPlugin plugin)
{
return GetPluginPages(plugin).Select(i => new ConfigurationPageInfo(plugin.Instance, i.Item1));
}
2023-01-31 11:18:10 +00:00
private IEnumerable<Tuple<PluginPageInfo, IPlugin>> GetPluginPages(LocalPlugin plugin)
{
if (plugin.Instance is not IHasWebPages hasWebPages)
{
2023-01-31 11:18:10 +00:00
return Enumerable.Empty<Tuple<PluginPageInfo, IPlugin>>();
}
2023-01-31 11:18:10 +00:00
return hasWebPages.GetPages().Select(i => new Tuple<PluginPageInfo, IPlugin>(i, plugin.Instance));
}
private IEnumerable<Tuple<PluginPageInfo, IPlugin>> GetPluginPages()
{
return _pluginManager.Plugins.SelectMany(GetPluginPages);
}
}