From eb299c18fdeaa0338abdb18b3b39e930e49e1154 Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Tue, 4 Apr 2023 20:28:58 -0400 Subject: [PATCH] Fix vod titles with slashes being interpretted as paths, fix other potential issues with invalid filename chars, fix potential crop issue with chatupdater GUI --- TwitchDownloaderWPF/PageChatUpdate.xaml.cs | 7 +++-- .../Services/FilenameService.cs | 30 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/TwitchDownloaderWPF/PageChatUpdate.xaml.cs b/TwitchDownloaderWPF/PageChatUpdate.xaml.cs index 37d0966c..84197897 100644 --- a/TwitchDownloaderWPF/PageChatUpdate.xaml.cs +++ b/TwitchDownloaderWPF/PageChatUpdate.xaml.cs @@ -127,8 +127,11 @@ private async void btnBrowse_Click(object sender, RoutedEventArgs e) } else { - numStartHour.Maximum = 0; - numEndHour.Maximum = 0; + if (VideoId != "-1") + { + numStartHour.Maximum = 0; + numEndHour.Maximum = 0; + } GqlClipResponse videoInfo = await TwitchHelper.GetClipInfo(VideoId); if (videoInfo.data.clip.video == null) { diff --git a/TwitchDownloaderWPF/Services/FilenameService.cs b/TwitchDownloaderWPF/Services/FilenameService.cs index aac2788e..560454b8 100644 --- a/TwitchDownloaderWPF/Services/FilenameService.cs +++ b/TwitchDownloaderWPF/Services/FilenameService.cs @@ -10,18 +10,24 @@ public static class FilenameService { private static string[] GetTemplateSubfolders(ref string fullPath) { - string[] returnString = fullPath.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries); + var returnString = fullPath.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries); fullPath = returnString[^1]; Array.Resize(ref returnString, returnString.Length - 1); + + for (var i = 0; i < returnString.Length; i++) + { + returnString[i] = RemoveInvalidFilenameChars(returnString[i]); + } + return returnString; } internal static string GetFilename(string template, string title, string id, DateTime date, string channel, TimeSpan cropStart, TimeSpan cropEnd) { var stringBuilder = new StringBuilder(template) - .Replace("{title}", title) + .Replace("{title}", RemoveInvalidFilenameChars(title)) .Replace("{id}", id) - .Replace("{channel}", channel) + .Replace("{channel}", RemoveInvalidFilenameChars(channel)) .Replace("{date}", date.ToString("Mdyy")) .Replace("{random_string}", Path.GetFileNameWithoutExtension(Path.GetRandomFileName())) .Replace("{crop_start}", string.Format(new TimeSpanHFormat(), @"{0:HH\-mm\-ss}", cropStart)) @@ -38,7 +44,7 @@ internal static string GetFilename(string template, string title, string id, Dat { var formatString = dateMatch.Groups[1].Value; stringBuilder.Remove(dateMatch.Groups[0].Index, dateMatch.Groups[0].Length); - stringBuilder.Insert(dateMatch.Groups[0].Index, date.ToString(formatString)); + stringBuilder.Insert(dateMatch.Groups[0].Index, RemoveInvalidFilenameChars(date.ToString(formatString))); } else { @@ -58,7 +64,7 @@ internal static string GetFilename(string template, string title, string id, Dat { var formatString = cropStartMatch.Groups[1].Value; stringBuilder.Remove(cropStartMatch.Groups[0].Index, cropStartMatch.Groups[0].Length); - stringBuilder.Insert(cropStartMatch.Groups[0].Index, cropStart.ToString(formatString)); + stringBuilder.Insert(cropStartMatch.Groups[0].Index, RemoveInvalidFilenameChars(cropStart.ToString(formatString))); } else { @@ -78,7 +84,7 @@ internal static string GetFilename(string template, string title, string id, Dat { var formatString = cropEndMatch.Groups[1].Value; stringBuilder.Remove(cropEndMatch.Groups[0].Index, cropEndMatch.Groups[0].Length); - stringBuilder.Insert(cropEndMatch.Groups[0].Index, cropEnd.ToString(formatString)); + stringBuilder.Insert(cropEndMatch.Groups[0].Index, RemoveInvalidFilenameChars(cropEnd.ToString(formatString))); } else { @@ -94,7 +100,17 @@ internal static string GetFilename(string template, string title, string id, Dat private static string RemoveInvalidFilenameChars(string filename) { - return string.Concat(filename.Split(Path.GetInvalidFileNameChars())); + if (string.IsNullOrWhiteSpace(filename)) + { + return filename; + } + + if (filename.IndexOfAny(Path.GetInvalidFileNameChars()) == -1) + { + return filename; + } + + return string.Join('_', filename.Split(Path.GetInvalidFileNameChars())); } } } \ No newline at end of file