优化合并路径判断和删除临时文件夹的逻辑

This commit is contained in:
nilaoda 2022-08-21 22:23:41 +08:00
parent 1228d03f62
commit 3c17a20447
2 changed files with 25 additions and 10 deletions

View File

@ -48,6 +48,24 @@ namespace N_m3u8DL_RE.DownloadManager
} }
} }
//若该文件夹为空,删除,同时判断其父文件夹,直到遇到根目录或不为空的目录
private void SafeDeleteDir(string dirPath)
{
if (string.IsNullOrEmpty(dirPath) || !Directory.Exists(dirPath))
return;
var parent = Path.GetDirectoryName(dirPath)!;
if (!Directory.EnumerateFileSystemEntries(dirPath).Any())
{
Directory.Delete(dirPath);
}
else
{
return;
}
SafeDeleteDir(parent);
}
//从文件读取KEY //从文件读取KEY
private async Task SearchKeyAsync(string? currentKID) private async Task SearchKeyAsync(string? currentKID)
{ {
@ -254,8 +272,8 @@ namespace N_m3u8DL_RE.DownloadManager
//修改输出后缀 //修改输出后缀
var outputExt = "." + streamSpec.Extension; var outputExt = "." + streamSpec.Extension;
if (streamSpec.Extension == null) outputExt = ".ts"; if (streamSpec.Extension == null) outputExt = ".ts";
else if (streamSpec.MediaType == MediaType.AUDIO) outputExt = ".m4a"; else if (streamSpec.MediaType == MediaType.AUDIO && streamSpec.Extension == "m4s") outputExt = ".m4a";
else if (streamSpec.MediaType != MediaType.SUBTITLES) outputExt = ".mp4"; else if (streamSpec.MediaType != MediaType.SUBTITLES && streamSpec.Extension == "m4s") outputExt = ".mp4";
var output = Path.Combine(saveDir, saveName + outputExt); var output = Path.Combine(saveDir, saveName + outputExt);
@ -489,10 +507,7 @@ namespace N_m3u8DL_RE.DownloadManager
{ {
File.Delete(file); File.Delete(file);
} }
if (!Directory.EnumerateFiles(tmpDir).Any()) SafeDeleteDir(tmpDir);
{
Directory.Delete(tmpDir);
}
} }
//重新读取init信息 //重新读取init信息
@ -576,10 +591,7 @@ namespace N_m3u8DL_RE.DownloadManager
{ {
OutputFiles.ForEach(f => File.Delete(f.FilePath)); OutputFiles.ForEach(f => File.Delete(f.FilePath));
var tmpDir = DownloaderConfig.TmpDir ?? Environment.CurrentDirectory; var tmpDir = DownloaderConfig.TmpDir ?? Environment.CurrentDirectory;
if (!Directory.EnumerateFiles(tmpDir).Any()) SafeDeleteDir(tmpDir);
{
Directory.Delete(tmpDir);
}
} }
else Logger.ErrorMarkUp($"Mux failed"); else Logger.ErrorMarkUp($"Mux failed");
} }

View File

@ -79,6 +79,9 @@ namespace N_m3u8DL_RE.Util
bool writeDate = true, string poster = "", string audioName = "", string title = "", bool writeDate = true, string poster = "", string audioName = "", string title = "",
string copyright = "", string comment = "", string encodingTool = "", string recTime = "") string copyright = "", string comment = "", string encodingTool = "", string recTime = "")
{ {
//改为绝对路径
outputPath = Path.GetFullPath(outputPath);
string dateString = string.IsNullOrEmpty(recTime) ? DateTime.Now.ToString("o") : recTime; string dateString = string.IsNullOrEmpty(recTime) ? DateTime.Now.ToString("o") : recTime;
StringBuilder command = new StringBuilder("-loglevel warning -i concat:\""); StringBuilder command = new StringBuilder("-loglevel warning -i concat:\"");