| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Registers a file server to serve static 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 file server will be added. |
| | 9 | | .PARAMETER Options |
| | 10 | | The FileServerOptions to configure the file server. |
| | 11 | | .PARAMETER RootPath |
| | 12 | | The root path from which to serve files. |
| | 13 | | .PARAMETER RequestPath |
| | 14 | | The path at which the file server 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 EnableDirectoryBrowsing |
| | 22 | | If specified, enables directory browsing for the file server. |
| | 23 | | .PARAMETER RedirectToAppendTrailingSlash |
| | 24 | | If specified, requests to the path will be redirected to append a trailing slash. |
| | 25 | | .PARAMETER ContentTypeMap |
| | 26 | | A hashtable mapping file extensions to MIME types (e.g., @{ ".yaml"="application/x-yaml"; ".yml"="application/x- |
| | 27 | | This allows for serving files with the correct `Content-Type` header. |
| | 28 | | .PARAMETER PassThru |
| | 29 | | If specified, the cmdlet will return the modified server instance. |
| | 30 | | .EXAMPLE |
| | 31 | | $server | Add-KrFileServer -RequestPath '/files' -EnableDirectoryBrowsing |
| | 32 | | This example adds a file server to the server for the path '/files', enabling directory browsing. |
| | 33 | | The file server will use the default options for serving static files. |
| | 34 | | .EXAMPLE |
| | 35 | | $server | Add-KrFileServer -Options $options |
| | 36 | | This example adds a file server to the server using the specified FileServerOptions. |
| | 37 | | .LINK |
| | 38 | | https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.fileserveroptions?view=aspnetcore-8.0 |
| | 39 | | #> |
| | 40 | | function Add-KrFileServer { |
| | 41 | | [KestrunRuntimeApi('Definition')] |
| | 42 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | 43 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 44 | | param( |
| | 45 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 46 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 47 | |
|
| | 48 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | 49 | | [Microsoft.AspNetCore.Builder.FileServerOptions]$Options, |
| | 50 | |
|
| | 51 | | [Parameter(ParameterSetName = 'Items')] |
| | 52 | | [string]$RootPath, |
| | 53 | |
|
| | 54 | | [Parameter(ParameterSetName = 'Items')] |
| | 55 | | [string]$RequestPath, |
| | 56 | |
|
| | 57 | | [Parameter(ParameterSetName = 'Items')] |
| | 58 | | [switch]$HttpsCompression, |
| | 59 | |
|
| | 60 | | [Parameter(ParameterSetName = 'Items')] |
| | 61 | | [switch]$ServeUnknownFileTypes, |
| | 62 | |
|
| | 63 | | [Parameter(ParameterSetName = 'Items')] |
| | 64 | | [string]$DefaultContentType, |
| | 65 | |
|
| | 66 | | [Parameter(ParameterSetName = 'Items')] |
| | 67 | | [switch]$EnableDirectoryBrowsing, |
| | 68 | |
|
| | 69 | | [Parameter(ParameterSetName = 'Items')] |
| | 70 | | [switch]$RedirectToAppendTrailingSlash, |
| | 71 | |
|
| | 72 | | [Parameter(ParameterSetName = 'Items')] |
| | 73 | | [hashtable]$ContentTypeMap, |
| | 74 | |
|
| | 75 | | [Parameter()] |
| | 76 | | [switch]$PassThru |
| | 77 | | ) |
| | 78 | | begin { |
| | 79 | | # Ensure the server instance is resolved |
| 0 | 80 | | $Server = Resolve-KestrunServer -Server $Server |
| 0 | 81 | | if ($null -eq $Server) { |
| 0 | 82 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 83 | | } |
| | 84 | | } |
| | 85 | | process { |
| 0 | 86 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| 0 | 87 | | $Options = [Microsoft.AspNetCore.Builder.FileServerOptions]::new() |
| | 88 | |
|
| 0 | 89 | | if (-not [string]::IsNullOrEmpty($RequestPath)) { |
| 0 | 90 | | $Options.RequestPath = [Microsoft.AspNetCore.Http.PathString]::new($RequestPath.TrimEnd('/')) |
| | 91 | | } |
| 0 | 92 | | if (-not [string]::IsNullOrEmpty($RootPath)) { |
| 0 | 93 | | $resolvedPath = Resolve-KrPath $RootPath -KestrunRoot |
| 0 | 94 | | $Options.FileProvider = [Microsoft.Extensions.FileProviders.PhysicalFileProvider]::new($resolvedPath) |
| | 95 | | } |
| 0 | 96 | | if ($EnableDirectoryBrowsing.IsPresent) { |
| 0 | 97 | | $Options.EnableDirectoryBrowsing = $true |
| | 98 | | } |
| 0 | 99 | | if ($ServeUnknownFileTypes.IsPresent) { |
| 0 | 100 | | $Options.ServeUnknownFileTypes = $true |
| | 101 | | } |
| 0 | 102 | | if ($HttpsCompression.IsPresent) { |
| 0 | 103 | | $Options.HttpsCompression = $true |
| | 104 | | } |
| 0 | 105 | | if (-not [string]::IsNullOrEmpty($DefaultContentType)) { |
| 0 | 106 | | $Options.DefaultContentType = $DefaultContentType |
| | 107 | | } |
| 0 | 108 | | if ($RedirectToAppendTrailingSlash.IsPresent) { |
| 0 | 109 | | $Options.RedirectToAppendTrailingSlash = $true |
| | 110 | | } |
| 0 | 111 | | if ($ContentTypeMap) { |
| 0 | 112 | | $provider = [Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider]::new() |
| 0 | 113 | | foreach ($k in $ContentTypeMap.Keys) { |
| 0 | 114 | | $ext = if ($k -like ".*") { $k } else { ".$k" } |
| 0 | 115 | | $mime = [string]$ContentTypeMap[$k] |
| 0 | 116 | | if ([string]::IsNullOrWhiteSpace($mime)) { continue } |
| 0 | 117 | | $provider.Mappings[$ext] = $mime |
| | 118 | | } |
| 0 | 119 | | $Options.StaticFileOptions.ContentTypeProvider = $provider |
| | 120 | | } |
| | 121 | | } |
| | 122 | | # Add the file server to the server |
| | 123 | | # Use the KestrunHostStaticFilesExtensions to add the file server |
| 0 | 124 | | [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddFileServer($Server, $Options) | Out-Null |
| | 125 | |
|
| 0 | 126 | | if ($PassThru.IsPresent) { |
| | 127 | | # if the PassThru switch is specified, return the server instance |
| | 128 | | # Return the modified server instance |
| 0 | 129 | | return $Server |
| | 130 | | } |
| | 131 | | } |
| | 132 | | } |
| | 133 | |
|