Skip to content

Commit 2eb7672

Browse files
committed
Merge branch 'master' of https://github.com/dotnetcore/surging
2 parents a39a06c + 7c8f296 commit 2eb7672

4 files changed

Lines changed: 54 additions & 35 deletions

File tree

src/Surging.Core/Surging.Core.KestrelHttpServer/HttpMessageListener.cs

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ public async Task OnReceived(IMessageSender sender,string messageId, HttpContext
8585
if (context.Request.HasFormContentType)
8686
{
8787
var collection = await GetFormCollection(context.Request);
88-
httpMessage.Parameters.Add("form", collection);
88+
foreach (var item in collection)
89+
{
90+
httpMessage.Parameters.Add(item.Key, item.Value);
91+
}
8992
if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage },
9093
sender, messageId, actionFilters)) return;
9194
httpMessage.Attachments = RestContext.GetContext().GetContextParameters();
@@ -189,59 +192,75 @@ public async Task<bool> OnException(HttpContext context, HttpServerMessageSender
189192
return true;
190193
}
191194

192-
private async Task<HttpFormCollection> GetFormCollection(HttpRequest request)
195+
private async Task<Dictionary<string, HttpFormCollection>> GetFormCollection(HttpRequest request)
193196
{
194-
var boundary = GetName("boundary=", request.ContentType);
197+
var boundary = GetName("boundary=", request.ContentType);
195198
var reader = new MultipartReader(boundary, request.Body);
196199
var collection = await GetMultipartForm(reader);
197-
var fileCollection = new HttpFormFileCollection();
198-
var fields = new Dictionary<string, StringValues>();
199-
foreach (var item in collection)
200+
201+
return collection.ToDictionary(item => item.Key, item =>
200202
{
201-
if (item.Value is HttpFormFileCollection)
203+
var fieldsDict = new Dictionary<string, StringValues>();
204+
if (item.Value.Fields.HasValue)
202205
{
203-
var itemCollection = item.Value as HttpFormFileCollection;
204-
fileCollection.AddRange(itemCollection);
206+
fieldsDict.Add(item.Key, item.Value.Fields.Value);
205207
}
206-
else
207-
{
208-
var itemCollection = item.Value as Dictionary<string, StringValues>;
209-
fields = fields.Concat(itemCollection).ToDictionary(k => k.Key, v => v.Value);
210208

211-
}
212-
}
213-
return new HttpFormCollection(fields, fileCollection);
209+
return new HttpFormCollection(fieldsDict, item.Value.HttpFormFileCollection);
210+
});
214211
}
215212

216-
private async Task<IDictionary<string,object>> GetMultipartForm(MultipartReader reader)
213+
private async Task<IDictionary<string, (StringValues? Fields, HttpFormFileCollection HttpFormFileCollection)>> GetMultipartForm(MultipartReader reader)
217214
{
218-
var section = await reader.ReadNextSectionAsync();
219-
var collection = new Dictionary<string, object>();
215+
var section = await reader.ReadNextSectionAsync();
216+
var collection = new Dictionary<string, (StringValues? Fields, HttpFormFileCollection HttpFormFileCollection)>();
220217
if (section != null)
221-
{
222-
var name=GetName("name=",section.ContentDisposition);
223-
var fileName = GetName("filename=",section.ContentDisposition);
218+
{
219+
var name = GetName("name=", section.ContentDisposition);
220+
var fileName = GetName("filename=", section.ContentDisposition);
224221
var buffer = new MemoryStream();
225222
await section.Body.CopyToAsync(buffer);
226-
if(string.IsNullOrEmpty(fileName))
223+
if (string.IsNullOrEmpty(fileName))
227224
{
228225
var fields = new Dictionary<string, StringValues>();
229226
StreamReader streamReader = new StreamReader(buffer);
230-
fields.Add(name, new StringValues(UTF8Encoding.Default.GetString(buffer.GetBuffer(),0,(int)buffer.Length)));
231-
collection.Add(name, fields);
227+
fields.Add(name, new StringValues(Encoding.Default.GetString(buffer.GetBuffer(), 0, (int)buffer.Length)));
228+
collection.Add(name, (fields[name], null));
232229
}
233230
else
234231
{
235232
var fileCollection = new HttpFormFileCollection();
236233
StreamReader streamReader = new StreamReader(buffer);
237-
fileCollection.Add(new HttpFormFile(buffer.Length,name,fileName,buffer.GetBuffer()));
238-
collection.Add(name, fileCollection);
234+
fileCollection.Add(new HttpFormFile(buffer.Length, name, fileName, buffer.GetBuffer()));
235+
collection.Add(name, (null, fileCollection));
239236
}
240-
var formCollection= await GetMultipartForm(reader);
241-
foreach(var item in formCollection)
237+
var formCollection = await GetMultipartForm(reader);
238+
foreach (var item in formCollection)
242239
{
243240
if (!collection.ContainsKey(item.Key))
244-
collection.Add(item.Key,item.Value);
241+
collection.Add(item.Key, item.Value);
242+
else
243+
{
244+
var (fields, httpFormFileCollection) = collection[item.Key];
245+
if (item.Value.Fields.HasValue && !fields.HasValue)
246+
{
247+
fields = item.Value.Fields.Value;
248+
}
249+
250+
if (httpFormFileCollection == null)
251+
{
252+
httpFormFileCollection = item.Value.HttpFormFileCollection;
253+
}
254+
else
255+
{
256+
var formFiles =
257+
item.Value.HttpFormFileCollection.Where(v =>
258+
!httpFormFileCollection.Exists(p => p.FileName == v.FileName));
259+
httpFormFileCollection.AddRange(formFiles);
260+
}
261+
262+
collection[item.Key] = (fields, httpFormFileCollection);
263+
}
245264
}
246265
}
247266
return collection;

src/Surging.IModuleServices/Surging.IModuleServices.Common/IUserService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public interface IUserService: IServiceKey
164164
/// </summary>
165165
/// <param name="form">HttpFormCollection 类型参数</param>
166166
/// <returns></returns>
167-
Task<bool> UploadFile(HttpFormCollection form);
167+
Task<bool> UploadFile(HttpFormCollection form1);
168168

169169
/// <summary>
170170
/// 测试下载文件

src/Surging.Modules/Surging.Modules.Common/Domain/PersonService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ public Task<string> GetUser(List<int> idList)
122122
return Task.FromResult("type is List<int>");
123123
}
124124

125-
public async Task<bool> UploadFile(HttpFormCollection form)
125+
public async Task<bool> UploadFile(HttpFormCollection form1)
126126
{
127-
var files = form.Files;
127+
var files = form1.Files;
128128
foreach (var file in files)
129129
{
130130
using (var stream = new FileStream(Path.Combine(AppContext.BaseDirectory, file.FileName), FileMode.Create))

src/Surging.Modules/Surging.Modules.Common/Domain/UserService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public Task<ApiResult<UserModel>> GetApiResult()
121121
return Task.FromResult(new ApiResult<UserModel>() { Value = new UserModel { Name = "fanly" }, StatusCode = 200 });
122122
}
123123

124-
public async Task<bool> UploadFile(HttpFormCollection form)
124+
public async Task<bool> UploadFile(HttpFormCollection form1)
125125
{
126-
var files = form.Files;
126+
var files = form1.Files;
127127
foreach (var file in files)
128128
{
129129
using (var stream = new FileStream(Path.Combine(AppContext.BaseDirectory, file.FileName), FileMode.OpenOrCreate))

0 commit comments

Comments
 (0)