Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/dotnetcore/surging
Browse files Browse the repository at this point in the history
  • Loading branch information
fanliang11 committed Mar 3, 2020
2 parents a39a06c + 7c8f296 commit 2eb7672
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ public async Task OnReceived(IMessageSender sender,string messageId, HttpContext
if (context.Request.HasFormContentType)
{
var collection = await GetFormCollection(context.Request);
httpMessage.Parameters.Add("form", collection);
foreach (var item in collection)
{
httpMessage.Parameters.Add(item.Key, item.Value);
}
if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage },
sender, messageId, actionFilters)) return;
httpMessage.Attachments = RestContext.GetContext().GetContextParameters();
Expand Down Expand Up @@ -189,59 +192,75 @@ public async Task<bool> OnException(HttpContext context, HttpServerMessageSender
return true;
}

private async Task<HttpFormCollection> GetFormCollection(HttpRequest request)
private async Task<Dictionary<string, HttpFormCollection>> GetFormCollection(HttpRequest request)
{
var boundary = GetName("boundary=", request.ContentType);
var boundary = GetName("boundary=", request.ContentType);
var reader = new MultipartReader(boundary, request.Body);
var collection = await GetMultipartForm(reader);
var fileCollection = new HttpFormFileCollection();
var fields = new Dictionary<string, StringValues>();
foreach (var item in collection)

return collection.ToDictionary(item => item.Key, item =>
{
if (item.Value is HttpFormFileCollection)
var fieldsDict = new Dictionary<string, StringValues>();
if (item.Value.Fields.HasValue)
{
var itemCollection = item.Value as HttpFormFileCollection;
fileCollection.AddRange(itemCollection);
fieldsDict.Add(item.Key, item.Value.Fields.Value);
}
else
{
var itemCollection = item.Value as Dictionary<string, StringValues>;
fields = fields.Concat(itemCollection).ToDictionary(k => k.Key, v => v.Value);

}
}
return new HttpFormCollection(fields, fileCollection);
return new HttpFormCollection(fieldsDict, item.Value.HttpFormFileCollection);
});
}

private async Task<IDictionary<string,object>> GetMultipartForm(MultipartReader reader)
private async Task<IDictionary<string, (StringValues? Fields, HttpFormFileCollection HttpFormFileCollection)>> GetMultipartForm(MultipartReader reader)
{
var section = await reader.ReadNextSectionAsync();
var collection = new Dictionary<string, object>();
var section = await reader.ReadNextSectionAsync();
var collection = new Dictionary<string, (StringValues? Fields, HttpFormFileCollection HttpFormFileCollection)>();
if (section != null)
{
var name=GetName("name=",section.ContentDisposition);
var fileName = GetName("filename=",section.ContentDisposition);
{
var name = GetName("name=", section.ContentDisposition);
var fileName = GetName("filename=", section.ContentDisposition);
var buffer = new MemoryStream();
await section.Body.CopyToAsync(buffer);
if(string.IsNullOrEmpty(fileName))
if (string.IsNullOrEmpty(fileName))
{
var fields = new Dictionary<string, StringValues>();
StreamReader streamReader = new StreamReader(buffer);
fields.Add(name, new StringValues(UTF8Encoding.Default.GetString(buffer.GetBuffer(),0,(int)buffer.Length)));
collection.Add(name, fields);
fields.Add(name, new StringValues(Encoding.Default.GetString(buffer.GetBuffer(), 0, (int)buffer.Length)));
collection.Add(name, (fields[name], null));
}
else
{
var fileCollection = new HttpFormFileCollection();
StreamReader streamReader = new StreamReader(buffer);
fileCollection.Add(new HttpFormFile(buffer.Length,name,fileName,buffer.GetBuffer()));
collection.Add(name, fileCollection);
fileCollection.Add(new HttpFormFile(buffer.Length, name, fileName, buffer.GetBuffer()));
collection.Add(name, (null, fileCollection));
}
var formCollection= await GetMultipartForm(reader);
foreach(var item in formCollection)
var formCollection = await GetMultipartForm(reader);
foreach (var item in formCollection)
{
if (!collection.ContainsKey(item.Key))
collection.Add(item.Key,item.Value);
collection.Add(item.Key, item.Value);
else
{
var (fields, httpFormFileCollection) = collection[item.Key];
if (item.Value.Fields.HasValue && !fields.HasValue)
{
fields = item.Value.Fields.Value;
}

if (httpFormFileCollection == null)
{
httpFormFileCollection = item.Value.HttpFormFileCollection;
}
else
{
var formFiles =
item.Value.HttpFormFileCollection.Where(v =>
!httpFormFileCollection.Exists(p => p.FileName == v.FileName));
httpFormFileCollection.AddRange(formFiles);
}

collection[item.Key] = (fields, httpFormFileCollection);
}
}
}
return collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public interface IUserService: IServiceKey
/// </summary>
/// <param name="form">HttpFormCollection 类型参数</param>
/// <returns></returns>
Task<bool> UploadFile(HttpFormCollection form);
Task<bool> UploadFile(HttpFormCollection form1);

/// <summary>
/// 测试下载文件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ public Task<string> GetUser(List<int> idList)
return Task.FromResult("type is List<int>");
}

public async Task<bool> UploadFile(HttpFormCollection form)
public async Task<bool> UploadFile(HttpFormCollection form1)
{
var files = form.Files;
var files = form1.Files;
foreach (var file in files)
{
using (var stream = new FileStream(Path.Combine(AppContext.BaseDirectory, file.FileName), FileMode.Create))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public Task<ApiResult<UserModel>> GetApiResult()
return Task.FromResult(new ApiResult<UserModel>() { Value = new UserModel { Name = "fanly" }, StatusCode = 200 });
}

public async Task<bool> UploadFile(HttpFormCollection form)
public async Task<bool> UploadFile(HttpFormCollection form1)
{
var files = form.Files;
var files = form1.Files;
foreach (var file in files)
{
using (var stream = new FileStream(Path.Combine(AppContext.BaseDirectory, file.FileName), FileMode.OpenOrCreate))
Expand Down

0 comments on commit 2eb7672

Please sign in to comment.