Created
September 10, 2022 17:56
-
-
Save BobbyWibowo/e68a6552ddd7470b914fb0add7409b44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 7 | |
# For simplicity's sake, this only accepts uploading 1 file at a time | |
Param( | |
[Parameter(ValueFromPipeline)][string] $File, | |
[string] $Token, | |
[int] $AlbumID, | |
[switch] $Silent = $False | |
) | |
IF (!$File) { | |
Write-Error "Missing -File argument." | |
Exit 1 | |
} | |
# Constants | |
Set-Variable MAX_RETRIES -Option Constant -Value 3 | |
# Build request headers dictionary | |
$Headers = @{ | |
"Accept" = "application/json" | |
"token" = $Token | |
"albumid" = "$AlbumID" | |
} | |
$Form = @{ | |
"files[]" = (Get-Item -Path $File) | |
} | |
# Upload file within For-loop | |
For ($i = 1; $i -le $MAX_RETRIES; $i++) { | |
If (!$Silent) { | |
Write-Output "Uploading ($i/$MAX_RETRIES)..." | |
} | |
$Request = (Invoke-WebRequest ` | |
-Method "POST" ` | |
-Uri "https://safe.fiery.me/api/upload" ` | |
-Headers $Headers ` | |
-Form $Form) | |
# Parse response body as JSON | |
$Parsed = $Request.Content | Out-String | ConvertFrom-Json | |
# Break For-loop early if successful | |
If ($Parsed.success) { | |
Break | |
} | |
} | |
# Output URL | |
Write-Output $Parsed.files.url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires PowerShell 7+
Sample usage with foo_discord_rich > Advanced > Artwork upload command:
pwsh.exe -Command "\path\to\SimpleUpload.ps1 -File $Input -Token YOUR_TOKEN_HERE -AlbumID YOUR_ALBUM_ID_HERE -Silent"