diff --git a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs index 209442b..4c05fef 100644 --- a/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs +++ b/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs @@ -41,9 +41,13 @@ namespace N_m3u8DL_RE.DownloadManager var type = streamSpec.MediaType ?? Common.Enum.MediaType.VIDEO; var dirName = $"{NowDateTime:yyyy-MM-dd_HH-mm-ss}_{streamSpec.GroupId}_{streamSpec.Codecs}_{streamSpec.Language}"; + //去除非法字符 + dirName = ConvertUtil.GetValidFileName(dirName, filterSlash: true); var tmpDir = Path.Combine(DownloaderConfig.TmpDir ?? Environment.CurrentDirectory, dirName); var saveDir = DownloaderConfig.SaveDir ?? Environment.CurrentDirectory; var saveName = DownloaderConfig.SaveName != null ? $"{DownloaderConfig.SaveName}.{type}.{streamSpec.Language}" : dirName; + //去除非法字符 + dirName = ConvertUtil.GetValidFileName(dirName, filterSlash: true); var headers = DownloaderConfig.Headers; var output = Path.Combine(saveDir, saveName + $".{streamSpec.Extension ?? "ts"}"); diff --git a/src/N_m3u8DL-RE/Util/ConvertUtil.cs b/src/N_m3u8DL-RE/Util/ConvertUtil.cs index 02076c3..4cc675e 100644 --- a/src/N_m3u8DL-RE/Util/ConvertUtil.cs +++ b/src/N_m3u8DL-RE/Util/ConvertUtil.cs @@ -51,5 +51,20 @@ namespace N_m3u8DL_RE.Util _ => throw new NotSupportedException($"{toFormat} not supported!") }; } + + public static string GetValidFileName(string input, string re = ".", bool filterSlash = false) + { + string title = input; + foreach (char invalidChar in Path.GetInvalidFileNameChars()) + { + title = title.Replace(invalidChar.ToString(), re); + } + if (filterSlash) + { + title = title.Replace("/", re); + title = title.Replace("\\", re); + } + return title.Trim('.'); + } } }