-
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 form files in complex form mapping (#50537)
* Support resolving IFormFile in complex form mapping * Feedback * Fix up FormFile integration in Blazor * Fix up FileConverter interfaces * Fix build * Fix FormFileCollection initialization * Revert "Revert "[Blazor] Update selenium versions (#50511)" (#50556)" This reverts commit 564a94d. * Update test for non-enhanced form * Revert "Revert "Revert "[Blazor] Update selenium versions (#50511)" (#50556)"" This reverts commit 78bf7d8. * Add support for IReadOnlyList<IBrowserFile>
- Loading branch information
1 parent
2595235
commit 2ac6452
Showing
20 changed files
with
559 additions
and
16 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/Components/Endpoints/src/FormMapping/BrowserFileFromFormFile.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,29 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using Microsoft.AspNetCore.Components.Forms; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints.FormMapping; | ||
|
||
internal sealed class BrowserFileFromFormFile(IFormFile formFile) : IBrowserFile | ||
{ | ||
public string Name => formFile.Name; | ||
|
||
public DateTimeOffset LastModified => DateTimeOffset.Parse(formFile.Headers.LastModified.ToString(), CultureInfo.InvariantCulture); | ||
|
||
public long Size => formFile.Length; | ||
|
||
public string ContentType => formFile.ContentType; | ||
|
||
public Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken cancellationToken = default) | ||
{ | ||
if (Size > maxAllowedSize) | ||
{ | ||
throw new IOException($"Supplied file with size {Size} bytes exceeds the maximum of {maxAllowedSize} bytes."); | ||
} | ||
|
||
return formFile.OpenReadStream(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
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,86 @@ | ||
// 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; | ||
#if COMPONENTS | ||
using Microsoft.AspNetCore.Components.Forms; | ||
#endif | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints.FormMapping; | ||
|
||
internal sealed class FileConverter<T> : FormDataConverter<T> | ||
{ | ||
[RequiresDynamicCode(FormMappingHelpers.RequiresDynamicCodeMessage)] | ||
[RequiresUnreferencedCode(FormMappingHelpers.RequiresUnreferencedCodeMessage)] | ||
internal override bool TryRead(ref FormDataReader reader, Type type, FormDataMapperOptions options, out T? result, out bool found) | ||
{ | ||
if (reader.FormFileCollection == null) | ||
{ | ||
result = default; | ||
found = false; | ||
return true; | ||
} | ||
|
||
#if COMPONENTS | ||
if (typeof(T) == typeof(IBrowserFile)) | ||
{ | ||
var targetFile = reader.FormFileCollection.GetFile(reader.CurrentPrefix.ToString()); | ||
if (targetFile != null) | ||
{ | ||
var browserFile = new BrowserFileFromFormFile(targetFile); | ||
result = (T)(IBrowserFile)browserFile; | ||
found = true; | ||
return true; | ||
} | ||
} | ||
|
||
if (typeof(T) == typeof(IReadOnlyList<IBrowserFile>)) | ||
{ | ||
var targetFiles = reader.FormFileCollection.GetFiles(reader.CurrentPrefix.ToString()); | ||
var buffer = ReadOnlyCollectionBufferAdapter<IBrowserFile>.CreateBuffer(); | ||
for (var i = 0; i < targetFiles.Count; i++) | ||
{ | ||
buffer = ReadOnlyCollectionBufferAdapter<IBrowserFile>.Add(ref buffer, new BrowserFileFromFormFile(targetFiles[i])); | ||
} | ||
result = (T)(IReadOnlyList<IBrowserFile>)ReadOnlyCollectionBufferAdapter<IBrowserFile>.ToResult(buffer); | ||
found = true; | ||
return true; | ||
} | ||
#endif | ||
|
||
if (typeof(T) == typeof(IReadOnlyList<IFormFile>)) | ||
{ | ||
result = (T)reader.FormFileCollection.GetFiles(reader.CurrentPrefix.ToString()); | ||
found = true; | ||
return true; | ||
} | ||
|
||
if (typeof(T) == typeof(IFormFileCollection)) | ||
{ | ||
result = (T)reader.FormFileCollection; | ||
found = true; | ||
return true; | ||
} | ||
|
||
var formFileCollection = reader.FormFileCollection; | ||
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
// 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; | ||
#if COMPONENTS | ||
using Microsoft.AspNetCore.Components.Forms; | ||
#endif | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints.FormMapping; | ||
|
||
internal sealed class FileConverterFactory : IFormDataConverterFactory | ||
{ | ||
[RequiresDynamicCode(FormMappingHelpers.RequiresDynamicCodeMessage)] | ||
[RequiresUnreferencedCode(FormMappingHelpers.RequiresUnreferencedCodeMessage)] | ||
#if COMPONENTS | ||
public bool CanConvert(Type type, FormDataMapperOptions options) => CanConvertCommon(type) || type == typeof(IBrowserFile) || type == typeof(IReadOnlyList<IBrowserFile>); | ||
#else | ||
public bool CanConvert(Type type, FormDataMapperOptions options) => CanConvertCommon(type); | ||
#endif | ||
|
||
private static bool CanConvertCommon(Type type) => type == typeof(IFormFile) || type == typeof(IFormFileCollection) || type == typeof(IReadOnlyList<IFormFile>); | ||
|
||
[RequiresDynamicCode(FormMappingHelpers.RequiresDynamicCodeMessage)] | ||
[RequiresUnreferencedCode(FormMappingHelpers.RequiresUnreferencedCodeMessage)] | ||
public FormDataConverter CreateConverter(Type type, FormDataMapperOptions options) | ||
{ | ||
return Activator.CreateInstance(typeof(FileConverter<>).MakeGenericType(type)) 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
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
Oops, something went wrong.