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

129 lines
5.0 KiB
C#
Raw Normal View History

#nullable enable
using System.Threading.Tasks;
using Jellyfin.Api.Models.ConfigurationDtos;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Serialization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Configuration Controller.
/// </summary>
[Route("System")]
[Authenticated]
public class ConfigurationController : BaseJellyfinApiController
{
private readonly IServerConfigurationManager _configurationManager;
private readonly IMediaEncoder _mediaEncoder;
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationController"/> class.
/// </summary>
/// <param name="configurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
/// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
/// <param name="jsonSerializer">Instance of the <see cref="IJsonSerializer"/> interface.</param>
public ConfigurationController(
IServerConfigurationManager configurationManager,
IMediaEncoder mediaEncoder,
IJsonSerializer jsonSerializer)
{
_configurationManager = configurationManager;
_mediaEncoder = mediaEncoder;
_jsonSerializer = jsonSerializer;
}
/// <summary>
/// Gets application configuration.
/// </summary>
/// <returns>Application configuration.</returns>
[HttpGet("Configuration")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<ServerConfiguration> GetConfiguration()
{
return Ok(_configurationManager.Configuration);
}
/// <summary>
/// Updates application configuration.
/// </summary>
/// <param name="configuration">Configuration.</param>
/// <returns>Status.</returns>
[HttpPost("Configuration")]
[Authenticated(Roles = "Admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult UpdateConfiguration([FromBody, BindRequired] ServerConfiguration configuration)
{
_configurationManager.ReplaceConfiguration(configuration);
return Ok();
}
/// <summary>
/// Gets a named configuration.
/// </summary>
/// <param name="key">Configuration key.</param>
/// <returns>Configuration.</returns>
[HttpGet("Configuration/{Key}")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<object> GetNamedConfiguration([FromRoute] string key)
{
return Ok(_configurationManager.GetConfiguration(key));
}
/// <summary>
/// Updates named configuration.
/// </summary>
/// <param name="key">Configuration key.</param>
/// <returns>Status.</returns>
[HttpPost("Configuration/{Key}")]
[Authenticated(Roles = "Admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult> UpdateNamedConfiguration([FromRoute] string key)
{
var configurationType = _configurationManager.GetConfigurationType(key);
/*
// TODO switch to System.Text.Json when https://github.com/dotnet/runtime/issues/30255 is fixed.
var configuration = await JsonSerializer.DeserializeAsync(Request.Body, configurationType);
*/
var configuration = await _jsonSerializer.DeserializeFromStreamAsync(Request.Body, configurationType)
.ConfigureAwait(false);
_configurationManager.SaveConfiguration(key, configuration);
return Ok();
}
/// <summary>
/// Gets a default MetadataOptions object.
/// </summary>
/// <returns>MetadataOptions.</returns>
[HttpGet("Configuration/MetadataOptions/Default")]
[Authenticated(Roles = "Admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<MetadataOptions> GetDefaultMetadataOptions()
{
return Ok(new MetadataOptions());
}
/// <summary>
/// Updates the path to the media encoder.
/// </summary>
/// <param name="mediaEncoderPath">Media encoder path form body.</param>
/// <returns>Status.</returns>
[HttpPost("MediaEncoder/Path")]
[Authenticated(Roles = "Admin", AllowBeforeStartupWizard = true)]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult UpdateMediaEncoderPath([FromForm, BindRequired] MediaEncoderPathDto mediaEncoderPath)
{
_mediaEncoder.UpdateEncoderPath(mediaEncoderPath.Path, mediaEncoderPath.PathType);
return Ok();
}
}
}