jellyfin/Jellyfin.Api/Controllers/ClientLogController.cs

79 lines
3 KiB
C#
Raw Normal View History

2021-11-20 16:47:05 +01:00
using System.Net.Mime;
2021-10-28 03:20:14 +02:00
using System.Threading.Tasks;
using Jellyfin.Api.Attributes;
using Jellyfin.Api.Extensions;
using Jellyfin.Api.Models.ClientLogDtos;
2021-04-26 15:02:26 +02:00
using MediaBrowser.Controller.ClientEvent;
2021-10-28 03:20:14 +02:00
using MediaBrowser.Controller.Configuration;
using Microsoft.AspNetCore.Authorization;
2021-04-26 15:02:26 +02:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2023-01-31 12:18:10 +01:00
namespace Jellyfin.Api.Controllers;
/// <summary>
/// Client log controller.
/// </summary>
2023-02-08 23:55:26 +01:00
[Authorize]
2023-01-31 12:18:10 +01:00
public class ClientLogController : BaseJellyfinApiController
2021-04-26 15:02:26 +02:00
{
2023-01-31 12:18:10 +01:00
private const int MaxDocumentSize = 1_000_000;
private readonly IClientEventLogger _clientEventLogger;
private readonly IServerConfigurationManager _serverConfigurationManager;
2021-04-26 15:02:26 +02:00
/// <summary>
2023-01-31 12:18:10 +01:00
/// Initializes a new instance of the <see cref="ClientLogController"/> class.
2021-04-26 15:02:26 +02:00
/// </summary>
2023-01-31 12:18:10 +01: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 15:02:26 +02:00
{
2023-01-31 12:18:10 +01:00
_clientEventLogger = clientEventLogger;
_serverConfigurationManager = serverConfigurationManager;
}
2021-04-26 15:02:26 +02:00
2023-01-31 12:18:10 +01: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 15:02:26 +02:00
{
2023-01-31 12:18:10 +01:00
return Forbid();
2021-04-26 15:02:26 +02:00
}
2023-01-31 12:18:10 +01:00
if (Request.ContentLength > MaxDocumentSize)
2021-10-27 02:42:17 +02:00
{
2023-01-31 12:18:10 +01:00
// Manually validate to return proper status code.
return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} bytes");
2021-10-27 02:42:17 +02:00
}
2023-01-31 12:18:10 +01:00
var (clientName, clientVersion) = GetRequestInformation();
var fileName = await _clientEventLogger.WriteDocumentAsync(clientName, clientVersion, Request.Body)
.ConfigureAwait(false);
return Ok(new ClientLogDocumentResponseDto(fileName));
}
2021-11-05 17:40:45 +01:00
2023-01-31 12:18:10 +01: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 15:02:26 +02:00
}
2021-10-27 02:42:17 +02:00
}