Merge pull request #344 from BlueHtml/main

[bug fix] 本地文件被多次解密导致报错 The input data is not a complete block
This commit is contained in:
nilaoda 2024-06-22 16:33:16 +08:00 committed by GitHub
commit a29c8b9482
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 48 additions and 47 deletions

View File

@ -1,4 +1,4 @@
using N_m3u8DL_RE.Common.Entity; using N_m3u8DL_RE.Common.Entity;
using N_m3u8DL_RE.Common.Enum; using N_m3u8DL_RE.Common.Enum;
using N_m3u8DL_RE.Common.Log; using N_m3u8DL_RE.Common.Log;
using N_m3u8DL_RE.Config; using N_m3u8DL_RE.Config;
@ -33,8 +33,10 @@ namespace N_m3u8DL_RE.Downloader
public async Task<DownloadResult?> DownloadSegmentAsync(MediaSegment segment, string savePath, SpeedContainer speedContainer, Dictionary<string, string>? headers = null) public async Task<DownloadResult?> DownloadSegmentAsync(MediaSegment segment, string savePath, SpeedContainer speedContainer, Dictionary<string, string>? headers = null)
{ {
var url = segment.Url; var url = segment.Url;
var dResult = await DownClipAsync(url, savePath, speedContainer, segment.StartRange, segment.StopRange, headers, DownloaderConfig.MyOptions.DownloadRetryCount); var (des, dResult) = await DownClipAsync(url, savePath, speedContainer, segment.StartRange, segment.StopRange, headers, DownloaderConfig.MyOptions.DownloadRetryCount);
if (dResult != null && dResult.Success && segment.EncryptInfo != null) if (dResult != null && dResult.Success && dResult.ActualFilePath != des)
{
if (segment.EncryptInfo != null)
{ {
if (segment.EncryptInfo.Method == EncryptMethod.AES_128) if (segment.EncryptInfo.Method == EncryptMethod.AES_128)
{ {
@ -73,10 +75,15 @@ namespace N_m3u8DL_RE.Downloader
await OtherUtil.DeGzipFileAsync(dResult.ActualFilePath); await OtherUtil.DeGzipFileAsync(dResult.ActualFilePath);
} }
} }
//处理完成后改名
File.Move(dResult.ActualFilePath, des);
dResult.ActualFilePath = des;
}
return dResult; return dResult;
} }
private async Task<DownloadResult?> DownClipAsync(string url, string path, SpeedContainer speedContainer, long? fromPosition, long? toPosition, Dictionary<string, string>? headers = null, int retryCount = 3) private async Task<(string des, DownloadResult? dResult)> DownClipAsync(string url, string path, SpeedContainer speedContainer, long? fromPosition, long? toPosition, Dictionary<string, string>? headers = null, int retryCount = 3)
{ {
CancellationTokenSource? cancellationTokenSource = null; CancellationTokenSource? cancellationTokenSource = null;
retry: retry:
@ -89,7 +96,7 @@ namespace N_m3u8DL_RE.Downloader
if (File.Exists(des)) if (File.Exists(des))
{ {
speedContainer.Add(new FileInfo(des).Length); speedContainer.Add(new FileInfo(des).Length);
return new DownloadResult() { ActualContentLength = 0, ActualFilePath = des }; return (des, new DownloadResult() { ActualContentLength = 0, ActualFilePath = des });
} }
//已解密跳过 //已解密跳过
@ -97,7 +104,7 @@ namespace N_m3u8DL_RE.Downloader
if (File.Exists(dec)) if (File.Exists(dec))
{ {
speedContainer.Add(new FileInfo(dec).Length); speedContainer.Add(new FileInfo(dec).Length);
return new DownloadResult() { ActualContentLength = 0, ActualFilePath = dec }; return (dec, new DownloadResult() { ActualContentLength = 0, ActualFilePath = dec });
} }
//另起线程进行监控 //另起线程进行监控
@ -118,14 +125,8 @@ namespace N_m3u8DL_RE.Downloader
//调用下载 //调用下载
var result = await DownloadUtil.DownloadToFileAsync(url, path, speedContainer, cancellationTokenSource, headers, fromPosition, toPosition); var result = await DownloadUtil.DownloadToFileAsync(url, path, speedContainer, cancellationTokenSource, headers, fromPosition, toPosition);
return (des, result);
//下载完成后改名
if (result.Success || !DownloaderConfig.CheckContentLength)
{
File.Move(path, des);
result.ActualFilePath = des;
return result;
}
throw new Exception("please retry"); throw new Exception("please retry");
} }
catch (Exception ex) catch (Exception ex)
@ -144,7 +145,7 @@ namespace N_m3u8DL_RE.Downloader
Logger.WarnMarkUp($"[grey]{ex.Message.EscapeMarkup()}[/]"); Logger.WarnMarkUp($"[grey]{ex.Message.EscapeMarkup()}[/]");
} }
//throw new Exception("download failed", ex); //throw new Exception("download failed", ex);
return null; return default;
} }
finally finally
{ {