| | 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 | |
|
| | 48 | | try { |
| | 49 | | # Check if the Context.Response is available |
| 0 | 50 | | if ($null -ne $Context.Response) { |
| | 51 | | # Resolve the file path relative to the Kestrun root if necessary |
| 0 | 52 | | $resolvedPath = Resolve-KrPath -Path $FilePath -KestrunRoot -Test |
| 0 | 53 | | Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath" |
| | 54 | | # Set the content disposition type if specified |
| 0 | 55 | | if ($ContentDisposition -ne [Kestrun.Models.ContentDispositionType]::NoContentDisposition) { |
| 0 | 56 | | $Context.Response.ContentDisposition.Type = $ContentDisposition.ToString() |
| | 57 | | } |
| | 58 | | # Set the file download name if specified |
| 0 | 59 | | if (!([string]::IsNullOrEmpty($FileDownloadName))) { |
| 0 | 60 | | $Context.Response.ContentDisposition.FileName = $FileDownloadName |
| | 61 | | } |
| | 62 | |
|
| | 63 | | # Call the C# method on the $Context.Response object |
| 0 | 64 | | $Context.Response.WriteFileResponse($resolvedPath, $ContentType, $StatusCode) |
| 0 | 65 | | 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 |
| 0 | 69 | | Write-KrLog -Level Error -Message 'Error writing file response.' -ErrorRecord $_ |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|