| | | 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 | | #> |
| | | 32 | | function 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 | | # Only works inside a route script block where $Context is available |
| | 0 | 48 | | if ($null -ne $Context.Response) { |
| | | 49 | | try { |
| | | 50 | | # Check if the Context.Response is available |
| | 0 | 51 | | if ($null -ne $Context.Response) { |
| | | 52 | | # Resolve the file path relative to the Kestrun root if necessary |
| | 0 | 53 | | $resolvedPath = Resolve-KrPath -Path $FilePath -KestrunRoot -Test |
| | 0 | 54 | | Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath" |
| | | 55 | | # Set the content disposition type if specified |
| | 0 | 56 | | if ($ContentDisposition -ne [Kestrun.Models.ContentDispositionType]::NoContentDisposition) { |
| | 0 | 57 | | $Context.Response.ContentDisposition.Type = $ContentDisposition.ToString() |
| | | 58 | | } |
| | | 59 | | # Set the file download name if specified |
| | 0 | 60 | | if (!([string]::IsNullOrEmpty($FileDownloadName))) { |
| | 0 | 61 | | $Context.Response.ContentDisposition.FileName = $FileDownloadName |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | # Call the C# method on the $Context.Response object |
| | 0 | 65 | | $Context.Response.WriteFileResponse($resolvedPath, $ContentType, $StatusCode) |
| | 0 | 66 | | Write-Information "File response written for $FilePath with download name $FileDownloadName" |
| | | 67 | | } |
| | | 68 | | } catch { |
| | | 69 | | # Handle any errors that occur during the file response writing |
| | 0 | 70 | | Write-KrLog -Level Error -Message 'Error writing file response.' -ErrorRecord $_ |
| | | 71 | | } |
| | | 72 | | } else { |
| | 0 | 73 | | Write-KrOutsideRouteWarning |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |