< Summary - Kestrun — Combined Coverage

Information
Class: Public.Service.Add-KrFileServer
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Service/Add-KrFileServer.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
0%
Covered lines: 0
Uncovered lines: 31
Coverable lines: 31
Total lines: 133
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-KrFileServer.ps1

#LineLine coverage
 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#>
 40function 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
 080        $Server = Resolve-KestrunServer -Server $Server
 081        if ($null -eq $Server) {
 082            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 83        }
 84    }
 85    process {
 086        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 087            $Options = [Microsoft.AspNetCore.Builder.FileServerOptions]::new()
 88
 089            if (-not [string]::IsNullOrEmpty($RequestPath)) {
 090                $Options.RequestPath = [Microsoft.AspNetCore.Http.PathString]::new($RequestPath.TrimEnd('/'))
 91            }
 092            if (-not [string]::IsNullOrEmpty($RootPath)) {
 093                $resolvedPath = Resolve-KrPath $RootPath -KestrunRoot
 094                $Options.FileProvider = [Microsoft.Extensions.FileProviders.PhysicalFileProvider]::new($resolvedPath)
 95            }
 096            if ($EnableDirectoryBrowsing.IsPresent) {
 097                $Options.EnableDirectoryBrowsing = $true
 98            }
 099            if ($ServeUnknownFileTypes.IsPresent) {
 0100                $Options.ServeUnknownFileTypes = $true
 101            }
 0102            if ($HttpsCompression.IsPresent) {
 0103                $Options.HttpsCompression = $true
 104            }
 0105            if (-not [string]::IsNullOrEmpty($DefaultContentType)) {
 0106                $Options.DefaultContentType = $DefaultContentType
 107            }
 0108            if ($RedirectToAppendTrailingSlash.IsPresent) {
 0109                $Options.RedirectToAppendTrailingSlash = $true
 110            }
 0111            if ($ContentTypeMap) {
 0112                $provider = [Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider]::new()
 0113                foreach ($k in $ContentTypeMap.Keys) {
 0114                    $ext = if ($k -like ".*") { $k } else { ".$k" }
 0115                    $mime = [string]$ContentTypeMap[$k]
 0116                    if ([string]::IsNullOrWhiteSpace($mime)) { continue }
 0117                    $provider.Mappings[$ext] = $mime
 118                }
 0119                $Options.StaticFileOptions.ContentTypeProvider = $provider
 120            }
 121        }
 122        # Add the file server to the server
 123        # Use the KestrunHostStaticFilesExtensions to add the file server
 0124        [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddFileServer($Server, $Options) | Out-Null
 125
 0126        if ($PassThru.IsPresent) {
 127            # if the PassThru switch is specified, return the server instance
 128            # Return the modified server instance
 0129            return $Server
 130        }
 131    }
 132}
 133

Methods/Properties

Add-KrFileServer()