59 lines
1.4 KiB
C#
59 lines
1.4 KiB
C#
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);
|
|
}
|
|
} |