Skip to content

Instantly share code, notes, and snippets.

@BobbyWibowo
Created September 10, 2022 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobbyWibowo/e68a6552ddd7470b914fb0add7409b44 to your computer and use it in GitHub Desktop.
Save BobbyWibowo/e68a6552ddd7470b914fb0add7409b44 to your computer and use it in GitHub Desktop.
#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
@BobbyWibowo
Copy link
Author

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment