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 1c04fdb7554..d20c8d1214d 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,7 +86,7 @@ public override Task ExistsAsync(BlobProviderExistsArgs args) return null; } var result = ossClient.GetObject(containerName, blobName); - return await TryCopyToMemoryStreamAsync(result.Content, args.CancellationToken); + return result.ResponseStream; } 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 00a82a0f3e8..7c520da4719 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,7 +103,7 @@ public override async Task ExistsAsync(BlobProviderExistsArgs args) Key = blobName }); - return await TryCopyToMemoryStreamAsync(response.ResponseStream, args.CancellationToken); + return response.ResponseStream; } } 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 5d45e749ea7..bea55c624de 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 @@ -66,8 +66,7 @@ public override async Task ExistsAsync(BlobProviderExistsArgs args) } var blobClient = GetBlobClient(args, blobName); - var download = await blobClient.DownloadAsync(); - return await TryCopyToMemoryStreamAsync(download.Value.Content, args.CancellationToken); + return await blobClient.OpenReadAsync(cancellationToken: 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 a221441749d..2a410005dbe 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 @@ -70,13 +70,7 @@ public override Task ExistsAsync(BlobProviderExistsArgs args) return await Policy.Handle() .WaitAndRetryAsync(2, retryCount => TimeSpan.FromSeconds(retryCount)) - .ExecuteAsync(async () => - { - using (var fileStream = File.OpenRead(filePath)) - { - return await TryCopyToMemoryStreamAsync(fileStream, args.CancellationToken); - } - }); + .ExecuteAsync(() => Task.FromResult(File.OpenRead(filePath))); } protected virtual Task ExistsAsync(string filePath) diff --git a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageCompressorContributor.cs b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageCompressorContributor.cs index 9a2f53b9887..da6b75b4b80 100644 --- a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageCompressorContributor.cs +++ b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageCompressorContributor.cs @@ -5,9 +5,6 @@ using Microsoft.Extensions.Options; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.Formats.Jpeg; -using SixLabors.ImageSharp.Formats.Png; -using SixLabors.ImageSharp.Formats.Webp; using Volo.Abp.DependencyInjection; using Volo.Abp.Http; @@ -32,7 +29,7 @@ public virtual async Task> TryCompressAsync( return new ImageCompressResult(stream, ImageProcessState.Unsupported); } - var image = await Image.LoadAsync(stream, cancellationToken); + using var image = await Image.LoadAsync(stream, cancellationToken); if (!CanCompress(image.Metadata.DecodedImageFormat!.DefaultMimeType)) { @@ -46,7 +43,7 @@ public virtual async Task> TryCompressAsync( return new ImageCompressResult(memoryStream, ImageProcessState.Done); } - memoryStream.Dispose(); + await memoryStream.DisposeAsync(); return new ImageCompressResult(stream, ImageProcessState.Canceled); } @@ -69,7 +66,7 @@ public virtual async Task> TryCompressAsync( } var newBytes = await result.Result.GetAllBytesAsync(cancellationToken); - result.Result.Dispose(); + await result.Result.DisposeAsync(); return new ImageCompressResult(newBytes, result.State); } @@ -100,7 +97,7 @@ protected virtual async Task GetStreamFromImageAsync( } catch { - memoryStream.Dispose(); + await memoryStream.DisposeAsync(); throw; } } @@ -110,11 +107,11 @@ protected virtual IImageEncoder GetEncoder(IImageFormat format) switch (format.DefaultMimeType) { case MimeTypes.Image.Jpeg: - return Options.JpegEncoder ?? new JpegEncoder(); + return Options.JpegEncoder; case MimeTypes.Image.Png: - return Options.PngEncoder ?? new PngEncoder(); + return Options.PngEncoder; case MimeTypes.Image.Webp: - return Options.WebpEncoder ?? new WebpEncoder(); + return Options.WebpEncoder; default: throw new NotSupportedException($"No encoder available for the given format: {format.Name}"); } diff --git a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageResizerContributor.cs b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageResizerContributor.cs index 6e17ddc9a80..e52155eaeb8 100644 --- a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageResizerContributor.cs +++ b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageResizerContributor.cs @@ -23,7 +23,7 @@ public virtual async Task> TryResizeAsync( return new ImageResizeResult(stream, ImageProcessState.Unsupported); } - var image = await Image.LoadAsync(stream, cancellationToken); + using var image = await Image.LoadAsync(stream, cancellationToken); if (!CanResize(image.Metadata.DecodedImageFormat!.DefaultMimeType)) { @@ -49,7 +49,7 @@ public virtual async Task> TryResizeAsync( } catch { - memoryStream.Dispose(); + await memoryStream.DisposeAsync(); throw; } } @@ -76,7 +76,7 @@ public virtual async Task> TryResizeAsync( var newBytes = await result.Result.GetAllBytesAsync(cancellationToken); - result.Result.Dispose(); + await result.Result.DisposeAsync(); return new ImageResizeResult(newBytes, result.State); } diff --git a/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageResizerContributor.cs b/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageResizerContributor.cs index 760acd0916b..0ba2c7c8335 100644 --- a/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageResizerContributor.cs +++ b/framework/src/Volo.Abp.Imaging.SkiaSharp/Volo/Abp/Imaging/SkiaSharpImageResizerContributor.cs @@ -25,21 +25,19 @@ public virtual async Task> TryResizeAsync(byte[] bytes return new ImageResizeResult(bytes, ImageProcessState.Unsupported); } - using (var memoryStream = new MemoryStream(bytes)) - { - var result = await TryResizeAsync(memoryStream, resizeArgs, mimeType, cancellationToken); + using var memoryStream = new MemoryStream(bytes); + var result = await TryResizeAsync(memoryStream, resizeArgs, mimeType, cancellationToken); - if (result.State != ImageProcessState.Done) - { - return new ImageResizeResult(bytes, result.State); - } + if (result.State != ImageProcessState.Done) + { + return new ImageResizeResult(bytes, result.State); + } - var newBytes = await result.Result.GetAllBytesAsync(cancellationToken); + var newBytes = await result.Result.GetAllBytesAsync(cancellationToken); - result.Result.Dispose(); + result.Result.Dispose(); - return new ImageResizeResult(newBytes, result.State); - } + return new ImageResizeResult(newBytes, result.State); } public virtual async Task> TryResizeAsync(Stream stream, ImageResizeArgs resizeArgs, string? mimeType = null, CancellationToken cancellationToken = default) @@ -51,21 +49,14 @@ public virtual async Task> TryResizeAsync(Stream strea var (memoryBitmapStream, memorySkCodecStream) = await CreateMemoryStream(stream); - using (var original = SKBitmap.Decode(memoryBitmapStream)) - { - using (var resized = original.Resize(new SKImageInfo(resizeArgs.Width, resizeArgs.Height), Options.SKFilterQuality)) - { - using (var image = SKImage.FromBitmap(resized)) - { - using (var codec = SKCodec.Create(memorySkCodecStream)) - { - var memoryStream = new MemoryStream(); - image.Encode(codec.EncodedFormat, Options.Quality).SaveTo(memoryStream); - return new ImageResizeResult(memoryStream, ImageProcessState.Done); - } - } - } - } + using var original = SKBitmap.Decode(memoryBitmapStream); + using var resized = original.Resize(new SKImageInfo(resizeArgs.Width, resizeArgs.Height), Options.SKFilterQuality); + using var image = SKImage.FromBitmap(resized); + using var codec = SKCodec.Create(memorySkCodecStream); + var memoryStream = new MemoryStream(); + using var skData = image.Encode(codec.EncodedFormat, Options.Quality); + skData.SaveTo(memoryStream); + return new ImageResizeResult(memoryStream, ImageProcessState.Done); } protected virtual async Task<(MemoryStream, MemoryStream)> CreateMemoryStream(Stream stream)