Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Aync File System#8
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| namespace DotNetRu.Auditor.Storage | ||
| { | ||
| internal static class AsyncEnumerable | ||
| { | ||
| public static IAsyncEnumerable<T> Empty<T>() | ||
| { | ||
| return EmptyAsyncEnumerable<T>.Instance; | ||
| } | ||
| private class EmptyAsyncEnumerable<T> : IAsyncEnumerable<T> | ||
| { | ||
| public static readonly IAsyncEnumerable<T> Instance = new EmptyAsyncEnumerable<T>(); | ||
| public async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = new()) | ||
| { | ||
| await Task.Yield(); | ||
| yield break; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using System; | ||
| using System.IO; | ||
| namespace DotNetRu.Auditor.Storage.FileSystem | ||
| { | ||
| public abstract class FileSystemEntry : IFileSystemEntry | ||
| { | ||
| protected FileSystemEntry(string fullName, bool exists) | ||
| { | ||
| FullName = Path.GetFullPath(fullName); | ||
| Name = Path.GetFileName(FullName); | ||
| Exists = exists; | ||
| } | ||
| public string Name { get; } | ||
| public string FullName { get; } | ||
| public bool Exists { get; } | ||
| protected string GetFullPath(string subPath) | ||
| { | ||
| var childrenPath = Path.Combine(FullName, subPath); | ||
| var childrenFullPath = Path.GetFullPath(childrenPath); | ||
| if (!childrenFullPath.StartsWith(FullName, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| throw new ArgumentException($"Children path «{childrenFullPath}» should be under the root «{FullName}»"); | ||
| } | ||
| return childrenFullPath; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| namespace DotNetRu.Auditor.Storage.FileSystem | ||
| { | ||
| /// <remarks> | ||
| /// In fact, most operating systems do not support asynchronous enumeration of file system contents. | ||
| /// This is why there is no asynchronous API in the official BCL. But we have a Blazor consumer that | ||
| /// throws an exception when using synchronization operations, and a GutHub HTTP API implementation | ||
| /// that can perform real asynchronous operations. | ||
| /// | ||
| /// To respect I/O operations, we decided to use asynchronous abstractions. But we assume that real | ||
| /// asynchronous operations will be rare, so we decided to use ValueTask. | ||
| /// </remarks>> | ||
| public interface IDirectory : IFileSystemEntry | ||
| { | ||
| ValueTask<IDirectory> GetDirectoryInfoAsync(string subPath); | ||
| ValueTask<IFile> GetFileInfoAsync(string subPath); | ||
| IAsyncEnumerable<IDirectory> EnumerateDirectoriesAsync(); | ||
| IAsyncEnumerable<IFile> EnumerateFilesAsync(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| namespace DotNetRu.Auditor.Storage.FileSystem | ||
| { | ||
| public interface IFile : IFileSystemEntry | ||
| { | ||
| ValueTask<Stream> OpenForReadAsync(); | ||
| ValueTask<Stream> OpenForWriteAsync(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace DotNetRu.Auditor.Storage.FileSystem | ||
| { | ||
| public interface IFileSystem : IDirectory | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace DotNetRu.Auditor.Storage.FileSystem | ||
| { | ||
| public interface IFileSystemEntry | ||
| { | ||
| string Name { get; } | ||
| string FullName { get; } | ||
| bool Exists { get; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| namespace DotNetRu.Auditor.Storage.FileSystem | ||
| { | ||
| public class NotFoundDirectory : FileSystemEntry, IDirectory | ||
| { | ||
| public NotFoundDirectory(string fullName) | ||
| : base(fullName, false) | ||
| { | ||
| } | ||
| public static ValueTask<IDirectory> ToTask(string fullName) => ValueTask.FromResult<IDirectory>(new NotFoundDirectory(fullName)); | ||
| public ValueTask<IDirectory> GetDirectoryInfoAsync(string subPath) => ToTask(subPath); | ||
| public ValueTask<IFile> GetFileInfoAsync(string subPath) => NotFoundFile.ToTask(subPath); | ||
| public IAsyncEnumerable<IDirectory> EnumerateDirectoriesAsync() => AsyncEnumerable.Empty<IDirectory>(); | ||
| public IAsyncEnumerable<IFile> EnumerateFilesAsync() => AsyncEnumerable.Empty<IFile>(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| namespace DotNetRu.Auditor.Storage.FileSystem | ||
| { | ||
| public class NotFoundFile : FileSystemEntry, IFile | ||
| { | ||
| public NotFoundFile(string fullName) | ||
| : base(fullName, false) | ||
| { | ||
| } | ||
| public static ValueTask<IFile> ToTask(string fullName) => ValueTask.FromResult<IFile>(new NotFoundFile(fullName)); | ||
| public static Exception ToException(string fullName) => new FileNotFoundException($"Could not find file: {fullName}", fullName); | ||
| public ValueTask<Stream> OpenForReadAsync() => throw ToException(FullName); | ||
| public ValueTask<Stream> OpenForWriteAsync() => throw ToException(FullName); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using System; | ||
| namespace DotNetRu.Auditor.Storage.FileSystem | ||
| { | ||
| public class NullFileSystem : NotFoundDirectory, IFileSystem | ||
| { | ||
| public static readonly NullFileSystem Instance = new NullFileSystem(); | ||
| public NullFileSystem() | ||
| : base(String.Empty) | ||
| { | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| namespace DotNetRu.Auditor.Storage.FileSystem.Physical | ||
| { | ||
| public class PhysicalDirectory : FileSystemEntry, IDirectory | ||
| { | ||
| public PhysicalDirectory(string fullName) | ||
| : base(fullName, Directory.Exists(fullName)) | ||
| { | ||
| } | ||
| public ValueTask<IDirectory> GetDirectoryInfoAsync(string subPath) | ||
| { | ||
| if (!Exists) | ||
| { | ||
| return NotFoundDirectory.ToTask(subPath); | ||
| } | ||
| var fullDirectoryName = GetFullPath(subPath); | ||
| var directory = new PhysicalDirectory(fullDirectoryName); | ||
| return ValueTask.FromResult<IDirectory>(directory); | ||
| } | ||
| public ValueTask<IFile> GetFileInfoAsync(string subPath) | ||
| { | ||
| if (!Exists) | ||
| { | ||
| return NotFoundFile.ToTask(subPath); | ||
| } | ||
| var fullFileName = GetFullPath(subPath); | ||
| var file = new PhysicalFile(fullFileName); | ||
| return ValueTask.FromResult<IFile>(file); | ||
| } | ||
| public async IAsyncEnumerable<IDirectory> EnumerateDirectoriesAsync() | ||
| { | ||
| if (!Exists) | ||
| { | ||
| yield break; | ||
| } | ||
| await Task.Yield(); | ||
| foreach (var directoryFullName in Directory.EnumerateDirectories(FullName)) | ||
| { | ||
| yield return new PhysicalDirectory(directoryFullName); | ||
| } | ||
| } | ||
| public async IAsyncEnumerable<IFile> EnumerateFilesAsync() | ||
| { | ||
| if (!Exists) | ||
| { | ||
| yield break; | ||
| } | ||
| await Task.Yield(); | ||
| foreach (var fileFullName in Directory.EnumerateFiles(FullName)) | ||
| { | ||
| yield return new PhysicalFile(fileFullName); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
kulakovt marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| namespace DotNetRu.Auditor.Storage.FileSystem.Physical | ||
| { | ||
| public sealed class PhysicalFile : FileSystemEntry, IFile | ||
| { | ||
| public PhysicalFile(string fullName) | ||
| : base(fullName, File.Exists(fullName)) | ||
| { | ||
| } | ||
| public ValueTask<Stream> OpenForReadAsync() | ||
| { | ||
| if (!Exists) | ||
| { | ||
| throw NotFoundFile.ToException(FullName); | ||
| } | ||
| var inputStream = File.OpenRead(FullName); | ||
| return ValueTask.FromResult<Stream>(inputStream); | ||
| } | ||
| public ValueTask<Stream> OpenForWriteAsync() | ||
| { | ||
| if (!Exists) | ||
| { | ||
| throw NotFoundFile.ToException(FullName); | ||
| } | ||
| var outputStream = File.OpenWrite(FullName); | ||
| return ValueTask.FromResult<Stream>(outputStream); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using System; | ||
| using System.IO; | ||
| namespace DotNetRu.Auditor.Storage.FileSystem.Physical | ||
| { | ||
| public sealed class PhysicalFileSystem : PhysicalDirectory, IFileSystem | ||
| { | ||
| public PhysicalFileSystem(string root) | ||
| : base(root) | ||
| { | ||
| if (!Path.IsPathRooted(root)) | ||
| { | ||
| throw new ArgumentException($"The path «{root}» must be absolute", nameof(root)); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| <AssemblyName>DotNetRu.Auditor.Storage</AssemblyName> | ||
| <RootNamespace>DotNetRu.Auditor.Storage</RootNamespace> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Data\Data.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.