< Summary - Kestrun — Combined Coverage

Information
Class: Public.Service.Add-KrStaticFilesService
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Service/Add-KrStaticFilesService.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 125
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Service/Add-KrStaticFilesService.ps1

#LineLine coverage
 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#>
 39function 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
 076        $Server = Resolve-KestrunServer -Server $Server
 077        if ($null -eq $Server) {
 078            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 79        }
 80    }
 81    process {
 082        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 083            $Options = [Microsoft.AspNetCore.Builder.StaticFileOptions]::new()
 084            if (-not [string]::IsNullOrEmpty($RootPath)) {
 085                $resolvedPath = Resolve-KrPath $RootPath -KestrunRoot
 086                $Options.FileProvider = [Microsoft.Extensions.FileProviders.PhysicalFileProvider]::new($resolvedPath)
 87            }
 088            if (-not [string]::IsNullOrEmpty($RequestPath)) {
 089                $Options.RequestPath = [Microsoft.AspNetCore.Http.PathString]::new($RequestPath.TrimEnd('/'))
 90            }
 091            if ($ServeUnknownFileTypes.IsPresent) {
 092                $Options.ServeUnknownFileTypes = $true
 93            }
 094            if ($HttpsCompression.IsPresent) {
 095                $Options.HttpsCompression = $true
 96            }
 097            if (-not [string]::IsNullOrEmpty($DefaultContentType)) {
 098                $Options.DefaultContentType = $DefaultContentType
 99            }
 0100            if ($RedirectToAppendTrailingSlash.IsPresent) {
 0101                $Options.RedirectToAppendTrailingSlash = $true
 102            }
 0103            if ($ContentTypeMap) {
 0104                $provider = [Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider]::new()
 0105                foreach ($k in $ContentTypeMap.Keys) {
 0106                    $ext = if ($k -like ".*") { $k } else { ".$k" }
 0107                    $mime = [string]$ContentTypeMap[$k]
 0108                    if ([string]::IsNullOrWhiteSpace($mime)) { continue }
 0109                    $provider.Mappings[$ext] = $mime
 110                }
 0111                $Options.StaticFileOptions.ContentTypeProvider = $provider
 112            }
 113        }
 114
 0115        [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddStaticFiles($Server, $Options) | Out-Null
 116        # Add the static file service to the server
 117
 0118        if ($PassThru.IsPresent) {
 119            # if the PassThru switch is specified, return the server instance
 120            # Return the modified server instance
 0121            return $Server
 122        }
 123    }
 124}
 125

Methods/Properties

Add-KrStaticFilesService()