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