feat(*): Add filename helper methods

This commit is contained in:
Josh Creek
2021-08-29 15:50:05 +01:00
parent 2c7a96d934
commit 4ad7c2a4e0
+29
View File
@@ -181,5 +181,34 @@ namespace YouTubeChannelDownloader
Console.WriteLine($"Recorded download in db of '{channelTitle} {youtubeVideo.VideoNumber} - {video.Title}'"); 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;
} }
} }