From 38103d3fa751c20a63dc744dd59641ef5d30e535 Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Fri, 14 Jun 2024 23:48:56 -0400 Subject: [PATCH 1/3] Fix ReplaceInvalidFilenameChars throwing on null input & add some null annotations --- TwitchDownloaderCore/Tools/FilenameService.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/TwitchDownloaderCore/Tools/FilenameService.cs b/TwitchDownloaderCore/Tools/FilenameService.cs index 8f73faeb..8ae0ddf2 100644 --- a/TwitchDownloaderCore/Tools/FilenameService.cs +++ b/TwitchDownloaderCore/Tools/FilenameService.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Text; @@ -9,7 +10,7 @@ namespace TwitchDownloaderCore.Tools { public static class FilenameService { - public static string GetFilename(string template, string title, string id, DateTime date, string channel, TimeSpan trimStart, TimeSpan trimEnd, long viewCount, string game) + public static string GetFilename(string template, [AllowNull] string title, [AllowNull] string id, DateTime date, [AllowNull] string channel, TimeSpan trimStart, TimeSpan trimEnd, long viewCount, [AllowNull] string game) { var videoLength = trimEnd - trimStart; @@ -86,8 +87,14 @@ private static string[] GetTemplateSubfolders(ref string fullPath) private static readonly char[] FilenameInvalidChars = Path.GetInvalidFileNameChars(); - public static string ReplaceInvalidFilenameChars(string filename) + [return: NotNullIfNotNull(nameof(filename))] + public static string ReplaceInvalidFilenameChars([AllowNull] string filename) { + if (string.IsNullOrEmpty(filename)) + { + return filename; + } + const string TIMESTAMP_PATTERN = /*lang=regex*/ @"(?<=\d):(?=\d\d)"; var newName = Regex.Replace(filename, TIMESTAMP_PATTERN, "_"); From a9a93c84668c15239c29e45375b6ee9c964ad8f7 Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sat, 15 Jun 2024 00:02:23 -0400 Subject: [PATCH 2/3] Update tests --- .../ToolTests/FilenameServiceTests.cs | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs b/TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs index 078607d8..6e346fa9 100644 --- a/TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs +++ b/TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs @@ -1,5 +1,4 @@ -using TwitchDownloaderCore.Extensions; -using TwitchDownloaderCore.Tools; +using TwitchDownloaderCore.Tools; namespace TwitchDownloaderCore.Tests.ToolTests { @@ -148,6 +147,17 @@ public void DoesNotInterpretBogusTemplateParameter() Assert.Equal(EXPECTED, result); } + [Fact] + public void GetFilenameDoesNotThrow_WhenNullOrDefaultInput() + { + const string TEMPLATE = "{title}-{id}-{date}-{channel}-{trim_start}-{trim_end}-{length}-{views}-{game}-{date_custom=\"s\"}-{trim_start_custom=\"hh\\-mm\\-ss\"}-{trim_end_custom=\"hh\\-mm\\-ss\"}-{length_custom=\"hh\\-mm\\-ss\"}"; + const string EXPECTED = "--1-1-01--00-00-00-00-00-00-00-00-00-0--0001-01-01T00_00_00-00-00-00-00-00-00-00-00-00"; + + var result = FilenameService.GetFilename(TEMPLATE, default, default, default, default, default, default, default, default); + + Assert.Equal(EXPECTED, result); + } + [Theory] [InlineData("\"", """)] [InlineData("*", "*")] @@ -166,6 +176,17 @@ public void CorrectlyReplacesInvalidFilenameCharacters(string str, string expect Assert.Equal(expected, actual); } + [Fact] + public void ReplaceInvalidFilenameCharactersDoesNotThrow_WhenNullInput() + { + const string? STR = null; + const string? EXPECTED = null; + + var actual = FilenameService.ReplaceInvalidFilenameChars(STR); + + Assert.Equal(EXPECTED, actual); + } + [Fact] public void GetNonCollidingNameWorks_WhenNoCollisionExists() { From a4ea7e68c8f581be5ed33baa0ec9f98986bc54ee Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sat, 15 Jun 2024 00:10:40 -0400 Subject: [PATCH 3/3] Use underscores instead of dashes --- TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs b/TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs index 6e346fa9..2a6a02cc 100644 --- a/TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs +++ b/TwitchDownloaderCore.Tests/ToolTests/FilenameServiceTests.cs @@ -150,8 +150,8 @@ public void DoesNotInterpretBogusTemplateParameter() [Fact] public void GetFilenameDoesNotThrow_WhenNullOrDefaultInput() { - const string TEMPLATE = "{title}-{id}-{date}-{channel}-{trim_start}-{trim_end}-{length}-{views}-{game}-{date_custom=\"s\"}-{trim_start_custom=\"hh\\-mm\\-ss\"}-{trim_end_custom=\"hh\\-mm\\-ss\"}-{length_custom=\"hh\\-mm\\-ss\"}"; - const string EXPECTED = "--1-1-01--00-00-00-00-00-00-00-00-00-0--0001-01-01T00_00_00-00-00-00-00-00-00-00-00-00"; + const string TEMPLATE = "{title}_{id}_{date}_{channel}_{trim_start}_{trim_end}_{length}_{views}_{game}_{date_custom=\"s\"}_{trim_start_custom=\"hh\\-mm\\-ss\"}_{trim_end_custom=\"hh\\-mm\\-ss\"}_{length_custom=\"hh\\-mm\\-ss\"}"; + const string EXPECTED = "__1-1-01__00-00-00_00-00-00_00-00-00_0__0001-01-01T00_00_00_00-00-00_00-00-00_00-00-00"; var result = FilenameService.GetFilename(TEMPLATE, default, default, default, default, default, default, default, default);