ClientLogController.cs 6.0 KB

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