-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support resolving IFormFile in complex form mapping
- Loading branch information
1 parent
2772a78
commit bd0082e
Showing
8 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/Components/Endpoints/src/FormMapping/Converters/FileConverter.cs
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,49 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints.FormMapping; | ||
|
||
internal sealed class FileConverter<T>(HttpContext? httpContext) : FormDataConverter<T>, ISingleValueConverter | ||
{ | ||
[RequiresDynamicCode(FormMappingHelpers.RequiresDynamicCodeMessage)] | ||
[RequiresUnreferencedCode(FormMappingHelpers.RequiresUnreferencedCodeMessage)] | ||
internal override bool TryRead(ref FormDataReader reader, Type type, FormDataMapperOptions options, out T? result, out bool found) | ||
{ | ||
if (httpContext == null) | ||
{ | ||
result = default; | ||
found = false; | ||
return true; | ||
} | ||
|
||
if (typeof(T) == typeof(IFormFileCollection)) | ||
{ | ||
result = (T)httpContext.Request.Form.Files; | ||
found = true; | ||
return true; | ||
} | ||
|
||
var formFileCollection = httpContext.Request.Form.Files; | ||
if (formFileCollection.Count == 0) | ||
{ | ||
result = default; | ||
found = false; | ||
return true; | ||
} | ||
|
||
var file = formFileCollection.GetFile(reader.CurrentPrefix.ToString()); | ||
if (file != null) | ||
{ | ||
result = (T)file; | ||
found = true; | ||
return true; | ||
} | ||
|
||
result = default; | ||
found = false; | ||
return true; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Components/Endpoints/src/FormMapping/Factories/FileConverterFactory.cs
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,22 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints.FormMapping; | ||
|
||
internal sealed class FileConverterFactory(IHttpContextAccessor httpContextAccessor) : IFormDataConverterFactory | ||
{ | ||
[RequiresDynamicCode(FormMappingHelpers.RequiresDynamicCodeMessage)] | ||
[RequiresUnreferencedCode(FormMappingHelpers.RequiresUnreferencedCodeMessage)] | ||
public bool CanConvert(Type type, FormDataMapperOptions options) => type == typeof(IFormFile) || type == typeof(IFormFileCollection); | ||
|
||
[RequiresDynamicCode(FormMappingHelpers.RequiresDynamicCodeMessage)] | ||
[RequiresUnreferencedCode(FormMappingHelpers.RequiresUnreferencedCodeMessage)] | ||
public FormDataConverter CreateConverter(Type type, FormDataMapperOptions options) | ||
{ | ||
return Activator.CreateInstance(typeof(FileConverter<>).MakeGenericType(type), httpContextAccessor.HttpContext) as FormDataConverter ?? | ||
throw new InvalidOperationException($"Unable to create converter for '{type.FullName}'."); | ||
} | ||
} |
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
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