Added AudioFileScanner and AudioImageExtractor logic.
This commit is contained in:
27
Harmonia.Core/Imaging/AudioImageExtractor.cs
Normal file
27
Harmonia.Core/Imaging/AudioImageExtractor.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Harmonia.Core.Tags;
|
||||
|
||||
namespace Harmonia.Core.Imaging;
|
||||
|
||||
public class AudioImageExtractor(ITagResolver tagResolver) : IAudioImageExtractor
|
||||
{
|
||||
public SongPictureInfo ExtractImage(string fileName)
|
||||
{
|
||||
SongTagInfo songTagInfo = tagResolver.GetSongTagInfo(fileName);
|
||||
|
||||
return ExtractImage(fileName, songTagInfo);
|
||||
}
|
||||
|
||||
public SongPictureInfo ExtractImage(string fileName, SongTagInfo songTagInfo)
|
||||
{
|
||||
if (songTagInfo.FrontCover != null)
|
||||
{
|
||||
using MemoryStream memoryStream = new(songTagInfo.FrontCover.Data);
|
||||
|
||||
return SongPictureInfo.FromStream(memoryStream);
|
||||
}
|
||||
else
|
||||
{
|
||||
return SongPictureInfo.FromFile(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Harmonia.Core/Imaging/IAudioImageExtractor.cs
Normal file
9
Harmonia.Core/Imaging/IAudioImageExtractor.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Harmonia.Core.Tags;
|
||||
|
||||
namespace Harmonia.Core.Imaging;
|
||||
|
||||
public interface IAudioImageExtractor
|
||||
{
|
||||
SongPictureInfo ExtractImage(string fileName);
|
||||
SongPictureInfo ExtractImage(string fileName, SongTagInfo songTagInfo);
|
||||
}
|
||||
59
Harmonia.Core/Imaging/SongPictureInfo.cs
Normal file
59
Harmonia.Core/Imaging/SongPictureInfo.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Harmonia.Core.Imaging;
|
||||
|
||||
public class SongPictureInfo : IDisposable
|
||||
{
|
||||
public required Stream Stream { get; init; }
|
||||
public string? ImageHash { get; init; }
|
||||
public required string ImageName { get; init; }
|
||||
public ulong Size { get; init; }
|
||||
|
||||
private SongPictureInfo()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static SongPictureInfo FromStream(Stream stream)
|
||||
{
|
||||
string imageHash = ComputeImageHash(stream);
|
||||
|
||||
return GetSongPictureInfo(stream, "Embedded", imageHash);
|
||||
}
|
||||
|
||||
private static string ComputeImageHash(Stream stream)
|
||||
{
|
||||
using var md5 = MD5.Create();
|
||||
byte[] hash = md5.ComputeHash(stream);
|
||||
|
||||
return Convert.ToHexStringLower(hash);
|
||||
}
|
||||
|
||||
public static SongPictureInfo FromFile(string fileName)
|
||||
{
|
||||
using FileStream fileStream = File.OpenRead(fileName);
|
||||
|
||||
return GetSongPictureInfo(fileStream, fileName);
|
||||
}
|
||||
|
||||
private static SongPictureInfo GetSongPictureInfo(Stream stream, string imageName, string? imageHash = null)
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
SongPictureInfo songPictureInfo = new()
|
||||
{
|
||||
Stream = stream,
|
||||
ImageHash = imageHash,
|
||||
ImageName = imageName,
|
||||
Size = (ulong)stream.Length
|
||||
};
|
||||
|
||||
return songPictureInfo;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Stream.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user