Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization of Stream and Resource Management #21631

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public override Task<bool> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public override async Task<bool> ExistsAsync(BlobProviderExistsArgs args)
Key = blobName
});

return await TryCopyToMemoryStreamAsync(response.ResponseStream, args.CancellationToken);
return response.ResponseStream;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public override async Task<bool> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,7 @@ public override Task<bool> ExistsAsync(BlobProviderExistsArgs args)

return await Policy.Handle<IOException>()
.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<bool> ExistsAsync(string filePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,7 +29,7 @@ public virtual async Task<ImageCompressResult<Stream>> TryCompressAsync(
return new ImageCompressResult<Stream>(stream, ImageProcessState.Unsupported);
}

var image = await Image.LoadAsync(stream, cancellationToken);
using var image = await Image.LoadAsync(stream, cancellationToken);

if (!CanCompress(image.Metadata.DecodedImageFormat!.DefaultMimeType))
{
Expand All @@ -46,7 +43,7 @@ public virtual async Task<ImageCompressResult<Stream>> TryCompressAsync(
return new ImageCompressResult<Stream>(memoryStream, ImageProcessState.Done);
}

memoryStream.Dispose();
await memoryStream.DisposeAsync();
return new ImageCompressResult<Stream>(stream, ImageProcessState.Canceled);
}

Expand All @@ -69,7 +66,7 @@ public virtual async Task<ImageCompressResult<byte[]>> TryCompressAsync(
}

var newBytes = await result.Result.GetAllBytesAsync(cancellationToken);
result.Result.Dispose();
await result.Result.DisposeAsync();
return new ImageCompressResult<byte[]>(newBytes, result.State);
}

Expand Down Expand Up @@ -100,7 +97,7 @@ protected virtual async Task<Stream> GetStreamFromImageAsync(
}
catch
{
memoryStream.Dispose();
await memoryStream.DisposeAsync();
throw;
}
}
Expand All @@ -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}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public virtual async Task<ImageResizeResult<Stream>> TryResizeAsync(
return new ImageResizeResult<Stream>(stream, ImageProcessState.Unsupported);
}

var image = await Image.LoadAsync(stream, cancellationToken);
using var image = await Image.LoadAsync(stream, cancellationToken);

if (!CanResize(image.Metadata.DecodedImageFormat!.DefaultMimeType))
{
Expand All @@ -49,7 +49,7 @@ public virtual async Task<ImageResizeResult<Stream>> TryResizeAsync(
}
catch
{
memoryStream.Dispose();
await memoryStream.DisposeAsync();
throw;
}
}
Expand All @@ -76,7 +76,7 @@ public virtual async Task<ImageResizeResult<byte[]>> TryResizeAsync(

var newBytes = await result.Result.GetAllBytesAsync(cancellationToken);

result.Result.Dispose();
await result.Result.DisposeAsync();

return new ImageResizeResult<byte[]>(newBytes, result.State);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@ public virtual async Task<ImageResizeResult<byte[]>> TryResizeAsync(byte[] bytes
return new ImageResizeResult<byte[]>(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<byte[]>(bytes, result.State);
}
if (result.State != ImageProcessState.Done)
{
return new ImageResizeResult<byte[]>(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<byte[]>(newBytes, result.State);
}
return new ImageResizeResult<byte[]>(newBytes, result.State);
}

public virtual async Task<ImageResizeResult<Stream>> TryResizeAsync(Stream stream, ImageResizeArgs resizeArgs, string? mimeType = null, CancellationToken cancellationToken = default)
Expand All @@ -51,21 +49,14 @@ public virtual async Task<ImageResizeResult<Stream>> 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<Stream>(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<Stream>(memoryStream, ImageProcessState.Done);
}

protected virtual async Task<(MemoryStream, MemoryStream)> CreateMemoryStream(Stream stream)
Expand Down
Loading