commit
62ea9b2821
@ -1,189 +0,0 @@
|
||||
#pragma warning disable CS1591
|
||||
#pragma warning disable SA1402
|
||||
#pragma warning disable SA1649
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Net;
|
||||
using MediaBrowser.Controller.Notifications;
|
||||
using MediaBrowser.Model.Dto;
|
||||
using MediaBrowser.Model.Notifications;
|
||||
using MediaBrowser.Model.Services;
|
||||
|
||||
namespace Emby.Notifications.Api
|
||||
{
|
||||
[Route("/Notifications/{UserId}", "GET", Summary = "Gets notifications")]
|
||||
public class GetNotifications : IReturn<NotificationResult>
|
||||
{
|
||||
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
|
||||
[ApiMember(Name = "IsRead", Description = "An optional filter by IsRead", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
|
||||
public bool? IsRead { get; set; }
|
||||
|
||||
[ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
|
||||
public int? StartIndex { get; set; }
|
||||
|
||||
[ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
|
||||
public int? Limit { get; set; }
|
||||
}
|
||||
|
||||
public class Notification
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
public bool IsRead { get; set; }
|
||||
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
public string Url { get; set; } = string.Empty;
|
||||
|
||||
public NotificationLevel Level { get; set; }
|
||||
}
|
||||
|
||||
public class NotificationResult
|
||||
{
|
||||
public IReadOnlyList<Notification> Notifications { get; set; } = Array.Empty<Notification>();
|
||||
|
||||
public int TotalRecordCount { get; set; }
|
||||
}
|
||||
|
||||
public class NotificationsSummary
|
||||
{
|
||||
public int UnreadCount { get; set; }
|
||||
|
||||
public NotificationLevel MaxUnreadNotificationLevel { get; set; }
|
||||
}
|
||||
|
||||
[Route("/Notifications/{UserId}/Summary", "GET", Summary = "Gets a notification summary for a user")]
|
||||
public class GetNotificationsSummary : IReturn<NotificationsSummary>
|
||||
{
|
||||
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
[Route("/Notifications/Types", "GET", Summary = "Gets notification types")]
|
||||
public class GetNotificationTypes : IReturn<List<NotificationTypeInfo>>
|
||||
{
|
||||
}
|
||||
|
||||
[Route("/Notifications/Services", "GET", Summary = "Gets notification types")]
|
||||
public class GetNotificationServices : IReturn<List<NameIdPair>>
|
||||
{
|
||||
}
|
||||
|
||||
[Route("/Notifications/Admin", "POST", Summary = "Sends a notification to all admin users")]
|
||||
public class AddAdminNotification : IReturnVoid
|
||||
{
|
||||
[ApiMember(Name = "Name", Description = "The notification's name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[ApiMember(Name = "Description", Description = "The notification's description", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
[ApiMember(Name = "ImageUrl", Description = "The notification's image url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public string? ImageUrl { get; set; }
|
||||
|
||||
[ApiMember(Name = "Url", Description = "The notification's info url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public string? Url { get; set; }
|
||||
|
||||
[ApiMember(Name = "Level", Description = "The notification level", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public NotificationLevel Level { get; set; }
|
||||
}
|
||||
|
||||
[Route("/Notifications/{UserId}/Read", "POST", Summary = "Marks notifications as read")]
|
||||
public class MarkRead : IReturnVoid
|
||||
{
|
||||
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
|
||||
[ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
|
||||
public string Ids { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
[Route("/Notifications/{UserId}/Unread", "POST", Summary = "Marks notifications as unread")]
|
||||
public class MarkUnread : IReturnVoid
|
||||
{
|
||||
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
|
||||
[ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
|
||||
public string Ids { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
[Authenticated]
|
||||
public class NotificationsService : IService
|
||||
{
|
||||
private readonly INotificationManager _notificationManager;
|
||||
private readonly IUserManager _userManager;
|
||||
|
||||
public NotificationsService(INotificationManager notificationManager, IUserManager userManager)
|
||||
{
|
||||
_notificationManager = notificationManager;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
|
||||
public object Get(GetNotificationTypes request)
|
||||
{
|
||||
return _notificationManager.GetNotificationTypes();
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
|
||||
public object Get(GetNotificationServices request)
|
||||
{
|
||||
return _notificationManager.GetNotificationServices().ToList();
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
|
||||
public object Get(GetNotificationsSummary request)
|
||||
{
|
||||
return new NotificationsSummary
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
public Task Post(AddAdminNotification request)
|
||||
{
|
||||
// This endpoint really just exists as post of a real with sickbeard
|
||||
var notification = new NotificationRequest
|
||||
{
|
||||
Date = DateTime.UtcNow,
|
||||
Description = request.Description,
|
||||
Level = request.Level,
|
||||
Name = request.Name,
|
||||
Url = request.Url,
|
||||
UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray()
|
||||
};
|
||||
|
||||
return _notificationManager.SendNotification(notification, CancellationToken.None);
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
|
||||
public void Post(MarkRead request)
|
||||
{
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
|
||||
public void Post(MarkUnread request)
|
||||
{
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
|
||||
public object Get(GetNotifications request)
|
||||
{
|
||||
return new NotificationResult();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,159 @@
|
||||
#nullable enable
|
||||
#pragma warning disable CA1801
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Jellyfin.Api.Models.NotificationDtos;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Notifications;
|
||||
using MediaBrowser.Model.Dto;
|
||||
using MediaBrowser.Model.Notifications;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Jellyfin.Api.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// The notification controller.
|
||||
/// </summary>
|
||||
public class NotificationsController : BaseJellyfinApiController
|
||||
{
|
||||
private readonly INotificationManager _notificationManager;
|
||||
private readonly IUserManager _userManager;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="NotificationsController" /> class.
|
||||
/// </summary>
|
||||
/// <param name="notificationManager">The notification manager.</param>
|
||||
/// <param name="userManager">The user manager.</param>
|
||||
public NotificationsController(INotificationManager notificationManager, IUserManager userManager)
|
||||
{
|
||||
_notificationManager = notificationManager;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a user's notifications.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user's ID.</param>
|
||||
/// <param name="isRead">An optional filter by notification read state.</param>
|
||||
/// <param name="startIndex">The optional index to start at. All notifications with a lower index will be omitted from the results.</param>
|
||||
/// <param name="limit">An optional limit on the number of notifications returned.</param>
|
||||
/// <response code="200">Notifications returned.</response>
|
||||
/// <returns>An <see cref="OkResult"/> containing a list of notifications.</returns>
|
||||
[HttpGet("{UserID}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public ActionResult<NotificationResultDto> GetNotifications(
|
||||
[FromRoute] string userId,
|
||||
[FromQuery] bool? isRead,
|
||||
[FromQuery] int? startIndex,
|
||||
[FromQuery] int? limit)
|
||||
{
|
||||
return new NotificationResultDto();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a user's notification summary.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user's ID.</param>
|
||||
/// <response code="200">Summary of user's notifications returned.</response>
|
||||
/// <returns>An <cref see="OkResult"/> containing a summary of the users notifications.</returns>
|
||||
[HttpGet("{UserID}/Summary")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public ActionResult<NotificationsSummaryDto> GetNotificationsSummary(
|
||||
[FromRoute] string userId)
|
||||
{
|
||||
return new NotificationsSummaryDto();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets notification types.
|
||||
/// </summary>
|
||||
/// <response code="200">All notification types returned.</response>
|
||||
/// <returns>An <cref see="OkResult"/> containing a list of all notification types.</returns>
|
||||
[HttpGet("Types")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
|
||||
{
|
||||
return _notificationManager.GetNotificationTypes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets notification services.
|
||||
/// </summary>
|
||||
/// <response code="200">All notification services returned.</response>
|
||||
/// <returns>An <cref see="OkResult"/> containing a list of all notification services.</returns>
|
||||
[HttpGet("Services")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public IEnumerable<NameIdPair> GetNotificationServices()
|
||||
{
|
||||
return _notificationManager.GetNotificationServices();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a notification to all admins.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the notification.</param>
|
||||
/// <param name="description">The description of the notification.</param>
|
||||
/// <param name="url">The URL of the notification.</param>
|
||||
/// <param name="level">The level of the notification.</param>
|
||||
/// <response code="200">Notification sent.</response>
|
||||
/// <returns>An <cref see="OkResult"/>.</returns>
|
||||
[HttpPost("Admin")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public ActionResult CreateAdminNotification(
|
||||
[FromQuery] string name,
|
||||
[FromQuery] string description,
|
||||
[FromQuery] string? url,
|
||||
[FromQuery] NotificationLevel? level)
|
||||
{
|
||||
var notification = new NotificationRequest
|
||||
{
|
||||
Name = name,
|
||||
Description = description,
|
||||
Url = url,
|
||||
Level = level ?? NotificationLevel.Normal,
|
||||
UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray(),
|
||||
Date = DateTime.UtcNow,
|
||||
};
|
||||
|
||||
_notificationManager.SendNotification(notification, CancellationToken.None);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets notifications as read.
|
||||
/// </summary>
|
||||
/// <param name="userId">The userID.</param>
|
||||
/// <param name="ids">A comma-separated list of the IDs of notifications which should be set as read.</param>
|
||||
/// <response code="200">Notifications set as read.</response>
|
||||
/// <returns>An <cref see="OkResult"/>.</returns>
|
||||
[HttpPost("{UserID}/Read")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public ActionResult SetRead(
|
||||
[FromRoute] string userId,
|
||||
[FromQuery] string ids)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets notifications as unread.
|
||||
/// </summary>
|
||||
/// <param name="userId">The userID.</param>
|
||||
/// <param name="ids">A comma-separated list of the IDs of notifications which should be set as unread.</param>
|
||||
/// <response code="200">Notifications set as unread.</response>
|
||||
/// <returns>An <cref see="OkResult"/>.</returns>
|
||||
[HttpPost("{UserID}/Unread")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public ActionResult SetUnread(
|
||||
[FromRoute] string userId,
|
||||
[FromQuery] string ids)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using MediaBrowser.Model.Notifications;
|
||||
|
||||
namespace Jellyfin.Api.Models.NotificationDtos
|
||||
{
|
||||
/// <summary>
|
||||
/// The notification DTO.
|
||||
/// </summary>
|
||||
public class NotificationDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the notification ID. Defaults to an empty string.
|
||||
/// </summary>
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the notification's user ID. Defaults to an empty string.
|
||||
/// </summary>
|
||||
public string UserId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the notification date.
|
||||
/// </summary>
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the notification has been read. Defaults to false.
|
||||
/// </summary>
|
||||
public bool IsRead { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the notification's name. Defaults to an empty string.
|
||||
/// </summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the notification's description. Defaults to an empty string.
|
||||
/// </summary>
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the notification's URL. Defaults to an empty string.
|
||||
/// </summary>
|
||||
public string Url { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the notification level.
|
||||
/// </summary>
|
||||
public NotificationLevel Level { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Jellyfin.Api.Models.NotificationDtos
|
||||
{
|
||||
/// <summary>
|
||||
/// A list of notifications with the total record count for pagination.
|
||||
/// </summary>
|
||||
public class NotificationResultDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the current page of notifications.
|
||||
/// </summary>
|
||||
public IReadOnlyList<NotificationDto> Notifications { get; set; } = Array.Empty<NotificationDto>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the total number of notifications.
|
||||
/// </summary>
|
||||
public int TotalRecordCount { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
#nullable enable
|
||||
|
||||
using MediaBrowser.Model.Notifications;
|
||||
|
||||
namespace Jellyfin.Api.Models.NotificationDtos
|
||||
{
|
||||
/// <summary>
|
||||
/// The notification summary DTO.
|
||||
/// </summary>
|
||||
public class NotificationsSummaryDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the number of unread notifications.
|
||||
/// </summary>
|
||||
public int UnreadCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the maximum unread notification level.
|
||||
/// </summary>
|
||||
public NotificationLevel? MaxUnreadNotificationLevel { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using Jellyfin.Server.Models;
|
||||
using Microsoft.AspNetCore.Mvc.Formatters;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Jellyfin.Server.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Camel Case Json Profile Formatter.
|
||||
/// </summary>
|
||||
public class CamelCaseJsonProfileFormatter : SystemTextJsonOutputFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CamelCaseJsonProfileFormatter"/> class.
|
||||
/// </summary>
|
||||
public CamelCaseJsonProfileFormatter() : base(JsonOptions.CamelCase)
|
||||
{
|
||||
SupportedMediaTypes.Clear();
|
||||
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json;profile=\"CamelCase\""));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using Jellyfin.Server.Models;
|
||||
using Microsoft.AspNetCore.Mvc.Formatters;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Jellyfin.Server.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Pascal Case Json Profile Formatter.
|
||||
/// </summary>
|
||||
public class PascalCaseJsonProfileFormatter : SystemTextJsonOutputFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PascalCaseJsonProfileFormatter"/> class.
|
||||
/// </summary>
|
||||
public PascalCaseJsonProfileFormatter() : base(JsonOptions.PascalCase)
|
||||
{
|
||||
SupportedMediaTypes.Clear();
|
||||
// Add application/json for default formatter
|
||||
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
|
||||
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json;profile=\"PascalCase\""));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Mime;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller.Authentication;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Net;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Server.Middleware
|
||||
{
|
||||
/// <summary>
|
||||
/// Exception Middleware.
|
||||
/// </summary>
|
||||
public class ExceptionMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<ExceptionMiddleware> _logger;
|
||||
private readonly IServerConfigurationManager _configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ExceptionMiddleware"/> class.
|
||||
/// </summary>
|
||||
/// <param name="next">Next request delegate.</param>
|
||||
/// <param name="logger">Instance of the <see cref="ILogger{ExceptionMiddleware}"/> interface.</param>
|
||||
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
||||
public ExceptionMiddleware(
|
||||
RequestDelegate next,
|
||||
ILogger<ExceptionMiddleware> logger,
|
||||
IServerConfigurationManager serverConfigurationManager)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
_configuration = serverConfigurationManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invoke request.
|
||||
/// </summary>
|
||||
/// <param name="context">Request context.</param>
|
||||
/// <returns>Task.</returns>
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (context.Response.HasStarted)
|
||||
{
|
||||
_logger.LogWarning("The response has already started, the exception middleware will not be executed.");
|
||||
throw;
|
||||
}
|
||||
|
||||
ex = GetActualException(ex);
|
||||
|
||||
bool ignoreStackTrace =
|
||||
ex is SocketException
|
||||
|| ex is IOException
|
||||
|| ex is OperationCanceledException
|
||||
|| ex is SecurityException
|
||||
|| ex is AuthenticationException
|
||||
|| ex is FileNotFoundException;
|
||||
|
||||
if (ignoreStackTrace)
|
||||
{
|
||||
_logger.LogError(
|
||||
"Error processing request: {ExceptionMessage}. URL {Method} {Url}.",
|
||||
ex.Message.TrimEnd('.'),
|
||||
context.Request.Method,
|
||||
context.Request.Path);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError(
|
||||
ex,
|
||||
"Error processing request. URL {Method} {Url}.",
|
||||
context.Request.Method,
|
||||
context.Request.Path);
|
||||
}
|
||||
|
||||
context.Response.StatusCode = GetStatusCode(ex);
|
||||
context.Response.ContentType = MediaTypeNames.Text.Plain;
|
||||
var errorContent = NormalizeExceptionMessage(ex.Message);
|
||||
await context.Response.WriteAsync(errorContent).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static Exception GetActualException(Exception ex)
|
||||
{
|
||||
if (ex is AggregateException agg)
|
||||
{
|
||||
var inner = agg.InnerException;
|
||||
if (inner != null)
|
||||
{
|
||||
return GetActualException(inner);
|
||||
}
|
||||
|
||||
var inners = agg.InnerExceptions;
|
||||
if (inners.Count > 0)
|
||||
{
|
||||
return GetActualException(inners[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return ex;
|
||||
}
|
||||
|
||||
private static int GetStatusCode(Exception ex)
|
||||
{
|
||||
switch (ex)
|
||||
{
|
||||
case ArgumentException _: return StatusCodes.Status400BadRequest;
|
||||
case SecurityException _: return StatusCodes.Status401Unauthorized;
|
||||
case DirectoryNotFoundException _:
|
||||
case FileNotFoundException _:
|
||||
case ResourceNotFoundException _: return StatusCodes.Status404NotFound;
|
||||
case MethodNotAllowedException _: return StatusCodes.Status405MethodNotAllowed;
|
||||
default: return StatusCodes.Status500InternalServerError;
|
||||
}
|
||||
}
|
||||
|
||||
private string NormalizeExceptionMessage(string msg)
|
||||
{
|
||||
if (msg == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
// Strip any information we don't want to reveal
|
||||
return msg.Replace(
|
||||
_configuration.ApplicationPaths.ProgramSystemPath,
|
||||
string.Empty,
|
||||
StringComparison.OrdinalIgnoreCase)
|
||||
.Replace(
|
||||
_configuration.ApplicationPaths.ProgramDataPath,
|
||||
string.Empty,
|
||||
StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Jellyfin.Server.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Json Options.
|
||||
/// </summary>
|
||||
public static class JsonOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets CamelCase json options.
|
||||
/// </summary>
|
||||
public static JsonSerializerOptions CamelCase
|
||||
{
|
||||
get
|
||||
{
|
||||
var options = DefaultJsonOptions;
|
||||
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets PascalCase json options.
|
||||
/// </summary>
|
||||
public static JsonSerializerOptions PascalCase
|
||||
{
|
||||
get
|
||||
{
|
||||
var options = DefaultJsonOptions;
|
||||
options.PropertyNamingPolicy = null;
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets base Json Serializer Options.
|
||||
/// </summary>
|
||||
private static JsonSerializerOptions DefaultJsonOptions => new JsonSerializerOptions();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue