From 1e3fb8e59ec04c2ec85504cb954dcd706df88ff4 Mon Sep 17 00:00:00 2001 From: jadyn Date: Wed, 27 Oct 2021 13:00:04 +0800 Subject: [PATCH 1/4] Fix CmsKit media download --- .../CmsKit/MediaDescriptors/MediaDescriptorAppService.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/MediaDescriptors/MediaDescriptorAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/MediaDescriptors/MediaDescriptorAppService.cs index 99700630e7..501b79e758 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/MediaDescriptors/MediaDescriptorAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/MediaDescriptors/MediaDescriptorAppService.cs @@ -24,6 +24,11 @@ namespace Volo.CmsKit.MediaDescriptors var entity = await MediaDescriptorRepository.GetAsync(id); var stream = await MediaContainer.GetAsync(id.ToString()); + if (stream.CanSeek) + { + stream.Position = 0; + } + return new RemoteStreamContent(stream, entity.Name, entity.MimeType); } } From 0f6c4a535682765e3a8af5c3e8232f7f39b60346 Mon Sep 17 00:00:00 2001 From: jadyn Date: Wed, 27 Oct 2021 14:52:09 +0800 Subject: [PATCH 2/4] Try seek blob stream position to begin --- .../Volo/Abp/BlobStoring/BlobContainer.cs | 21 +++++++++++++++++-- .../MediaDescriptorAppService.cs | 5 ----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs index c4d45b840a..443a616d5e 100644 --- a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs +++ b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs @@ -194,14 +194,14 @@ namespace Volo.Abp.BlobStoring var blobNormalizeNaming = BlobNormalizeNamingService.NormalizeNaming(Configuration, ContainerName, name); - return await Provider.GetOrNullAsync( + return TrySeekStreamToBegin(await Provider.GetOrNullAsync( new BlobProviderGetArgs( blobNormalizeNaming.ContainerName, Configuration, blobNormalizeNaming.BlobName, CancellationTokenProvider.FallbackToProvider(cancellationToken) ) - ); + )); } } @@ -214,5 +214,22 @@ namespace Volo.Abp.BlobStoring return CurrentTenant.Id; } + + protected virtual Stream TrySeekStreamToBegin(Stream stream) + { + if (stream == null) + { + return stream; + } + + if (!stream.CanSeek) + { + return stream; + } + + stream.Seek(0, SeekOrigin.Begin); + + return stream; + } } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/MediaDescriptors/MediaDescriptorAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/MediaDescriptors/MediaDescriptorAppService.cs index 501b79e758..99700630e7 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/MediaDescriptors/MediaDescriptorAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/MediaDescriptors/MediaDescriptorAppService.cs @@ -24,11 +24,6 @@ namespace Volo.CmsKit.MediaDescriptors var entity = await MediaDescriptorRepository.GetAsync(id); var stream = await MediaContainer.GetAsync(id.ToString()); - if (stream.CanSeek) - { - stream.Position = 0; - } - return new RemoteStreamContent(stream, entity.Name, entity.MimeType); } } From 8fbb765dba492920fa437db01373ba85523e21b7 Mon Sep 17 00:00:00 2001 From: jadyn Date: Wed, 27 Oct 2021 15:11:56 +0800 Subject: [PATCH 3/4] Refactor --- .../Volo/Abp/BlobStoring/BlobContainer.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs index 443a616d5e..008f1ec2cb 100644 --- a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs +++ b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs @@ -217,18 +217,10 @@ namespace Volo.Abp.BlobStoring protected virtual Stream TrySeekStreamToBegin(Stream stream) { - if (stream == null) - { - return stream; - } - - if (!stream.CanSeek) + if (stream != null && stream.CanSeek) { - return stream; + stream.Seek(0, SeekOrigin.Begin); } - - stream.Seek(0, SeekOrigin.Begin); - return stream; } } From c8e63168abab643457e9166be468c7957e657945 Mon Sep 17 00:00:00 2001 From: jadyn Date: Wed, 27 Oct 2021 15:11:56 +0800 Subject: [PATCH 4/4] Copy stream and seek stream position to start --- .../Abp/BlobStoring/Aliyun/AliyunBlobProvider.cs | 4 +--- .../Volo/Abp/BlobStoring/Aws/AwsBlobProvider.cs | 4 +--- .../Abp/BlobStoring/Azure/AzureBlobProvider.cs | 4 +--- .../FileSystem/FileSystemBlobProvider.cs | 4 +--- .../Abp/BlobStoring/Minio/MinioBlobProvider.cs | 13 +++---------- .../Volo/Abp/BlobStoring/BlobContainer.cs | 13 ++----------- .../Volo/Abp/BlobStoring/BlobProviderBase.cs | 14 ++++++++++++++ 7 files changed, 23 insertions(+), 33 deletions(-) diff --git a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProvider.cs b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProvider.cs index 26e83ad4a5..14439cce0c 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProvider.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProvider.cs @@ -86,9 +86,7 @@ namespace Volo.Abp.BlobStoring.Aliyun 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) diff --git a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProvider.cs b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProvider.cs index a922da2222..8d9a00eeb6 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProvider.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aws/Volo/Abp/BlobStoring/Aws/AwsBlobProvider.cs @@ -103,9 +103,7 @@ namespace Volo.Abp.BlobStoring.Aws Key = blobName }); - var memoryStream = new MemoryStream(); - await response.ResponseStream.CopyToAsync(memoryStream); - return memoryStream; + return await TryCopyToMemoryStreamAsync(response.ResponseStream, args.CancellationToken); } } diff --git a/framework/src/Volo.Abp.BlobStoring.Azure/Volo/Abp/BlobStoring/Azure/AzureBlobProvider.cs b/framework/src/Volo.Abp.BlobStoring.Azure/Volo/Abp/BlobStoring/Azure/AzureBlobProvider.cs index 34181f2427..7ab14878dd 100644 --- a/framework/src/Volo.Abp.BlobStoring.Azure/Volo/Abp/BlobStoring/Azure/AzureBlobProvider.cs +++ b/framework/src/Volo.Abp.BlobStoring.Azure/Volo/Abp/BlobStoring/Azure/AzureBlobProvider.cs @@ -67,9 +67,7 @@ namespace Volo.Abp.BlobStoring.Azure 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) diff --git a/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs index cbfe4bcaf7..69d83aa3e5 100644 --- a/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs +++ b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs @@ -74,9 +74,7 @@ namespace Volo.Abp.BlobStoring.FileSystem { using (var fileStream = File.OpenRead(filePath)) { - var memoryStream = new MemoryStream(); - await fileStream.CopyToAsync(memoryStream, args.CancellationToken); - return memoryStream; + return await TryCopyToMemoryStreamAsync(fileStream, args.CancellationToken); } }); } diff --git a/framework/src/Volo.Abp.BlobStoring.Minio/Volo/Abp/BlobStoring/Minio/MinioBlobProvider.cs b/framework/src/Volo.Abp.BlobStoring.Minio/Volo/Abp/BlobStoring/Minio/MinioBlobProvider.cs index e0f52fffda..9587aeffc3 100644 --- a/framework/src/Volo.Abp.BlobStoring.Minio/Volo/Abp/BlobStoring/Minio/MinioBlobProvider.cs +++ b/framework/src/Volo.Abp.BlobStoring.Minio/Volo/Abp/BlobStoring/Minio/MinioBlobProvider.cs @@ -75,20 +75,13 @@ namespace Volo.Abp.BlobStoring.Minio 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) diff --git a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs index 008f1ec2cb..c4d45b840a 100644 --- a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs +++ b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs @@ -194,14 +194,14 @@ namespace Volo.Abp.BlobStoring 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) ) - )); + ); } } @@ -214,14 +214,5 @@ namespace Volo.Abp.BlobStoring return CurrentTenant.Id; } - - protected virtual Stream TrySeekStreamToBegin(Stream stream) - { - if (stream != null && stream.CanSeek) - { - stream.Seek(0, SeekOrigin.Begin); - } - return stream; - } } } diff --git a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderBase.cs b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderBase.cs index ea2bd1ed72..2bf677d0e8 100644 --- a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderBase.cs +++ b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderBase.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Threading; using System.Threading.Tasks; namespace Volo.Abp.BlobStoring @@ -12,5 +13,18 @@ namespace Volo.Abp.BlobStoring public abstract Task ExistsAsync(BlobProviderExistsArgs args); public abstract Task GetOrNullAsync(BlobProviderGetArgs args); + + protected virtual async Task 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; + } } }