From abab98898039ef98f3f77d71251b7bfeb4e46707 Mon Sep 17 00:00:00 2001 From: Patrick Garcia Date: Sun, 24 Sep 2023 04:19:52 +0200 Subject: [PATCH] chore: change to framework internal enumeration for stream modes. (#292) --- .../Storage/FileStorageTestsBase.cs | 2 +- src/Foundatio/Storage/FolderFileStorage.cs | 20 +++++++++++++++---- src/Foundatio/Storage/IFileStorage.cs | 9 ++++++++- src/Foundatio/Storage/InMemoryFileStorage.cs | 4 ++-- src/Foundatio/Storage/ScopedFileStorage.cs | 4 ++-- src/Foundatio/Storage/StreamMode.cs | 13 ++++++++++++ 6 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 src/Foundatio/Storage/StreamMode.cs diff --git a/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs b/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs index 58897c51..97cdd7f4 100644 --- a/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs +++ b/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs @@ -468,7 +468,7 @@ public virtual async Task WillWriteStreamContentAsync() { using (storage) { - using (var writer = new StreamWriter(await storage.GetFileStreamAsync(path, FileAccess.ReadWrite), Encoding.UTF8, 1024, false)) { + using (var writer = new StreamWriter(await storage.GetFileStreamAsync(path, StreamMode.Write), Encoding.UTF8, 1024, false)) { await writer.WriteAsync(testContent); } diff --git a/src/Foundatio/Storage/FolderFileStorage.cs b/src/Foundatio/Storage/FolderFileStorage.cs index 38ca2bc6..9e2b4312 100644 --- a/src/Foundatio/Storage/FolderFileStorage.cs +++ b/src/Foundatio/Storage/FolderFileStorage.cs @@ -44,9 +44,19 @@ public FolderFileStorage(Builder _serializer; public Task GetFileStreamAsync(string path, CancellationToken cancellationToken = default) - => GetFileStreamAsync(path, FileAccess.Read, cancellationToken); + => GetFileStreamAsync(path, StreamMode.Read, cancellationToken); - public Task GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default) { + public Task GetFileStreamAsync(string path, StreamMode streamMode, CancellationToken cancellationToken = default) { + var stream = streamMode switch { + StreamMode.Read => GetFileStreamAsync(path, FileAccess.Read), + StreamMode.Write => GetFileStreamAsync(path, FileAccess.Write), + _ => throw new NotSupportedException($"Stream mode {streamMode} is not supported."), + }; + + return Task.FromResult(stream); + } + + public Stream GetFileStreamAsync(string path, FileAccess fileAccess) { if (String.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path)); @@ -59,12 +69,14 @@ public Task GetFileStreamAsync(string path, FileAccess fileAccess, Cance var fileMode = GetFileModeForFileAccess(fileAccess); try { - return Task.FromResult(File.Open(fullPath, fileMode, fileAccess)); + return File.Open(fullPath, fileMode, fileAccess); } catch (IOException ex) when (ex is FileNotFoundException or DirectoryNotFoundException) { _logger.LogError(ex, "Unable to get file stream for {Path}: {Message}", normalizedPath, ex.Message); - return Task.FromResult(null); + return null; } } + + private FileMode GetFileModeForFileAccess(FileAccess fileAccess) { return fileAccess switch { FileAccess.Read => FileMode.Open, diff --git a/src/Foundatio/Storage/IFileStorage.cs b/src/Foundatio/Storage/IFileStorage.cs index e0dc8d8c..99f3eaa1 100644 --- a/src/Foundatio/Storage/IFileStorage.cs +++ b/src/Foundatio/Storage/IFileStorage.cs @@ -13,7 +13,14 @@ namespace Foundatio.Storage { public interface IFileStorage : IHaveSerializer, IDisposable { [Obsolete($"Use {nameof(GetFileStreamAsync)} with {nameof(FileAccess)} instead to define read or write behaviour of stream.")] Task GetFileStreamAsync(string path, CancellationToken cancellationToken = default); - Task GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default); + /// + /// Gets a file stream in the specified mode + /// + /// Path to the file in the file storage + /// What the stream is used for + /// Token to cancel + /// Stream in the specified mode + Task GetFileStreamAsync(string path, StreamMode streamMode, CancellationToken cancellationToken = default); Task GetFileInfoAsync(string path); Task ExistsAsync(string path); Task SaveFileAsync(string path, Stream stream, CancellationToken cancellationToken = default); diff --git a/src/Foundatio/Storage/InMemoryFileStorage.cs b/src/Foundatio/Storage/InMemoryFileStorage.cs index 30947a32..8dbf797b 100644 --- a/src/Foundatio/Storage/InMemoryFileStorage.cs +++ b/src/Foundatio/Storage/InMemoryFileStorage.cs @@ -38,9 +38,9 @@ public InMemoryFileStorage(Builder _serializer; public Task GetFileStreamAsync(string path, CancellationToken cancellationToken = default) => - GetFileStreamAsync(path, FileAccess.Read, cancellationToken); + GetFileStreamAsync(path, StreamMode.Read, cancellationToken); - public Task GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default) { + public Task GetFileStreamAsync(string path, StreamMode streamMode, CancellationToken cancellationToken = default) { if (String.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path)); diff --git a/src/Foundatio/Storage/ScopedFileStorage.cs b/src/Foundatio/Storage/ScopedFileStorage.cs index f890fdc8..4ed2e0a5 100644 --- a/src/Foundatio/Storage/ScopedFileStorage.cs +++ b/src/Foundatio/Storage/ScopedFileStorage.cs @@ -21,9 +21,9 @@ public ScopedFileStorage(IFileStorage storage, string scope) { ISerializer IHaveSerializer.Serializer => UnscopedStorage.Serializer; public Task GetFileStreamAsync(string path, CancellationToken cancellationToken = default) - => GetFileStreamAsync(path, FileAccess.Read, cancellationToken); + => GetFileStreamAsync(path, StreamMode.Read, cancellationToken); - public Task GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default) { + public Task GetFileStreamAsync(string path, StreamMode streamMode, CancellationToken cancellationToken = default) { if (String.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path)); diff --git a/src/Foundatio/Storage/StreamMode.cs b/src/Foundatio/Storage/StreamMode.cs new file mode 100644 index 00000000..bdfdb2de --- /dev/null +++ b/src/Foundatio/Storage/StreamMode.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Foundatio.Storage; + +/// +/// Tells what the stream will be used for +/// +public enum StreamMode { + Read, + Write +}