matze / wastebin

wastebin is a pastebin 📝
https://bin.bloerg.net
MIT License
303 stars 28 forks source link

Paste from PWSH - WT #53

Closed CrazyWolf13 closed 5 months ago

CrazyWolf13 commented 5 months ago

Hi

Awesome tool you made!!

I'd like to write a ps1 function that allows me to pipe into wastebin and then get the url-->

cat "Hello World!" | pasteToWaste

I tried this approach, but cannot really get anything to work, have you got an idea?

function pasteToWaste {
    if ($args.Count -eq 0) {
        Write-Error "No file path specified."
        return
    }

    $FilePath = $args[0]

    if (Test-Path $FilePath) {
        $Content = Get-Content $FilePath -Raw
    } else {
        Write-Error "File path does not exist."
        return
    }

    $uri = "http://bin.mydomain.tld"
    try {
        $response = Invoke-RestMethod -Uri $uri -Method Post -Body @{text=$Content} -ErrorAction Stop
        $url = "http://bin.mydomain.tld/$($response.key)"
        Write-Output $url
    } catch {
        Write-Error "Failed to upload the document. Error: $_"
    }
}
CrazyWolf13 commented 5 months ago
$WastebinServerUrl = "https://bin.mydomain.tld"
$DefaultExpirationTime = 3600  # Default expiration time: 1 hour (in seconds)
$DefaultBurnAfterReading = $false  # Default value for burn after reading setting

function ptw {
    [CmdletBinding()]
    param (
        [Parameter(Position=0)]
        [string]$FilePath,

        [Parameter(Position=1)]
        [int]$ExpirationTime = $DefaultExpirationTime,

        [Parameter(Position=2)]
        [bool]$BurnAfterReading = $DefaultBurnAfterReading
    )

    process {
        if (-not $FilePath) {
            Write-Host "File path not provided."
            return
        }

        if (-not (Test-Path $FilePath)) {
            Write-Host "File '$FilePath' not found."
            return
        }

        try {
            $FileContent = Get-Content -Path $FilePath -Raw

            $Payload = @{
                text = $FileContent
                extension = $null
                expires = $ExpirationTime
                burn_after_reading = $BurnAfterReading
            } | ConvertTo-Json

            $Response = Invoke-RestMethod -Uri $WastebinServerUrl -Method Post -Body $Payload -ContentType 'application/json'
            $Path = $Response.path -replace '\.\w+$'

            Write-Host ""
            Write-Host "$WastebinServerUrl$Path"
        }
        catch {
            Write-Host "Error occurred: $_"
        }
    }
}
function pptw {
    param (
        [Parameter(ValueFromPipeline=$true)]
        [string]$InputContent,
        [int]$ExpirationTime = $DefaultExpirationTime,
        [bool]$BurnAfterReading = $DefaultBurnAfterReading
    )
    begin {
        $AllInputContent = @()  # Array to store all lines of input
    }
    process {
        $AllInputContent += $InputContent  # Add each line to the array
    }

    end {
        try {
            # Concatenate all lines into a single string
            $CombinedInput = $AllInputContent -join "`r`n"

            $Payload = @{
                text = $CombinedInput
                extension = $null
                expires = $ExpirationTime
                burn_after_reading = $BurnAfterReading
            } | ConvertTo-Json

            $Response = Invoke-RestMethod -Uri $WastebinServerUrl -Method Post -Body $Payload -ContentType 'application/json'
            $Path = $Response.path -replace '\.\w+$'

            Write-Host ""
            Write-Host "$WastebinServerUrl$Path"
        }
        catch {
            Write-Host "Error occurred: $_"
        }
    }
}

I could solve it myself, if anyone has a similar problem, the above worked for me, the function ptw (PasteToWaste) takes ptw {filepath} {time} {true/false(burn after read)} and pptw is when using the pipe, like this: cat test.txt | pptw