Skip to content

Commit

Permalink
Overload extensions (#2)
Browse files Browse the repository at this point in the history
* Add overload method without cancellation token

* Add read entire file content without streaming

* Add read entire file extensions

* Update unit tests

* Update readme file

* Fix the overload extension for files
  • Loading branch information
Jandini committed Oct 10, 2023
1 parent de3983d commit 2241da0
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 24 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -3,5 +3,22 @@
[![Build](https://github.com/Jandini/AnyoneDrive/actions/workflows/build.yml/badge.svg)](https://github.com/Jandini/AnyoneDrive/actions/workflows/build.yml)
[![NuGet](https://github.com/Jandini/AnyoneDrive/actions/workflows/nuget.yml/badge.svg)](https://github.com/Jandini/AnyoneDrive/actions/workflows/nuget.yml)
---

AnyoneDrive provides access to OneDrive's public shares using a familiar approach inspired by FileInfo and DirectoryInfo.
The library offers the simplest form of read only access, requiring no authentication, to OneDrive's publicly available content.


```
var httpClient = new HttpClient();
var root = new OneDriveFolderInfo("https://1drv.ms/f/s!AuveTnis1UC4ylzSgCoulSxnQGB9");
var files = await root.root.GetFilesAsync(httpClient);
```



Please note that the link to the public folder must be accessible through public shares, and the link should include a share ID that begins with 's!'.


Created from [JandaBox](https://github.com/Jandini/JandaBox)
Box icon created by [Freepik - Flaticon](https://www.flaticon.com/free-icons/box)
19 changes: 10 additions & 9 deletions src/AnyoneDrive.Tests/OneDriveFile_Must.cs
Expand Up @@ -3,36 +3,37 @@ namespace AnyoneDrive.Tests
public class OneDriveFile_Must
{

const string TEST_SHARE = "https://1drv.ms/f/s!AuveTnis1UC4ylzSgCoulSxnQGB9";



[Fact]
public async void Get_Stream()
{
var httpClient = new HttpClient();
var root = new OneDriveFolderInfo("https://1drv.ms/f/s!AuveTnis1UC4ylwrkuYqhdobhUGy?e=aq9M7h");
var folders = await root.GetFoldersAsync(httpClient, CancellationToken.None);
var root = new OneDriveFolderInfo(TEST_SHARE);
var folders = await root.GetFoldersAsync(httpClient);

Assert.NotNull(folders);

var files = await folders.First().GetFilesAsync(httpClient, CancellationToken.None);
var files = await folders.First().GetFilesAsync(httpClient);
Assert.Equal(5, files.Length);

using var stream = await files[0].GetStreamAsync(httpClient, CancellationToken.None);
using var stream = await files[0].GetStreamAsync(httpClient);
}


[Fact]
public async void Read_Stream()
{
var httpClient = new HttpClient();
var root = new OneDriveFolderInfo("https://1drv.ms/f/s!AuveTnis1UC4ylwrkuYqhdobhUGy?e=aq9M7h");
var folders = await root.GetFoldersAsync(httpClient, CancellationToken.None);
var root = new OneDriveFolderInfo(TEST_SHARE);
var folders = await root.GetFoldersAsync(httpClient);

Assert.NotNull(folders);

var files = await folders.First().GetFilesAsync(httpClient, CancellationToken.None);
var files = await folders.First().GetFilesAsync(httpClient);

using var stream = await files[0].GetStreamAsync(httpClient, CancellationToken.None);
using var stream = await files[0].GetStreamAsync(httpClient);

byte[] buffer = await stream.ReadBlockAsync(65536);
Assert.Equal(5959, buffer.Length);
Expand Down
18 changes: 9 additions & 9 deletions src/AnyoneDrive.Tests/OneDriveFolder_Must.cs
Expand Up @@ -3,14 +3,14 @@ namespace AnyoneDrive.Tests
public class OneDriveFolder_Must
{

const string TEST_SHARE = "https://1drv.ms/f/s!AuveTnis1UC4ylwrkuYqhdobhUGy?e=aq9M7h";
const string TEST_SHARE = "https://1drv.ms/f/s!AuveTnis1UC4ylzSgCoulSxnQGB9";

[Fact]
public async void Get_Two_Items()
{

var root = new OneDriveFolderInfo(TEST_SHARE);
var items = await root.GetItemsAsync(new HttpClient(), CancellationToken.None);
var items = await root.GetItemsAsync(new HttpClient());

Assert.Equal(2, items.Length);
}
Expand All @@ -20,7 +20,7 @@ public async void Get_One_File()
{

var root = new OneDriveFolderInfo(TEST_SHARE);
var items = await root.GetFilesAsync(new HttpClient(), CancellationToken.None);
var items = await root.GetFilesAsync(new HttpClient());

Assert.Single(items);
}
Expand All @@ -31,7 +31,7 @@ public async void Get_One_Folder()
{

var root = new OneDriveFolderInfo(TEST_SHARE);
var items = await root.GetFoldersAsync(new HttpClient(), CancellationToken.None);
var items = await root.GetFoldersAsync(new HttpClient());

Assert.Single(items);
}
Expand All @@ -42,13 +42,13 @@ public async void Get_SubFolder_Files()
{
var httpClient = new HttpClient();
var root = new OneDriveFolderInfo(TEST_SHARE);
var folders = await root.GetFoldersAsync(httpClient, CancellationToken.None);
var folders = await root.GetFoldersAsync(httpClient);

Assert.NotNull(folders);
Assert.Single(folders);
Assert.Equal("Single folder", folders[0].Name);

var files = await folders.First().GetFilesAsync(httpClient, CancellationToken.None);
var files = await folders.First().GetFilesAsync(httpClient);

Assert.Equal(5, files.Length);
}
Expand All @@ -60,15 +60,15 @@ public async void Read_File()
{
var httpClient = new HttpClient();
var root = new OneDriveFolderInfo(TEST_SHARE);
var folders = await root.GetFoldersAsync(httpClient, CancellationToken.None);
var folders = await root.GetFoldersAsync(httpClient);

Assert.NotNull(folders);

var files = await folders.First().GetFilesAsync(httpClient, CancellationToken.None);
var files = await folders.First().GetFilesAsync(httpClient);

Assert.Equal(5, files.Length);

using var stream = await files[0].GetStreamAsync(httpClient, CancellationToken.None);
using var stream = await files[0].GetStreamAsync(httpClient);
}
}
}
87 changes: 84 additions & 3 deletions src/AnyoneDrive/OneDriveExtensions.cs
Expand Up @@ -39,6 +39,13 @@ private static async Task<OneDriveCollection<OneDriveItem>> GetSharesAsync(OneDr
/// <returns>An array of OneDriveItemInfo representing the items within the folder.</returns>
public static async Task<OneDriveItemInfo[]> GetItemsAsync(this OneDriveFolderInfo folderInfo, HttpClient httpClient, CancellationToken cancellationToken) => (await GetSharesAsync(folderInfo, httpClient, cancellationToken)).Collection.Select(item => item.Folder != null ? new OneDriveFolderInfo(item) : (OneDriveItemInfo)new OneDriveFileInfo(item)).ToArray();

/// <summary>
/// Retrieves a collection of OneDrive items within the specified folder.
/// </summary>
/// <param name="folderInfo">Information about the OneDrive folder to retrieve items from.</param>
/// <param name="httpClient">The HttpClient instance used to make the API request.</param>
/// <returns>An array of OneDriveItemInfo representing the items within the folder.</returns>
public static async Task<OneDriveItemInfo[]> GetItemsAsync(this OneDriveFolderInfo folderInfo, HttpClient httpClient) => await folderInfo.GetItemsAsync(httpClient, CancellationToken.None);


/// <summary>
Expand All @@ -50,6 +57,13 @@ private static async Task<OneDriveCollection<OneDriveItem>> GetSharesAsync(OneDr
/// <returns>An array of OneDriveFileInfo representing the files within the folder.</returns>
public static async Task<OneDriveFileInfo[]> GetFilesAsync(this OneDriveFolderInfo folderInfo, HttpClient httpClient, CancellationToken cancellationToken) => (await GetSharesAsync(folderInfo, httpClient, cancellationToken)).Collection.Where(item => item.File != null).Select(item => new OneDriveFileInfo(item)).ToArray();

/// <summary>
/// Retrieves a collection of OneDrive files within the specified folder.
/// </summary>
/// <param name="folderInfo">Information about the OneDrive folder to retrieve files from.</param>
/// <param name="httpClient">The HttpClient instance used to make the API request.</param>
/// <returns>An array of OneDriveFileInfo representing the files within the folder.</returns>
public static async Task<OneDriveFileInfo[]> GetFilesAsync(this OneDriveFolderInfo folderInfo, HttpClient httpClient) => await folderInfo.GetFilesAsync(httpClient, CancellationToken.None);


/// <summary>
Expand All @@ -61,17 +75,25 @@ private static async Task<OneDriveCollection<OneDriveItem>> GetSharesAsync(OneDr
/// <returns>An array of OneDriveFolderInfo representing the folders within the folder.</returns>
public static async Task<OneDriveFolderInfo[]> GetFoldersAsync(this OneDriveFolderInfo folderInfo, HttpClient httpClient, CancellationToken cancellationToken) => (await GetSharesAsync(folderInfo, httpClient, cancellationToken)).Collection.Where(item => item.Folder != null).Select(item => new OneDriveFolderInfo(item)).ToArray();

/// <summary>
/// Retrieves a collection of OneDrive folders within the specified folder.
/// </summary>
/// <param name="folderInfo">Information about the OneDrive folder to retrieve folders from.</param>
/// <param name="httpClient">The HttpClient instance used to make the API request.</param>
/// <param name="cancellationToken">A CancellationToken for canceling the asynchronous operation.</param>
/// <returns>An array of OneDriveFolderInfo representing the folders within the folder.</returns>

public static async Task<OneDriveFolderInfo[]> GetFoldersAsync(this OneDriveFolderInfo folderInfo, HttpClient httpClient) => await folderInfo.GetFoldersAsync(httpClient, CancellationToken.None);


/// <summary>
/// Retrieves a stream containing the content of the specified OneDrive file.
/// Retrieves a stream containing the content of the specified OneDrive file. The stream is returned immediatelly.
/// </summary>
/// <param name="fileInfo">Information about the OneDrive file to retrieve the content of.</param>
/// <param name="httpClient">The HttpClient instance used to make the API request.</param>
/// <param name="cancellationToken">A CancellationToken for canceling the asynchronous operation.</param>
/// <returns>
/// A stream containing the content of the OneDrive file.
/// The stream should be read in chunks to efficiently process the file content.
/// A stream containing the content of the OneDrive file. The stream does not support length or position.
/// </returns>
public static async Task<Stream> GetStreamAsync(this OneDriveFileInfo fileInfo, HttpClient httpClient, CancellationToken cancellationToken)
{
Expand All @@ -81,6 +103,65 @@ public static async Task<Stream> GetStreamAsync(this OneDriveFileInfo fileInfo,
return await response.Content.ReadAsStreamAsync();
}

/// <summary>
/// Retrieves a stream containing the content of the specified OneDrive file. The stream is returned immediatelly.
/// </summary>
/// <param name="fileInfo">Information about the OneDrive file to retrieve the content of.</param>
/// <param name="httpClient">The HttpClient instance used to make the API request.</param>
/// <returns>
/// A stream containing the content of the OneDrive file. The stream does not support length or position.
/// </returns>
public static async Task<Stream> GetStreamAsync(this OneDriveFileInfo fileInfo, HttpClient httpClient) => await fileInfo.GetStreamAsync(httpClient, CancellationToken.None);


/// <summary>
/// Reads the content of a file as a stream asynchronously.
/// </summary>
/// <param name="fileInfo">The OneDriveFileInfo representing the file to be read.</param>
/// <param name="httpClient">The HttpClient used for making the HTTP request.</param>
/// <param name="cancellationToken">The cancellation token for canceling the operation.</param>
/// <returns>A stream containing the file's content.</returns>
public static async Task<Stream> ReadAsStreamAsync(this OneDriveFileInfo fileInfo, HttpClient httpClient, CancellationToken cancellationToken)
{
var response = await httpClient.GetAsync(fileInfo.Url, cancellationToken);
response.EnsureSuccessStatusCode();

return await response.Content.ReadAsStreamAsync();
}

/// <summary>
/// Reads the content of a file as a stream asynchronously.
/// </summary>
/// <param name="fileInfo">The OneDriveFileInfo representing the file to be read.</param>
/// <param name="httpClient">The HttpClient used for making the HTTP request.</param>
/// <returns>A stream containing the file's content.</returns>
public static async Task<Stream> ReadAsStreamAsync(this OneDriveFileInfo fileInfo, HttpClient httpClient) => await fileInfo.ReadAsStreamAsync(httpClient);


/// <summary>
/// Reads the content of a file as a byte array asynchronously.
/// </summary>
/// <param name="fileInfo">The OneDriveFileInfo representing the file to be read.</param>
/// <param name="httpClient">The HttpClient used for making the HTTP request.</param>
/// <param name="cancellationToken">The cancellation token for canceling the operation.</param>
/// <returns>An array of bytes containing the file's content.</returns>
public static async Task<byte[]> ReadAsByteArrayAsync(this OneDriveFileInfo fileInfo, HttpClient httpClient, CancellationToken cancellationToken)
{
var response = await httpClient.GetAsync(fileInfo.Url, cancellationToken);
response.EnsureSuccessStatusCode();

return await response.Content.ReadAsByteArrayAsync();
}


/// <summary>
/// Reads the content of a file as a byte array asynchronously.
/// </summary>
/// <param name="fileInfo">The OneDriveFileInfo representing the file to be read.</param>
/// <param name="httpClient">The HttpClient used for making the HTTP request.</param>
/// <returns>An array of bytes containing the file's content.</returns>
public static async Task<byte[]> ReadAsByteArrayAsync(this OneDriveFileInfo fileInfo, HttpClient httpClient) => await fileInfo.ReadAsByteArrayAsync(httpClient);



/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/AnyoneDrive/OneDriveFolderInfo.cs
Expand Up @@ -2,7 +2,6 @@
{
public class OneDriveFolderInfo : OneDriveItemInfo
{

internal OneDriveFolderInfo(OneDriveItem item)
: base(item)
{
Expand Down
3 changes: 1 addition & 2 deletions src/AnyoneDrive/OneDriveItemInfo.cs
Expand Up @@ -28,6 +28,7 @@ public class OneDriveItemInfo

internal OneDriveItemInfo()
{

}

/// <summary>
Expand All @@ -40,7 +41,5 @@ internal OneDriveItemInfo(OneDriveItem item)
CreatedDateTime = item.CreatedDateTime;
LastUpdatedDateTime = item.LastModifiedDateTime;
}


}
}

0 comments on commit 2241da0

Please sign in to comment.