| | 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 | |
|
| | 12 | | namespace Jellyfin.Api.Controllers; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Client log controller. |
| | 16 | | /// </summary> |
| | 17 | | [Authorize] |
| | 18 | | public class ClientLogController : BaseJellyfinApiController |
| | 19 | | { |
| | 20 | | private const int MaxDocumentSize = 1_000_000; |
| | 21 | | private readonly IClientEventLogger _clientEventLogger; |
| | 22 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | 23 | |
|
| | 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.</p |
| 0 | 29 | | public ClientLogController( |
| 0 | 30 | | IClientEventLogger clientEventLogger, |
| 0 | 31 | | IServerConfigurationManager serverConfigurationManager) |
| | 32 | | { |
| 0 | 33 | | _clientEventLogger = clientEventLogger; |
| 0 | 34 | | _serverConfigurationManager = serverConfigurationManager; |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Upload a document. |
| | 39 | | /// </summary> |
| | 40 | | /// <response code="200">Document saved.</response> |
| | 41 | | /// <response code="403">Event logging disabled.</response> |
| | 42 | | /// <response code="413">Upload size too large.</response> |
| | 43 | | /// <returns>Create response.</returns> |
| | 44 | | [HttpPost("Document")] |
| | 45 | | [ProducesResponseType(typeof(ClientLogDocumentResponseDto), StatusCodes.Status200OK)] |
| | 46 | | [ProducesResponseType(StatusCodes.Status403Forbidden)] |
| | 47 | | [ProducesResponseType(StatusCodes.Status413PayloadTooLarge)] |
| | 48 | | [AcceptsFile(MediaTypeNames.Text.Plain)] |
| | 49 | | [RequestSizeLimit(MaxDocumentSize)] |
| | 50 | | public async Task<ActionResult<ClientLogDocumentResponseDto>> LogFile() |
| | 51 | | { |
| | 52 | | if (!_serverConfigurationManager.Configuration.AllowClientLogUpload) |
| | 53 | | { |
| | 54 | | return Forbid(); |
| | 55 | | } |
| | 56 | |
|
| | 57 | | if (Request.ContentLength > MaxDocumentSize) |
| | 58 | | { |
| | 59 | | // Manually validate to return proper status code. |
| | 60 | | return StatusCode(StatusCodes.Status413PayloadTooLarge, $"Payload must be less than {MaxDocumentSize:N0} byt |
| | 61 | | } |
| | 62 | |
|
| | 63 | | var (clientName, clientVersion) = GetRequestInformation(); |
| | 64 | | var fileName = await _clientEventLogger.WriteDocumentAsync(clientName, clientVersion, Request.Body) |
| | 65 | | .ConfigureAwait(false); |
| | 66 | | return Ok(new ClientLogDocumentResponseDto(fileName)); |
| | 67 | | } |
| | 68 | |
|
| | 69 | | private (string ClientName, string ClientVersion) GetRequestInformation() |
| | 70 | | { |
| 0 | 71 | | var clientName = HttpContext.User.GetClient() ?? "unknown-client"; |
| 0 | 72 | | var clientVersion = HttpContext.User.GetIsApiKey() |
| 0 | 73 | | ? "apikey" |
| 0 | 74 | | : HttpContext.User.GetVersion() ?? "unknown-version"; |
| | 75 | |
|
| 0 | 76 | | return (clientName, clientVersion); |
| | 77 | | } |
| | 78 | | } |