Skip to content

Commit

Permalink
Fixes #748 (#1261)
Browse files Browse the repository at this point in the history
* Added support for file upload via REST

* Added support for pushing the filecontents to PS

* Removed some debug logging.

* Should now pass test
  • Loading branch information
BoSen29 authored and adamdriscoll committed Oct 17, 2019
1 parent eeefccd commit 9302d7b
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/UniversalDashboard/Controllers/ComponentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public async Task<IActionResult> DeleteEndpoint()

SetQueryStringValues(variables);

if (!await TryProcessBodyAsForm(Request, variables))
if (!await TryProcessBodyAsFormOrFile(Request, variables))
{
//If we made it here we either have a non-form content type
//or the request was made with the default content type of form
Expand All @@ -402,16 +402,16 @@ public async Task<IActionResult> DeleteEndpoint()
return StatusCode(404);
}

private async Task<bool> TryProcessBodyAsForm(HttpRequest request, Dictionary<string,object> variables)
private async Task<bool> TryProcessBodyAsFormOrFile(HttpRequest request, Dictionary<string,object> variables)
{
if (HttpContext.Request.HasFormContentType)
{
{
Log.Debug("HasFormContentType");

var form = await Request.ReadFormAsync(new FormOptions() { BufferBody = true });

if (form != null && form.Any())
{
{
foreach (var value in form)
{
if (value.Value.Count == 1) {
Expand All @@ -422,6 +422,29 @@ private async Task<bool> TryProcessBodyAsForm(HttpRequest request, Dictionary<st
}
return true;
}
return false;
}
if (HttpContext.Request.Method == "POST") {
//file upload only avaliable in POST method.
if (HttpContext.Request.ContentType.Contains("image/") || HttpContext.Request.ContentType.Contains("file/"))
{
Log.Debug("HasFileOrImageContenttype");
using (MemoryStream stream = new MemoryStream())
{
await HttpContext.Request.Body.CopyToAsync(stream);
if (stream != null) {
variables.Add("File", stream.ToArray());
Log.Debug("File from RESTAPI found.");
return true;
}
else
{
Log.Debug("Filestream is empty.");
//return it true, to prevent it from processing it as RAW
return true;
}
}
}
}
return false;
}
Expand All @@ -448,11 +471,12 @@ public async Task<IActionResult> PostEndpoint()
var variables = new Dictionary<string, object>();
SetQueryStringValues(variables);

if (!await TryProcessBodyAsForm(Request, variables))
if (!await TryProcessBodyAsFormOrFile(Request, variables))
{
//If we made it here we either have a non-form content type
//or the request was made with the default content type of form
//when it is really something else (probably application/json)
Log.Debug("Processing as RAW");
ProcessBodyAsRaw(Request, variables);
}

Expand All @@ -479,7 +503,7 @@ public async Task<IActionResult> PutEndpoint()
var variables = new Dictionary<string, object>();
SetQueryStringValues(variables);

if (!await TryProcessBodyAsForm(Request, variables))
if (!await TryProcessBodyAsFormOrFile(Request, variables))
{
//If we made it here we either have a non-form content type
//or the request was made with the default content type of form
Expand Down

0 comments on commit 9302d7b

Please sign in to comment.