The read method will let users either list all files or get the content of one specific file. So, we will need to create a new GET route. The data is returned in a JSON structure as well. Take a look at the following example, within which the new route that delivers a list of file objects is created:
New-PolarisGetRoute -Path "/files" -Scriptblock {
$gciParameters = @{
Path = $Request.PolarisPath
Filter = '*'
ErrorAction = 'SilentlyContinue'
}
$gciParameters.Filter = if ($request.Query['Name'])
{
$request.Query['Name']
}
elseif ($request.Body.Name)
{
$request.Body.Name
}
$files = Get-ChildItem @gciParameters | Select-Object -Property @{Name = 'Name'; Expression = {$_.Name}},@{Name = 'Content'; Expression = {$_ | Get-Content -Raw}}
$Response.Send(($files | ConvertTo-Json));
} -Force
The read method uses either query parameters (such as ?Name=fileName.txt) or a JSON request body to filter files for the user. If neither is specified, we return all existing files. Again, the Polaris middleware ensures that the correct folder is used.