Skip to content

Commit

Permalink
Allow use of IAsyncEnumerable for listing items.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramondeklein committed Jan 19, 2024
1 parent f041388 commit 5f92248
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion NWebDav.Server/Handlers/CopyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private async Task CopyAsync(IStoreItem source, IStoreCollection destinationColl
var newCollection = (IStoreCollection)copyResult.Item!;

// Copy all children of the source collection
foreach (var entry in await sourceCollection.GetItemsAsync(cancellationToken).ConfigureAwait(false))
await foreach (var entry in sourceCollection.GetItemsAsync(cancellationToken).ConfigureAwait(false))
await CopyAsync(entry, newCollection, entry.Name, overwrite, depth - 1, newBaseUri, errors, cancellationToken).ConfigureAwait(false);
}
}
Expand Down
2 changes: 1 addition & 1 deletion NWebDav.Server/Handlers/DeleteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private async Task<DavStatusCode> DeleteItemAsync(IStoreCollection collection, s
var subBaseUri = UriHelper.Combine(baseUri, name);

// Delete all entries first
foreach (var entry in await deleteCollection.GetItemsAsync(cancellationToken).ConfigureAwait(false))
await foreach (var entry in deleteCollection.GetItemsAsync(cancellationToken).ConfigureAwait(false))
await DeleteItemAsync(deleteCollection, entry.Name, subBaseUri, cancellationToken).ConfigureAwait(false);
}

Expand Down
2 changes: 1 addition & 1 deletion NWebDav.Server/Handlers/MoveHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private async Task MoveAsync(IStoreCollection sourceCollection, IStoreItem moveI
}

// Move all sub items
foreach (var entry in await moveCollection.GetItemsAsync(cancellationToken).ConfigureAwait(false))
await foreach (var entry in moveCollection.GetItemsAsync(cancellationToken).ConfigureAwait(false))
await MoveAsync(moveCollection, entry, newCollectionResult.Collection, entry.Name, overwrite, subBaseUri, errors, cancellationToken).ConfigureAwait(false);

// Delete the source collection
Expand Down
2 changes: 1 addition & 1 deletion NWebDav.Server/Handlers/PropFindHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private async Task AddEntriesAsync(IStoreCollection collection, int depth, Uri u
if (depth > 0)
{
// Add all child collections
foreach (var childEntry in await collection.GetItemsAsync(cancellationToken).ConfigureAwait(false))
await foreach (var childEntry in collection.GetItemsAsync(cancellationToken).ConfigureAwait(false))
{
var subUri = UriHelper.Combine(uri, childEntry.Name);
if (childEntry is IStoreCollection subCollection)
Expand Down
25 changes: 11 additions & 14 deletions NWebDav.Server/Stores/DiskStoreCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -43,22 +44,18 @@ public DiskStoreCollection(DiskStoreBase store, DiskStoreCollectionPropertyManag
return Task.FromResult(_store.CreateFromPath(fullPath));
}

public Task<IEnumerable<IStoreItem>> GetItemsAsync(CancellationToken cancellationToken)
// Not async, but this is the easiest way to return an IAsyncEnumerable
public async IAsyncEnumerable<IStoreItem> GetItemsAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
IEnumerable<IStoreItem> GetItemsInternal()
{
// Add all directories
cancellationToken.ThrowIfCancellationRequested();
foreach (var subDirectory in DirectoryInfo.GetDirectories())
yield return _store.CreateCollection(subDirectory);

// Add all files
cancellationToken.ThrowIfCancellationRequested();
foreach (var file in DirectoryInfo.GetFiles())
yield return _store.CreateItem(file);
}
// Add all directories
cancellationToken.ThrowIfCancellationRequested();
foreach (var subDirectory in DirectoryInfo.GetDirectories())
yield return _store.CreateCollection(subDirectory);

return Task.FromResult(GetItemsInternal());
// Add all files
cancellationToken.ThrowIfCancellationRequested();
foreach (var file in DirectoryInfo.GetFiles())
yield return _store.CreateItem(file);
}

public Task<StoreItemResult> CreateItemAsync(string name, bool overwrite, CancellationToken cancellationToken)
Expand Down
2 changes: 1 addition & 1 deletion NWebDav.Server/Stores/IStoreCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IStoreCollection : IStoreItem
// Get specific item (or all items)
Task<IStoreItem?> GetItemAsync(string name, CancellationToken cancellationToken);

Task<IEnumerable<IStoreItem>> GetItemsAsync(CancellationToken cancellationToken);
IAsyncEnumerable<IStoreItem> GetItemsAsync(CancellationToken cancellationToken);

// Create items and collections and add to the collection
Task<StoreItemResult> CreateItemAsync(string name, bool overwrite, CancellationToken cancellationToken);
Expand Down

0 comments on commit 5f92248

Please sign in to comment.