mirror of
https://github.com/real-zony/ZonyLrcToolsX.git
synced 2025-09-06 05:36:53 +00:00
refactor: Common components are moved to the Common library.
This commit is contained in:
65
src/ZonyLrcTools.Common/Infrastructure/IO/FileScanner.cs
Normal file
65
src/ZonyLrcTools.Common/Infrastructure/IO/FileScanner.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using ZonyLrcTools.Common.Infrastructure.DependencyInject;
|
||||
using ZonyLrcTools.Common.Infrastructure.Exceptions;
|
||||
using ZonyLrcTools.Common.Infrastructure.Extensions;
|
||||
|
||||
namespace ZonyLrcTools.Common.Infrastructure.IO
|
||||
{
|
||||
public class FileScanner : IFileScanner, ITransientDependency
|
||||
{
|
||||
public ILogger<FileScanner> Logger { get; set; }
|
||||
|
||||
public FileScanner()
|
||||
{
|
||||
Logger = NullLogger<FileScanner>.Instance;
|
||||
}
|
||||
|
||||
public Task<List<FileScannerResult>> ScanAsync(string path, IEnumerable<string> extensions)
|
||||
{
|
||||
if (extensions == null || !extensions.Any())
|
||||
{
|
||||
throw new ErrorCodeException(ErrorCodes.FileSuffixIsEmpty);
|
||||
}
|
||||
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
throw new ErrorCodeException(ErrorCodes.DirectoryNotExist);
|
||||
}
|
||||
|
||||
var files = new List<FileScannerResult>();
|
||||
foreach (var extension in extensions)
|
||||
{
|
||||
var tempResult = new ConcurrentBag<string>();
|
||||
SearchFile(tempResult, path, extension);
|
||||
|
||||
files.Add(new FileScannerResult(
|
||||
Path.GetExtension(extension) ?? throw new ErrorCodeException(ErrorCodes.UnableToGetTheFileExtension),
|
||||
tempResult.ToList()));
|
||||
}
|
||||
|
||||
return Task.FromResult(files);
|
||||
}
|
||||
|
||||
private void SearchFile(ConcurrentBag<string> files, string folder, string extension)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var file in Directory.GetFiles(folder, extension))
|
||||
{
|
||||
files.Add(file);
|
||||
}
|
||||
|
||||
foreach (var directory in Directory.GetDirectories(folder))
|
||||
{
|
||||
SearchFile(files, directory, extension);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogWarningWithErrorCode(ErrorCodes.ScanFileError, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
namespace ZonyLrcTools.Common.Infrastructure.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件扫描结果对象。
|
||||
/// </summary>
|
||||
public class FileScannerResult
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前路径对应的扩展名。
|
||||
/// </summary>
|
||||
public string ExtensionName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前扩展名下面的所有文件路径集合。
|
||||
/// </summary>
|
||||
public List<string> FilePaths { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造一个新的 <see cref="FileScannerResult"/> 对象。
|
||||
/// </summary>
|
||||
/// <param name="extensionName">当前路径对应的扩展名。</param>
|
||||
/// <param name="filePaths">当前扩展名下面的所有文件路径集合。</param>
|
||||
public FileScannerResult(string extensionName, List<string> filePaths)
|
||||
{
|
||||
ExtensionName = extensionName;
|
||||
FilePaths = filePaths;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
namespace ZonyLrcTools.Common.Infrastructure.IO
|
||||
{
|
||||
public static class FileStreamExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 将字节数据通过缓冲区的形式,写入到文件当中。
|
||||
/// </summary>
|
||||
/// <param name="fileStream">需要写入数据的文件流。</param>
|
||||
/// <param name="data">等待写入的数据。</param>
|
||||
/// <param name="bufferSize">缓冲区大小。</param>
|
||||
public static async Task WriteBytesToFileAsync(this FileStream fileStream, byte[] data, int bufferSize = 1024)
|
||||
{
|
||||
await using (fileStream)
|
||||
{
|
||||
var count = data.Length / 1024;
|
||||
var modCount = data.Length % 1024;
|
||||
if (count <= 0)
|
||||
{
|
||||
await fileStream.WriteAsync(data, 0, modCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
await fileStream.WriteAsync(data, i * 1024, 1024);
|
||||
}
|
||||
|
||||
if (modCount != 0)
|
||||
{
|
||||
await fileStream.WriteAsync(data, count * 1024, modCount);
|
||||
}
|
||||
}
|
||||
|
||||
await fileStream.FlushAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
src/ZonyLrcTools.Common/Infrastructure/IO/IFileScanner.cs
Normal file
15
src/ZonyLrcTools.Common/Infrastructure/IO/IFileScanner.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace ZonyLrcTools.Common.Infrastructure.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// 音乐文件扫描器,用于扫描音乐文件。
|
||||
/// </summary>
|
||||
public interface IFileScanner
|
||||
{
|
||||
/// <summary>
|
||||
/// 扫描指定路径下面的歌曲文件。
|
||||
/// </summary>
|
||||
/// <param name="path">等待扫描的路径。</param>
|
||||
/// <param name="extensions">需要搜索的歌曲后缀名。</param>
|
||||
Task<List<FileScannerResult>> ScanAsync(string path, IEnumerable<string> extensions);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user