From 010b0adf96379b86b7864e018498955fa98c2ed4 Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Thu, 2 Dec 2021 13:03:39 -0600 Subject: [PATCH] GetFileListAsync get everything in one go instead of trying to page. --- src/Foundatio/Storage/IFileStorage.cs | 26 ++++++++++------------ src/Foundatio/Storage/ScopedFileStorage.cs | 11 +++++++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/Foundatio/Storage/IFileStorage.cs b/src/Foundatio/Storage/IFileStorage.cs index 02040927..751224c1 100644 --- a/src/Foundatio/Storage/IFileStorage.cs +++ b/src/Foundatio/Storage/IFileStorage.cs @@ -135,20 +135,18 @@ public static async Task 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 SaveFileAsync(this IFileStorage storage, string path, string contents) { @@ -161,7 +159,7 @@ public static Task SaveFileAsync(this IFileStorage storage, string path, s public static async Task> GetFileListAsync(this IFileStorage storage, string searchPattern = null, int? limit = null, CancellationToken cancellationToken = default) { var files = new List(); 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()); diff --git a/src/Foundatio/Storage/ScopedFileStorage.cs b/src/Foundatio/Storage/ScopedFileStorage.cs index 6b4ded3a..93959b43 100644 --- a/src/Foundatio/Storage/ScopedFileStorage.cs +++ b/src/Foundatio/Storage/ScopedFileStorage.cs @@ -95,14 +95,21 @@ public async Task 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>)null); + return new PagedFileListResult(unscopedResult.Files, unscopedResult.HasMore, unscopedResult.HasMore ? s => NextPage(unscopedResult) : null); } private async Task 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() { }