using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Graph;
using OwlCore.AbstractStorage;
using OwlCore.Validation.Mime;
namespace StrixMusic.Cores.OneDrive.Storage
{
///
/// Wraps around an instance of to implement a file for AbstractStorage.
///
public class OneDriveFileData : IFileData
{
private readonly GraphServiceClient _graphClient;
private readonly DriveItem _driveItem;
///
/// Creates a new instance of .
///
/// The service that handles API requests to Microsoft Graph.
/// An instance of that represents the underlying OneDrive folder.
public OneDriveFileData(GraphServiceClient graphClient, DriveItem driveItem)
{
_graphClient = graphClient;
_driveItem = driveItem;
Properties = new OneDriveFileDataProperties(_driveItem);
}
///
public string? Id => _driveItem.Id;
///
public string Path => _driveItem.AdditionalData["@microsoft.graph.downloadUrl"].ToString().Replace("ValueKind = String : ", string.Empty).Replace("\"", string.Empty);
///
public string Name => _driveItem.Name;
///
public string DisplayName => _driveItem.Name;
///
public string FileExtension => MimeTypeMap.GetExtension(_driveItem.File.MimeType);
///
public IFileDataProperties Properties { get; set; }
///
public async Task GetParentAsync()
{
var parent = await _graphClient.Drive.Items[_driveItem.ParentReference.DriveId].Request().GetAsync();
return new OneDriveFolderData(_graphClient, parent);
}
///
public Task GetStreamAsync(FileAccessMode accessMode = FileAccessMode.Read)
{
return _graphClient.Drive.Items[_driveItem.Id].Content.Request().GetAsync();
}
///
public Task Delete()
{
throw new NotSupportedException();
}
///
public Task WriteAllBytesAsync(byte[] bytes)
{
throw new NotSupportedException();
}
///
public Task GetThumbnailAsync(ThumbnailMode thumbnailMode, uint requiredSize)
{
switch (thumbnailMode)
{
case ThumbnailMode.MusicView:
var thumbnails = _driveItem.Thumbnails;
if (thumbnails == null || thumbnails.Count == 0)
return Task.FromResult(new MemoryStream());
return Task.FromResult(thumbnails[0].Source.Content);
default:
throw new ArgumentOutOfRangeException();
}
}
}
}