Update documentation; use information from authorization; return generated filename

This commit is contained in:
Cody Robibero 2021-10-28 16:11:14 -06:00
parent 91204fc9f0
commit 0e584f6840
4 changed files with 33 additions and 41 deletions

View File

@ -45,17 +45,22 @@ namespace Jellyfin.Api.Controllers
/// </summary> /// </summary>
/// <param name="clientLogEventDto">The client log dto.</param> /// <param name="clientLogEventDto">The client log dto.</param>
/// <response code="204">Event logged.</response> /// <response code="204">Event logged.</response>
/// <response code="403">Event logging disabled.</response>
/// <returns>Submission status.</returns> /// <returns>Submission status.</returns>
[HttpPost] [HttpPost]
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult LogEvent([FromBody] ClientLogEventDto clientLogEventDto) [ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult> LogEvent([FromBody] ClientLogEventDto clientLogEventDto)
{ {
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload) if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
{ {
return Forbid(); return Forbid();
} }
Log(clientLogEventDto); var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request)
.ConfigureAwait(false);
Log(clientLogEventDto, authorizationInfo);
return NoContent(); return NoContent();
} }
@ -64,19 +69,24 @@ namespace Jellyfin.Api.Controllers
/// </summary> /// </summary>
/// <param name="clientLogEventDtos">The list of client log dtos.</param> /// <param name="clientLogEventDtos">The list of client log dtos.</param>
/// <response code="204">All events logged.</response> /// <response code="204">All events logged.</response>
/// <response code="403">Event logging disabled.</response>
/// <returns>Submission status.</returns> /// <returns>Submission status.</returns>
[HttpPost("Bulk")] [HttpPost("Bulk")]
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos) [ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult> LogEvents([FromBody] ClientLogEventDto[] clientLogEventDtos)
{ {
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload) if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
{ {
return Forbid(); return Forbid();
} }
var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request)
.ConfigureAwait(false);
foreach (var dto in clientLogEventDtos) foreach (var dto in clientLogEventDtos)
{ {
Log(dto); Log(dto, authorizationInfo);
} }
return NoContent(); return NoContent();
@ -85,12 +95,17 @@ namespace Jellyfin.Api.Controllers
/// <summary> /// <summary>
/// Upload a document. /// Upload a document.
/// </summary> /// </summary>
/// <returns>Submission status.</returns> /// <response code="200">Document saved.</response>
/// <response code="403">Event logging disabled.</response>
/// <response code="413">Upload size too large.</response>
/// <returns>Created file name.</returns>
[HttpPost("Document")] [HttpPost("Document")]
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
[AcceptsFile(MediaTypeNames.Text.Plain)] [AcceptsFile(MediaTypeNames.Text.Plain)]
[RequestSizeLimit(MaxDocumentSize)] [RequestSizeLimit(MaxDocumentSize)]
public async Task<ActionResult> LogFile() public async Task<ActionResult<string>> LogFile()
{ {
if (!_serverConfigurationManager.Configuration.AllowClientLogUpload) if (!_serverConfigurationManager.Configuration.AllowClientLogUpload)
{ {
@ -106,20 +121,20 @@ namespace Jellyfin.Api.Controllers
var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request) var authorizationInfo = await _authorizationContext.GetAuthorizationInfo(Request)
.ConfigureAwait(false); .ConfigureAwait(false);
await _clientEventLogger.WriteDocumentAsync(authorizationInfo, Request.Body) var fileName = await _clientEventLogger.WriteDocumentAsync(authorizationInfo, Request.Body)
.ConfigureAwait(false); .ConfigureAwait(false);
return NoContent(); return Ok(fileName);
} }
private void Log(ClientLogEventDto dto) private void Log(ClientLogEventDto dto, AuthorizationInfo authorizationInfo)
{ {
_clientEventLogger.Log(new ClientLogEvent( _clientEventLogger.Log(new ClientLogEvent(
dto.Timestamp, dto.Timestamp,
dto.Level, dto.Level,
dto.UserId, authorizationInfo.UserId,
dto.ClientName, authorizationInfo.Client,
dto.ClientVersion, authorizationInfo.Version,
dto.DeviceId, authorizationInfo.DeviceId,
dto.Message)); dto.Message));
} }
} }

View File

@ -21,30 +21,6 @@ namespace Jellyfin.Api.Models.ClientLogDtos
[Required] [Required]
public LogLevel Level { get; set; } public LogLevel Level { get; set; }
/// <summary>
/// Gets or sets the user id.
/// </summary>
public Guid? UserId { get; set; }
/// <summary>
/// Gets or sets the client name.
/// </summary>
[Required]
public string ClientName { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the client version.
/// </summary>
[Required]
public string ClientVersion { get; set; } = string.Empty;
///
/// <summary>
/// Gets or sets the device id.
/// </summary>
[Required]
public string DeviceId { get; set; } = string.Empty;
/// <summary> /// <summary>
/// Gets or sets the log message. /// Gets or sets the log message.
/// </summary> /// </summary>

View File

@ -44,13 +44,14 @@ namespace MediaBrowser.Controller.ClientEvent
} }
/// <inheritdoc /> /// <inheritdoc />
public async Task WriteDocumentAsync(AuthorizationInfo authorizationInfo, Stream fileContents) public async Task<string> WriteDocumentAsync(AuthorizationInfo authorizationInfo, Stream fileContents)
{ {
var fileName = $"upload_{authorizationInfo.Client}_{(authorizationInfo.IsApiKey ? "apikey" : authorizationInfo.Version)}_{DateTime.UtcNow:yyyyMMddHHmmss}.log"; var fileName = $"upload_{authorizationInfo.Client}_{(authorizationInfo.IsApiKey ? "apikey" : authorizationInfo.Version)}_{DateTime.UtcNow:yyyyMMddHHmmss}.log";
var logFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, fileName); var logFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, fileName);
await using var fileStream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None); await using var fileStream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
await fileContents.CopyToAsync(fileStream).ConfigureAwait(false); await fileContents.CopyToAsync(fileStream).ConfigureAwait(false);
await fileStream.FlushAsync().ConfigureAwait(false); await fileStream.FlushAsync().ConfigureAwait(false);
return fileName;
} }
} }
} }

View File

@ -21,7 +21,7 @@ namespace MediaBrowser.Controller.ClientEvent
/// </summary> /// </summary>
/// <param name="authorizationInfo">The current authorization info.</param> /// <param name="authorizationInfo">The current authorization info.</param>
/// <param name="fileContents">The file contents to write.</param> /// <param name="fileContents">The file contents to write.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <returns>The created file name.</returns>
Task WriteDocumentAsync(AuthorizationInfo authorizationInfo, Stream fileContents); Task<string> WriteDocumentAsync(AuthorizationInfo authorizationInfo, Stream fileContents);
} }
} }