Public/Send-RyverFile.ps1
function Send-RyverFile { <# .SYNOPSIS Send a Ryver file .DESCRIPTION Send a Ryver file .PARAMETER Token Token to use for the Ryver API Default value is the value set by Set-PSRyverConfig .PARAMETER Content Content of the file to send. Text is editable after sending. .Parameter FileType If specified, override the FileType determined by the filename. List of types: https://api.ryver.com/types/file#file_types .PARAMETER Channel Optional channel, private group, or IM channel to send file to. Can be an encoded ID, or a name. .PARAMETER FileName Required filename for this file. Used to determine syntax highlighting and other functionality. .PARAMETER Title Optional title of the file .PARAMETER Comment Optional initial comment for the file .PARAMETER ForceVerbose If specified, don't explicitly remove verbose output from Invoke-RestMethod *** WARNING *** This will expose your token in verbose output .EXAMPLE Send-RyverFile -Token $Token ` -Channel general ` -Content 'get-help about_* | get-random' ` -FileType perl ` -filename example.ps1 # Send a ryver file that turns into a snippet. # Use perl, because PowerShell syntax highlighting is sad .EXAMPLE Send-RyverFile -Channel '@tlindsay42' ` -path C:\homer.gif ` -title homer # Send a gif to @tlindsay42 using a previously-stored token (Set-PSRyverConfig) .FUNCTIONALITY Ryver #> [CmdletBinding(DefaultParameterSetName = 'Content' )] param ( [String] $Token = $Script:PSRyver.Token, [Parameter( ParameterSetName = 'Content', Mandatory = $true)] [String] $Content, [validatescript({Test-Path -PathType Leaf -Path $_})] [Parameter( ParameterSetName = 'File', Mandatory = $true)] [String] $Path, [Parameter( ParameterSetName = 'Content' )] [String] $FileType, [String[]]$Channel, [String] $FileName, [String] $Title, [String] $Comment, [Switch]$ForceVerbose = $Script:PSRyver.ForceVerbose ) process { if ($Content) { $body = @{} switch ($psboundparameters.keys) { 'Content' {$body.content = $content} 'Channel' {$body.channels = $Channel -join ", " } 'FileName' {$body.filename = $FileName} 'Title' {$body.Title = $Title} 'Comment' {$body.initial_comment = $Comment} 'FileType' {$body.filetype = $FileType} } Write-Verbose "Invoke-RyverRestMethod -Body $($body | Format-List | Out-String)" $Params = @{ Method = 'files.upload' Body = $Body Token = $Token ForceVerbose = $ForceVerbose } $response = Invoke-RyverRestMethod @Params } else { $fileName = (Split-Path -Path $Path -Leaf) $uri = 'https://ryver.com/api/files.upload' if ($IsCoreCLR) { # PowerShell Core implementation $multipartContent = [System.Net.Http.MultipartFormDataContent]::new() # Add file contents $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data') $fileHeader.Name = 'file' $fileHeader.FileName = $FileName $fileStream = [System.IO.FileStream]::new($Path, [System.IO.FileMode]::Open) $fileContent = [System.Net.Http.StreamContent]::new($fileStream) $fileContent.Headers.ContentDisposition = $fileHeader $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse('multipart/form-data') $multipartContent.Add($fileContent) # Add token $tokenHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data') $tokenHeader.Name = 'token' $tokenContent = [System.Net.Http.StringContent]::new($token) $tokenContent.Headers.ContentDisposition = $tokenHeader $multipartContent.Add($tokenContent) switch ($psboundparameters.keys) { 'Channel' { # Add channel $channelHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data') $channelHeader.Name = 'channels' $channelContent = [System.Net.Http.StringContent]::new(($Channel -join ', ')) $channelContent.Headers.ContentDisposition = $channelHeader $multipartContent.Add($channelContent) } 'FileName' { # Add file name $filenameHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data') $filenameHeader.Name = 'filename' $filenameContent = [System.Net.Http.StringContent]::new($FileName) $filenameContent.Headers.ContentDisposition = $filenameHeader $multipartContent.Add($filenameContent) } 'Title' { # Add title $titleHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data') $titleHeader.Name = 'title' $titleContent = [System.Net.Http.StringContent]::new($Title) $titleContent.Headers.ContentDisposition = $titleHeader $multipartContent.Add($titleContent) } 'Comment' { # Add comment $commentHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data') $commentHeader.Name = 'initial_comment' $commentContent = [System.Net.Http.StringContent]::new($Comment) $commentContent.Headers.ContentDisposition = $commentHeader $multipartContent.Add($commentContent) } } try { $response = Invoke-RestMethod -Uri $uri -Method Post -Body $multipartContent } catch [System.Net.WebException] { Write-Error( "Rest call failed for $uri`: $_" ) throw $_ } finally { $fileStream.Close() } } else { # Legacy Windows PowerShell implementation $LF = "`r`n" $readFile = [System.IO.File]::ReadAllBytes($Path) $enc = [System.Text.Encoding]::GetEncoding("iso-8859-1") $fileEnc = $enc.GetString($readFile) $boundary = [System.Guid]::NewGuid().ToString() $bodyLines = "--$boundary$LF" + "Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"$LF" + "Content-Type: 'multipart/form-data'$LF$LF" + "$fileEnc$LF" + "--$boundary$LF" + "Content-Disposition: form-data; name=`"token`"$LF" + "Content-Type: 'multipart/form-data'$LF$LF" + "$token$LF" switch ($psboundparameters.keys) { 'Channel' {$bodyLines += ("--$boundary$LF" + "Content-Disposition: form-data; name=`"channels`"$LF" + "Content-Type: multipart/form-data$LF$LF" + ($Channel -join ", ") + $LF)} 'FileName' {$bodyLines += ("--$boundary$LF" + "Content-Disposition: form-data; name=`"filename`"$LF" + "Content-Type: multipart/form-data$LF$LF" + "$FileName$LF")} 'Title' {$bodyLines += ("--$boundary$LF" + "Content-Disposition: form-data; name=`"title`"$LF" + "Content-Type: multipart/form-data$LF$LF" + "$Title$LF")} 'Comment' {$bodyLines += ("--$boundary$LF" + "Content-Disposition: form-data; name=`"initial_comment`"$LF" + "Content-Type: multipart/form-data$LF$LF" + "$Comment$LF")} } $bodyLines += "--$boundary--$LF" try { $Params = @{ Uri = $uri Method = 'Post' ContentType = "multipart/form-data; boundary=`"$boundary`"" Body = $bodyLines } if(-not $ForceVerbose) { $Params.Add('Verbose', $false) } if($ForceVerbose) { $Params.Add('Verbose', $true) } $response = Invoke-RestMethod @Params } catch [System.Net.WebException] { Write-Error( "Rest call failed for $uri`: $_" ) throw $_ } } } $response } } |