using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Models.ClientLogDtos;
using MediaBrowser.Controller.ClientEvent;
using MediaBrowser.Model.ClientLog;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
///
/// Client log controller.
///
[Authorize(Policy = Policies.DefaultAuthorization)]
public class ClientLogController : BaseJellyfinApiController
{
private readonly IClientEventLogger _clientEventLogger;
///
/// Initializes a new instance of the class.
///
/// Instance of the interface.
public ClientLogController(IClientEventLogger clientEventLogger)
{
_clientEventLogger = clientEventLogger;
}
///
/// Post event from client.
///
/// The client log dto.
/// Event logged.
/// Submission status.
[HttpPost]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult LogEvent([FromBody] ClientLogEventDto clientLogEventDto)
{
Log(clientLogEventDto);
return NoContent();
}
///
/// Bulk post events from client.
///
/// The list of client log dtos.
/// All events logged.
/// Submission status.
[HttpPost("Bulk")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos)
{
foreach (var dto in clientLogEventDtos)
{
Log(dto);
}
return NoContent();
}
///
/// Upload a log file.
///
/// The file.
/// Submission status.
[HttpPost("File")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task LogFile(IFormFile file)
{
await _clientEventLogger.WriteFileAsync(file.FileName, file.OpenReadStream())
.ConfigureAwait(false);
return NoContent();
}
private void Log(ClientLogEventDto dto)
{
_clientEventLogger.Log(new ClientLogEvent(
dto.Timestamp,
dto.Level,
dto.UserId,
dto.ClientName,
dto.ClientVersion,
dto.DeviceId,
dto.Message));
}
}
}