From 00ca4abbe1138d880fca36e1f99da14e6fab252a Mon Sep 17 00:00:00 2001 From: gnattu Date: Tue, 24 Sep 2024 05:15:46 +0800 Subject: [PATCH 1/2] Sanitize CustomTagDelimiters server side The API requires an array type and does not support runtime generated default value. Use server side helper function to sanitize it into char. --- .../Configuration/LibraryOptions.cs | 20 ++++++++++++++++--- .../MediaInfo/AudioFileProber.cs | 6 +++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/MediaBrowser.Model/Configuration/LibraryOptions.cs b/MediaBrowser.Model/Configuration/LibraryOptions.cs index 04283cc9ecf7..b0fcc2d0adaa 100644 --- a/MediaBrowser.Model/Configuration/LibraryOptions.cs +++ b/MediaBrowser.Model/Configuration/LibraryOptions.cs @@ -2,12 +2,13 @@ using System; using System.ComponentModel; +using System.Linq; namespace MediaBrowser.Model.Configuration { public class LibraryOptions { - private static readonly char[] _defaultTagDelimiters = ['/', '|', ';', '\\']; + private static readonly string[] _defaultTagDelimiters = ["/", "|", ";", "\\"]; public LibraryOptions() { @@ -126,8 +127,7 @@ public LibraryOptions() [DefaultValue(false)] public bool UseCustomTagDelimiters { get; set; } - [DefaultValue(typeof(LibraryOptions), nameof(_defaultTagDelimiters))] - public char[] CustomTagDelimiters { get; set; } + public string[] CustomTagDelimiters { get; set; } public string[] DelimiterWhitelist { get; set; } @@ -149,5 +149,19 @@ public LibraryOptions() return null; } + + public char[] GetCustomTagDelimiters() + { + return CustomTagDelimiters.Select(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(); + } } } diff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs index 80bb1a514c61..2e0d21c6a3e5 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs @@ -178,7 +178,7 @@ private async Task FetchDataFromTags(Audio audio, Model.MediaInfo.MediaInfo medi if (libraryOptions.UseCustomTagDelimiters) { - albumArtists = albumArtists.SelectMany(a => SplitWithCustomDelimiter(a, libraryOptions.CustomTagDelimiters, libraryOptions.DelimiterWhitelist)).ToArray(); + albumArtists = albumArtists.SelectMany(a => SplitWithCustomDelimiter(a, libraryOptions.GetCustomTagDelimiters(), libraryOptions.DelimiterWhitelist)).ToArray(); } foreach (var albumArtist in albumArtists) @@ -210,7 +210,7 @@ private async Task FetchDataFromTags(Audio audio, Model.MediaInfo.MediaInfo medi if (libraryOptions.UseCustomTagDelimiters) { - performers = performers.SelectMany(p => SplitWithCustomDelimiter(p, libraryOptions.CustomTagDelimiters, libraryOptions.DelimiterWhitelist)).ToArray(); + performers = performers.SelectMany(p => SplitWithCustomDelimiter(p, libraryOptions.GetCustomTagDelimiters(), libraryOptions.DelimiterWhitelist)).ToArray(); } foreach (var performer in performers) @@ -313,7 +313,7 @@ private async Task FetchDataFromTags(Audio audio, Model.MediaInfo.MediaInfo medi if (libraryOptions.UseCustomTagDelimiters) { - genres = genres.SelectMany(g => SplitWithCustomDelimiter(g, libraryOptions.CustomTagDelimiters, libraryOptions.DelimiterWhitelist)).ToArray(); + genres = genres.SelectMany(g => SplitWithCustomDelimiter(g, libraryOptions.GetCustomTagDelimiters(), libraryOptions.DelimiterWhitelist)).ToArray(); } audio.Genres = options.ReplaceAllMetadata || audio.Genres is null || audio.Genres.Length == 0 From 0ffddacf11795b8a50606b6515c1dc6828ad8dd0 Mon Sep 17 00:00:00 2001 From: gnattu Date: Tue, 24 Sep 2024 12:36:05 +0800 Subject: [PATCH 2/2] Move GetCustomTagDelimiters to Extension --- .../Configuration/LibraryOptions.cs | 14 -------- .../Extensions/LibraryOptionsExtension.cs | 32 +++++++++++++++++++ .../MediaInfo/AudioFileProber.cs | 1 + 3 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 MediaBrowser.Model/Extensions/LibraryOptionsExtension.cs diff --git a/MediaBrowser.Model/Configuration/LibraryOptions.cs b/MediaBrowser.Model/Configuration/LibraryOptions.cs index b0fcc2d0adaa..6054ba34e590 100644 --- a/MediaBrowser.Model/Configuration/LibraryOptions.cs +++ b/MediaBrowser.Model/Configuration/LibraryOptions.cs @@ -149,19 +149,5 @@ public LibraryOptions() return null; } - - public char[] GetCustomTagDelimiters() - { - return CustomTagDelimiters.Select(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(); - } } } diff --git a/MediaBrowser.Model/Extensions/LibraryOptionsExtension.cs b/MediaBrowser.Model/Extensions/LibraryOptionsExtension.cs new file mode 100644 index 000000000000..4a814f22a3e4 --- /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; + +/// +/// Extensions for . +/// +public static class LibraryOptionsExtension +{ + /// + /// Get the custom tag delimiters. + /// + /// This LibraryOptions. + /// CustomTagDelimiters in char[]. + public static char[] GetCustomTagDelimiters(this LibraryOptions options) + { + ArgumentNullException.ThrowIfNull(options); + + return options.CustomTagDelimiters.Select(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(); + } +} diff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs index 2e0d21c6a3e5..cb233d31e7a5 100644 --- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs +++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs @@ -16,6 +16,7 @@ using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Extensions; using MediaBrowser.Model.MediaInfo; using Microsoft.Extensions.Logging;