Browse Source

Reverse the contributors so that the services added later are executed first.

pull/23497/head
maliming 1 year ago
parent
commit
e96634e07f
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 30
      framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs
  2. 54
      framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs

30
framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs

@ -12,9 +12,9 @@ namespace Volo.Abp.Imaging;
public class ImageCompressor : IImageCompressor, ITransientDependency public class ImageCompressor : IImageCompressor, ITransientDependency
{ {
protected IEnumerable<IImageCompressorContributor> ImageCompressorContributors { get; } protected IEnumerable<IImageCompressorContributor> ImageCompressorContributors { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; } protected ICancellationTokenProvider CancellationTokenProvider { get; }
public ImageCompressor(IEnumerable<IImageCompressorContributor> imageCompressorContributors, ICancellationTokenProvider cancellationTokenProvider) public ImageCompressor(IEnumerable<IImageCompressorContributor> imageCompressorContributors, ICancellationTokenProvider cancellationTokenProvider)
{ {
ImageCompressorContributors = imageCompressorContributors.Reverse(); ImageCompressorContributors = imageCompressorContributors.Reverse();
@ -27,12 +27,12 @@ public class ImageCompressor : IImageCompressor, ITransientDependency
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
Check.NotNull(stream, nameof(stream)); Check.NotNull(stream, nameof(stream));
if(!stream.CanRead) if(!stream.CanRead)
{ {
return new ImageCompressResult<Stream>(stream, ImageProcessState.Unsupported); return new ImageCompressResult<Stream>(stream, ImageProcessState.Unsupported);
} }
if(!stream.CanSeek) if(!stream.CanSeek)
{ {
var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
@ -41,12 +41,12 @@ public class ImageCompressor : IImageCompressor, ITransientDependency
stream = memoryStream; stream = memoryStream;
} }
foreach (var imageCompressorContributor in ImageCompressorContributors) foreach (var imageCompressorContributor in ImageCompressorContributors.Reverse())
{ {
var result = await imageCompressorContributor.TryCompressAsync(stream, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken)); var result = await imageCompressorContributor.TryCompressAsync(stream, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken));
SeekToBegin(stream); SeekToBegin(stream);
if (result.State == ImageProcessState.Unsupported) if (result.State == ImageProcessState.Unsupported)
{ {
continue; continue;
@ -54,7 +54,7 @@ public class ImageCompressor : IImageCompressor, ITransientDependency
return result; return result;
} }
return new ImageCompressResult<Stream>(stream, ImageProcessState.Unsupported); return new ImageCompressResult<Stream>(stream, ImageProcessState.Unsupported);
} }
@ -64,22 +64,22 @@ public class ImageCompressor : IImageCompressor, ITransientDependency
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
Check.NotNull(bytes, nameof(bytes)); Check.NotNull(bytes, nameof(bytes));
foreach (var imageCompressorContributor in ImageCompressorContributors) foreach (var imageCompressorContributor in ImageCompressorContributors.Reverse())
{ {
var result = await imageCompressorContributor.TryCompressAsync(bytes, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken)); var result = await imageCompressorContributor.TryCompressAsync(bytes, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken));
if (result.State == ImageProcessState.Unsupported) if (result.State == ImageProcessState.Unsupported)
{ {
continue; continue;
} }
return result; return result;
} }
return new ImageCompressResult<byte[]>(bytes, ImageProcessState.Unsupported); return new ImageCompressResult<byte[]>(bytes, ImageProcessState.Unsupported);
} }
protected virtual void SeekToBegin(Stream stream) protected virtual void SeekToBegin(Stream stream)
{ {
if (stream.CanSeek) if (stream.CanSeek)
@ -87,4 +87,4 @@ public class ImageCompressor : IImageCompressor, ITransientDependency
stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin);
} }
} }
} }

54
framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs

