去除文件名中的非法字符

This commit is contained in:
nilaoda 2022-07-20 15:05:35 +08:00
parent 34fc09d73b
commit 9a446ade28
2 changed files with 19 additions and 0 deletions

View File

@ -41,9 +41,13 @@ namespace N_m3u8DL_RE.DownloadManager
var type = streamSpec.MediaType ?? Common.Enum.MediaType.VIDEO; var type = streamSpec.MediaType ?? Common.Enum.MediaType.VIDEO;
var dirName = $"{NowDateTime:yyyy-MM-dd_HH-mm-ss}_{streamSpec.GroupId}_{streamSpec.Codecs}_{streamSpec.Language}"; 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 tmpDir = Path.Combine(DownloaderConfig.TmpDir ?? Environment.CurrentDirectory, dirName);
var saveDir = DownloaderConfig.SaveDir ?? Environment.CurrentDirectory; var saveDir = DownloaderConfig.SaveDir ?? Environment.CurrentDirectory;
var saveName = DownloaderConfig.SaveName != null ? $"{DownloaderConfig.SaveName}.{type}.{streamSpec.Language}" : dirName; var saveName = DownloaderConfig.SaveName != null ? $"{DownloaderConfig.SaveName}.{type}.{streamSpec.Language}" : dirName;
//去除非法字符
dirName = ConvertUtil.GetValidFileName(dirName, filterSlash: true);
var headers = DownloaderConfig.Headers; var headers = DownloaderConfig.Headers;
var output = Path.Combine(saveDir, saveName + $".{streamSpec.Extension ?? "ts"}"); var output = Path.Combine(saveDir, saveName + $".{streamSpec.Extension ?? "ts"}");

View File

@ -51,5 +51,20 @@ namespace N_m3u8DL_RE.Util
_ => throw new NotSupportedException($"{toFormat} not supported!") _ => 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('.');
}
} }
} }