Skip to content

Commit

Permalink
Merge branch 'master' into replace-with-async-lock
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith authored Sep 24, 2023
2 parents bb44c02 + abab988 commit 0eace55
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
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 @@ -45,9 +45,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 @@ -60,12 +70,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
2 changes: 1 addition & 1 deletion src/Foundatio/Storage/InMemoryFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ 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 async Task<Stream> GetFileStreamAsync(string path, FileAccess fileAccess, CancellationToken cancellationToken = default) {
if (String.IsNullOrEmpty(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
}

0 comments on commit 0eace55

Please sign in to comment.