From c3d98d60b503c34c0b09de06405a956cd0702232 Mon Sep 17 00:00:00 2001 From: real-zony Date: Sun, 15 Dec 2024 15:20:33 +0800 Subject: [PATCH] fix: Fixed the garbled text issue caused by the CSV file encoding. (#156) --- Directory.Packages.props | 7 +---- src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj | 1 + .../MusicScanner/CsvFileMusicScanner.cs | 29 ++++++++++++++++--- .../ZonyLrcTools.Common.csproj | 1 + 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 6332605..dff2e4c 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -39,12 +39,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - + \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj b/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj index 5076ab1..63aac7c 100644 --- a/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj +++ b/src/ZonyLrcTools.Cli/ZonyLrcTools.Cli.csproj @@ -14,6 +14,7 @@ + diff --git a/src/ZonyLrcTools.Common/MusicScanner/CsvFileMusicScanner.cs b/src/ZonyLrcTools.Common/MusicScanner/CsvFileMusicScanner.cs index 24994f9..825a4a5 100644 --- a/src/ZonyLrcTools.Common/MusicScanner/CsvFileMusicScanner.cs +++ b/src/ZonyLrcTools.Common/MusicScanner/CsvFileMusicScanner.cs @@ -1,3 +1,5 @@ +using System.Text; +using Ude; using ZonyLrcTools.Common.Infrastructure.DependencyInject; namespace ZonyLrcTools.Common.MusicScanner; @@ -13,11 +15,15 @@ public class CsvFileMusicScanner : ITransientDependency /// CSV 文件的路径。 /// 歌词文件的输出目录。 /// 输出的歌词文件格式,默认是 "{Artist} - {Title}.lrc" 的形式。 - public async Task> GetMusicInfoFromCsvFileAsync(string csvFilePath, string outputDirectory, string pattern) + public async Task> GetMusicInfoFromCsvFileAsync(string csvFilePath, string outputDirectory, + string pattern) { - var csvFileContent = await File.ReadAllTextAsync(csvFilePath); - var csvFileLines = csvFileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); - return csvFileLines.Skip(1).Select(line => GetMusicInfoFromCsvFileLine(line, outputDirectory, pattern)).ToList(); + var encoding = DetectFileEncoding(csvFilePath); + + var csvFileContent = await File.ReadAllTextAsync(csvFilePath, encoding); + var csvFileLines = csvFileContent.Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries); + return csvFileLines.Skip(1).Select(line => GetMusicInfoFromCsvFileLine(line, outputDirectory, pattern)) + .ToList(); } private MusicInfo GetMusicInfoFromCsvFileLine(string csvFileLine, string outputDirectory, string pattern) @@ -29,4 +35,19 @@ public class CsvFileMusicScanner : ITransientDependency var musicInfo = new MusicInfo(fakeFilePath, name, artist); return musicInfo; } + + private Encoding DetectFileEncoding(string filePath) + { + using var fileStream = File.OpenRead(filePath); + var detector = new CharsetDetector(); + detector.Feed(fileStream); + detector.DataEnd(); + + if (detector.Charset != null) + { + return Encoding.GetEncoding(detector.Charset); + } + + return Encoding.Default; + } } \ No newline at end of file diff --git a/src/ZonyLrcTools.Common/ZonyLrcTools.Common.csproj b/src/ZonyLrcTools.Common/ZonyLrcTools.Common.csproj index 5da452c..3a0279e 100644 --- a/src/ZonyLrcTools.Common/ZonyLrcTools.Common.csproj +++ b/src/ZonyLrcTools.Common/ZonyLrcTools.Common.csproj @@ -15,6 +15,7 @@ +