Skip to content

Commit

Permalink
Fixed the null reference exception prompted when downloading pure music.
Browse files Browse the repository at this point in the history
  • Loading branch information
real-zony committed Jul 24, 2022
1 parent 7463709 commit 55f720c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/ZonyLrcTools.Cli/Commands/SubCommand/DownloadCommand.cs
Expand Up @@ -50,7 +50,7 @@ public class DownloadCommand : ToolCommandBase

[Option("-d|--dir", CommandOptionType.SingleValue, Description = "指定需要扫描的目录。")]
[DirectoryExists]
public string Directory { get; set; }
public string SongsDirectory { get; set; }

[Option("-l|--lyric", CommandOptionType.NoValue, Description = "指定程序需要下载歌词文件。")]
public bool DownloadLyric { get; set; }
Expand All @@ -61,6 +61,8 @@ public class DownloadCommand : ToolCommandBase
[Option("-n|--number", CommandOptionType.SingleValue, Description = "指定下载时候的线程数量。(默认值 2)")]
public int ParallelNumber { get; set; } = 2;

[Option] public string ErrorMessage { get; set; } = Path.Combine(Directory.GetCurrentDirectory(), "error.log");

#endregion

protected override async Task<int> OnExecuteAsync(CommandLineApplication app)
Expand All @@ -83,7 +85,7 @@ protected override async Task<int> OnExecuteAsync(CommandLineApplication app)

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)
.ToList();

Expand Down Expand Up @@ -180,9 +182,11 @@ async Task InternalDownloadLogicAsync(ILyricDownloader downloader)
File.Delete(lyricFilePath);
}

info.IsSuccessful = true;

if (lyric.IsPruneMusic)
{
info.IsSuccessful = true;
return;
}

await using var stream = new FileStream(lyricFilePath, FileMode.Create);
Expand All @@ -205,10 +209,6 @@ async Task InternalDownloadLogicAsync(ILyricDownloader downloader)
_logger.LogError($"下载歌词文件时发生错误:{ex.Message},歌曲名: {info.Name},歌手: {info.Artist}");
info.IsSuccessful = false;
}
finally
{
info.IsSuccessful = true;
}
}

foreach (var downloader in downloaderList)
Expand Down
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
Expand Down
Expand Up @@ -69,5 +69,13 @@ public async Task DownloaderAsync_Issue88_Test()

lyric.ShouldNotBeNull();
}

[Fact]
public async Task UnknownIssue_Test()
{
var lyric = await _lyricDownloader.DownloadAsync("主題歌Arrietty's Song", "Cécile Corbel");

lyric.ShouldNotBeNull();
}
}
}

0 comments on commit 55f720c

Please sign in to comment.