@ -13,36 +13,36 @@ namespace Volo.Abp.Imaging;
public class ImageResizer : IImageResizer, ITransientDependency public class ImageResizer : IImageResizer, ITransientDependency
{ {
protected IEnumerable<IImageResizerContributor> ImageResizerContributors { get; } protected IEnumerable<IImageResizerContributor> ImageResizerContributors { get; }
protected ImageResizeOptions ImageResizeOptions { get; } protected ImageResizeOptions ImageResizeOptions { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; } protected ICancellationTokenProvider CancellationTokenProvider { get; }
public ImageResizer( public ImageResizer(
IEnumerable<IImageResizerContributor> imageResizerContributors, IEnumerable<IImageResizerContributor> imageResizerContributors,
IOptions<ImageResizeOptions> imageResizeOptions, IOptions<ImageResizeOptions> imageResizeOptions,
ICancellationTokenProvider cancellationTokenProvider) ICancellationTokenProvider cancellationTokenProvider)
{ {
ImageResizerContributors = imageResizerContributors.Reverse(); ImageResizerContributors = imageResizerContributors.Reverse();
CancellationTokenProvider = cancellationTokenProvider; CancellationTokenProvider = cancellationTokenProvider;
ImageResizeOptions = imageResizeOptions.Value; ImageResizeOptions = imageResizeOptions.Value;
} }
public virtual async Task<ImageResizeResult<Stream>> ResizeAsync( public virtual async Task<ImageResizeResult<Stream>> ResizeAsync(
[NotNull] Stream stream, [NotNull] Stream stream,
ImageResizeArgs resizeArgs, ImageResizeArgs resizeArgs,
string? mimeType = null, string? mimeType = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
Check.NotNull(stream, nameof(stream)); Check.NotNull(stream, nameof(stream));
ChangeDefaultResizeMode(resizeArgs); ChangeDefaultResizeMode(resizeArgs);
if(!stream.CanRead) if(!stream.CanRead)
{ {
return new ImageResizeResult<Stream>(stream, ImageProcessState.Unsupported); return new ImageResizeResult<Stream>(stream, ImageProcessState.Unsupported);
} }
if(!stream.CanSeek) if(!stream.CanSeek)
{ {
var memoryStream = new MemoryStream(); var memoryStream = new MemoryStream();
@ -50,13 +50,13 @@ public class ImageResizer : IImageResizer, ITransientDependency
SeekToBegin(memoryStream); SeekToBegin(memoryStream);
stream = memoryStream; stream = memoryStream;
} }
foreach (var imageResizerContributor in ImageResizerContributors) foreach (var imageResizerContributor in ImageResizerContributors.Reverse())
{ {
var result = await imageResizerContributor.TryResizeAsync(stream, resizeArgs, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken)); var result = await imageResizerContributor.TryResizeAsync(stream, resizeArgs, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken));
SeekToBegin(stream); SeekToBegin(stream);
if (result.State == ImageProcessState.Unsupported) if (result.State == ImageProcessState.Unsupported)
{ {
continue; continue;
@ -64,24 +64,24 @@ public class ImageResizer : IImageResizer, ITransientDependency
return result; return result;
} }
return new ImageResizeResult<Stream>(stream, ImageProcessState.Unsupported); return new ImageResizeResult<Stream>(stream, ImageProcessState.Unsupported);
} }
public virtual async Task<ImageResizeResult<byte[]>> ResizeAsync( public virtual async Task<ImageResizeResult<byte[]>> ResizeAsync(
[NotNull] byte[] bytes, [NotNull] byte[] bytes,
ImageResizeArgs resizeArgs, ImageResizeArgs resizeArgs,
string? mimeType = null, string? mimeType = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
Check.NotNull(bytes, nameof(bytes)); Check.NotNull(bytes, nameof(bytes));
ChangeDefaultResizeMode(resizeArgs); ChangeDefaultResizeMode(resizeArgs);
foreach (var imageResizerContributor in ImageResizerContributors) foreach (var imageResizerContributor in ImageResizerContributors.Reverse())
{ {
var result = await imageResizerContributor.TryResizeAsync(bytes, resizeArgs, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken)); var result = await imageResizerContributor.TryResizeAsync(bytes, resizeArgs, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken));
if (result.State == ImageProcessState.Unsupported) if (result.State == ImageProcessState.Unsupported)
{ {
continue; continue;
@ -89,10 +89,10 @@ public class ImageResizer : IImageResizer, ITransientDependency
return result; return result;
} }
return new ImageResizeResult<byte[]>(bytes, ImageProcessState.Unsupported); return new ImageResizeResult<byte[]>(bytes, ImageProcessState.Unsupported);
} }
protected virtual void ChangeDefaultResizeMode(ImageResizeArgs resizeArgs) protected virtual void ChangeDefaultResizeMode(ImageResizeArgs resizeArgs)
{ {
if (resizeArgs.Mode == ImageResizeMode.Default) if (resizeArgs.Mode == ImageResizeMode.Default)
@ -100,7 +100,7 @@ public class ImageResizer : IImageResizer, ITransientDependency
resizeArgs.Mode = ImageResizeOptions.DefaultResizeMode; resizeArgs.Mode = ImageResizeOptions.DefaultResizeMode;
} }
} }
protected virtual void SeekToBegin(Stream stream) protected virtual void SeekToBegin(Stream stream)
{ {
if (stream.CanSeek) if (stream.CanSeek)
@ -108,4 +108,4 @@ public class ImageResizer : IImageResizer, ITransientDependency
stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin);
} }
} }
} }

Loading…
Cancel
Save