/
BrowserFileExtensions.cs
36 lines (33 loc) · 1.79 KB
/
BrowserFileExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.AspNetCore.Components.Forms;
/// <summary>
/// Contains helper methods for <see cref="IBrowserFile"/>.
/// </summary>
public static class BrowserFileExtensions
{
/// <summary>
/// Attempts to convert the current image file to a new one of the specified file type and maximum file dimensions.
/// <para>
/// Caution: there is no guarantee that the file will be converted, or will even be a valid image file at all, either
/// before or after conversion. The conversion is requested within the browser before it is transferred to .NET
/// code, so the resulting data should be treated as untrusted.
/// </para>
/// </summary>
/// <remarks>
/// The image will be scaled to fit the specified dimensions while preserving the original aspect ratio.
/// </remarks>
/// <param name="browserFile">The <see cref="IBrowserFile"/> to convert to a new image file.</param>
/// <param name="format">The new image format.</param>
/// <param name="maxWidth">The maximum image width.</param>
/// <param name="maxHeight">The maximum image height</param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
public static ValueTask<IBrowserFile> RequestImageFileAsync(this IBrowserFile browserFile, string format, int maxWidth, int maxHeight)
{
if (browserFile is BrowserFile browserFileInternal)
{
return browserFileInternal.Owner.ConvertToImageFileAsync(browserFileInternal, format, maxWidth, maxHeight);
}
throw new InvalidOperationException($"Cannot perform this operation on custom {typeof(IBrowserFile)} implementations.");
}
}