Skip to content

Commit

Permalink
Copy stream and seek stream position to start
Browse files Browse the repository at this point in the history
  • Loading branch information
JadynWong committed Oct 27, 2021
1 parent 8fbb765 commit c8e6316
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
return null;
}
var result = ossClient.GetObject(containerName, blobName);
var memoryStream = new MemoryStream();
await result.Content.CopyToAsync(memoryStream);
return memoryStream;
return await TryCopyToMemoryStreamAsync(result.Content, args.CancellationToken);
}

protected virtual string GetContainerName(BlobProviderArgs args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
Key = blobName
});

var memoryStream = new MemoryStream();
await response.ResponseStream.CopyToAsync(memoryStream);
return memoryStream;
return await TryCopyToMemoryStreamAsync(response.ResponseStream, args.CancellationToken);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)

var blobClient = GetBlobClient(args, blobName);
var download = await blobClient.DownloadAsync();
var memoryStream = new MemoryStream();
await download.Value.Content.CopyToAsync(memoryStream);
return memoryStream;
return await TryCopyToMemoryStreamAsync(download.Value.Content, args.CancellationToken);
}

protected virtual BlobClient GetBlobClient(BlobProviderArgs args, string blobName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
{
using (var fileStream = File.OpenRead(filePath))
{
var memoryStream = new MemoryStream();
await fileStream.CopyToAsync(memoryStream, args.CancellationToken);
return memoryStream;
return await TryCopyToMemoryStreamAsync(fileStream, args.CancellationToken);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,13 @@ public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
return null;
}

var memoryStream = new MemoryStream();
Stream blobStream = null;
await client.GetObjectAsync(containerName, blobName, (stream) =>
{
if (stream != null)
{
stream.CopyTo(memoryStream);
}
else
{
memoryStream = null;
}
blobStream = stream;
});

return memoryStream;
return await TryCopyToMemoryStreamAsync(blobStream, args.CancellationToken);
}

protected virtual MinioClient GetMinioClient(BlobProviderArgs args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ public class BlobContainer : IBlobContainer
var blobNormalizeNaming =
BlobNormalizeNamingService.NormalizeNaming(Configuration, ContainerName, name);

return TrySeekStreamToBegin(await Provider.GetOrNullAsync(
return await Provider.GetOrNullAsync(
new BlobProviderGetArgs(
blobNormalizeNaming.ContainerName,
Configuration,
blobNormalizeNaming.BlobName,
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
));
);
}
}

Expand All @@ -214,14 +214,5 @@ public class BlobContainer : IBlobContainer

return CurrentTenant.Id;
}

protected virtual Stream TrySeekStreamToBegin(Stream stream)
{
if (stream != null && stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}
return stream;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Volo.Abp.BlobStoring
Expand All @@ -12,5 +13,18 @@ public abstract class BlobProviderBase : IBlobProvider
public abstract Task<bool> ExistsAsync(BlobProviderExistsArgs args);

public abstract Task<Stream> GetOrNullAsync(BlobProviderGetArgs args);

protected virtual async Task<Stream> TryCopyToMemoryStreamAsync(Stream stream, CancellationToken cancellationToken = default)
{
if(stream == null)
{
return null;
}

var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream, cancellationToken);
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}
}
}

0 comments on commit c8e6316

Please sign in to comment.