Skip to content
Snippets Groups Projects
Verified Commit 500629cd authored by Volkor Barbarian Warrior's avatar Volkor Barbarian Warrior
Browse files

update to support API betterer

parent 2cb824da
No related branches found
Tags 1.2.5
No related merge requests found
Pipeline #59 failed
stages:
# - test
- package
# test:
# stage: test
# image: alpine:latest
# tags:
# - docker
# cache:
# paths:
# - binaries/*.apk
# script:
# - apk update && apk add curl libnotify bash jq
# - bash
# - cp supload.sh /usr/bin/supload
# - supload -v
# - supload -h
# - echo "test" | supload -V -
# - supload -V LICENSE
# - supload -V supload.sh
# - supload -Vd https://volkor.me supload.sh
package:linux:deb:
stage: package
image: cdrx/fpm-debian:latest
......
......@@ -23,6 +23,9 @@
#domain="https://cz0.au/"
#domain="https://api.teknik.io/v1/Upload"
## An option for API Keys, if unset, will not send any authorisation.
#api_key="set-me"
## Open screenshots in browser by default (-o is useless when true)
#browser=false
......
#!/bin/bash
# SUpload (Screenshot-Upload)
version="1.2.4"
version="1.2.5"
## Colours
r='\033[0;31m'
......@@ -73,17 +73,56 @@ upload_file() {
## Upload to server (with proper error handling!)
echo "Uploading File: ($1)"
response=$(curl $curl_options -F file=@"$1" "$base_url") || { echo -e >&2 "${r}ERROR:${nc} Curl upload failure. (Invalid file, or bad internet connection)"; exit 1; }
$debug && echo -e "${r}DEBUG:${nc} RESONSE: $response"
# Don't send an API Key if not configured.
if [ -n "$api_key" ]; then
response=$(curl $curl_options -H "Authorization: Bearer $api_key" -F file=@"$1" "$base_url") || { echo -e >&2 "${r}ERROR:${nc} Curl upload failure. (Invalid file, or bad internet connection)"; exit 1; }
else
response=$(curl $curl_options -F file=@"$1" "$base_url") || { echo -e >&2 "${r}ERROR:${nc} Curl upload failure. (Invalid file, or bad internet connection)"; exit 1; }
fi
$debug && echo -e "${r}DEBUG:${nc} RESPONSE: $response"
# Sometimes the server will have a panic attack and respond in HTML anyway.
if [[ "${response}" =~ "<html" ]]; then
echo -e "${r}ERROR:${nc} HTML Curl Error - Website responded in html (file too large? / bad domain?)"
echo -e "${r}ERROR:${nc} HTML Curl Error - Website responded in html (file too large? / wrong domain?)"
exit 1
fi
$debug && echo -e "${r}DEBUG:${nc} RESPONSE: $response"
# Parse the JSON error responses
error_code=$(echo "$response" | jq -r '.error')
case "$error_code" in
"MissingAPIKey")
echo -e "${r}ERROR:${nc} Missing API Key"
exit 1
;;
"InvalidAPIKey")
echo -e "${r}ERROR:${nc} Invalid API Key"
exit 1
;;
"APIKeyRevoked")
reason=$(echo "$response" | jq -r '.reason')
echo -e "${r}ERROR:${nc} API Key Revoked - $reason"
exit 1
;;
"DatabaseFailure")
echo -e "${r}ERROR:${nc} Database Failure"
exit 1
;;
"FileUploadServerError")
echo -e "${r}ERROR:${nc} File Upload Server Error"
exit 1
;;
"BadRequest")
echo -e "${r}ERROR:${nc} Bad Request"
exit 1
;;
*)
# No matching error found. Do nothing.
;;
esac
# Parse the URL from the reply.
url=$(echo "$response" | jq -r '..? | .url // empty') # https://github.com/stedolan/jq/issues/354 allows third-party uploading
deletionlink=$(echo "$response" | jq -r '..? | .adminurl // empty') # VERY likely broken for most third-party sites
$debug && echo -e "${r}DEBUG:${nc} URL: $url"
## Browser, clipboard and logging.
......@@ -96,6 +135,7 @@ upload_file() {
# Clipboard handling
clipboard
# Logging
if [ -f "${log_file}" ]; then
if [ "$log" = "true" ]; then
echo -n "$(date +"[%Y-%m-%d %H:%M:%S]")" >> "$log_file" && echo " Upload Response: $response" >> "$log_file"
......@@ -116,13 +156,7 @@ upload_finished() {
else
echo "URL: ${url}"
fi
if [ -z ${deletionlink+x} ]; then
echo -e "${r}DEBUG:${nc} Deletion URL not found :("
else
echo "Deletion URL: ${deletionlink}"
fi
if [ "$notification" = "true" ] ; then
type notify-send >/dev/null 2>&1 || { echo -e >&2 "${r}Warning:${nc} Notifications are enabled but notify-send is not installed - giving up!"; exit 1; }
notify-send "File uploaded." "URL: ${url}" & exit 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment