Skip to content

Commit 7560489

Browse files
committed
multipart works
1 parent ac8f457 commit 7560489

File tree

11 files changed

+244
-9
lines changed

11 files changed

+244
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,6 @@ ModelManifest.xml
240240

241241
# FAKE - F# Make
242242
.fake/
243+
244+
# DB
245+
musicdb.sqlite

src/NetCoreStack.Proxy/DefaultProxyContentStreamProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ protected virtual StringContent SerializeToString(object value)
2424
return new StringContent(JsonConvert.SerializeObject(value), Encoding.UTF8, "application/json");
2525
}
2626

27+
protected virtual MultipartFormDataContent GetMultipartFormDataContent()
28+
{
29+
MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();
30+
31+
return multipartFormDataContent;
32+
}
33+
2734
protected virtual HttpContent CreateHttpContent(object value)
2835
{
2936
HttpContent content;

src/NetCoreStack.Proxy/Extensions/DictionaryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void Merge(this IDictionary<string, object> instance, IDictionary<
3535
}
3636
}
3737

38-
public static void MergeArgs(this IDictionary<string, object> dictionary, object[] args, ParameterDescriptor[] parameters)
38+
public static void MergeArgs(this IDictionary<string, object> dictionary, object[] args, ProxyParameterDescriptor[] parameters)
3939
{
4040
if (args.Length == 0)
4141
return;

src/NetCoreStack.Proxy/Internal/DefaultProxyTypeManager.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Mvc.Abstractions;
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc.Abstractions;
23
using Microsoft.AspNetCore.Mvc.ModelBinding;
34
using NetCoreStack.Contracts;
45
using NetCoreStack.Proxy.Extensions;
@@ -88,13 +89,16 @@ private IList<ProxyDescriptor> GetProxyDescriptors()
8889
proxyMethodDescriptor.HttpMethod = HttpMethod.Get;
8990
}
9091

91-
proxyMethodDescriptor.Parameters = new List<ParameterDescriptor>();
92+
proxyMethodDescriptor.Parameters = new List<ProxyParameterDescriptor>();
9293
foreach (var parameter in method.GetParameters())
9394
{
94-
proxyMethodDescriptor.Parameters.Add(new ParameterDescriptor()
95+
var parameterType = parameter.ParameterType;
96+
var properties = parameterType.GetProperties().ToList();
97+
98+
proxyMethodDescriptor.Parameters.Add(new ProxyParameterDescriptor(properties)
9599
{
96100
Name = parameter.Name,
97-
ParameterType = parameter.ParameterType,
101+
ParameterType = parameterType,
98102
BindingInfo = BindingInfo.GetBindingInfo(parameter.GetCustomAttributes().OfType<object>())
99103
});
100104
}

src/NetCoreStack.Proxy/ProxyMethodDescriptor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.AspNetCore.Mvc;
2-
using Microsoft.AspNetCore.Mvc.Abstractions;
32
using NetCoreStack.Contracts;
43
using NetCoreStack.Proxy.Extensions;
54
using System;
@@ -23,7 +22,7 @@ public class ProxyMethodDescriptor
2322

2423
public TimeSpan? Timeout { get; set; }
2524

26-
public List<ParameterDescriptor> Parameters { get; set; }
25+
public List<ProxyParameterDescriptor> Parameters { get; set; }
2726

2827
public bool IsVoidReturn { get; }
2928
public bool IsTaskReturn { get; }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc.Abstractions;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
7+
namespace NetCoreStack.Proxy
8+
{
9+
public class ProxyParameterDescriptor : ParameterDescriptor
10+
{
11+
public List<PropertyInfo> Properties { get; }
12+
13+
public bool EnsureMultipartFormData { get; }
14+
15+
16+
17+
public ProxyParameterDescriptor(List<PropertyInfo> properties)
18+
{
19+
Properties = properties;
20+
EnsureMultipartFormData = Properties
21+
.Where(p => typeof(IFormFile).IsAssignableFrom(p.PropertyType) ||
22+
typeof(byte[]).IsAssignableFrom(p.PropertyType)).Any();
23+
}
24+
}
25+
}

test/NetCoreStack.Api.Hosting/Controllers/AlbumController.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Http.Internal;
2+
using Microsoft.AspNetCore.Mvc;
23
using NetCoreStack.Contracts;
34
using NetCoreStack.Data.Interfaces;
45
using NetCoreStack.Domain.Contracts;
56
using NetCoreStack.Domain.Contracts.ApiContracts;
67
using NetCoreStack.Mvc.Extensions;
8+
using System.IO;
79
using System.Linq;
10+
using System.Text;
811
using System.Threading.Tasks;
912

1013
namespace NetCoreStack.WebClient.Hosting.Controllers
@@ -99,6 +102,43 @@ public async Task<AlbumViewModel> SaveAlbum([FromBody]AlbumViewModel model)
99102
return model;
100103
}
101104

