Skip to content

Commit a440364

Browse files
committed
fixes and improvements after running integration tests
1 parent a01d204 commit a440364

34 files changed

+616
-270
lines changed

FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="AWSSDK.S3" Version="3.7.205.23" />
21-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
20+
<PackageReference Include="AWSSDK.S3" Version="3.7.305.3" />
21+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
2222
<PrivateAssets>all</PrivateAssets>
2323
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2424
</PackageReference>

FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using SharpGrip.FileSystem.Exceptions;
1010
using SharpGrip.FileSystem.Extensions;
1111
using SharpGrip.FileSystem.Models;
12+
using SharpGrip.FileSystem.Utilities;
1213
using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException;
1314
using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException;
1415

@@ -36,7 +37,7 @@ public override void Connect()
3637

3738
public override async Task<IFile> GetFileAsync(string virtualPath, CancellationToken cancellationToken = default)
3839
{
39-
var path = GetPath(virtualPath);
40+
var path = GetPath(virtualPath).RemoveLeadingForwardSlash();
4041

4142
try
4243
{
@@ -104,7 +105,8 @@ public override async Task<IDirectory> GetDirectoryAsync(string virtualPath, Can
104105
public override async Task<IEnumerable<IFile>> GetFilesAsync(string virtualPath = "", CancellationToken cancellationToken = default)
105106
{
106107
await GetDirectoryAsync(virtualPath, cancellationToken);
107-
var path = GetPath(virtualPath).EnsureTrailingForwardSlash();
108+
109+
var path = GetPath(virtualPath).RemoveLeadingForwardSlash().EnsureTrailingForwardSlash();
108110

109111
if (path == "/")
110112
{
@@ -146,7 +148,8 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string virtualPath
146148
public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string virtualPath = "", CancellationToken cancellationToken = default)
147149
{
148150
await GetDirectoryAsync(virtualPath, cancellationToken);
149-
var path = GetPath(virtualPath).EnsureTrailingForwardSlash();
151+
152+
var path = GetPath(virtualPath).RemoveLeadingForwardSlash().EnsureTrailingForwardSlash();
150153

151154
if (path == "/")
152155
{
@@ -192,7 +195,7 @@ public override async Task CreateDirectoryAsync(string virtualPath, Cancellation
192195
throw new DirectoryExistsException(GetPath(virtualPath), Prefix);
193196
}
194197

195-
var path = GetPath(virtualPath).EnsureTrailingForwardSlash();
198+
var path = GetPath(virtualPath).RemoveLeadingForwardSlash().EnsureTrailingForwardSlash();
196199

197200
try
198201
{
@@ -209,7 +212,7 @@ public override async Task DeleteDirectoryAsync(string virtualPath, Cancellation
209212
{
210213
await GetDirectoryAsync(virtualPath, cancellationToken);
211214

212-
var path = GetPath(virtualPath).EnsureTrailingForwardSlash();
215+
var path = GetPath(virtualPath).RemoveLeadingForwardSlash().EnsureTrailingForwardSlash();
213216

214217
try
215218
{
@@ -242,7 +245,8 @@ public override async Task DeleteDirectoryAsync(string virtualPath, Cancellation
242245
public override async Task DeleteFileAsync(string virtualPath, CancellationToken cancellationToken = default)
243246
{
244247
await GetFileAsync(virtualPath, cancellationToken);
245-
var path = GetPath(virtualPath);
248+
249+
var path = GetPath(virtualPath).RemoveLeadingForwardSlash();
246250

247251
try
248252
{
@@ -257,13 +261,20 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
257261
public override async Task<Stream> ReadFileStreamAsync(string virtualPath, CancellationToken cancellationToken = default)
258262
{
259263
await GetFileAsync(virtualPath, cancellationToken);
260-
var path = GetPath(virtualPath);
264+
265+
var path = GetPath(virtualPath).RemoveLeadingForwardSlash();
261266

262267
try
263268
{
264-
var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
269+
// Performance issue:
270+
// The stream returned from the service does not support seeking and therefore cannot determine the content length (required when creating files from this stream).
271+
// Copy the response stream to a new memory stream and return that one instead.
272+
273+
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
274+
275+
var memoryStream = await StreamUtilities.CopyContentsToMemoryStreamAsync(response.ResponseStream, true, cancellationToken);
265276

266-
return response.ResponseStream;
277+
return memoryStream;
267278
}
268279
catch (Exception exception)
269280
{
@@ -278,7 +289,7 @@ public override async Task WriteFileAsync(string virtualPath, Stream contents, b
278289
throw new FileExistsException(GetPath(virtualPath), Prefix);
279290
}
280291

281-
var path = GetPath(virtualPath);
292+
var path = GetPath(virtualPath).RemoveLeadingForwardSlash();
282293

283294
try
284295
{

FileSystem.Adapters.AmazonS3/src/ModelFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static DirectoryModel CreateDirectory(S3Object directory, string virtualP
4949

5050
return new DirectoryModel
5151
{
52-
Name = name,
52+
Name = name.RemoveTrailingForwardSlash(),
5353
Path = directory.Key.RemoveTrailingForwardSlash(),
5454
VirtualPath = virtualPath,
5555
LastModifiedDateTime = directory.LastModified

FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.0" />
21-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
20+
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.1" />
21+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
2222
<PrivateAssets>all</PrivateAssets>
2323
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2424
</PackageReference>

FileSystem.Adapters.AzureBlobStorage/src/AzureBlobStorageAdapter.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override async Task<IFile> GetFileAsync(string virtualPath, CancellationT
4141

4242
try
4343
{
44-
await foreach (var item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, directoryPath))
44+
await foreach (var item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, directoryPath, cancellationToken))
4545
{
4646
if (item.Name == path)
4747
{
@@ -63,11 +63,16 @@ public override async Task<IDirectory> GetDirectoryAsync(string virtualPath, Can
6363
var directoryPath = GetLastPathPart(path);
6464
var parentDirectoryPath = GetParentPathPart(path);
6565

66-
path = path.RemoveLeadingForwardSlash();
66+
path = path.RemoveLeadingForwardSlash().EnsureTrailingForwardSlash();
6767

6868
try
6969
{
70-
await foreach (var item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, parentDirectoryPath))
70+
if (path == "/")
71+
{
72+
return ModelFactory.CreateDirectory("", path, virtualPath);
73+
}
74+
75+
await foreach (var item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, parentDirectoryPath, cancellationToken))
7176
{
7277
if (item.Name.StartsWith(path))
7378
{
@@ -95,11 +100,11 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string virtualPath
95100
{
96101
var files = new List<IFile>();
97102

98-
await foreach (var item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, path))
103+
await foreach (var item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, path, cancellationToken))
99104
{
100105
var directoryPath = GetParentPathPart(item.Name);
101106

102-
if (directoryPath == path && item.Name != directoryPath + "/")
107+
if (directoryPath == path && item.Name != directoryPath + "/" && !item.Name.Contains(Configuration.DirectoryPlaceholder))
103108
{
104109
files.Add(ModelFactory.CreateFile(item, GetVirtualPath(item.Name)));
105110
}
@@ -130,7 +135,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string v
130135
path = "";
131136
}
132137

133-
await foreach (var item in client.GetBlobsByHierarchyAsync(BlobTraits.None, BlobStates.None, "/", path))
138+
await foreach (var item in client.GetBlobsByHierarchyAsync(BlobTraits.None, BlobStates.None, "/", path, cancellationToken))
134139
{
135140
if (item.IsPrefix)
136141
{
@@ -153,13 +158,11 @@ public override async Task CreateDirectoryAsync(string virtualPath, Cancellation
153158
throw new DirectoryExistsException(GetPath(virtualPath), Prefix);
154159
}
155160

156-
var path = GetPath(virtualPath);
157-
158-
path = path.EnsureTrailingForwardSlash();
161+
var path = GetPath(virtualPath).RemoveLeadingForwardSlash().EnsureTrailingForwardSlash();
159162

160163
try
161164
{
162-
await client.UploadBlobAsync(path, Stream.Null, cancellationToken);
165+
await client.UploadBlobAsync(path + Configuration.DirectoryPlaceholder, Stream.Null, cancellationToken);
163166
}
164167
catch (Exception exception)
165168
{
@@ -173,11 +176,11 @@ public override async Task DeleteDirectoryAsync(string virtualPath, Cancellation
173176

174177
var path = GetPath(virtualPath);
175178

176-
path = path.EnsureTrailingForwardSlash();
179+
path = path.RemoveLeadingForwardSlash().EnsureTrailingForwardSlash();
177180

178181
try
179182
{
180-
await foreach (var item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, path))
183+
await foreach (var item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, path, cancellationToken))
181184
{
182185
await client.DeleteBlobAsync(item.Name, DeleteSnapshotsOption.IncludeSnapshots, cancellationToken: cancellationToken);
183186
}

FileSystem.Adapters.AzureBlobStorage/src/AzureBlobStorageAdapterConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ namespace SharpGrip.FileSystem.Adapters.AzureBlobStorage
44
{
55
public class AzureBlobStorageAdapterConfiguration : AdapterConfiguration
66
{
7+
public string DirectoryPlaceholder { get; set; } = "___sharp-grip-file-system-placeholder___";
78
}
89
}

FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.17.0" />
21-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
20+
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.17.1" />
21+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
2222
<PrivateAssets>all</PrivateAssets>
2323
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2424
</PackageReference>

FileSystem.Adapters.AzureFileStorage/src/AzureFileStorageAdapter.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using Azure;
77
using Azure.Storage.Files.Shares;
88
using SharpGrip.FileSystem.Exceptions;
9+
using SharpGrip.FileSystem.Extensions;
910
using SharpGrip.FileSystem.Models;
11+
using SharpGrip.FileSystem.Utilities;
1012
using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException;
1113
using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException;
1214

@@ -157,6 +159,20 @@ public override async Task DeleteDirectoryAsync(string virtualPath, Cancellation
157159

158160
try
159161
{
162+
var files = await GetFilesAsync(virtualPath, cancellationToken);
163+
164+
foreach (var file in files)
165+
{
166+
await DeleteFileAsync(file.VirtualPath, cancellationToken);
167+
}
168+
169+
var subDirectories = await GetDirectoriesAsync(virtualPath, cancellationToken);
170+
171+
foreach (var subDirectory in subDirectories)
172+
{
173+
await DeleteDirectoryAsync(subDirectory.VirtualPath, cancellationToken);
174+
}
175+
160176
await client.DeleteDirectoryAsync(GetPath(virtualPath), cancellationToken);
161177
}
162178
catch (Exception exception)
@@ -194,10 +210,16 @@ public override async Task<Stream> ReadFileStreamAsync(string virtualPath, Cance
194210

195211
try
196212
{
213+
// Performance issue:
214+
// The stream returned from the service does not support seeking and therefore cannot determine the content length (required when creating files from this stream).
215+
// Copy the response stream to a new memory stream and return that one instead.
216+
197217
var directory = client.GetDirectoryClient(directoryPath);
198-
var download = await directory.GetFileClient(filePath).DownloadAsync(cancellationToken: cancellationToken);
218+
var response = await directory.GetFileClient(filePath).DownloadAsync(cancellationToken: cancellationToken);
219+
220+
var memoryStream = await StreamUtilities.CopyContentsToMemoryStreamAsync(response.Value.Content, true, cancellationToken);
199221

200-
return download.Value.Content;
222+
return memoryStream;
201223
}
202224
catch (Exception exception)
203225
{
@@ -214,12 +236,17 @@ public override async Task WriteFileAsync(string virtualPath, Stream contents, b
214236

215237
var path = GetPath(virtualPath);
216238
var filePath = GetLastPathPart(path);
217-
var directoryPath = GetParentPathPart(path);
239+
var directoryPath = GetParentPathPart(path).EnsureTrailingForwardSlash();
218240

219241
try
220242
{
221243
var directory = client.GetDirectoryClient(directoryPath);
222-
await directory.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
244+
245+
if (directoryPath != "/")
246+
{
247+
await directory.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
248+
}
249+
223250
var file = directory.GetFileClient(filePath);
224251

225252
contents.Seek(0, SeekOrigin.Begin);

FileSystem.Adapters.Dropbox/FileSystem.Adapters.Dropbox.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<ItemGroup>
2020
<PackageReference Include="Dropbox.Api" Version="6.37.0" />
21-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
21+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
2222
<PrivateAssets>all</PrivateAssets>
2323
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2424
</PackageReference>

0 commit comments

Comments
 (0)