Fixed the null reference exception prompted when downloading pure music.

This commit is contained in:
real-zony 2022-07-24 23:48:46 +08:00
parent 7463709a39
commit 55f720c1a1
3 changed files with 16 additions and 7 deletions

View File

@ -50,7 +50,7 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
[Option("-d|--dir", CommandOptionType.SingleValue, Description = "指定需要扫描的目录。")] [Option("-d|--dir", CommandOptionType.SingleValue, Description = "指定需要扫描的目录。")]
[DirectoryExists] [DirectoryExists]
public string Directory { get; set; } public string SongsDirectory { get; set; }
[Option("-l|--lyric", CommandOptionType.NoValue, Description = "指定程序需要下载歌词文件。")] [Option("-l|--lyric", CommandOptionType.NoValue, Description = "指定程序需要下载歌词文件。")]
public bool DownloadLyric { get; set; } public bool DownloadLyric { get; set; }
@ -61,6 +61,8 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
[Option("-n|--number", CommandOptionType.SingleValue, Description = "指定下载时候的线程数量。(默认值 2)")] [Option("-n|--number", CommandOptionType.SingleValue, Description = "指定下载时候的线程数量。(默认值 2)")]
public int ParallelNumber { get; set; } = 2; public int ParallelNumber { get; set; } = 2;
[Option] public string ErrorMessage { get; set; } = Path.Combine(Directory.GetCurrentDirectory(), "error.log");
#endregion #endregion
protected override async Task<int> OnExecuteAsync(CommandLineApplication app) protected override async Task<int> OnExecuteAsync(CommandLineApplication app)
@ -83,7 +85,7 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
private async Task<List<string>> ScanMusicFilesAsync() private async Task<List<string>> ScanMusicFilesAsync()
{ {
var files = (await _fileScanner.ScanAsync(Directory, _options.SupportFileExtensions)) var files = (await _fileScanner.ScanAsync(SongsDirectory, _options.SupportFileExtensions))
.SelectMany(t => t.FilePaths) .SelectMany(t => t.FilePaths)
.ToList(); .ToList();
@ -180,9 +182,11 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
File.Delete(lyricFilePath); File.Delete(lyricFilePath);
} }
info.IsSuccessful = true;
if (lyric.IsPruneMusic) if (lyric.IsPruneMusic)
{ {
info.IsSuccessful = true; return;
} }
await using var stream = new FileStream(lyricFilePath, FileMode.Create); await using var stream = new FileStream(lyricFilePath, FileMode.Create);
@ -205,10 +209,6 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
_logger.LogError($"下载歌词文件时发生错误:{ex.Message},歌曲名: {info.Name},歌手: {info.Artist}。"); _logger.LogError($"下载歌词文件时发生错误:{ex.Message},歌曲名: {info.Name},歌手: {info.Artist}。");
info.IsSuccessful = false; info.IsSuccessful = false;
} }
finally
{
info.IsSuccessful = true;
}
} }
foreach (var downloader in downloaderList) foreach (var downloader in downloaderList)

View File

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Shouldly; using Shouldly;
using Xunit; using Xunit;

View File

@ -69,5 +69,13 @@ namespace ZonyLrcTools.Tests.Infrastructure.Lyric
lyric.ShouldNotBeNull(); lyric.ShouldNotBeNull();
} }
[Fact]
public async Task UnknownIssue_Test()
{
var lyric = await _lyricDownloader.DownloadAsync("主題歌Arrietty's Song", "Cécile Corbel");
lyric.ShouldNotBeNull();
}
} }
} }