aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Controllers/ClientLogController.cs
blob: 95d07c9307859e4e8e2c0c9a7b5c3fd40d5e1537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Net.Mime;
using System.Threading.Tasks;
using Jellyfin.Api.Attributes;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Helpers;
using Jellyfin.Api.Models.ClientLogDtos;
using MediaBrowser.Controller.ClientEvent;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.ClientLog;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Jellyfin.Api.Controllers
{
    /// <summary>
    /// Client log controller.
    /// </summary>
    [Authorize(Policy = Policies.DefaultAuthorization)]
    public class ClientLogController : BaseJellyfinApiController
    {
        private const int MaxDocumentSize = 1_000_000;
        private readonly IClientEventLogger _clientEventLogger;
        private readonly IServerConfigurationManager _serverConfigurationManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="ClientLogController"/> class.
        /// </summary>
        /// <param name="clientEventLogger">Instance of the <see cref="IClientEventLogger"/> interface.</param>
        /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
        public ClientLogController(
            IClientEventLogger clientEventLogger,
            IServerConfigurationManager serverConfigurationManager)
        {
            _clientEventLogger = clientEventLogger;
            _serverConfigurationManager = serverConfigurationManager;
        }

        /// <summary>
        /// Post event from client.
        /// </summary>
        /// <param name="clientLogEventDto">The client log dto.</param>
        /// <response code="204">Event logged.</response>
        /// <response code="403">Event logging disabled.</response>
        /// <returns>Submission status.</returns>
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status403Forbidden)]
        public ActionResult LogEvent([FromBody] ClientLogEventDto clientLogEventDto)
        {
            if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
            {
                return Forbid();
            }

            var (clientName, clientVersion, userId, deviceId) = GetRequestInformation();
            Log(clientLogEventDto, userId, clientName, clientVersion, deviceId);
            return NoContent();
        }

        /// <summary>
        /// Bulk post events from client.
        /// </summary>
        /// <param name="clientLogEventDtos">The list of client log dtos.</param>
        /// <response code="204">All events logged.</response>
        /// <response code="403">Event logging disabled.</response>
        /// <returns>Submission status.</returns>
        [HttpPost("Bulk")]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status403Forbidden)]
        public ActionResult LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos)
        {
            if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
            {
                return Forbid();
            }

            var (clientName, clientVersion, userId, deviceId) = GetRequestInformation();
            foreach (var dto in clientLogEventDtos)
            {
                Log(dto, userId, clientName, clientVersion, deviceId);
            }

            return NoContent();
        }

        /// <summary>
        /// Upload a document.
        /// </summary>
        /// <response code="200">Document saved.</response>
        /// <response code="403">Event logging disabled.</response>
        /// <response code="413">Upload size too large.</response>
        /// <returns>Create response.</returns>
        [HttpPost("Document")]
        [ProducesResponseType(typeof(ClientLogDocumentResponseDto), StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status403Forbidden)]
        [ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
        [AcceptsFile(MediaTypeNames.Text.Plain)]
        [RequestSizeLimit(MaxDocumentSize)]
        public async Task<ActionResult<ClientLogDocumentResponseDto>> LogFile()
        {
            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");
            }

            var (clientName, clientVersion, _, _) = GetRequestInformation();
            var fileName = await _clientEventLogger.WriteDocumentAsync(clientName, clientVersion, Request.Body)
                .ConfigureAwait(false);
            return Ok(new ClientLogDocumentResponseDto(fileName));
        }

        private void Log(
            ClientLogEventDto dto,
            Guid userId,
            string clientName,
            string clientVersion,
            string deviceId)
        {
            _clientEventLogger.Log(new ClientLogEvent(
                dto.Timestamp,
                dto.Level,
                userId,
                clientName,
                clientVersion,
                deviceId,
                dto.Message));
        }

        private (string ClientName, string ClientVersion, Guid UserId, string DeviceId) GetRequestInformation()
        {
            var clientName = ClaimHelpers.GetClient(HttpContext.User) ?? "unknown-client";
            var clientVersion = ClaimHelpers.GetIsApiKey(HttpContext.User)
                ? "apikey"
                : ClaimHelpers.GetVersion(HttpContext.User) ?? "unknown-version";
            var userId = ClaimHelpers.GetUserId(HttpContext.User) ?? Guid.Empty;
            var deviceId = ClaimHelpers.GetDeviceId(HttpContext.User) ?? "unknown-device-id";

            return (clientName, clientVersion, userId, deviceId);
        }
    }
}