ClientLogController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Net.Mime;
  2. using System.Threading.Tasks;
  3. using Jellyfin.Api.Attributes;
  4. using Jellyfin.Api.Constants;
  5. using Jellyfin.Api.Models.ClientLogDtos;
  6. using MediaBrowser.Controller.ClientEvent;
  7. using MediaBrowser.Controller.Configuration;
  8. using MediaBrowser.Controller.Net;
  9. using MediaBrowser.Model.ClientLog;
  10. using Microsoft.AspNetCore.Authorization;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Mvc;
  13. namespace Jellyfin.Api.Controllers
  14. {
  15. /// <summary>
  16. /// Client log controller.
  17. /// </summary>
  18. [Authorize(Policy = Policies.DefaultAuthorization)]
  19. public class ClientLogController : BaseJellyfinApiController
  20. {
  21. private const int MaxDocumentSize = 1_000_000;
  22. private readonly IClientEventLogger _clientEventLogger;
  23. private readonly IAuthorizationContext _authorizationContext;
  24. private readonly IServerConfigurationManager _serverConfigurationManager;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="ClientLogController"/> class.
  27. /// </summary>
  28. /// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param>
  29. /// <param name="authorizationContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
  30. /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  31. public ClientLogController(
  32. IClientEventLogger clientEventLogger,
  33. IAuthorizationContext authorizationContext,
  34. IServerConfigurationManager serverConfigurationManager)
  35. {
  36. _clientEventLogger = clientEventLogger;
  37. _authorizationContext = authorizationContext;
  38. _serverConfigurationManager = serverConfigurationManager;
  39. }
  40. /// <summary>
  41. /// Post event from client.
  42. /// </summary>
  43. /// <param name="clientLogEventDto">The client log dto.</param>
  44. /// <response code="204">Event logged.</response>
  45. /// <response code="403">Event logging disabled.</response>
  46. /// <returns>Submission status.</returns>
  47. [HttpPost]
  48. [ProducesResponseType(StatusCodes.Status204NoContent)]
  49. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  50. public async Task<ActionResult> LogEvent([FromBody] ClientLogEventDto clientLogEventDto)
  51. {
  52. if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
  53. {
  54. return Forbid();
  55. }
  56. var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request)
  57. .ConfigureAwait(false);
  58. Log(clientLogEventDto, authorizationInfo);
  59. return NoContent();
  60. }
  61. /// <summary>
  62. /// Bulk post events from client.
  63. /// </summary>
  64. /// <param name="clientLogEventDtos">The list of client log dtos.</param>
  65. /// <response code="204">All events logged.</response>
  66. /// <response code="403">Event logging disabled.</response>
  67. /// <returns>Submission status.</returns>
  68. [HttpPost("Bulk")]
  69. [ProducesResponseType(StatusCodes.Status204NoContent)]
  70. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  71. public async Task<ActionResult> LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos)
  72. {
  73. if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
  74. {
  75. return Forbid();
  76. }
  77. var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request)
  78. .ConfigureAwait(false);
  79. foreach (var dto in clientLogEventDtos)
  80. {
  81. Log(dto, authorizationInfo);
  82. }
  83. return NoContent();
  84. }
  85. /// <summary>
  86. /// Upload a document.
  87. /// </summary>
  88. /// <response code="200">Document saved.</response>
  89. /// <response code="403">Event logging disabled.</response>
  90. /// <response code="413">Upload size too large.</response>
  91. /// <returns>Create response.</returns>
  92. [HttpPost("Document")]
  93. [ProducesResponseType(typeof(ClientLogDocumentResponseDto), StatusCodes.Status200OK)]
  94. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  95. [ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
  96. [AcceptsFile(MediaTypeNames.Text.Plain)]
  97. [RequestSizeLimit(MaxDocumentSize)]
  98. public async Task<ActionResult<ClientLogDocumentResponseDto>> LogFile()
  99. {
  100. if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
  101. {
  102. return Forbid();
  103. }
  104. if (Request.ContentLength > MaxDocumentSize)
  105. {
  106. // Manually validate to return proper status code.
  107. return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} bytes");
  108. }
  109. var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request)
  110. .ConfigureAwait(false);
  111. var fileName = await _clientEventLogger.WriteDocumentAsync(authorizationInfo, Request.Body)
  112. .ConfigureAwait(false);
  113. return Ok(new ClientLogDocumentResponseDto(fileName));
  114. }
  115. private void Log(ClientLogEventDto dto, AuthorizationInfo authorizationInfo)
  116. {
  117. _clientEventLogger.Log(new ClientLogEvent(
  118. dto.Timestamp,
  119. dto.Level,
  120. authorizationInfo.UserId,
  121. authorizationInfo.Client,
  122. authorizationInfo.Version,
  123. authorizationInfo.DeviceId,
  124. dto.Message));
  125. }
  126. }
  127. }