2021-10-28 03:20:14 +02:00
|
|
|
|
using System.Net.Mime;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Jellyfin.Api.Attributes;
|
2021-10-27 02:42:17 +02:00
|
|
|
|
using Jellyfin.Api.Constants;
|
2021-05-04 15:16:35 +02:00
|
|
|
|
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 MediaBrowser.Controller.Net;
|
2021-04-26 15:02:26 +02:00
|
|
|
|
using MediaBrowser.Model.ClientLog;
|
2021-05-04 15:16:35 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-04-26 15:02:26 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Client log controller.
|
|
|
|
|
/// </summary>
|
2021-05-04 15:16:35 +02:00
|
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2021-04-26 15:02:26 +02:00
|
|
|
|
public class ClientLogController : BaseJellyfinApiController
|
|
|
|
|
{
|
2021-10-28 03:20:14 +02:00
|
|
|
|
private const int MaxDocumentSize = 1_000_000;
|
2021-04-26 15:02:26 +02:00
|
|
|
|
private readonly IClientEventLogger _clientEventLogger;
|
2021-10-28 03:20:14 +02:00
|
|
|
|
private readonly IAuthorizationContext _authorizationContext;
|
|
|
|
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
2021-04-26 15:02:26 +02: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 03:20:14 +02: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 15:02:26 +02:00
|
|
|
|
{
|
|
|
|
|
_clientEventLogger = clientEventLogger;
|
2021-10-28 03:20:14 +02:00
|
|
|
|
_authorizationContext = authorizationContext;
|
|
|
|
|
_serverConfigurationManager = serverConfigurationManager;
|
2021-04-26 15:02:26 +02: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 03:20:14 +02:00
|
|
|
|
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
|
|
|
|
|
{
|
|
|
|
|
return Forbid();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:02:26 +02: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 03:20:14 +02:00
|
|
|
|
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
|
|
|
|
|
{
|
|
|
|
|
return Forbid();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:02:26 +02:00
|
|
|
|
foreach (var dto in clientLogEventDtos)
|
|
|
|
|
{
|
|
|
|
|
Log(dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 02:42:17 +02:00
|
|
|
|
/// <summary>
|
2021-10-28 03:20:14 +02:00
|
|
|
|
/// Upload a document.
|
2021-10-27 02:42:17 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Submission status.</returns>
|
2021-10-28 03:20:14 +02:00
|
|
|
|
[HttpPost("Document")]
|
2021-10-27 02:42:17 +02:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2021-10-28 03:20:14 +02:00
|
|
|
|
[AcceptsFile(MediaTypeNames.Text.Plain)]
|
|
|
|
|
[RequestSizeLimit(MaxDocumentSize)]
|
|
|
|
|
public async Task<ActionResult> LogFile()
|
2021-10-27 02:42:17 +02:00
|
|
|
|
{
|
2021-10-28 03:20:14 +02: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 02:42:17 +02:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:02:26 +02: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 02:42:17 +02:00
|
|
|
|
}
|