2
0

ClientLogController.cs 3.0 KB

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