ClientLogController.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Net.Mime;
  2. using System.Threading.Tasks;
  3. using Jellyfin.Api.Attributes;
  4. using Jellyfin.Api.Constants;
  5. using Jellyfin.Api.Helpers;
  6. using Jellyfin.Api.Models.ClientLogDtos;
  7. using MediaBrowser.Controller.ClientEvent;
  8. using MediaBrowser.Controller.Configuration;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. namespace Jellyfin.Api.Controllers
  13. {
  14. /// <summary>
  15. /// Client log controller.
  16. /// </summary>
  17. [Authorize(Policy = Policies.DefaultAuthorization)]
  18. public class ClientLogController : BaseJellyfinApiController
  19. {
  20. private const int MaxDocumentSize = 1_000_000;
  21. private readonly IClientEventLogger _clientEventLogger;
  22. private readonly IServerConfigurationManager _serverConfigurationManager;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="ClientLogController"/> class.
  25. /// </summary>
  26. /// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param>
  27. /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  28. public ClientLogController(
  29. IClientEventLogger clientEventLogger,
  30. IServerConfigurationManager serverConfigurationManager)
  31. {
  32. _clientEventLogger = clientEventLogger;
  33. _serverConfigurationManager = serverConfigurationManager;
  34. }
  35. /// <summary>
  36. /// Upload a document.
  37. /// </summary>
  38. /// <response code="200">Document saved.</response>
  39. /// <response code="403">Event logging disabled.</response>
  40. /// <response code="413">Upload size too large.</response>
  41. /// <returns>Create response.</returns>
  42. [HttpPost("Document")]
  43. [ProducesResponseType(typeof(ClientLogDocumentResponseDto), StatusCodes.Status200OK)]
  44. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  45. [ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
  46. [AcceptsFile(MediaTypeNames.Text.Plain)]
  47. [RequestSizeLimit(MaxDocumentSize)]
  48. public async Task<ActionResult<ClientLogDocumentResponseDto>> LogFile()
  49. {
  50. if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
  51. {
  52. return Forbid();
  53. }
  54. if (Request.ContentLength > MaxDocumentSize)
  55. {
  56. // Manually validate to return proper status code.
  57. return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} bytes");
  58. }
  59. var (clientName, clientVersion) = GetRequestInformation();
  60. var fileName = await _clientEventLogger.WriteDocumentAsync(clientName, clientVersion, Request.Body)
  61. .ConfigureAwait(false);
  62. return Ok(new ClientLogDocumentResponseDto(fileName));
  63. }
  64. private (string ClientName, string ClientVersion) GetRequestInformation()
  65. {
  66. var clientName = ClaimHelpers.GetClient(HttpContext.User) ?? "unknown-client";
  67. var clientVersion = ClaimHelpers.GetIsApiKey(HttpContext.User)
  68. ? "apikey"
  69. : ClaimHelpers.GetVersion(HttpContext.User) ?? "unknown-version";
  70. return (clientName, clientVersion);
  71. }
  72. }
  73. }