aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Extensions
diff options
context:
space:
mode:
authorJPVenson <github@jpb.email>2024-10-08 09:34:34 +0000
committerJPVenson <github@jpb.email>2024-10-08 09:34:34 +0000
commitd3a3d9fce3b891eb0be274a0cdc45a989e557652 (patch)
treebd232ef477c259f1fafa204016f6efd4dcb8691f /MediaBrowser.Model/Extensions
parentee1bdf4e222125ed7382165fd7e09599ca4bd4aa (diff)
parentaaf20592bb0bbdf4f0f0d99fed091758e68ae850 (diff)
Merge remote-tracking branch 'jellyfinorigin/master' into feature/EFUserData
Diffstat (limited to 'MediaBrowser.Model/Extensions')
-rw-r--r--MediaBrowser.Model/Extensions/ContainerHelper.cs145
-rw-r--r--MediaBrowser.Model/Extensions/LibraryOptionsExtension.cs32
2 files changed, 177 insertions, 0 deletions
diff --git a/MediaBrowser.Model/Extensions/ContainerHelper.cs b/MediaBrowser.Model/Extensions/ContainerHelper.cs
new file mode 100644
index 000000000..c86328ba6
--- /dev/null
+++ b/MediaBrowser.Model/Extensions/ContainerHelper.cs
@@ -0,0 +1,145 @@
+using System;
+using System.Collections.Generic;
+using Jellyfin.Extensions;
+
+namespace MediaBrowser.Model.Extensions;
+
+/// <summary>
+/// Defines the <see cref="ContainerHelper"/> class.
+/// </summary>
+public static class ContainerHelper
+{
+ /// <summary>
+ /// Compares two containers, returning true if an item in <paramref name="inputContainer"/> exists
+ /// in <paramref name="profileContainers"/>.
+ /// </summary>
+ /// <param name="profileContainers">The comma-delimited string being searched.
+ /// If the parameter begins with the <c>-</c> character, the operation is reversed.</param>
+ /// <param name="inputContainer">The comma-delimited string being matched.</param>
+ /// <returns>The result of the operation.</returns>
+ public static bool ContainsContainer(string? profileContainers, string? inputContainer)
+ {
+ var isNegativeList = false;
+ if (profileContainers != null && profileContainers.StartsWith('-'))
+ {
+ isNegativeList = true;
+ profileContainers = profileContainers[1..];
+ }
+
+ return ContainsContainer(profileContainers, isNegativeList, inputContainer);
+ }
+
+ /// <summary>
+ /// Compares two containers, returning true if an item in <paramref name="inputContainer"/> exists
+ /// in <paramref name="profileContainers"/>.
+ /// </summary>
+ /// <param name="profileContainers">The comma-delimited string being searched.
+ /// If the parameter begins with the <c>-</c> character, the operation is reversed.</param>
+ /// <param name="inputContainer">The comma-delimited string being matched.</param>
+ /// <returns>The result of the operation.</returns>
+ public static bool ContainsContainer(string? profileContainers, ReadOnlySpan<char> inputContainer)
+ {
+ var isNegativeList = false;
+ if (profileContainers != null && profileContainers.StartsWith('-'))
+ {
+ isNegativeList = true;
+ profileContainers = profileContainers[1..];
+ }
+
+ return ContainsContainer(profileContainers, isNegativeList, inputContainer);
+ }
+
+ /// <summary>
+ /// Compares two containers, returning <paramref name="isNegativeList"/> if an item in <paramref name="inputContainer"/>
+ /// does not exist in <paramref name="profileContainers"/>.
+ /// </summary>
+ /// <param name="profileContainers">The comma-delimited string being searched.</param>
+ /// <param name="isNegativeList">The boolean result to return if a match is not found.</param>
+ /// <param name="inputContainer">The comma-delimited string being matched.</param>
+ /// <returns>The result of the operation.</returns>
+ public static bool ContainsContainer(string? profileContainers, bool isNegativeList, string? inputContainer)
+ {
+ if (string.IsNullOrEmpty(inputContainer))
+ {
+ return isNegativeList;
+ }
+
+ return ContainsContainer(profileContainers, isNegativeList, inputContainer.AsSpan());
+ }
+
+ /// <summary>
+ /// Compares two containers, returning <paramref name="isNegativeList"/> if an item in <paramref name="inputContainer"/>
+ /// does not exist in <paramref name="profileContainers"/>.
+ /// </summary>
+ /// <param name="profileContainers">The comma-delimited string being searched.</param>
+ /// <param name="isNegativeList">The boolean result to return if a match is not found.</param>
+ /// <param name="inputContainer">The comma-delimited string being matched.</param>
+ /// <returns>The result of the operation.</returns>
+ public static bool ContainsContainer(string? profileContainers, bool isNegativeList, ReadOnlySpan<char> inputContainer)
+ {
+ if (string.IsNullOrEmpty(profileContainers))
+ {
+ // Empty profiles always support all containers/codecs.
+ return true;
+ }
+
+ var allInputContainers = inputContainer.Split(',');
+ var allProfileContainers = profileContainers.SpanSplit(',');
+ foreach (var container in allInputContainers)
+ {
+ if (!container.IsEmpty)
+ {
+ foreach (var profile in allProfileContainers)
+ {
+ if (!profile.IsEmpty && container.Equals(profile, StringComparison.OrdinalIgnoreCase))
+ {
+ return !isNegativeList;
+ }
+ }
+ }
+ }
+
+ return isNegativeList;
+ }
+
+ /// <summary>
+ /// Compares two containers, returning <paramref name="isNegativeList"/> if an item in <paramref name="inputContainer"/>
+ /// does not exist in <paramref name="profileContainers"/>.
+ /// </summary>
+ /// <param name="profileContainers">The profile containers being matched searched.</param>
+ /// <param name="isNegativeList">The boolean result to return if a match is not found.</param>
+ /// <param name="inputContainer">The comma-delimited string being matched.</param>
+ /// <returns>The result of the operation.</returns>
+ public static bool ContainsContainer(IReadOnlyList<string>? profileContainers, bool isNegativeList, string inputContainer)
+ {
+ if (profileContainers is null)
+ {
+ // Empty profiles always support all containers/codecs.
+ return true;
+ }
+
+ var allInputContainers = Split(inputContainer);
+ foreach (var container in allInputContainers)
+ {
+ foreach (var profile in profileContainers)
+ {
+ if (string.Equals(profile, container, StringComparison.OrdinalIgnoreCase))
+ {
+ return !isNegativeList;
+ }
+ }
+ }
+
+ return isNegativeList;
+ }
+
+ /// <summary>
+ /// Splits and input string.
+ /// </summary>
+ /// <param name="input">The input string.</param>
+ /// <returns>The result of the operation.</returns>
+ public static string[] Split(string? input)
+ {
+ return input?.Split(',', StringSplitOptions.RemoveEmptyEntries) ?? [];
+ }
+}
diff --git a/MediaBrowser.Model/Extensions/LibraryOptionsExtension.cs b/MediaBrowser.Model/Extensions/LibraryOptionsExtension.cs
new file mode 100644
index 000000000..4a814f22a
--- /dev/null
+++ b/MediaBrowser.Model/Extensions/LibraryOptionsExtension.cs
@@ -0,0 +1,32 @@
+using System;
+using System.Linq;
+using MediaBrowser.Model.Configuration;
+
+namespace MediaBrowser.Model.Extensions;
+
+/// <summary>
+/// Extensions for <see cref="LibraryOptions"/>.
+/// </summary>
+public static class LibraryOptionsExtension
+{
+ /// <summary>
+ /// Get the custom tag delimiters.
+ /// </summary>
+ /// <param name="options">This LibraryOptions.</param>
+ /// <returns>CustomTagDelimiters in char[].</returns>
+ public static char[] GetCustomTagDelimiters(this LibraryOptions options)
+ {
+ ArgumentNullException.ThrowIfNull(options);
+
+ return options.CustomTagDelimiters.Select<string, char?>(x =>
+ {
+ var isChar = char.TryParse(x, out var c);
+ if (isChar)
+ {
+ return c;
+ }
+
+ return null;
+ }).Where(x => x is not null).Select(x => x!.Value).ToArray();
+ }
+}