|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# SUpload (Screenshot-Upload)
|
|
|
|
version="1.2.4"
|
|
|
|
|
|
|
|
## Colours
|
|
|
|
r='\033[0;31m'
|
|
|
|
nc='\033[0m'
|
|
|
|
|
|
|
|
## Default Configs
|
|
|
|
## WARNING: Do not change these files here, change them in the config file
|
|
|
|
|
|
|
|
debug=false
|
|
|
|
domain="https://cz0.au/"
|
|
|
|
sshot="spectacle"
|
|
|
|
photo_format="date +%F-%T.png"
|
|
|
|
video_format="date +%F-%T.mp4"
|
|
|
|
ffmpeg_config="-framerate 60 -preset veryfast -vcodec libx264 -pix_fmt yuv420p -vf crop=floor(iw/2)*2:floor(ih/2)*2 -movflags faststart -tune zerolatency -crf 25"
|
|
|
|
file_dir="${HOME}/Pictures/screenshots"
|
|
|
|
browser=false
|
|
|
|
clipboard=true
|
|
|
|
notification=true
|
|
|
|
log=true
|
|
|
|
log_file="${HOME}/.supload.log"
|
|
|
|
curl_options="--progress-bar"
|
|
|
|
|
|
|
|
## Read from user config if exists, else read from system-wide (just use inbuilt defaults if none)
|
|
|
|
### User config takes priority over system-wide
|
|
|
|
config=$HOME/.config/supload/settings.conf
|
|
|
|
if [ -f "${config}" ] ; then
|
|
|
|
source "${config}"
|
|
|
|
elif [ -f "/etc/supload.conf" ] ; then
|
|
|
|
source "/etc/supload.conf"
|
|
|
|
fi
|
|
|
|
|
|
|
|
## Check dependencies (in case of a bad installation)
|
|
|
|
type jq >/dev/null 2>&1 || { echo -e >&2 "${r}ERROR:${nc} I require jq but it's not installed. Aborting."; exit 1; }
|
|
|
|
type curl >/dev/null 2>&1 || { echo -e >&2 "${r}ERROR:${nc} I require curl but it's not installed. Aborting."; exit 1; }
|
|
|
|
|
|
|
|
## Create screenshot folder if in use.
|
|
|
|
mkdir -p "$file_dir"
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
|
|
|
Usage: supload [OPTIONS] [FILE]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-d Use custom domain use -d <domain>
|
|
|
|
-h Show this menu
|
|
|
|
-o Automatically open in browser
|
|
|
|
-s Screenshot mode (Area Selection)
|
|
|
|
-g Screen Recording Mode (ffcast)
|
|
|
|
-V Verbose (debug mode)
|
|
|
|
-v Shows current version of SUpload
|
|
|
|
|
|
|
|
Examples: https://git.volkor.me/Volkor/SUpload
|
|
|
|
|
|
|
|
EOF
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
## Actual file uploading
|
|
|
|
upload_file() {
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} BASE_URL: $base_url"
|
|
|
|
|
|
|
|
## if base_url is still unset (not using -d) set it as the config
|
|
|
|
if [ -z ${base_url+x} ]; then
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} BASE_URL unset, replacing with DOMAIN from config file"
|
|
|
|
base_url=$domain
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} BASE_URL now set to: $base_url"
|
|
|
|
fi
|
|
|
|
|
|
|
|
## 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"
|
|
|
|
if [[ "${response}" =~ "<html" ]]; then
|
|
|
|
echo -e "${r}ERROR:${nc} HTML Curl Error - Website responded in html (file too large? / bad domain?)"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} RESPONSE: $response"
|
|
|
|
|
|
|
|
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.
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} BROWSER: $browser"
|
|
|
|
if [ "$browser" = "true" ] ; then
|
|
|
|
echo "Opening browser"
|
|
|
|
hash xdg-open && xdg-open "$url" &>/dev/null || echo "${r}ERROR:${nc} xdg-open was not found, or failed to run."
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Clipboard handling
|
|
|
|
clipboard
|
|
|
|
|
|
|
|
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"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
touch "$log_file"
|
|
|
|
fi
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} LOG: $log"
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} LOG_FILE: $log_file"
|
|
|
|
upload_finished
|
|
|
|
}
|
|
|
|
|
|
|
|
upload_finished() {
|
|
|
|
|
|
|
|
if [ -z ${url+x} ]; then
|
|
|
|
echo -e "${r}ERROR:${nc} URL was not found in response."
|
|
|
|
exit 1
|
|
|
|
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
|
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
clipboard() {
|
|
|
|
# Check if the clipboard setting is enabled
|
|
|
|
if [[ "$clipboard" = "true" ]]; then
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} CLIPBOARD: $clipboard"
|
|
|
|
if [[ "$XDG_SESSION_TYPE" = "wayland" ]]; then
|
|
|
|
# Check if desktop session is "plasmawayland"
|
|
|
|
if [[ "$DESKTOP_SESSION" = "plasmawayland" ]]; then
|
|
|
|
# Use qdbus to set clipboard contents
|
|
|
|
if command -v qdbus >/dev/null 2>&1; then
|
|
|
|
qdbus org.kde.klipper /klipper setClipboardContents "${url}"
|
|
|
|
else
|
|
|
|
echo -e "${r}ERROR:${nc} qdbus not found, unable to copy to clipboard."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# Check if wl-copy is available
|
|
|
|
if command -v wl-copy >/dev/null 2>&1; then
|
|
|
|
# Remove newline character from the URL and copy to clipboard with wl-copy
|
|
|
|
echo "${url}" | tr -d '\n' | wl-copy
|
|
|
|
else
|
|
|
|
echo -e "${r}ERROR:${nc} wl-copy not found, unable to copy to clipboard. Please install wl-clipboard package."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# If wayland isn't set, fall back to x11
|
|
|
|
if command -v xclip >/dev/null 2>&1; then
|
|
|
|
echo "${url}" | tr -d '\n' | xclip -selection clipboard
|
|
|
|
echo "Copied URL to clipboard"
|
|
|
|
else
|
|
|
|
echo -e "${r}ERROR:${nc} xclip not found, unable to copy to clipboard."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
while getopts "d:hosgvV" opt; do
|
|
|
|
case $opt in
|
|
|
|
d)
|
|
|
|
## TODO: Check if the args are actually set.
|
|
|
|
base_url=${OPTARG}
|
|
|
|
;;
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
o)
|
|
|
|
browser=true
|
|
|
|
;;
|
|
|
|
s)
|
|
|
|
screenshot=true
|
|
|
|
;;
|
|
|
|
g)
|
|
|
|
video=true
|
|
|
|
;;
|
|
|
|
V)
|
|
|
|
debug=true
|
|
|
|
;;
|
|
|
|
v)
|
|
|
|
echo "SUpload Version: "$version
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo -e "${r}ERROR:${nc} Invalid Option."
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
file="${*:$OPTIND}"
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} OPTIONS: $*"
|
|
|
|
|
|
|
|
# Screenshot
|
|
|
|
if [ "$screenshot" = "true" ] ; then
|
|
|
|
echo "Click and hold to select screenshot area or click for window."
|
|
|
|
screen_path="$file_dir/$($photo_format)"
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} SCREEN_PATH: $screen_path"
|
|
|
|
|
|
|
|
case $sshot in
|
|
|
|
watershot)
|
|
|
|
screen_prog="watershot path" ;;
|
|
|
|
maim)
|
|
|
|
screen_prog="maim -s -m 10 -o" ;;
|
|
|
|
scrot)
|
|
|
|
screen_prog="scrot -s" ;;
|
|
|
|
spectacle)
|
|
|
|
screen_prog="spectacle -nbro" ;;
|
|
|
|
*)
|
|
|
|
echo -e "${r}ERROR:${nc} Invalid SSHOT in config, check the documentation"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} SSHOT: $sshot"
|
|
|
|
|
|
|
|
if ! $screen_prog "${screen_path}"; then
|
|
|
|
echo -e "${r}ERROR:${nc} Failed to take screenshot ($screen_prog crashed/user exited)"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
upload_file "${screen_path}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Video
|
|
|
|
if [ "$video" = "true" ] ; then
|
|
|
|
video_path="$file_dir/$($video_format)"
|
|
|
|
$debug && echo -e "${r}DEBUG:${nc} VIDEO_PATH: $video_path"
|
|
|
|
|
|
|
|
|
|
|
|
echo -e "${r}WARNING: PRESS ${nc}q${r} to end screen recording!${nc}"
|
|
|
|
if ! ffmpeg -f x11grab $(slop -f '-video_size %wx%h -i +%x,%y') $ffmpeg_config $video_path; then
|
|
|
|
echo -e "${r}ERROR:${nc} Failed to take screenshot (ffcast crashed/user exited)"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
upload_file "${video_path}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$1" = "" ] ; then
|
|
|
|
echo -e "${r}ERROR:${nc} You must supply either STDIN (-) or a file!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
upload_file "$file"
|