Files
Wino-Mail/Wino.Core.WinUI/Extensions/StorageFileExtensions.cs
T

31 lines
866 B
C#
Raw Normal View History

2025-02-14 01:43:52 +01:00
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
using Wino.Core.Domain.Models.Common;
2025-09-29 11:23:44 +02:00
namespace Wino.Core.WinUI.Extensions;
2025-02-16 11:54:23 +01:00
public static class StorageFileExtensions
{
2025-02-16 11:54:23 +01:00
public static async Task<SharedFile> ToSharedFileAsync(this Windows.Storage.StorageFile storageFile)
{
2025-02-16 11:54:23 +01:00
var content = await storageFile.ToByteArrayAsync();
2025-02-14 01:43:52 +01:00
2025-02-16 11:54:23 +01:00
return new SharedFile(storageFile.Path, content);
}
2025-02-14 01:43:52 +01:00
2025-02-16 11:54:23 +01:00
public static async Task<byte[]> ToByteArrayAsync(this StorageFile file)
{
if (file == null)
throw new ArgumentNullException(nameof(file));
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
using (var stream = await file.OpenReadAsync())
using (var memoryStream = new MemoryStream())
{
await stream.AsStreamForRead().CopyToAsync(memoryStream);
return memoryStream.ToArray();
2025-02-14 01:43:52 +01:00
}
}
}