99using SharpGrip . FileSystem . Exceptions ;
1010using SharpGrip . FileSystem . Extensions ;
1111using SharpGrip . FileSystem . Models ;
12+ using SharpGrip . FileSystem . Utilities ;
1213using DirectoryNotFoundException = SharpGrip . FileSystem . Exceptions . DirectoryNotFoundException ;
1314using 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 {
0 commit comments