From 9a446ade281dd5309f5d9e2731e959d2cfb9bb6c Mon Sep 17 00:00:00 2001 From: nilaoda Date: Wed, 20 Jul 2022 15:05:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E9=9D=9E=E6=B3=95=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DownloadManager/SimpleDownloadManager.cs | 4 ++++ src/N_m3u8DL-RE/Util/ConvertUtil.cs | 15 +++++++++++++++ 2 files changed, 19 insertions(+) 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('.'); + } } }