From 4ad7c2a4e0382db9062007ac8a029f80d3b16d0a Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Sun, 29 Aug 2021 15:50:05 +0100 Subject: [PATCH] feat(*): Add filename helper methods --- YouTubeChannelDownloader/Program.cs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/YouTubeChannelDownloader/Program.cs b/YouTubeChannelDownloader/Program.cs index 517155c..6689c6a 100644 --- a/YouTubeChannelDownloader/Program.cs +++ b/YouTubeChannelDownloader/Program.cs @@ -181,5 +181,34 @@ namespace YouTubeChannelDownloader Console.WriteLine($"Recorded download in db of '{channelTitle} {youtubeVideo.VideoNumber} - {video.Title}'"); } + + private static string MakeValidFileName(string originalFileName) + { + char[] invalids = System.IO.Path.GetInvalidFileNameChars(); + string validFileName = String.Join("_", originalFileName.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.'); + + // Additionally remove Windows characters explicitly, in case we're running on Linux (e.g. in Docker) but need to access the files in Windows + string[] charsToRemove = new string[] { "<", ">", ":", "\"", "/", "\\", "|", "?", "*" }; + + validFileName = RemoveCharsFromString(charsToRemove, validFileName); + + return validFileName; + } + + private static string RemoveSlashesFromString(string str) + { + string[] charsToRemove = new string[] { "/", @"\" }; + + return RemoveCharsFromString(charsToRemove, str); + } + + private static string RemoveCharsFromString(string[] charsToRemove, string str) + { + foreach (string c in charsToRemove) + { + str = str.Replace(c, string.Empty); + } + + return str; } }