| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Creates a new form part rule. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function creates and adds a form part rule to the server's form options collection. |
| | | 6 | | It allows you to specify various parameters for the form part rule, such as name, description, |
| | | 7 | | allowed content types, maximum size, and storage options. |
| | | 8 | | .PARAMETER Rules |
| | | 9 | | An array of existing form part rules to which the new rule will be added. |
| | | 10 | | .PARAMETER Name |
| | | 11 | | The name of the form part rule. |
| | | 12 | | .PARAMETER Scope |
| | | 13 | | The multipart scope that this rule applies to. When omitted, the rule applies only at the root level. |
| | | 14 | | .PARAMETER Description |
| | | 15 | | A description of the form part rule. |
| | | 16 | | .PARAMETER Required |
| | | 17 | | Indicates whether the form part is required. |
| | | 18 | | .PARAMETER AllowOnlyOne |
| | | 19 | | Indicates whether only one instance of the form part is allowed. |
| | | 20 | | .PARAMETER AllowedContentTypes |
| | | 21 | | An array of allowed content types for the form part. |
| | | 22 | | .PARAMETER AllowedExtensions |
| | | 23 | | An array of allowed file extensions for the form part. |
| | | 24 | | .PARAMETER MaxBytes |
| | | 25 | | The maximum size in bytes for the form part. |
| | | 26 | | .PARAMETER DecodeMode |
| | | 27 | | The decode mode for the form part. |
| | | 28 | | .PARAMETER DestinationPath |
| | | 29 | | The destination path where the form part should be stored. |
| | | 30 | | .PARAMETER StoreToDisk |
| | | 31 | | Indicates whether the form part should be stored to disk. |
| | | 32 | | .EXAMPLE |
| | | 33 | | New-KrFormPartRule -Name 'file' -Required -AllowedContentTypes 'text/plain' -MaxBytes 1048576 |
| | | 34 | | This example adds a form part rule named 'file' that is required, allows only 'text/plain' content type, |
| | | 35 | | and has a maximum size of 1 MB. |
| | | 36 | | .NOTES |
| | | 37 | | This function is part of the Kestrun.Forms module and is used to define form part rules. |
| | | 38 | | #> |
| | | 39 | | function New-KrFormPartRule { |
| | | 40 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | | 41 | | [KestrunRuntimeApi('Definition')] |
| | | 42 | | [CmdletBinding()] |
| | | 43 | | [OutputType([Kestrun.Forms.KrFormPartRule[]])] |
| | | 44 | | [OutputType([System.Array])] |
| | | 45 | | param( |
| | | 46 | | [Parameter(ValueFromPipeline)] |
| | | 47 | | [Kestrun.Forms.KrFormPartRule[]] $Rules, |
| | | 48 | | |
| | | 49 | | [Parameter(Mandatory = $true)] |
| | | 50 | | [string] $Name, |
| | | 51 | | |
| | | 52 | | [Parameter()] |
| | | 53 | | [string] $Scope, |
| | | 54 | | |
| | | 55 | | [Parameter()] |
| | | 56 | | [string] $Description, |
| | | 57 | | |
| | | 58 | | [Parameter()] |
| | | 59 | | [switch] $Required, |
| | | 60 | | |
| | | 61 | | [Parameter()] |
| | | 62 | | [switch] $AllowOnlyOne, |
| | | 63 | | |
| | | 64 | | [Parameter()] |
| | | 65 | | [string[]] $AllowedContentTypes, |
| | | 66 | | |
| | | 67 | | [Parameter()] |
| | | 68 | | [string[]] $AllowedExtensions, |
| | | 69 | | |
| | | 70 | | [Parameter()] |
| | | 71 | | [long] $MaxBytes, |
| | | 72 | | |
| | | 73 | | [Parameter()] |
| | | 74 | | [KrPartDecodeMode] $DecodeMode = [KrPartDecodeMode]::None, |
| | | 75 | | |
| | | 76 | | [Parameter()] |
| | | 77 | | [string] $DestinationPath, |
| | | 78 | | |
| | | 79 | | [Parameter()] |
| | | 80 | | [switch] $StoreToDisk |
| | | 81 | | ) |
| | | 82 | | begin { |
| | 0 | 83 | | $bag = [System.Collections.Generic.List[Kestrun.Forms.KrFormPartRule]]::new() |
| | | 84 | | # Ensure the server instance is resolved |
| | 0 | 85 | | $Server = Resolve-KestrunServer |
| | | 86 | | } |
| | | 87 | | process { |
| | 0 | 88 | | if ( $null -ne $Rules ) { |
| | 0 | 89 | | $bag.AddRange($Rules) |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | end { |
| | 0 | 93 | | $Rule = [Kestrun.Forms.KrFormPartRule]::new() |
| | 0 | 94 | | $Rule.Name = $Name |
| | 0 | 95 | | if ($PSBoundParameters.ContainsKey('Scope')) { |
| | 0 | 96 | | $Rule.Scope = $Scope |
| | | 97 | | } |
| | 0 | 98 | | if ($PSBoundParameters.ContainsKey('Description')) { |
| | 0 | 99 | | $Rule.Description = $Description |
| | | 100 | | } |
| | 0 | 101 | | if ($PSBoundParameters.ContainsKey('Required')) { |
| | 0 | 102 | | $Rule.Required = $Required.IsPresent |
| | | 103 | | } |
| | 0 | 104 | | if ($PSBoundParameters.ContainsKey('AllowOnlyOne')) { |
| | 0 | 105 | | $Rule.AllowMultiple = -not $AllowOnlyOne.IsPresent |
| | | 106 | | } |
| | 0 | 107 | | if ($PSBoundParameters.ContainsKey('AllowedContentTypes')) { |
| | 0 | 108 | | $Rule.AllowedContentTypes.Clear() |
| | 0 | 109 | | foreach ($type in $AllowedContentTypes) { |
| | 0 | 110 | | $Rule.AllowedContentTypes.Add($type) |
| | | 111 | | } |
| | | 112 | | } |
| | 0 | 113 | | if ($PSBoundParameters.ContainsKey('AllowedExtensions')) { |
| | 0 | 114 | | $Rule.AllowedExtensions.Clear() |
| | 0 | 115 | | foreach ($ext in $AllowedExtensions) { |
| | 0 | 116 | | $Rule.AllowedExtensions.Add($ext) |
| | | 117 | | } |
| | | 118 | | } |
| | 0 | 119 | | if ($PSBoundParameters.ContainsKey('MaxBytes')) { |
| | 0 | 120 | | $Rule.MaxBytes = $MaxBytes |
| | | 121 | | } |
| | 0 | 122 | | if ($PSBoundParameters.ContainsKey('DecodeMode')) { |
| | 0 | 123 | | $Rule.DecodeMode = $DecodeMode |
| | | 124 | | } |
| | 0 | 125 | | if ($PSBoundParameters.ContainsKey('DestinationPath')) { |
| | 0 | 126 | | $Rule.DestinationPath = $DestinationPath |
| | | 127 | | } |
| | 0 | 128 | | if ($PSBoundParameters.ContainsKey('StoreToDisk')) { |
| | 0 | 129 | | $Rule.StoreToDisk = $StoreToDisk.IsPresent |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | if (-not ($Server.AddFormPartRule($Rule))) { |
| | 0 | 133 | | throw "Failed to add form part rule named '$Name' because a rule with the same name already exists." |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | # Add to output collection |
| | 0 | 137 | | $bag.Add($Rule) | Out-Null |
| | 0 | 138 | | , [Kestrun.Forms.KrFormPartRule[]] $bag.ToArray() |
| | | 139 | | } |
| | | 140 | | } |