Updated SongPictureInfo use to async copy of picture data stream. Update other classes accordingly.
This commit is contained in:
@@ -2,23 +2,23 @@
|
||||
|
||||
namespace Harmonia.Core.Imaging;
|
||||
|
||||
public class SongPictureInfo : IDisposable
|
||||
public class SongPictureInfo
|
||||
{
|
||||
public required Stream Stream { get; init; }
|
||||
public string? ImageHash { get; init; }
|
||||
public required byte[] Data { get; init; }
|
||||
public string? ImageHash { get; private set; }
|
||||
public required string ImageName { get; init; }
|
||||
public long Size { get; init; }
|
||||
public long Size => Data.Length;
|
||||
|
||||
private SongPictureInfo()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static SongPictureInfo FromStream(Stream stream)
|
||||
public static async Task<SongPictureInfo> FromStreamAsync(Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
string imageHash = ComputeImageHash(stream);
|
||||
|
||||
return GetSongPictureInfo(stream, "Embedded", imageHash);
|
||||
return await GetSongPictureInfoAsync(stream, "Embedded", imageHash, cancellationToken);
|
||||
}
|
||||
|
||||
private static string ComputeImageHash(Stream stream)
|
||||
@@ -29,31 +29,30 @@ public class SongPictureInfo : IDisposable
|
||||
return Convert.ToHexStringLower(hash);
|
||||
}
|
||||
|
||||
public static SongPictureInfo FromFile(string fileName)
|
||||
public static async Task<SongPictureInfo> FromFileAsync(string fileName, CancellationToken cancellationToken)
|
||||
{
|
||||
using FileStream fileStream = File.OpenRead(fileName);
|
||||
|
||||
return GetSongPictureInfo(fileStream, fileName);
|
||||
return await GetSongPictureInfoAsync(fileStream, fileName, cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
private static SongPictureInfo GetSongPictureInfo(Stream stream, string imageName, string? imageHash = null)
|
||||
private static async Task<SongPictureInfo> GetSongPictureInfoAsync(Stream stream, string imageName, string? imageHash = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Data = await GetImageDataAsync(stream, cancellationToken),
|
||||
ImageHash = imageHash,
|
||||
ImageName = imageName
|
||||
};
|
||||
}
|
||||
|
||||
public static async Task<byte[]> GetImageDataAsync(Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
SongPictureInfo songPictureInfo = new()
|
||||
{
|
||||
Stream = stream,
|
||||
ImageHash = imageHash,
|
||||
ImageName = imageName,
|
||||
Size = stream.Length
|
||||
};
|
||||
using MemoryStream memoryStream = new();
|
||||
await stream.CopyToAsync(memoryStream, cancellationToken);
|
||||
|
||||
return songPictureInfo;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Stream.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user