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] 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, "_");