jellyfin/Jellyfin.Api/Controllers/ClientLogController.cs

127 lines
4.6 KiB
C#
Raw Normal View History

2021-10-28 01:20:14 +00:00
using System.Net.Mime;
using System.Threading.Tasks;
using Jellyfin.Api.Attributes;
2021-10-27 00:42:17 +00:00
using Jellyfin.Api.Constants;
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 MediaBrowser.Controller.Net;
2021-04-26 13:02:26 +00:00
using MediaBrowser.Model.ClientLog;
using Microsoft.AspNetCore.Authorization;
2021-04-26 13:02:26 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Client log controller.
/// </summary>
[Authorize(Policy = Policies.DefaultAuthorization)]
2021-04-26 13:02:26 +00:00
public class ClientLogController : BaseJellyfinApiController
{
2021-10-28 01:20:14 +00:00
private const int MaxDocumentSize = 1_000_000;
2021-04-26 13:02:26 +00:00
private readonly IClientEventLogger _clientEventLogger;
2021-10-28 01:20:14 +00:00
private readonly IAuthorizationContext _authorizationContext;
private readonly IServerConfigurationManager _serverConfigurationManager;
2021-04-26 13:02:26 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="ClientLogController"/> class.
/// </summary>
/// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param>
2021-10-28 01:20:14 +00:00
/// <param name="authorizationContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
public ClientLogController(
IClientEventLogger clientEventLogger,
IAuthorizationContext authorizationContext,
IServerConfigurationManager serverConfigurationManager)
2021-04-26 13:02:26 +00:00
{
_clientEventLogger = clientEventLogger;
2021-10-28 01:20:14 +00:00
_authorizationContext = authorizationContext;
_serverConfigurationManager = serverConfigurationManager;
2021-04-26 13:02:26 +00:00
}
/// <summary>
/// Post event from client.
/// </summary>
/// <param name="clientLogEventDto">The client log dto.</param>
/// <response code="204">Event logged.</response>
/// <returns>Submission status.</returns>
[HttpPost]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult LogEvent([FromBody] ClientLogEventDto clientLogEventDto)
{
2021-10-28 01:20:14 +00:00
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
{
return Forbid();
}
2021-04-26 13:02:26 +00:00
Log(clientLogEventDto);
return NoContent();
}
/// <summary>
/// Bulk post events from client.
/// </summary>
/// <param name="clientLogEventDtos">The list of client log dtos.</param>
/// <response code="204">All events logged.</response>
/// <returns>Submission status.</returns>
[HttpPost("Bulk")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos)
{
2021-10-28 01:20:14 +00:00
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
{
return Forbid();
}
2021-04-26 13:02:26 +00:00
foreach (var dto in clientLogEventDtos)
{
Log(dto);
}
return NoContent();
}
2021-10-27 00:42:17 +00:00
/// <summary>
2021-10-28 01:20:14 +00:00
/// Upload a document.
2021-10-27 00:42:17 +00:00
/// </summary>
/// <returns>Submission status.</returns>
2021-10-28 01:20:14 +00:00
[HttpPost("Document")]
2021-10-27 00:42:17 +00:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2021-10-28 01:20:14 +00:00
[AcceptsFile(MediaTypeNames.Text.Plain)]
[RequestSizeLimit(MaxDocumentSize)]
public async Task<ActionResult> LogFile()
2021-10-27 00:42:17 +00:00
{
2021-10-28 01:20:14 +00:00
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
{
return Forbid();
}
if (Request.ContentLength > MaxDocumentSize)
{
// Manually validate to return proper status code.
return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} bytes");
}
var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request)
.ConfigureAwait(false);
await _clientEventLogger.WriteDocumentAsync(authorizationInfo, Request.Body)
2021-10-27 00:42:17 +00:00
.ConfigureAwait(false);
return NoContent();
}
2021-04-26 13:02:26 +00:00
private void Log(ClientLogEventDto dto)
{
_clientEventLogger.Log(new ClientLogEvent(
dto.Timestamp,
dto.Level,
dto.UserId,
dto.ClientName,
dto.ClientVersion,
dto.DeviceId,
dto.Message));
}
}
2021-10-27 00:42:17 +00:00
}