From 7a503833fe88987485a4e9284dd92d4dad09924d Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 30 Mar 2021 11:08:55 +0800 Subject: [PATCH 1/2] Add RemoteStreamContent class type to RemoteStreamContentInputFormatter. --- .../Mvc/ContentFormatters/RemoteStreamContentInputFormatter.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentInputFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentInputFormatter.cs index ba07e28f77..340544f79d 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentInputFormatter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentInputFormatter.cs @@ -15,7 +15,8 @@ namespace Volo.Abp.AspNetCore.Mvc.ContentFormatters protected override bool CanReadType(Type type) { - return typeof(IRemoteStreamContent) == type; + return type == typeof(IRemoteStreamContent) || + type == typeof(RemoteStreamContent); } public override Task ReadRequestBodyAsync(InputFormatterContext context) From 2e9b3e6d19a1fa0f297dda8bb17b3ad69d305f9d Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 30 Mar 2021 12:00:19 +0800 Subject: [PATCH 2/2] Check stream CanSeek before change Position. --- .../RemoteStreamContentOutputFormatter.cs | 6 +++++- .../System/IO/AbpStreamExtensions.cs | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentOutputFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentOutputFormatter.cs index 685822a4b6..188306227a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentOutputFormatter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ContentFormatters/RemoteStreamContentOutputFormatter.cs @@ -28,7 +28,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ContentFormatters using (var stream = remoteStream.GetStream()) { - stream.Position = 0; + if (stream.CanSeek) + { + stream.Position = 0; + } + await stream.CopyToAsync(context.HttpContext.Response.Body); } } diff --git a/framework/src/Volo.Abp.Core/System/IO/AbpStreamExtensions.cs b/framework/src/Volo.Abp.Core/System/IO/AbpStreamExtensions.cs index bd85773a9b..62ad5a53b5 100644 --- a/framework/src/Volo.Abp.Core/System/IO/AbpStreamExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/IO/AbpStreamExtensions.cs @@ -9,7 +9,10 @@ namespace System.IO { using (var memoryStream = new MemoryStream()) { - stream.Position = 0; + if (stream.CanSeek) + { + stream.Position = 0; + } stream.CopyTo(memoryStream); return memoryStream.ToArray(); } @@ -19,7 +22,10 @@ namespace System.IO { using (var memoryStream = new MemoryStream()) { - stream.Position = 0; + if (stream.CanSeek) + { + stream.Position = 0; + } await stream.CopyToAsync(memoryStream, cancellationToken); return memoryStream.ToArray(); } @@ -27,7 +33,10 @@ namespace System.IO public static Task CopyToAsync(this Stream stream, Stream destination, CancellationToken cancellationToken) { - stream.Position = 0; + if (stream.CanSeek) + { + stream.Position = 0; + } return stream.CopyToAsync( destination, 81920, //this is already the default value, but needed to set to be able to pass the cancellationToken