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

Refactor IRemoteStreamContent related functions. #7460

Merged
merged 4 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.Mvc.Auditing;
using Volo.Abp.AspNetCore.Mvc.Content;
using Volo.Abp.AspNetCore.Mvc.ContentFormatters;
using Volo.Abp.AspNetCore.Mvc.Conventions;
using Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
using Volo.Abp.AspNetCore.Mvc.Features;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Threading.Tasks;
using Microsoft.Net.Http.Headers;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Net.Http.Headers;
using Volo.Abp.Content;

namespace Volo.Abp.AspNetCore.Mvc.Content
namespace Volo.Abp.AspNetCore.Mvc.ContentFormatters
{
public class RemoteStreamContentInputFormatter : InputFormatter
{
Expand All @@ -20,9 +20,10 @@ protected override bool CanReadType(Type type)

public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
return InputFormatterResult.SuccessAsync(
new InternalRemoteStreamContent(context.HttpContext)
);
return InputFormatterResult.SuccessAsync(new RemoteStreamContent(context.HttpContext.Request.Body)
{
ContentType = context.HttpContext.Request.ContentType
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.Net.Http.Headers;
using Volo.Abp.Content;

namespace Volo.Abp.AspNetCore.Mvc.Content
namespace Volo.Abp.AspNetCore.Mvc.ContentFormatters
{
public class RemoteStreamContentOutputFormatter : OutputFormatter
{
Expand All @@ -21,9 +21,16 @@ protected override bool CanWriteType(Type type)
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
var remoteStream = (IRemoteStreamContent)context.Object;
using (var stream = remoteStream.GetStream())

if (remoteStream != null)
{
await stream.CopyToAsync(context.HttpContext.Response.Body);
context.HttpContext.Response.ContentType = remoteStream.ContentType;

using (var stream = remoteStream.GetStream())
{
stream.Position = 0;
await stream.CopyToAsync(context.HttpContext.Response.Body);
}
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.DynamicProxy;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Http.Client.Content;
using Volo.Abp.Http.Modeling;
using Volo.Abp.Http.ProxyScripting.Generators;
using Volo.Abp.Json;
Expand Down Expand Up @@ -112,7 +111,10 @@ private async Task<T> MakeRequestAndGetResultAsync<T>(IAbpMethodInvocation invoc
/* returning a class that holds a reference to response
* content just to be sure that GC does not dispose of
* it before we finish doing our work with the stream */
return (T)((object)new ReferencedRemoteStreamContent(await responseContent.ReadAsStreamAsync(), responseContent));
return (T)(object)new RemoteStreamContent(await responseContent.ReadAsStreamAsync())
{
ContentType = responseContent.Headers.ContentType?.ToString()
};
}

var stringContent = await responseContent.ReadAsStringAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Shouldly;
using Volo.Abp.Content;

namespace Volo.Abp.AspNetCore.Mvc.ContentFormatters
{
[Route("api/remote-stream-content-test")]
public class RemoteStreamContentTestController : AbpController
{
[HttpGet]
[Route("Download")]
public async Task<IRemoteStreamContent> DownloadAsync()
{
var memoryStream = new MemoryStream();
await memoryStream.WriteAsync(Encoding.UTF8.GetBytes("DownloadAsync"));

return new RemoteStreamContent(memoryStream)
{
ContentType = "application/rtf"
};
}

[HttpPost]
[Route("Upload")]
public async Task<string> UploadAsync([FromBody]IRemoteStreamContent streamContent)
{
using (var reader = new StreamReader(streamContent.GetStream()))
{
return await reader.ReadToEndAsync() + ":" + streamContent.ContentType;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Shouldly;
using Xunit;

namespace Volo.Abp.AspNetCore.Mvc.ContentFormatters
{
public class RemoteStreamContentTestController_Tests : AspNetCoreMvcTestBase
{
[Fact]
public async Task DownloadAsync()
{
var result = await GetResponseAsync("/api/remote-stream-content-test/download");
result.Content.Headers.ContentType?.ToString().ShouldBe("application/rtf");
(await result.Content.ReadAsStringAsync()).ShouldBe("DownloadAsync");
}

[Fact]
public async Task UploadAsync()
{
using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/api/remote-stream-content-test/upload"))
{
var memoryStream = new MemoryStream();
await memoryStream.WriteAsync(Encoding.UTF8.GetBytes("UploadAsync"));
memoryStream.Position = 0;
requestMessage.Content = new StreamContent(memoryStream);
requestMessage.Content.Headers.Add("Content-Type", "application/rtf");

var response = await Client.SendAsync(requestMessage);

(await response.Content.ReadAsStringAsync()).ShouldBe("UploadAsync:application/rtf");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute.Extensions;
using Shouldly;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Content;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Http.Client;
using Volo.Abp.TestApp.Application;
Expand Down Expand Up @@ -168,5 +171,31 @@ public async Task GetWithComplexType()
result.Inner1.Value2.ShouldBe("value two");
result.Inner1.Inner2.Value3.ShouldBe("value three");
}

[Fact]
public async Task DownloadAsync()
{
var result = await _peopleAppService.DownloadAsync();

result.ContentType.ShouldBe("application/rtf");
using (var reader = new StreamReader(result.GetStream()))
{
var str = await reader.ReadToEndAsync();
str.ShouldBe("DownloadAsync");
}
}

[Fact]
public async Task UploadAsync()
{
var memoryStream = new MemoryStream();
await memoryStream.WriteAsync(Encoding.UTF8.GetBytes("UploadAsync"));
memoryStream.Position = 0;
var result = await _peopleAppService.UploadAsync(new RemoteStreamContent(memoryStream)
{
ContentType = "application/rtf"
});
result.ShouldBe("UploadAsync:application/rtf");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Content;
using Volo.Abp.TestApp.Application.Dto;

namespace Volo.Abp.TestApp.Application
Expand All @@ -20,5 +21,9 @@ public interface IPeopleAppService : ICrudAppService<PersonDto, Guid>
Task GetWithAuthorized();

Task<GetWithComplexTypeInput> GetWithComplexType(GetWithComplexTypeInput input);

Task<IRemoteStreamContent> DownloadAsync();

Task<string> UploadAsync(IRemoteStreamContent streamContent);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Dtos;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Services;
using Volo.Abp.Content;
using Volo.Abp.TestApp.Application.Dto;

namespace Volo.Abp.TestApp.Application
Expand Down Expand Up @@ -64,5 +67,24 @@ public Task<GetWithComplexTypeInput> GetWithComplexType(GetWithComplexTypeInput
{
return Task.FromResult(input);
}

public async Task<IRemoteStreamContent> DownloadAsync()
{
var memoryStream = new MemoryStream();
await memoryStream.WriteAsync(Encoding.UTF8.GetBytes("DownloadAsync"));

return new RemoteStreamContent(memoryStream)
{
ContentType = "application/rtf"
};
}

public async Task<string> UploadAsync(IRemoteStreamContent streamContent)
{
using (var reader = new StreamReader(streamContent.GetStream()))
{
return await reader.ReadToEndAsync() + ":" + streamContent.ContentType;
}
}
}
}