Skip to content

Instantly share code, notes, and snippets.

@torgro
Last active February 16, 2018 13:45
Show Gist options
  • Save torgro/a9b0c2cb1f4de1cfed3ef432d23e56b6 to your computer and use it in GitHub Desktop.
Save torgro/a9b0c2cb1f4de1cfed3ef432d23e56b6 to your computer and use it in GitHub Desktop.
$GetFileEndpoint = New-UDEndpoint -Url "/file/" -Method "GET" -Endpoint {
Param(
$Authorization
,
$Name
)
if ($request.headers.ContainsKey("Authorization"))
{
$Authorization = $request.headers["Authorization"].ToString()
}
if ($request.headers.ContainsKey("Name"))
{
$Name = $request.headers["Name"].ToString()
}
if ([string]::IsNullOrEmpty($Authorization))
{
throw "You shall not pass"
}
$secretBytes = [System.Convert]::FromBase64String(($Authorization -replace "Basic "))
[string]$Secret = [System.Text.Encoding]::UTF8.GetString($secretBytes)
$path = Join-Path -Path c: -ChildPath temp | Join-Path -ChildPath Api
if ($Secret -eq 'foo:bar')
{
$respons = [pscustomobject]@{
Content = ""
StatusCode = 200
}
$output = foreach ($f in (Get-ChildItem -Path $path))
{
$fileObj = [pscustomobject]@{
Name = $f.Name
Size = $f.Length
BaseName = $f.BaseName
Extension = $f.Extension
}
if ([string]::IsNullOrEmpty($Name))
{
$fileObj
}
else
{
if ($f.Name -like $Name)
{
$fileObj
}
}
}
$respons.Content = $output | ConvertTo-Json
$respons | ConvertTo-Json
}
else
{
throw "You shall not pass"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment