mirror of
https://github.com/real-zony/ZonyLrcToolsX.git
synced 2025-07-02 05:10:42 +00:00
feat: Support configure lyrics file encoding.
This commit is contained in:
parent
f3b1dacb0c
commit
279eba48f8
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using McMaster.Extensions.CommandLineUtils;
|
using McMaster.Extensions.CommandLineUtils;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@ -185,9 +186,10 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
await using var stream = new FileStream(lyricFilePath, FileMode.Create);
|
await using var stream = new FileStream(lyricFilePath, FileMode.Create);
|
||||||
await using var sw = new StreamWriter(stream);
|
await using var sw = new BinaryWriter(stream);
|
||||||
await sw.WriteAsync(lyric.ToString());
|
|
||||||
await sw.FlushAsync();
|
sw.Write(EncodingConvert(lyric));
|
||||||
|
await stream.FlushAsync();
|
||||||
}
|
}
|
||||||
catch (ErrorCodeException ex)
|
catch (ErrorCodeException ex)
|
||||||
{
|
{
|
||||||
@ -221,6 +223,17 @@ namespace ZonyLrcTools.Cli.Commands.SubCommand
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private byte[] EncodingConvert(LyricItemCollection lyric)
|
||||||
|
{
|
||||||
|
var supportEncodings = Encoding.GetEncodings();
|
||||||
|
if (supportEncodings.All(x => x.Name != _options.Provider.Lyric.Config.FileEncoding))
|
||||||
|
{
|
||||||
|
throw new ErrorCodeException(ErrorCodes.NotSupportedFileEncoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding(_options.Provider.Lyric.Config.FileEncoding), lyric.GetUtf8Bytes());
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region > Ablum image download logic <
|
#region > Ablum image download logic <
|
||||||
|
@ -31,4 +31,9 @@ public class LyricConfigOption
|
|||||||
/// 如果歌词文件已经存在,是否跳过这些文件
|
/// 如果歌词文件已经存在,是否跳过这些文件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsSkipExistLyricFiles { get; set; } = false;
|
public bool IsSkipExistLyricFiles { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 歌词文件的编码格式。
|
||||||
|
/// </summary>
|
||||||
|
public string FileEncoding { get; set; } = "utf-8";
|
||||||
}
|
}
|
@ -27,6 +27,11 @@ namespace ZonyLrcTools.Cli.Infrastructure.Exceptions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public const int NoFilesWereScanned = 10004;
|
public const int NoFilesWereScanned = 10004;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 文本: 指定的编码不受支持,请检查配置,所有受支持的编码名称。
|
||||||
|
/// </summary>
|
||||||
|
public const int NotSupportedFileEncoding = 10005;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region > 警告信息 <
|
#region > 警告信息 <
|
||||||
|
@ -107,5 +107,10 @@ namespace ZonyLrcTools.Cli.Infrastructure.Lyric
|
|||||||
ForEach(lyric => lyricBuilder.Append(lyric).Append(Option.LineBreak));
|
ForEach(lyric => lyricBuilder.Append(lyric).Append(Option.LineBreak));
|
||||||
return lyricBuilder.ToString().TrimEnd(Option.LineBreak);
|
return lyricBuilder.ToString().TrimEnd(Option.LineBreak);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] GetUtf8Bytes()
|
||||||
|
{
|
||||||
|
return Encoding.UTF8.GetBytes(ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using McMaster.Extensions.CommandLineUtils;
|
using McMaster.Extensions.CommandLineUtils;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@ -23,6 +24,8 @@ namespace ZonyLrcTools.Cli
|
|||||||
{
|
{
|
||||||
public static async Task<int> Main(string[] args)
|
public static async Task<int> Main(string[] args)
|
||||||
{
|
{
|
||||||
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||||
|
|
||||||
ConfigureLogger();
|
ConfigureLogger();
|
||||||
ConfigureErrorMessage();
|
ConfigureErrorMessage();
|
||||||
|
|
||||||
@ -94,7 +97,8 @@ namespace ZonyLrcTools.Cli
|
|||||||
switch (ex)
|
switch (ex)
|
||||||
{
|
{
|
||||||
case ErrorCodeException exception:
|
case ErrorCodeException exception:
|
||||||
Log.Logger.Error($"出现了未处理的异常。\n错误代码: {exception.ErrorCode}\n错误信息: {ErrorCodeHelper.GetMessage(exception.ErrorCode)}\n原始信息:{exception.Message}\n调用栈:{exception.StackTrace}");
|
Log.Logger.Error(
|
||||||
|
$"出现了未处理的异常。\n错误代码: {exception.ErrorCode}\n错误信息: {ErrorCodeHelper.GetMessage(exception.ErrorCode)}\n原始信息:{exception.Message}\n调用栈:{exception.StackTrace}");
|
||||||
Environment.Exit(exception.ErrorCode);
|
Environment.Exit(exception.ErrorCode);
|
||||||
return exception.ErrorCode;
|
return exception.ErrorCode;
|
||||||
case { } unknownException:
|
case { } unknownException:
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
"10001": "待搜索的后缀不能为空。",
|
"10001": "待搜索的后缀不能为空。",
|
||||||
"10002": "需要扫描的目录不存在,请确认路径是否正确。",
|
"10002": "需要扫描的目录不存在,请确认路径是否正确。",
|
||||||
"10003": "不能获取文件的后缀信息。",
|
"10003": "不能获取文件的后缀信息。",
|
||||||
"10004": "没有扫描到任何音乐文件。"
|
"10004": "没有扫描到任何音乐文件。",
|
||||||
|
"10005": "指定的编码不受支持,请检查配置,所有受支持的编码名称,请参考: https://docs.microsoft.com/en-us/dotnet/api/system.text.encodinginfo.codepage?view=net-6.0#system-text-encodinginfo-codepage。"
|
||||||
},
|
},
|
||||||
"Warning": {
|
"Warning": {
|
||||||
"50001": "扫描文件时出现了错误。",
|
"50001": "扫描文件时出现了错误。",
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
|
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||||
|
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
|
||||||
<PackageReference Include="TagLibSharp" Version="2.2.0" />
|
<PackageReference Include="TagLibSharp" Version="2.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -45,3 +45,4 @@ globalOption:
|
|||||||
lineBreak: "\n" # 换行符的类型,记得使用双引号指定。
|
lineBreak: "\n" # 换行符的类型,记得使用双引号指定。
|
||||||
isEnableTranslation: true # 是否启用翻译歌词。
|
isEnableTranslation: true # 是否启用翻译歌词。
|
||||||
isSkipExistLyricFiles: false # 如果歌词文件已经存在,是否跳过这些文件。
|
isSkipExistLyricFiles: false # 如果歌词文件已经存在,是否跳过这些文件。
|
||||||
|
fileEncoding: 'utf-8' # 歌词文件的编码格式。
|
Loading…
x
Reference in New Issue
Block a user