fix: 一个坏文件不再中断整批任务

三个问题会让单个坏文件毁掉整次任务:

1. LoggerHelper.LogWarningInfo 在错误码小于 50000 时会把异常重新抛出。
   而这个方法的所有调用点都在 catch 块里,异常于是穿透调用方的 catch,
   整批任务就此中断。现在它只负责记录,要不要往上抛由调用方决定。

2. LyricsDownloader 把"这个源没返回歌词"当成了"这首歌是纯音乐":它把
   IsSuccessful 置为 true 并跳出 provider 循环,后面的源根本没被问到。
   现在只有真正拿到歌词才算成功;空结果会继续问下一个源,所有源都为空
   才判为跳过。

3. LyricsDownloader 在写文件之前就把这一首标成成功。当配置的
   fileEncoding 非法时,写入抛异常、异常被记进日志,而这一首仍然报告
   成功 —— 磁盘上留下 0 字节的 .lrc,界面上却是绿色。现在先做编码转换
   再建文件,失败时不留空壳,也如实报告失败。

顺带:
- IAlbumDownloader / AlbumDownloader 增加可选的逐首回调,调用方据此可以
  渲染进度;CLI 不传,行为不变。
- AlbumDownloader 不再在封面真正写盘之前记成功日志(原先它永远把所有
  曲目都算作成功)。
- 各下载循环补上 WarpTask 的释放;回调抛出的异常会被捕获并记录,不再
  掀翻整次运行。
This commit is contained in:
aL1CesT committed 2026-11-09 04:27:41 +08:00
1 parent 9cfcc02a4a
commit 1c0f8e8d3a
4 files changed
+159 -33

No files matched your search

@@ -1,4 +1,4 @@
using ZonyLrcTools.Common.Infrastructure.DependencyInject;
using ZonyLrcTools.Common.Infrastructure.DependencyInject;
using ZonyLrcTools.Common.Infrastructure.Exceptions;
using ZonyLrcTools.Common.Infrastructure.Extensions;
using ZonyLrcTools.Common.Infrastructure.IO;
@@ -27,6 +27,7 @@ public class AlbumDownloader : IAlbumDownloader, ISingletonDependency
public async Task DownloadAsync(List<MusicInfo> needDownloadMusicInfos,
int parallelCount = 2,
Func<MusicInfo, Task>? callback = null,
CancellationToken cancellationToken = default)
{
await _logger.InfoAsync("开始下载专辑图像数据...");
@@ -34,35 +35,97 @@ public class AlbumDownloader : IAlbumDownloader, ISingletonDependency
var provider = AvailableProviders.FirstOrDefault(d => d.DownloaderName == InternalAlbumProviderNames.NetEase);
if (provider == null)
{
// 没有任何可用的专辑图像源,全部标记为失败并回调,避免调用方一直等待。
foreach (var info in needDownloadMusicInfos)
{
info.IsSuccessful = false;
if (callback == null)
{
continue;
}
try
{
await callback(info);
}
catch (Exception ex)
{
await _logger.WarnAsync($"处理专辑下载回调时发生错误:{ex.Message},歌曲名: {info.Name}。");
}
}
return;
}
var warpTask = new WarpTask(parallelCount);
if (parallelCount <= 0)
{
parallelCount = 1;
}
using var warpTask = new WarpTask(parallelCount);
var warpTaskList = needDownloadMusicInfos.Select(info =>
warpTask.RunAsync(() =>
Task.Run(async () =>
{
_logger.LogSuccessful(info);
try
{
cancellationToken.ThrowIfCancellationRequested();
var album = await provider.DownloadAsync(info.Name, info.Artist);
var filePath = Path.Combine(Path.GetDirectoryName(info.FilePath)!, $"{Path.GetFileNameWithoutExtension(info.FilePath)}.png");
if (File.Exists(filePath) || album.Length <= 0)
var filePath = Path.Combine(Path.GetDirectoryName(info.FilePath)!,
$"{Path.GetFileNameWithoutExtension(info.FilePath)}.png");
if (album.Length <= 0)
{
info.IsSuccessful = false;
return;
}
// 已存在同名图像视为成功,不重复覆盖用户已有的封面。
if (File.Exists(filePath))
{
info.IsSuccessful = true;
return;
}
await new FileStream(filePath, FileMode.Create).WriteBytesToFileAsync(album);
info.IsSuccessful = true;
_logger.LogSuccessful(info);
}
catch (ErrorCodeException ex)
{
info.IsSuccessful = false;
_logger.LogWarningInfo(ex);
}
catch (OperationCanceledException)
{
info.IsSuccessful = false;
}
catch (Exception ex)
{
info.IsSuccessful = false;
await _logger.ErrorAsync($"下载专辑图像时发生错误:{ex.Message},歌曲名: {info.Name},歌手: {info.Artist}。");
}
finally
{
// 回调是调用方(GUI)提供的 UI 代码,它抛异常不应该拖垮整批下载。
if (callback != null)
{
try
{
await callback(info);
}
catch (Exception ex)
{
await _logger.WarnAsync($"处理专辑下载回调时发生错误:{ex.Message},歌曲名: {info.Name}。");
}
}
}
}, cancellationToken), cancellationToken));
await Task.WhenAll(warpTaskList);
await _logger.InfoAsync($"专辑数据下载完成,成功: {needDownloadMusicInfos.Count(m => m.IsSuccessful)} 失败{needDownloadMusicInfos.Count(m => m.IsSuccessful == false)}。");
}
}
}