Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storage streammode #292

Merged
merged 2 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
20 changes: 16 additions & 4 deletions src/Foundatio/Storage/FolderFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,19 @@ public FolderFileStorage(Builder<FolderFileStorageOptionsBuilder, FolderFileStor
ISerializer IHaveSerializer.Serializer => _serializer;

public Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default)
=> GetFileStreamAsync(path, FileAccess.Read, cancellationToken);
=> GetFileStreamAsync(path, StreamMode.Read, cancellationToken);

public Task<Stream> GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default) {
public Task<Stream> 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));

Expand All @@ -59,12 +69,14 @@ public Task<Stream> GetFileStreamAsync(string path, FileAccess fileAccess, Cance
var fileMode = GetFileModeForFileAccess(fileAccess);

try {
return Task.FromResult<Stream>(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<Stream>(null);
return null;
}
}


private FileMode GetFileModeForFileAccess(FileAccess fileAccess) {
return fileAccess switch {
FileAccess.Read => FileMode.Open,
Expand Down
9 changes: 8 additions & 1 deletion src/Foundatio/Storage/IFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default);
Task<Stream> GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default);
/// <summary>
/// Gets a file stream in the specified mode
/// </summary>
/// <param name="path">Path to the file in the file storage</param>
/// <param name="streamMode">What the stream is used for</param>
/// <param name="cancellationToken">Token to cancel</param>
/// <returns>Stream in the specified mode</returns>
Task<Stream> GetFileStreamAsync(string path, StreamMode streamMode, CancellationToken cancellationToken = default);
Task<FileSpec> GetFileInfoAsync(string path);
Task<bool> ExistsAsync(string path);
Task<bool> SaveFileAsync(string path, Stream stream, CancellationToken cancellationToken = default);
Expand Down
4 changes: 2 additions & 2 deletions src/Foundatio/Storage/InMemoryFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public InMemoryFileStorage(Builder<InMemoryFileStorageOptionsBuilder, InMemoryFi
ISerializer IHaveSerializer.Serializer => _serializer;

public Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default) =>
GetFileStreamAsync(path, FileAccess.Read, cancellationToken);
GetFileStreamAsync(path, StreamMode.Read, cancellationToken);

public Task<Stream> GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default) {
public Task<Stream> GetFileStreamAsync(string path, StreamMode streamMode, CancellationToken cancellationToken = default) {
if (String.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));

Expand Down
4 changes: 2 additions & 2 deletions src/Foundatio/Storage/ScopedFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public ScopedFileStorage(IFileStorage storage, string scope) {
ISerializer IHaveSerializer.Serializer => UnscopedStorage.Serializer;

public Task<Stream> GetFileStreamAsync(string path, CancellationToken cancellationToken = default)
=> GetFileStreamAsync(path, FileAccess.Read, cancellationToken);
=> GetFileStreamAsync(path, StreamMode.Read, cancellationToken);

public Task<Stream> GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default) {
public Task<Stream> GetFileStreamAsync(string path, StreamMode streamMode, CancellationToken cancellationToken = default) {
if (String.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));

Expand Down
13 changes: 13 additions & 0 deletions src/Foundatio/Storage/StreamMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Foundatio.Storage;

/// <summary>
/// Tells what the stream will be used for
/// </summary>
public enum StreamMode {
Read,
Write
}