|
| 1 | +using System.Net; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | + |
| 4 | +using Google; |
| 5 | +using Google.Cloud.Storage.V1; |
| 6 | + |
| 7 | +namespace Ramstack.FileSystem.Google; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Represents an implementation of <see cref="VirtualDirectory"/> that maps a directory |
| 11 | +/// to the path within a Google Cloud Storage bucket. |
| 12 | +/// </summary> |
| 13 | +internal sealed class GcsDirectory : VirtualDirectory |
| 14 | +{ |
| 15 | + private readonly GoogleFileSystem _fs; |
| 16 | + private readonly string _prefix; |
| 17 | + |
| 18 | + /// <inheritdoc /> |
| 19 | + public override IVirtualFileSystem FileSystem => _fs; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Initializes a new instance of the <see cref="GcsDirectory"/> class. |
| 23 | + /// </summary> |
| 24 | + /// <param name="fileSystem">The file system associated with this directory.</param> |
| 25 | + /// <param name="path">The path to the directory within the Google Cloud Storage bucket.</param> |
| 26 | + public GcsDirectory(GoogleFileSystem fileSystem, string path) : base(path) |
| 27 | + { |
| 28 | + _fs = fileSystem; |
| 29 | + _prefix = path == "/" ? "" : $"{path[1..]}/"; |
| 30 | + } |
| 31 | + |
| 32 | + /// <inheritdoc /> |
| 33 | + protected override ValueTask<VirtualNodeProperties?> GetPropertiesCoreAsync(CancellationToken cancellationToken) => |
| 34 | + new ValueTask<VirtualNodeProperties?>(VirtualNodeProperties.None); |
| 35 | + |
| 36 | + /// <inheritdoc /> |
| 37 | + protected override ValueTask<bool> ExistsCoreAsync(CancellationToken cancellationToken) => |
| 38 | + new ValueTask<bool>(true); |
| 39 | + |
| 40 | + /// <inheritdoc /> |
| 41 | + protected override ValueTask CreateCoreAsync(CancellationToken cancellationToken) => |
| 42 | + default; |
| 43 | + |
| 44 | + /// <inheritdoc /> |
| 45 | + protected override async ValueTask DeleteCoreAsync(CancellationToken cancellationToken) |
| 46 | + { |
| 47 | + await foreach (var obj in _fs.StorageClient.ListObjectsAsync(_fs.BucketName, _prefix).WithCancellation(cancellationToken).ConfigureAwait(false)) |
| 48 | + { |
| 49 | + try |
| 50 | + { |
| 51 | + await _fs.StorageClient |
| 52 | + .DeleteObjectAsync(_fs.BucketName, obj.Name, cancellationToken: cancellationToken) |
| 53 | + .ConfigureAwait(false); |
| 54 | + } |
| 55 | + catch (GoogleApiException e) when (e.HttpStatusCode == HttpStatusCode.NotFound) |
| 56 | + { |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// <inheritdoc /> |
| 62 | + protected override async IAsyncEnumerable<VirtualNode> GetFileNodesCoreAsync([EnumeratorCancellation] CancellationToken cancellationToken) |
| 63 | + { |
| 64 | + var options = new ListObjectsOptions |
| 65 | + { |
| 66 | + Delimiter = "/", |
| 67 | + IncludeFoldersAsPrefixes = true, |
| 68 | + IncludeTrailingDelimiter = true |
| 69 | + }; |
| 70 | + |
| 71 | + var responses = _fs.StorageClient |
| 72 | + .ListObjectsAsync(_fs.BucketName, _prefix, options) |
| 73 | + .AsRawResponses(); |
| 74 | + |
| 75 | + await foreach (var page in responses.WithCancellation(cancellationToken).ConfigureAwait(false)) |
| 76 | + { |
| 77 | + if (page.Prefixes is not null) |
| 78 | + foreach (var prefix in page.Prefixes) |
| 79 | + yield return new GcsDirectory(_fs, VirtualPath.Normalize(prefix)); |
| 80 | + |
| 81 | + if (page.Items is null) |
| 82 | + continue; |
| 83 | + |
| 84 | + foreach (var obj in page.Items) |
| 85 | + { |
| 86 | + var properties = VirtualNodeProperties.CreateFileProperties( |
| 87 | + obj.TimeCreatedDateTimeOffset.GetValueOrDefault(), |
| 88 | + DateTimeOffset.MinValue, |
| 89 | + obj.UpdatedDateTimeOffset.GetValueOrDefault(), |
| 90 | + (long)(obj.Size ?? 0)); |
| 91 | + yield return new GcsFile(_fs, VirtualPath.Normalize(obj.Name), properties); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// <inheritdoc /> |
| 97 | + protected override async IAsyncEnumerable<VirtualFile> GetFilesCoreAsync([EnumeratorCancellation] CancellationToken cancellationToken) |
| 98 | + { |
| 99 | + var options = new ListObjectsOptions |
| 100 | + { |
| 101 | + Delimiter = "/", |
| 102 | + IncludeFoldersAsPrefixes = true, |
| 103 | + IncludeTrailingDelimiter = true |
| 104 | + }; |
| 105 | + |
| 106 | + var response = _fs.StorageClient.ListObjectsAsync(_fs.BucketName, _prefix, options); |
| 107 | + |
| 108 | + await foreach (var obj in response.WithCancellation(cancellationToken).ConfigureAwait(false)) |
| 109 | + { |
| 110 | + var properties = VirtualNodeProperties.CreateFileProperties( |
| 111 | + obj.TimeCreatedDateTimeOffset.GetValueOrDefault(), |
| 112 | + DateTimeOffset.MinValue, |
| 113 | + obj.UpdatedDateTimeOffset.GetValueOrDefault(), |
| 114 | + (long)(obj.Size ?? 0)); |
| 115 | + |
| 116 | + yield return new GcsFile(_fs, VirtualPath.Normalize(obj.Name), properties); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// <inheritdoc /> |
| 121 | + protected override async IAsyncEnumerable<VirtualDirectory> GetDirectoriesCoreAsync([EnumeratorCancellation] CancellationToken cancellationToken) |
| 122 | + { |
| 123 | + var options = new ListObjectsOptions |
| 124 | + { |
| 125 | + Delimiter = "/", |
| 126 | + IncludeFoldersAsPrefixes = true, |
| 127 | + IncludeTrailingDelimiter = true |
| 128 | + }; |
| 129 | + |
| 130 | + var response = _fs.StorageClient |
| 131 | + .ListObjectsAsync(_fs.BucketName, _prefix, options) |
| 132 | + .AsRawResponses(); |
| 133 | + |
| 134 | + await foreach (var page in response.WithCancellation(cancellationToken).ConfigureAwait(false)) |
| 135 | + { |
| 136 | + if (page.Prefixes is null) |
| 137 | + continue; |
| 138 | + |
| 139 | + foreach (var prefix in page.Prefixes) |
| 140 | + yield return new GcsDirectory(_fs, VirtualPath.Normalize(prefix)); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments