ClientLogController.cs 3.3 KB

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