jellyfin/Jellyfin.Api/Extensions/DtoExtensions.cs
Bond_009 635d67d458 Revert "Use System.Net.IPNetwork"
This reverts commit 117d05d288da1d412159a29c0cb8d5c8259e48ae.
2023-11-14 21:16:33 +01:00

115 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Security.Claims;
using Jellyfin.Extensions;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
namespace Jellyfin.Api.Extensions;
/// <summary>
/// Dto Extensions.
/// </summary>
public static class DtoExtensions
{
/// <summary>
/// Add additional fields depending on client.
/// </summary>
/// <remarks>
/// Use in place of GetDtoOptions.
/// Legacy order: 2.
/// </remarks>
/// <param name="dtoOptions">DtoOptions object.</param>
/// <param name="user">Current claims principal.</param>
/// <returns>Modified DtoOptions object.</returns>
internal static DtoOptions AddClientFields(
this DtoOptions dtoOptions, ClaimsPrincipal user)
{
dtoOptions.Fields ??= Array.Empty<ItemFields>();
string? client = user.GetClient();
// No client in claim
if (string.IsNullOrEmpty(client))
{
return dtoOptions;
}
if (!dtoOptions.ContainsField(ItemFields.RecursiveItemCount))
{
if (client.Contains("kodi", StringComparison.OrdinalIgnoreCase) ||
client.Contains("wmc", StringComparison.OrdinalIgnoreCase) ||
client.Contains("media center", StringComparison.OrdinalIgnoreCase) ||
client.Contains("classic", StringComparison.OrdinalIgnoreCase))
{
int oldLen = dtoOptions.Fields.Count;
var arr = new ItemFields[oldLen + 1];
dtoOptions.Fields.CopyTo(arr, 0);
arr[oldLen] = ItemFields.RecursiveItemCount;
dtoOptions.Fields = arr;
}
}
if (!dtoOptions.ContainsField(ItemFields.ChildCount))
{
if (client.Contains("kodi", StringComparison.OrdinalIgnoreCase) ||
client.Contains("wmc", StringComparison.OrdinalIgnoreCase) ||
client.Contains("media center", StringComparison.OrdinalIgnoreCase) ||
client.Contains("classic", StringComparison.OrdinalIgnoreCase) ||
client.Contains("roku", StringComparison.OrdinalIgnoreCase) ||
client.Contains("samsung", StringComparison.OrdinalIgnoreCase) ||
client.Contains("androidtv", StringComparison.OrdinalIgnoreCase))
{
int oldLen = dtoOptions.Fields.Count;
var arr = new ItemFields[oldLen + 1];
dtoOptions.Fields.CopyTo(arr, 0);
arr[oldLen] = ItemFields.ChildCount;
dtoOptions.Fields = arr;
}
}
return dtoOptions;
}
/// <summary>
/// Add additional DtoOptions.
/// </summary>
/// <remarks>
/// Converted from IHasDtoOptions.
/// Legacy order: 3.
/// </remarks>
/// <param name="dtoOptions">DtoOptions object.</param>
/// <param name="enableImages">Enable images.</param>
/// <param name="enableUserData">Enable user data.</param>
/// <param name="imageTypeLimit">Image type limit.</param>
/// <param name="enableImageTypes">Enable image types.</param>
/// <returns>Modified DtoOptions object.</returns>
internal static DtoOptions AddAdditionalDtoOptions(
this DtoOptions dtoOptions,
bool? enableImages,
bool? enableUserData,
int? imageTypeLimit,
IReadOnlyList<ImageType> enableImageTypes)
{
dtoOptions.EnableImages = enableImages ?? true;
if (imageTypeLimit.HasValue)
{
dtoOptions.ImageTypeLimit = imageTypeLimit.Value;
}
if (enableUserData.HasValue)
{
dtoOptions.EnableUserData = enableUserData.Value;
}
if (enableImageTypes.Count != 0)
{
dtoOptions.ImageTypes = enableImageTypes;
}
return dtoOptions;
}
}