using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Graph;
using OwlCore.AbstractStorage;
namespace StrixMusic.Cores.OneDrive.Storage
{
///
/// Implementation of for OneDrive.
///
public class OneDriveFolderData : IFolderData
{
private const string EXPAND_STRING = "children";
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 OneDriveFolderData(GraphServiceClient graphClient, DriveItem driveItem)
{
_graphClient = graphClient;
_driveItem = driveItem;
}
///
/// A unique identifier returned from OneDrive api, a folder can be uniquely identified by this id. Helpful during record reads.
///
public string? Id => _driveItem.Id;
///
public string Name => _driveItem.Name;
///
public string Path => _driveItem.WebUrl;
///
public Task CreateFileAsync(string desiredName)
{
throw new NotSupportedException();
}
///
public Task CreateFileAsync(string desiredName, CreationCollisionOption options)
{
throw new NotSupportedException();
}
///
public Task CreateFolderAsync(string desiredName)
{
throw new NotSupportedException();
}
///
public Task CreateFolderAsync(string desiredName, CreationCollisionOption options)
{
throw new NotSupportedException();
}
///
public Task DeleteAsync()
{
throw new NotSupportedException();
}
///
public Task EnsureExists()
{
throw new NotSupportedException();
}
///
public async Task GetFileAsync(string name)
{
var driveItem = await _graphClient.Drive.Items[Id].Request().Expand(EXPAND_STRING).GetAsync();
foreach (var item in driveItem.Children)
{
if (item.File is null)
continue;
if (item.Name == name)
return new OneDriveFileData(_graphClient, item);
}
return null;
}
///
public async Task> GetFilesAsync()
{
var driveItem = await _graphClient.Drive.Items[Id].Request().Expand(EXPAND_STRING).GetAsync();
var results = new List();
foreach (var item in driveItem.Children)
{
if (item.File is null)
continue;
results.Add(new OneDriveFileData(_graphClient, item));
}
return results;
}
///
public async Task GetFolderAsync(string name)
{
var driveItem = await _graphClient.Drive.Items[Id].Request().Expand(EXPAND_STRING).GetAsync();
foreach (var item in driveItem.Children)
{
if (item.Folder is null)
continue;
if (item.Name == name)
return new OneDriveFolderData(_graphClient, item);
}
return null;
}
///
public async Task> GetFoldersAsync()
{
var driveItem = await _graphClient.Drive.Items[Id].Request().Expand(EXPAND_STRING).GetAsync();
var results = new List();
foreach (var item in driveItem.Children)
{
if (item.Folder is null)
continue;
results.Add(new OneDriveFolderData(_graphClient, item));
}
return results;
}
///
public async Task GetParentAsync()
{
if (_driveItem.ParentReference is null)
return null;
var parent = await _graphClient.Drive.Items[_driveItem.ParentReference.DriveId].Request().GetAsync();
return new OneDriveFolderData(_graphClient, parent);
}
}
}