| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Registers a static file server to serve files from a specified path. |
| | 4 | | .DESCRIPTION |
| | 5 | | This cmdlet allows you to serve static files from a specified path using the Kestrun server. |
| | 6 | | It can be used to serve files like images, stylesheets, and scripts. |
| | 7 | | .PARAMETER Server |
| | 8 | | The Kestrun server instance to which the static file service will be added. |
| | 9 | | .PARAMETER Options |
| | 10 | | The StaticFileOptions to configure the static file service. |
| | 11 | | .PARAMETER RootPath |
| | 12 | | The root path from which to serve static files. |
| | 13 | | .PARAMETER RequestPath |
| | 14 | | The path at which the static file service will be registered. |
| | 15 | | .PARAMETER HttpsCompression |
| | 16 | | If specified, enables HTTPS compression for the static files. |
| | 17 | | .PARAMETER ServeUnknownFileTypes |
| | 18 | | If specified, allows serving files with unknown MIME types. |
| | 19 | | .PARAMETER DefaultContentType |
| | 20 | | The default content type to use for files served by the static file service. |
| | 21 | | .PARAMETER RedirectToAppendTrailingSlash |
| | 22 | | If specified, redirects requests to append a trailing slash to the URL. |
| | 23 | | .PARAMETER ContentTypeMap |
| | 24 | | A hashtable mapping file extensions to MIME types. |
| | 25 | | .PARAMETER PassThru |
| | 26 | | If specified, the cmdlet will return the modified server instance after adding the static file service. |
| | 27 | | .EXAMPLE |
| | 28 | | $server | Add-KrStaticFilesService -RequestPath '/static' -HttpsCompression -ServeUnknownFileTypes -DefaultContentTy |
| | 29 | | This example adds a static file service to the server for the path '/static', enabling HTTPS compression, allowing s |
| | 30 | | setting the default content type to 'application/octet-stream', and redirecting requests to append a trailing slash. |
| | 31 | | .EXAMPLE |
| | 32 | | $server | Add-KrStaticFilesService -Options $options |
| | 33 | | This example adds a static file service to the server using the specified StaticFileOptions. |
| | 34 | | .LINK |
| | 35 | | https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.staticfileoptions?view=aspnetcore-8.0 |
| | 36 | | .NOTES |
| | 37 | | ContentTypeProvider and ContentTypeProviderOptions are not supported yet. |
| | 38 | | #> |
| | 39 | | function Add-KrStaticFilesService { |
| | 40 | | [KestrunRuntimeApi('Definition')] |
| | 41 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | 42 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 43 | | param( |
| | 44 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 45 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 46 | |
|
| | 47 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | 48 | | [Microsoft.AspNetCore.Builder.StaticFileOptions]$Options, |
| | 49 | |
|
| | 50 | | [Parameter(ParameterSetName = 'Items')] |
| | 51 | | [string]$RootPath, |
| | 52 | |
|
| | 53 | | [Parameter(ParameterSetName = 'Items')] |
| | 54 | | [string]$RequestPath, |
| | 55 | |
|
| | 56 | | [Parameter(ParameterSetName = 'Items')] |
| | 57 | | [switch]$HttpsCompression, |
| | 58 | |
|
| | 59 | | [Parameter(ParameterSetName = 'Items')] |
| | 60 | | [switch]$ServeUnknownFileTypes, |
| | 61 | |
|
| | 62 | | [Parameter(ParameterSetName = 'Items')] |
| | 63 | | [string]$DefaultContentType, |
| | 64 | |
|
| | 65 | | [Parameter(ParameterSetName = 'Items')] |
| | 66 | | [switch]$RedirectToAppendTrailingSlash, |
| | 67 | |
|
| | 68 | | [Parameter(ParameterSetName = 'Items')] |
| | 69 | | [hashtable]$ContentTypeMap, |
| | 70 | |
|
| | 71 | | [Parameter()] |
| | 72 | | [switch]$PassThru |
| | 73 | | ) |
| | 74 | | begin { |
| | 75 | | # Ensure the server instance is resolved |
| 0 | 76 | | $Server = Resolve-KestrunServer -Server $Server |
| 0 | 77 | | if ($null -eq $Server) { |
| 0 | 78 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 79 | | } |
| | 80 | | } |
| | 81 | | process { |
| 0 | 82 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| 0 | 83 | | $Options = [Microsoft.AspNetCore.Builder.StaticFileOptions]::new() |
| 0 | 84 | | if (-not [string]::IsNullOrEmpty($RootPath)) { |
| 0 | 85 | | $resolvedPath = Resolve-KrPath $RootPath -KestrunRoot |
| 0 | 86 | | $Options.FileProvider = [Microsoft.Extensions.FileProviders.PhysicalFileProvider]::new($resolvedPath) |
| | 87 | | } |
| 0 | 88 | | if (-not [string]::IsNullOrEmpty($RequestPath)) { |
| 0 | 89 | | $Options.RequestPath = [Microsoft.AspNetCore.Http.PathString]::new($RequestPath.TrimEnd('/')) |
| | 90 | | } |
| 0 | 91 | | if ($ServeUnknownFileTypes.IsPresent) { |
| 0 | 92 | | $Options.ServeUnknownFileTypes = $true |
| | 93 | | } |
| 0 | 94 | | if ($HttpsCompression.IsPresent) { |
| 0 | 95 | | $Options.HttpsCompression = $true |
| | 96 | | } |
| 0 | 97 | | if (-not [string]::IsNullOrEmpty($DefaultContentType)) { |
| 0 | 98 | | $Options.DefaultContentType = $DefaultContentType |
| | 99 | | } |
| 0 | 100 | | if ($RedirectToAppendTrailingSlash.IsPresent) { |
| 0 | 101 | | $Options.RedirectToAppendTrailingSlash = $true |
| | 102 | | } |
| 0 | 103 | | if ($ContentTypeMap) { |
| 0 | 104 | | $provider = [Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider]::new() |
| 0 | 105 | | foreach ($k in $ContentTypeMap.Keys) { |
| 0 | 106 | | $ext = if ($k -like ".*") { $k } else { ".$k" } |
| 0 | 107 | | $mime = [string]$ContentTypeMap[$k] |
| 0 | 108 | | if ([string]::IsNullOrWhiteSpace($mime)) { continue } |
| 0 | 109 | | $provider.Mappings[$ext] = $mime |
| | 110 | | } |
| 0 | 111 | | $Options.StaticFileOptions.ContentTypeProvider = $provider |
| | 112 | | } |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddStaticFiles($Server, $Options) | Out-Null |
| | 116 | | # Add the static file service to the server |
| | 117 | |
|
| 0 | 118 | | if ($PassThru.IsPresent) { |
| | 119 | | # if the PassThru switch is specified, return the server instance |
| | 120 | | # Return the modified server instance |
| 0 | 121 | | return $Server |
| | 122 | | } |
| | 123 | | } |
| | 124 | | } |
| | 125 | |
|