Skip to content

Commit

Permalink
GetFileListAsync get everything in one go instead of trying to page.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Dec 2, 2021
1 parent e13e92d commit 010b0ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
26 changes: 12 additions & 14 deletions src/Foundatio/Storage/IFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,18 @@ public static async Task<byte[]> GetFileContentsRawAsync(this IFileStorage stora
if (String.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));

using (var stream = await storage.GetFileStreamAsync(path).AnyContext()) {
if (stream == null)
return null;

var buffer = new byte[16 * 1024];
using (var ms = new MemoryStream()) {
int read;
while ((read = await stream.ReadAsync(buffer, 0, buffer.Length).AnyContext()) > 0) {
await ms.WriteAsync(buffer, 0, read).AnyContext();
}

return ms.ToArray();
}
using var stream = await storage.GetFileStreamAsync(path).AnyContext();
if (stream == null)
return null;

var buffer = new byte[16 * 1024];
using var ms = new MemoryStream();
int read;
while ((read = await stream.ReadAsync(buffer, 0, buffer.Length).AnyContext()) > 0) {
await ms.WriteAsync(buffer, 0, read).AnyContext();
}

return ms.ToArray();
}

public static Task<bool> SaveFileAsync(this IFileStorage storage, string path, string contents) {
Expand All @@ -161,7 +159,7 @@ public static Task<bool> SaveFileAsync(this IFileStorage storage, string path, s
public static async Task<IReadOnlyCollection<FileSpec>> GetFileListAsync(this IFileStorage storage, string searchPattern = null, int? limit = null, CancellationToken cancellationToken = default) {
var files = new List<FileSpec>();
limit ??= Int32.MaxValue;
var result = await storage.GetPagedFileListAsync(Math.Min(limit.Value, 100), searchPattern, cancellationToken).AnyContext();
var result = await storage.GetPagedFileListAsync(limit.Value, searchPattern, cancellationToken).AnyContext();
do {
files.AddRange(result.Files);
} while (result.HasMore && files.Count < limit.Value && await result.NextPageAsync().AnyContext());
Expand Down
11 changes: 9 additions & 2 deletions src/Foundatio/Storage/ScopedFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,21 @@ public async Task<PagedFileListResult> GetPagedFileListAsync(int pageSize = 100,
foreach (var file in unscopedResult.Files)
file.Path = file.Path.Substring(_pathPrefix.Length);

return new PagedFileListResult(unscopedResult.Files, unscopedResult.HasMore, unscopedResult.HasMore ? s => NextPage(unscopedResult) : (Func<PagedFileListResult, Task<NextPageResult>>)null);
return new PagedFileListResult(unscopedResult.Files, unscopedResult.HasMore, unscopedResult.HasMore ? s => NextPage(unscopedResult) : null);
}

private async Task<NextPageResult> NextPage(PagedFileListResult result) {
var success = await result.NextPageAsync().AnyContext();
result = result.DeepClone();
foreach (var file in result.Files)
file.Path = file.Path.Substring(_pathPrefix.Length);
return new NextPageResult { Success = success, HasMore = result.HasMore, Files = result.Files, NextPageFunc = s => NextPage(result) };

return new NextPageResult {
Success = success,
HasMore = result.HasMore,
Files = result.Files,
NextPageFunc = s => NextPage(result)
};
}

public void Dispose() { }
Expand Down

0 comments on commit 010b0ad

Please sign in to comment.