< Summary - Kestrun — Combined Coverage

Information
Class: Public.Response.Write-KrFileResponse
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Write-KrFileResponse.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
0%
Covered lines: 0
Uncovered lines: 10
Coverable lines: 10
Total lines: 72
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/Response/Write-KrFileResponse.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Sends a file as the HTTP response.
 4
 5    .DESCRIPTION
 6        Writes a file from disk to the response body. The file path is resolved
 7        relative to the Kestrun root if required. Additional options allow
 8        specifying the download name, forcing inline display and custom content
 9        type.
 10    .PARAMETER FilePath
 11        The path to the file to send in the response. This can be an absolute path
 12        or a relative path from the Kestrun root.
 13    .PARAMETER ContentType
 14        The content type of the file being sent. If not specified, it will be determined
 15        based on the file extension.
 16    .PARAMETER StatusCode
 17        The HTTP status code to set for the response. Defaults to 200 (OK).
 18    .PARAMETER FileDownloadName
 19        The name to use for the file when downloaded. If not specified, the original
 20        file name will be used.
 21    .PARAMETER ContentDisposition
 22        Specifies how the content should be presented in the response. Options include
 23        inline and attachment.
 24    .EXAMPLE
 25        Write-KrFileResponse -FilePath "C:\path\to\file.txt" -ContentType "text/plain" -StatusCode 200 -FileDownloadName
 26        Sends the file at "C:\path\to\file.txt" as a downloadable attachment
 27        with the name "download.txt" and a content type of "text/plain". The response
 28        status code is set to 200 (OK).
 29    .NOTES
 30        This function is designed to be used in the context of a Kestrun server response.
 31#>
 32function Write-KrFileResponse {
 33    [KestrunRuntimeApi('Route')]
 34    [CmdletBinding()]
 35    param(
 36        [Parameter(Mandatory = $true)]
 37        [string]$FilePath,
 38        [Parameter()]
 39        [string]$ContentType,
 40        [Parameter()]
 41        [int]$StatusCode = 200,
 42        [Parameter()]
 43        [string]$FileDownloadName,
 44        [Parameter()]
 45        [Kestrun.Models.ContentDispositionType]$ContentDisposition = [Kestrun.Models.ContentDispositionType]::NoContentD
 46    )
 47
 48    try {
 49        # Check if the Context.Response is available
 050        if ($null -ne $Context.Response) {
 51            # Resolve the file path relative to the Kestrun root if necessary
 052            $resolvedPath = Resolve-KrPath -Path $FilePath -KestrunRoot -Test
 053            Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath"
 54            # Set the content disposition type if specified
 055            if ($ContentDisposition -ne [Kestrun.Models.ContentDispositionType]::NoContentDisposition) {
 056                $Context.Response.ContentDisposition.Type = $ContentDisposition.ToString()
 57            }
 58            # Set the file download name if specified
 059            if (!([string]::IsNullOrEmpty($FileDownloadName))) {
 060                $Context.Response.ContentDisposition.FileName = $FileDownloadName
 61            }
 62
 63            # Call the C# method on the $Context.Response object
 064            $Context.Response.WriteFileResponse($resolvedPath, $ContentType, $StatusCode)
 065            Write-Information "File response written for $FilePath with download name $FileDownloadName"
 66        }
 67    } catch {
 68        # Handle any errors that occur during the file response writing
 069        Write-KrLog -Level Error -Message 'Error writing file response.' -ErrorRecord $_
 70    }
 71}
 72

Methods/Properties

Write-KrFileResponse()