Skip to content

Instantly share code, notes, and snippets.

@torgro
Created February 17, 2018 23:29
Show Gist options
  • Select an option

  • Save torgro/420aebb4f75851c4fac2fb7ba12bb333 to your computer and use it in GitHub Desktop.

Select an option

Save torgro/420aebb4f75851c4fac2fb7ba12bb333 to your computer and use it in GitHub Desktop.

Revisions

  1. torgro created this gist Feb 17, 2018.
    50 changes: 50 additions & 0 deletions NewCreateFileEndpoint.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    $CreateFileEndpoint = New-UDEndpoint -Url "/file/" -Method "Post" -Endpoint {
    Param(
    $Authorization
    ,
    $FileName
    ,
    $Content
    )

    $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 ([string]::IsNullOrEmpty($Authorization))
    {
    throw "You shall not pass"
    }

    if (-not (Test-Path -Path $path))
    {
    $null = New-Item -Path $path -ItemType directory
    }

    if ($Secret -eq 'foo:bar')
    {
    $respons = [pscustomobject]@{
    Content = ""
    StatusCode = 200
    }

    $fullPath = Join-Path -Path $path -ChildPath $FileName

    Set-Content -Path $fullPath -Value $Content

    $file = Get-ChildItem -Path $fullPath
    $newFile = [pscustomobject]@{
    Name = $file.Name
    Size = $file.Length
    BaseName = $file.BaseName
    Extension = $file.Extension
    }
    $respons.Content = $newFile | ConvertTo-Json
    $respons | ConvertTo-Json
    }
    else
    {
    throw "You shall not pass"
    }
    }