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

79 lines
3.0 KiB
C#
Raw Normal View History

2021-11-20 15:47:05 +00:00
using System.Net.Mime;
2021-10-28 01:20:14 +00:00
using System.Threading.Tasks;
using Jellyfin.Api.Attributes;
using Jellyfin.Api.Extensions;
using Jellyfin.Api.Models.ClientLogDtos;
2021-04-26 13:02:26 +00:00
using MediaBrowser.Controller.ClientEvent;
2021-10-28 01:20:14 +00:00
using MediaBrowser.Controller.Configuration;
using Microsoft.AspNetCore.Authorization;
2021-04-26 13:02:26 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Controllers;
/// <summary>
/// Client log controller.
/// </summary>
2023-02-08 22:55:26 +00:00
[Authorize]
2023-01-31 11:18:10 +00:00
public class ClientLogController : BaseJellyfinApiController
2021-04-26 13:02:26 +00:00
{
2023-01-31 11:18:10 +00:00
private const int MaxDocumentSize = 1_000_000;
private readonly IClientEventLogger _clientEventLogger;
private readonly IServerConfigurationManager _serverConfigurationManager;
2021-04-26 13:02:26 +00:00
/// <summary>
2023-01-31 11:18:10 +00:00
/// Initializes a new instance of the <see cref="ClientLogController"/> class.
2021-04-26 13:02:26 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param>
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
public ClientLogController(
IClientEventLogger clientEventLogger,
IServerConfigurationManager serverConfigurationManager)
2021-04-26 13:02:26 +00:00
{
2023-01-31 11:18:10 +00:00
_clientEventLogger = clientEventLogger;
_serverConfigurationManager = serverConfigurationManager;
}
2021-04-26 13:02:26 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Upload a document.
/// </summary>
/// <response code="200">Document saved.</response>
/// <response code="403">Event logging disabled.</response>
/// <response code="413">Upload size too large.</response>
/// <returns>Create response.</returns>
[HttpPost("Document")]
[ProducesResponseType(typeof(ClientLogDocumentResponseDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
[AcceptsFile(MediaTypeNames.Text.Plain)]
[RequestSizeLimit(MaxDocumentSize)]
public async Task<ActionResult<ClientLogDocumentResponseDto>> LogFile()
{
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
2021-04-26 13:02:26 +00:00
{
2023-01-31 11:18:10 +00:00
return Forbid();
2021-04-26 13:02:26 +00:00
}
2023-01-31 11:18:10 +00:00
if (Request.ContentLength > MaxDocumentSize)
2021-10-27 00:42:17 +00:00
{
2023-01-31 11:18:10 +00:00
// Manually validate to return proper status code.
return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} bytes");
2021-10-27 00:42:17 +00:00
}
2023-01-31 11:18:10 +00:00
var (clientName, clientVersion) = GetRequestInformation();
var fileName = await _clientEventLogger.WriteDocumentAsync(clientName, clientVersion, Request.Body)
.ConfigureAwait(false);
return Ok(new ClientLogDocumentResponseDto(fileName));
}
2021-11-05 16:40:45 +00:00
2023-01-31 11:18:10 +00:00
private (string ClientName, string ClientVersion) GetRequestInformation()
{
var clientName = HttpContext.User.GetClient() ?? "unknown-client";
var clientVersion = HttpContext.User.GetIsApiKey()
? "apikey"
: HttpContext.User.GetVersion() ?? "unknown-version";
return (clientName, clientVersion);
2021-04-26 13:02:26 +00:00
}
2021-10-27 00:42:17 +00:00
}