Core: Filter invalid directory name for Windows

This commit is contained in:
genteure 2022-08-01 21:34:09 +08:00
parent e0b01bbd4f
commit bf40fe27ec

View File

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using BililiveRecorder.Core.Config.V3; using BililiveRecorder.Core.Config.V3;
using Fluid; using Fluid;
@ -224,11 +225,16 @@ namespace BililiveRecorder.Core.Templating
} }
} }
private static readonly Regex invalidDirectoryNameRegex = new Regex(@"([ .])([\\\/])", RegexOptions.Compiled);
internal static string RemoveInvalidFileName(string input, bool ignore_slash = true) internal static string RemoveInvalidFileName(string input, bool ignore_slash = true)
{ {
foreach (var c in Path.GetInvalidFileNameChars()) foreach (var c in Path.GetInvalidFileNameChars())
if (!ignore_slash || c != '\\' && c != '/') if (!ignore_slash || c != '\\' && c != '/')
input = input.Replace(c, '_'); input = input.Replace(c, '_');
input = invalidDirectoryNameRegex.Replace(input, "$1_$2");
return input; return input;
} }