From 34f82f989fff2840c821368259b20e2455443f10 Mon Sep 17 00:00:00 2001 From: John Andrews Date: Sun, 4 Feb 2024 15:16:11 +1300 Subject: [PATCH] FF-1269 - Video Has Stream now check for regex before using a regex --- .../FfmpegBuilderTrackSorter.cs | 13 ++----------- VideoNodes/Helpers/GeneralHelper.cs | 17 +++++++++++++++++ VideoNodes/LogicalNodes/VideoHasStream.cs | 15 +++++++++++---- 3 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 VideoNodes/Helpers/GeneralHelper.cs diff --git a/VideoNodes/FfmpegBuilderNodes/FfmpegBuilderTrackSorter.cs b/VideoNodes/FfmpegBuilderNodes/FfmpegBuilderTrackSorter.cs index 8cfc2ea2..ef7cb64f 100644 --- a/VideoNodes/FfmpegBuilderNodes/FfmpegBuilderTrackSorter.cs +++ b/VideoNodes/FfmpegBuilderNodes/FfmpegBuilderTrackSorter.cs @@ -1,5 +1,6 @@ using System.Globalization; using FileFlows.VideoNodes.FfmpegBuilderNodes.Models; +using FileFlows.VideoNodes.Helpers; namespace FileFlows.VideoNodes.FfmpegBuilderNodes; @@ -294,7 +295,7 @@ public class FfmpegBuilderTrackSorter : FfmpegBuilderNode } else if (IsMathOperation(comparison)) result = ApplyMathOperation(value.ToString(), comparison) ? 0 : 1; - else if (IsRegex(comparison)) + else if (GeneralHelper.IsRegex(comparison)) result = Regex.IsMatch(value.ToString(), comparison, RegexOptions.IgnoreCase) ? 0 : 1; else if (value != null && double.TryParse(value.ToString(), out double dbl)) result = dbl; @@ -456,16 +457,6 @@ public class FfmpegBuilderTrackSorter : FfmpegBuilderNode // Check if the comparison string starts with <=, <, >, >=, ==, or = return new[] { "<=", "<", ">", ">=", "==", "=" }.Any(comparison.StartsWith); } - - /// - /// Checks if the comparison string represents a regular expression. - /// - /// The comparison string to check. - /// True if the comparison is a regular expression, otherwise false. - private static bool IsRegex(string comparison) - { - return new[] { "?", "|", "^", "$" }.Any(ch => comparison.Contains(ch)); - } /// /// Applies a mathematical operation to the value based on the specified operation string. diff --git a/VideoNodes/Helpers/GeneralHelper.cs b/VideoNodes/Helpers/GeneralHelper.cs new file mode 100644 index 00000000..ec9aba71 --- /dev/null +++ b/VideoNodes/Helpers/GeneralHelper.cs @@ -0,0 +1,17 @@ +namespace FileFlows.VideoNodes.Helpers; + +/// +/// General helper +/// +public class GeneralHelper +{ + /// + /// Checks if the input string represents a regular expression. + /// + /// The input string to check. + /// True if the input is a regular expression, otherwise false. + public static bool IsRegex(string input) + { + return new[] { "?", "|", "^", "$" }.Any(ch => input.Contains(ch)); + } +} \ No newline at end of file diff --git a/VideoNodes/LogicalNodes/VideoHasStream.cs b/VideoNodes/LogicalNodes/VideoHasStream.cs index 537d8983..c3c3e812 100644 --- a/VideoNodes/LogicalNodes/VideoHasStream.cs +++ b/VideoNodes/LogicalNodes/VideoHasStream.cs @@ -1,5 +1,6 @@ using FileFlows.VideoNodes.FfmpegBuilderNodes; using FileFlows.VideoNodes.FfmpegBuilderNodes.Models; +using FileFlows.VideoNodes.Helpers; namespace FileFlows.VideoNodes; @@ -209,14 +210,20 @@ public class VideoHasStream : VideoNode if (string.IsNullOrEmpty(value)) return MatchResult.NoMatch; - var rgx = new Regex(pattern, RegexOptions.IgnoreCase); - if(rgx.IsMatch(value)) - return MatchResult.Matched; + + if (GeneralHelper.IsRegex(pattern)) + { + var rgx = new Regex(pattern, RegexOptions.IgnoreCase); + if (rgx.IsMatch(value)) + return MatchResult.Matched; + } if (value.ToLower() == "hevc" && (pattern.ToLower() == "h265" || pattern == "265" || pattern.ToLower() == "h.265")) return MatchResult.Matched; // special case - return MatchResult.NoMatch; + return pattern.ToLowerInvariant().Trim() == value.ToLowerInvariant().Trim() + ? MatchResult.Matched + : MatchResult.NoMatch; } catch (Exception) {