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;
|
2021-05-04 13:16:35 +00:00
|
|
|
|
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;
|
2021-05-04 13:16:35 +00:00
|
|
|
|
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>
|
2021-05-04 13:16:35 +00:00
|
|
|
|
[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>
|
2021-10-28 22:11:14 +00:00
|
|
|
|
/// <response code="403">Event logging disabled.</response>
|
2021-04-26 13:02:26 +00:00
|
|
|
|
/// <returns>Submission status.</returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2021-10-28 22:11:14 +00:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
|
|
|
public async Task<ActionResult> LogEvent([FromBody] ClientLogEventDto clientLogEventDto)
|
2021-04-26 13:02:26 +00:00
|
|
|
|
{
|
2021-10-28 01:20:14 +00:00
|
|
|
|
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
|
|
|
|
|
{
|
|
|
|
|
return Forbid();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 22:11:14 +00:00
|
|
|
|
var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
Log(clientLogEventDto, authorizationInfo);
|
2021-04-26 13:02:26 +00:00
|
|
|
|
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>
|
2021-10-28 22:11:14 +00:00
|
|
|
|
/// <response code="403">Event logging disabled.</response>
|
2021-04-26 13:02:26 +00:00
|
|
|
|
/// <returns>Submission status.</returns>
|
|
|
|
|
[HttpPost("Bulk")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2021-10-28 22:11:14 +00:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
|
|
|
public async Task<ActionResult> LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos)
|
2021-04-26 13:02:26 +00:00
|
|
|
|
{
|
2021-10-28 01:20:14 +00:00
|
|
|
|
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
|
|
|
|
|
{
|
|
|
|
|
return Forbid();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 22:11:14 +00:00
|
|
|
|
var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-04-26 13:02:26 +00:00
|
|
|
|
foreach (var dto in clientLogEventDtos)
|
|
|
|
|
{
|
2021-10-28 22:11:14 +00:00
|
|
|
|
Log(dto, authorizationInfo);
|
2021-04-26 13:02:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>
|
2021-10-28 22:11:14 +00:00
|
|
|
|
/// <response code="200">Document saved.</response>
|
|
|
|
|
/// <response code="403">Event logging disabled.</response>
|
|
|
|
|
/// <response code="413">Upload size too large.</response>
|
|
|
|
|
/// <returns>Created file name.</returns>
|
2021-10-28 01:20:14 +00:00
|
|
|
|
[HttpPost("Document")]
|
2021-10-28 22:11:14 +00:00
|
|
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
|
2021-10-28 01:20:14 +00:00
|
|
|
|
[AcceptsFile(MediaTypeNames.Text.Plain)]
|
|
|
|
|
[RequestSizeLimit(MaxDocumentSize)]
|
2021-10-28 22:11:14 +00:00
|
|
|
|
public async Task<ActionResult<string>> 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);
|
|
|
|
|
|
2021-10-28 22:11:14 +00:00
|
|
|
|
var fileName = await _clientEventLogger.WriteDocumentAsync(authorizationInfo, Request.Body)
|
2021-10-27 00:42:17 +00:00
|
|
|
|
.ConfigureAwait(false);
|
2021-10-28 22:11:14 +00:00
|
|
|
|
return Ok(fileName);
|
2021-10-27 00:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 22:11:14 +00:00
|
|
|
|
private void Log(ClientLogEventDto dto, AuthorizationInfo authorizationInfo)
|
2021-04-26 13:02:26 +00:00
|
|
|
|
{
|
|
|
|
|
_clientEventLogger.Log(new ClientLogEvent(
|
|
|
|
|
dto.Timestamp,
|
|
|
|
|
dto.Level,
|
2021-10-28 22:11:14 +00:00
|
|
|
|
authorizationInfo.UserId,
|
|
|
|
|
authorizationInfo.Client,
|
|
|
|
|
authorizationInfo.Version,
|
|
|
|
|
authorizationInfo.DeviceId,
|
2021-04-26 13:02:26 +00:00
|
|
|
|
dto.Message));
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-27 00:42:17 +00:00
|
|
|
|
}
|