jellyfin/Jellyfin.Api/Controllers/ClientLogController.cs

81 lines
3.3 KiB
C#
Raw Normal View History

2021-11-20 08:47:05 -07:00
using System.Net.Mime;
2021-10-27 18:20:14 -07:00
using System.Threading.Tasks;
using Jellyfin.Api.Attributes;
2021-10-26 17:42:17 -07:00
using Jellyfin.Api.Constants;
2021-11-05 09:40:45 -07:00
using Jellyfin.Api.Helpers;
using Jellyfin.Api.Models.ClientLogDtos;
2021-04-26 06:02:26 -07:00
using MediaBrowser.Controller.ClientEvent;
2021-10-27 18:20:14 -07:00
using MediaBrowser.Controller.Configuration;
using Microsoft.AspNetCore.Authorization;
2021-04-26 06:02:26 -07:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Client log controller.
/// </summary>
[Authorize(Policy = Policies.DefaultAuthorization)]
2021-04-26 06:02:26 -07:00
public class ClientLogController : BaseJellyfinApiController
{
2021-10-27 18:20:14 -07:00
private const int MaxDocumentSize = 1_000_000;
2021-04-26 06:02:26 -07:00
private readonly IClientEventLogger _clientEventLogger;
2021-10-27 18:20:14 -07:00
private readonly IServerConfigurationManager _serverConfigurationManager;
2021-04-26 06:02:26 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="ClientLogController"/> class.
/// </summary>
/// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param>
2021-10-27 18:20:14 -07:00
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
public ClientLogController(
IClientEventLogger clientEventLogger,
IServerConfigurationManager serverConfigurationManager)
2021-04-26 06:02:26 -07:00
{
_clientEventLogger = clientEventLogger;
2021-10-27 18:20:14 -07:00
_serverConfigurationManager = serverConfigurationManager;
2021-04-26 06:02:26 -07:00
}
2021-10-26 17:42:17 -07:00
/// <summary>
2021-10-27 18:20:14 -07:00
/// Upload a document.
2021-10-26 17:42:17 -07:00
/// </summary>
/// <response code="200">Document saved.</response>
/// <response code="403">Event logging disabled.</response>
/// <response code="413">Upload size too large.</response>
2021-10-29 05:33:34 -07:00
/// <returns>Create response.</returns>
2021-10-27 18:20:14 -07:00
[HttpPost("Document")]
2021-10-29 05:33:34 -07:00
[ProducesResponseType(typeof(ClientLogDocumentResponseDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
2021-10-27 18:20:14 -07:00
[AcceptsFile(MediaTypeNames.Text.Plain)]
[RequestSizeLimit(MaxDocumentSize)]
2021-10-29 05:33:34 -07:00
public async Task<ActionResult<ClientLogDocumentResponseDto>> LogFile()
2021-10-26 17:42:17 -07:00
{
2021-10-27 18:20:14 -07:00
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
{
return Forbid();
}
if (Request.ContentLength > MaxDocumentSize)
{
// Manually validate to return proper status code.
return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} bytes");
}
2021-11-20 08:47:05 -07:00
var (clientName, clientVersion) = GetRequestInformation();
2021-11-05 09:40:45 -07:00
var fileName = await _clientEventLogger.WriteDocumentAsync(clientName, clientVersion, Request.Body)
2021-10-26 17:42:17 -07:00
.ConfigureAwait(false);
2021-10-29 05:33:34 -07:00
return Ok(new ClientLogDocumentResponseDto(fileName));
2021-10-26 17:42:17 -07:00
}
2021-11-20 08:47:05 -07:00
private (string ClientName, string ClientVersion) GetRequestInformation()
2021-11-05 09:40:45 -07:00
{
var clientName = ClaimHelpers.GetClient(HttpContext.User) ?? "unknown-client";
var clientVersion = ClaimHelpers.GetIsApiKey(HttpContext.User)
? "apikey"
: ClaimHelpers.GetVersion(HttpContext.User) ?? "unknown-version";
2021-11-20 08:47:05 -07:00
return (clientName, clientVersion);
2021-11-05 09:40:45 -07:00
}
2021-04-26 06:02:26 -07:00
}
2021-10-26 17:42:17 -07:00
}