ClientLogController.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Threading.Tasks;
  2. using Jellyfin.Api.Constants;
  3. using Jellyfin.Api.Models.ClientLogDtos;
  4. using MediaBrowser.Controller.ClientEvent;
  5. using MediaBrowser.Model.ClientLog;
  6. using Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.Mvc;
  9. namespace Jellyfin.Api.Controllers
  10. {
  11. /// <summary>
  12. /// Client log controller.
  13. /// </summary>
  14. [Authorize(Policy = Policies.DefaultAuthorization)]
  15. public class ClientLogController : BaseJellyfinApiController
  16. {
  17. private readonly IClientEventLogger _clientEventLogger;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="ClientLogController"/> class.
  20. /// </summary>
  21. /// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param>
  22. public ClientLogController(IClientEventLogger clientEventLogger)
  23. {
  24. _clientEventLogger = clientEventLogger;
  25. }
  26. /// <summary>
  27. /// Post event from client.
  28. /// </summary>
  29. /// <param name="clientLogEventDto">The client log dto.</param>
  30. /// <response code="204">Event logged.</response>
  31. /// <returns>Submission status.</returns>
  32. [HttpPost]
  33. [ProducesResponseType(StatusCodes.Status204NoContent)]
  34. public ActionResult LogEvent([FromBody] ClientLogEventDto clientLogEventDto)
  35. {
  36. Log(clientLogEventDto);
  37. return NoContent();
  38. }
  39. /// <summary>
  40. /// Bulk post events from client.
  41. /// </summary>
  42. /// <param name="clientLogEventDtos">The list of client log dtos.</param>
  43. /// <response code="204">All events logged.</response>
  44. /// <returns>Submission status.</returns>
  45. [HttpPost("Bulk")]
  46. [ProducesResponseType(StatusCodes.Status204NoContent)]
  47. public ActionResult LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos)
  48. {
  49. foreach (var dto in clientLogEventDtos)
  50. {
  51. Log(dto);
  52. }
  53. return NoContent();
  54. }
  55. /// <summary>
  56. /// Upload a log file.
  57. /// </summary>
  58. /// <param name="file">The file.</param>
  59. /// <returns>Submission status.</returns>
  60. [HttpPost("File")]
  61. [ProducesResponseType(StatusCodes.Status204NoContent)]
  62. public async Task<ActionResult> LogFile(IFormFile file)
  63. {
  64. await _clientEventLogger.WriteFileAsync(file.FileName, file.OpenReadStream())
  65. .ConfigureAwait(false);
  66. return NoContent();
  67. }
  68. private void Log(ClientLogEventDto dto)
  69. {
  70. _clientEventLogger.Log(new ClientLogEvent(
  71. dto.Timestamp,
  72. dto.Level,
  73. dto.UserId,
  74. dto.ClientName,
  75. dto.ClientVersion,
  76. dto.DeviceId,
  77. dto.Message));
  78. }
  79. }
  80. }