105+
[HttpPost(nameof(SaveAlbumSubmit))]
106+
public async Task<AlbumViewModel> SaveAlbumSubmit(AlbumViewModelSubmit model)
107+
{
108+
await Task.CompletedTask;
109+
var req = HttpContext.Request;
110+
req.EnableRewind();
111+
112+
// Arguments: Stream, Encoding, detect encoding, buffer size
113+
// AND, the most important: keep stream opened
114+
using (StreamReader reader = new StreamReader(req.Body, Encoding.UTF8, true, 1024, true))
115+
{
116+
var bodyStr = reader.ReadToEnd();
117+
}
118+
119+
long id = 0;
120+
var objectState = ObjectState.Added;
121+
if (!model.IsNew)
122+
{
123+
id = model.Id;
124+
objectState = ObjectState.Modified;
125+
}
126+
127+
var album = new Album
128+
{
129+
AlbumArtUrl = model.AlbumArtUrl,
130+
ArtistId = model.ArtistId,
131+
GenreId = model.GenreId,
132+
Id = id,
133+
ObjectState = objectState,
134+
Price = model.Price,
135+
Title = model.Title
136+
};
137+
138+
_unitOfWork.Repository<Album>().SaveAllChanges(album);
139+
return model;
140+
}
141+
102142
[HttpPut(nameof(UpdateAlbum))]
103143
public async Task<AlbumViewModel> UpdateAlbum(long id, [FromBody]AlbumViewModel model)
104144
{

test/NetCoreStack.Domain.Contracts/AlbumViewModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NetCoreStack.Contracts;
1+
using Microsoft.AspNetCore.Http;
2+
using NetCoreStack.Contracts;
23
using System.ComponentModel.DataAnnotations;
34

45
namespace NetCoreStack.Domain.Contracts
@@ -48,4 +49,9 @@ public class AlbumBsonViewModel
4849
[PropertyDescriptor(EnableFilter = true)]
4950
public string AlbumArtUrl { get; set; }
5051
}
52+
53+
public class AlbumViewModelSubmit : AlbumViewModel
54+
{
55+
public IFormFile Image { get; set; }
56+
}
5157
}

test/NetCoreStack.Domain.Contracts/ApiContracts/IAlbumApi.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public interface IAlbumApi : IApiContract
1313
[HttpPostMarker]
1414
Task<AlbumViewModel> SaveAlbum(AlbumViewModel model);
1515

16+
[HttpPostMarker]
17+
Task<AlbumViewModel> SaveAlbumSubmit(AlbumViewModelSubmit model);
18+
1619
[HttpPutMarker]
1720
Task<AlbumViewModel> UpdateAlbum(long id, AlbumViewModel model);
1821

test/NetCoreStack.WebClient.Hosting/Controllers/HomeController.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public async Task<IActionResult> Index()
4848
return View();
4949
}
5050

51+
public async Task<IActionResult> IndexSubmit()
52+
{
53+
await IndexInitializer();
54+
return View();
55+
}
56+
5157
public async Task<IActionResult> IndexMongo()
5258
{
5359
await IndexInitializer();
@@ -80,6 +86,13 @@ public async Task<IActionResult> SaveAlbum([FromBody]AlbumViewModel model)
8086
return Ok();
8187
}
8288

89+
[HttpPost]
90+
public async Task<IActionResult> SaveAlbumSubmit(AlbumViewModelSubmit model)
91+
{
92+
var albumCollection = await _albumApi.SaveAlbumSubmit(model);
93+
return Ok();
94+
}
95+
8396
[HttpPost]
8497
public async Task<IActionResult> UpdateAlbum()
8598
{

0 commit comments

Comments
 (0)