From a1e96698436042f1681cc20c127fb9397f08d88d Mon Sep 17 00:00:00 2001 From: real-zony Date: Sun, 20 Mar 2022 19:10:51 +0800 Subject: [PATCH] feat: Fixed a possible null reference exception. --- .../Commands/SubCommand/DownloadCommand.cs | 4 +++- .../Infrastructure/Tag/DefaultTagLoader.cs | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/ZonyLrcTools.Cli/Commands/SubCommand/DownloadCommand.cs b/src/ZonyLrcTools.Cli/Commands/SubCommand/DownloadCommand.cs index 9e98d05..d0e829b 100644 --- a/src/ZonyLrcTools.Cli/Commands/SubCommand/DownloadCommand.cs +++ b/src/ZonyLrcTools.Cli/Commands/SubCommand/DownloadCommand.cs @@ -103,7 +103,9 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand var warpTask = new WarpTask(ParallelNumber); var warpTaskList = files.Select(file => warpTask.RunAsync(() => Task.Run(async () => await _tagLoader.LoadTagAsync(file)))); - var result = (await Task.WhenAll(warpTaskList)).Where(m => !string.IsNullOrEmpty(m.Name) || !string.IsNullOrEmpty(m.Artist)); + var result = (await Task.WhenAll(warpTaskList)) + .Where(m => m != null) + .Where(m => !string.IsNullOrEmpty(m.Name) || !string.IsNullOrEmpty(m.Artist)); _logger.LogInformation($"已成功加载 {files.Count} 个音乐文件的标签信息。"); diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Tag/DefaultTagLoader.cs b/src/ZonyLrcTools.Cli/Infrastructure/Tag/DefaultTagLoader.cs index bf5b699..4c63892 100644 --- a/src/ZonyLrcTools.Cli/Infrastructure/Tag/DefaultTagLoader.cs +++ b/src/ZonyLrcTools.Cli/Infrastructure/Tag/DefaultTagLoader.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using ZonyLrcTools.Cli.Config; using ZonyLrcTools.Cli.Infrastructure.DependencyInject; @@ -15,14 +16,18 @@ namespace ZonyLrcTools.Cli.Infrastructure.Tag { protected readonly IEnumerable TagInfoProviders; protected readonly IBlockWordDictionary BlockWordDictionary; + protected readonly ILogger _logger; + protected ToolOptions Options; public DefaultTagLoader(IEnumerable tagInfoProviders, IBlockWordDictionary blockWordDictionary, - IOptions options) + IOptions options, + ILogger logger) { TagInfoProviders = tagInfoProviders; BlockWordDictionary = blockWordDictionary; + _logger = logger; Options = options.Value; } @@ -43,6 +48,8 @@ namespace ZonyLrcTools.Cli.Infrastructure.Tag } } + _logger.LogWarning($"{filePath} 没有找到正确的标签信息,请考虑调整正则表达式。"); + return null; }