using System; using System.IO; using System.Threading.Tasks; using CommunityToolkit.Diagnostics; using OwlCore.AbstractStorage; using OwlCore.Extensions; using Windows.Storage; using FileAccessMode = OwlCore.AbstractStorage.FileAccessMode; namespace StrixMusic.Sdk.WinUI.Models { /// public class FileData : IFileData { /// /// The underlying instance in use. /// internal StorageFile StorageFile { get; } /// /// Creates a new instance of . /// /// The to wrap. public FileData(StorageFile storageFile) { StorageFile = storageFile; Properties = new FileDataProperties(storageFile); Id = StorageFile.Path.HashMD5Fast(); } /// public string Id { get; } /// public string Path => StorageFile.Path; /// public string Name => StorageFile.Name; /// public string DisplayName => StorageFile.DisplayName; /// public string FileExtension => StorageFile.FileType; /// public IFileDataProperties Properties { get; set; } /// public async Task GetParentAsync() { var storageFile = await StorageFile.GetParentAsync(); Guard.IsNotNull(storageFile, nameof(storageFile)); return new FolderData(storageFile); } /// public Task Delete() { return StorageFile.DeleteAsync().AsTask(); } /// public async Task GetStreamAsync(FileAccessMode accessMode = FileAccessMode.Read) { var stream = await StorageFile.OpenAsync((Windows.Storage.FileAccessMode)accessMode); return stream.AsStream(); } /// public Task WriteAllBytesAsync(byte[] bytes) { return FileIO.WriteBytesAsync(StorageFile, bytes).AsTask(); } /// public async Task GetThumbnailAsync(OwlCore.AbstractStorage.ThumbnailMode thumbnailMode, uint requiredSize) { var thumbnail = await StorageFile.GetThumbnailAsync((Windows.Storage.FileProperties.ThumbnailMode)thumbnailMode, requiredSize); return thumbnail.AsStream(); } } }