Skip to content

Commit

Permalink
Merge pull request #83 from jonnii/bug/more-file-upload-fixes
Browse files Browse the repository at this point in the history
[Bug] More file upload fixes
  • Loading branch information
jonnii authored Aug 9, 2018
2 parents 7deba5e + 0646994 commit 7f5f9d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
74 changes: 34 additions & 40 deletions src/SpeakEasy.IntegrationTests/Controllers/InvoicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public IActionResult Get(int id)
}

[HttpPost]
public async Task<IActionResult> Post(/*[FromForm] IList<IFormFile> files*/)
public async Task<IActionResult> Post()
{
if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
{
Expand All @@ -38,58 +38,52 @@ public async Task<IActionResult> Post(/*[FromForm] IList<IFormFile> files*/)
DefaultFormOptions.MultipartBoundaryLengthLimit);

var reader = new MultipartReader(boundary, HttpContext.Request.Body);
try
{
var section = await reader.ReadNextSectionAsync().ConfigureAwait(false);

while (section != null)
{
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(
section.ContentDisposition,
out ContentDispositionHeaderValue contentDisposition);
var section = await reader.ReadNextSectionAsync().ConfigureAwait(false);

if (!hasContentDispositionHeader)
{
section = await reader.ReadNextSectionAsync().ConfigureAwait(false);
continue;
}
while (section != null)
{
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(
section.ContentDisposition,
out ContentDispositionHeaderValue contentDisposition);

switch (contentDisposition)
{
case var disposition when MultipartRequestHelper.HasAttachment(disposition):
fileInfos.Add(await GetTextFileInfo(section, contentDisposition).ConfigureAwait(false));
break;
if (!hasContentDispositionHeader)
{
section = await reader.ReadNextSectionAsync().ConfigureAwait(false);
continue;
}

case var disposition when MultipartRequestHelper.HasFormDataContentDisposition(disposition):
switch (contentDisposition)
{
case var disposition when MultipartRequestHelper.HasAttachment(disposition):
fileInfos.Add(await GetTextFileInfo(section, contentDisposition).ConfigureAwait(false));
break;

var (key, value) = await GetKeyAndValue(section, contentDisposition).ConfigureAwait(false);
case var disposition when MultipartRequestHelper.HasFormDataContentDisposition(disposition):

formAccumulator.Append(key, value);
var (key, value) = await GetKeyAndValue(section, contentDisposition).ConfigureAwait(false);

if (formAccumulator.ValueCount > DefaultFormOptions.ValueCountLimit)
{
throw new InvalidDataException($"Form key count limit {DefaultFormOptions.ValueCountLimit} exceeded.");
}
formAccumulator.Append(key, value);

break;
}
if (formAccumulator.ValueCount > DefaultFormOptions.ValueCountLimit)
{
throw new InvalidDataException($"Form key count limit {DefaultFormOptions.ValueCountLimit} exceeded.");
}

section = await reader.ReadNextSectionAsync().ConfigureAwait(false);
break;
}

return Ok(new FileUploadResult
{
TextFileInfos = fileInfos.ToArray(),
Parameters = formAccumulator
.GetResults()
.Select(x => new Parameter { Key = x.Key, Value = x.Value })
.ToArray()
});
section = await reader.ReadNextSectionAsync().ConfigureAwait(false);
}
catch (Exception ex)

return Ok(new FileUploadResult
{
throw;
}
TextFileInfos = fileInfos.ToArray(),
Parameters = formAccumulator
.GetResults()
.Select(x => new Parameter { Key = x.Key, Value = x.Value })
.ToArray()
});
}

private async Task<(string Key, string Value)> GetKeyAndValue(
Expand Down
2 changes: 1 addition & 1 deletion src/SpeakEasy/Contents/MultipartMimeContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public MultipartMimeContent(Resource resource, IFile[] files)

stringContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = parameter.Name
Name = $"\"{parameter.Name}\""
};

content.Add(stringContent);
Expand Down

0 comments on commit 7f5f9d7

Please sign in to comment.