-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'alistair-file_upload_support'
- Loading branch information
Showing
116 changed files
with
132 additions
and
7,134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Uploading Files | ||
|
||
As of 1.11.0, Wolverine supports file uploads through the standard ASP.Net Core `IFile` or `IFileCollection` types. All you need | ||
to do to is to have an input parameter to your Wolverine.HTTP endpoint of these types like so: | ||
|
||
snippet: sample_using_file_uploads | ||
|
||
See [Upload files in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-7.0) | ||
for more information about these types. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Reflection; | ||
using JasperFx.CodeGeneration; | ||
using JasperFx.CodeGeneration.Frames; | ||
using JasperFx.CodeGeneration.Model; | ||
using Lamar; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Wolverine.Http.CodeGen; | ||
|
||
public class FromFileStrategy : IParameterStrategy | ||
{ | ||
public bool TryMatch(HttpChain chain, IContainer container, ParameterInfo parameter, out Variable? variable) | ||
{ | ||
if (parameter.ParameterType == typeof(IFormFile)) | ||
{ | ||
var frame = new FromFileValue(parameter); | ||
chain.Middleware.Add(frame); | ||
variable = frame.Variable; | ||
return true; | ||
} else if (parameter.ParameterType == typeof(IFormFileCollection)) | ||
{ | ||
var frame = new FromFileValues(parameter); | ||
chain.Middleware.Add(frame); | ||
variable = frame.Variable; | ||
return true; | ||
} | ||
variable = null; | ||
return false; | ||
} | ||
} | ||
|
||
internal class FromFileValue : SyncFrame | ||
{ | ||
private Variable? _httpContext; | ||
public FromFileValue(ParameterInfo parameter) | ||
{ | ||
Variable = new Variable(parameter.ParameterType, parameter.Name!, this); | ||
} | ||
|
||
public Variable Variable { get; } | ||
|
||
public override IEnumerable<Variable> FindVariables(IMethodVariables chain) | ||
{ | ||
_httpContext = chain.FindVariable(typeof(HttpContext)); | ||
yield return _httpContext; | ||
} | ||
|
||
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) | ||
{ | ||
writer.WriteComment("Retrieve header value from the request"); | ||
writer.Write( | ||
$"var {Variable.Usage} = {nameof(HttpHandler.ReadSingleFormFileValue)}({_httpContext!.Usage});"); | ||
Next?.GenerateCode(method, writer); | ||
} | ||
} | ||
|
||
internal class FromFileValues : SyncFrame | ||
{ | ||
private Variable? _httpContext; | ||
public FromFileValues(ParameterInfo parameter) | ||
{ | ||
Variable = new Variable(parameter.ParameterType, parameter.Name!, this); | ||
} | ||
|
||
public Variable Variable { get; } | ||
|
||
public override IEnumerable<Variable> FindVariables(IMethodVariables chain) | ||
{ | ||
_httpContext = chain.FindVariable(typeof(HttpContext)); | ||
yield return _httpContext; | ||
} | ||
|
||
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) | ||
{ | ||
writer.WriteComment("Retrieve header value from the request"); | ||
writer.Write( | ||
$"var {Variable.Usage} = {nameof(HttpHandler.ReadManyFormFileValues)}({_httpContext!.Usage});"); | ||
Next?.GenerateCode(method, writer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Wolverine.Http; | ||
|
||
namespace WolverineWebApi; | ||
|
||
#region sample_using_file_uploads | ||
|
||
public class FileUploadEndpoint | ||
{ | ||
// If you have exactly one file upload, take | ||
// in IFormFile | ||
[WolverinePost("/upload/file")] | ||
public static Task Upload(IFormFile file) | ||
{ | ||
// access the file data | ||
return Task.CompletedTask; | ||
} | ||
|
||
// If you have multiple files at one time, | ||
// use IFormCollection | ||
[WolverinePost("/upload/files")] | ||
public static Task Upload(IFormFileCollection files) | ||
{ | ||
// access files | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
#endregion |
Oops, something went wrong.