Instantly share code, notes, and snippets.
Last active
July 12, 2022 11:55
-
Star
4
(4)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save Xenthys/7179779c7a4ae0fc428e578226deca7f to your computer and use it in GitHub Desktop.
Shell convenience script for ShareXen
This file contains 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
#!/bin/sh | |
# Shell convenience script for ShareXen | |
# Source: https://github.com/Xenthys/ShareXen | |
# GNU/Linux dependencies: jq curl maim xclip notify-send | |
# - jq: parse ShareXen API JSON results | |
# - curl: query the ShareXen API itself | |
# - maim: take screenshots | |
# - xclip: copy URL to clipboard | |
# - notify-send: notify once uploaded | |
# OSX dependencies: jq curl screencapture pbcopy terminal-notifier | |
# - jq: parse ShareXen API JSON results | |
# - curl: query the ShareXen API itself | |
# - screencapture: take screenshots | |
# - pbcopy: copy URL to clipboard | |
# - terminal-notifier: notify once uploaded | |
###################### | |
# USER CONFIGURATION # | |
###################### | |
ADDRESS='https://your.domain/sharexen.php' | |
TOKEN='change-me' | |
#[ Settings below are optional ]# | |
#[ and can be empty or unset ]# | |
# Save generated URLs | |
LOGFILE="$HOME/.sharexen.log" | |
# Locally store screenshots | |
FOLDER="$HOME/Pictures/ShareXen" | |
############################# | |
# END OF USER CONFIGURATION # | |
############################# | |
is_mac() | |
{ | |
uname | grep -q "Darwin" | |
} | |
notify() | |
{ | |
if is_mac | |
then | |
if command_exists "terminal-notifier" | |
then | |
note=$(echo $1 | sed 's/\\n/ /g') | |
terminal-notifier -title "ShareXen" -message "$note" | |
fi | |
else | |
if command_exists "notify-send" | |
then | |
notify-send "ShareXen" "$1" | |
fi | |
fi | |
} | |
log() | |
{ | |
MSG="[$(date '+%Y-%m-%d %T')] $1" | |
if [ -z "${1##Error:*}" ] | |
then | |
notify "$1" | |
echo >&2 "$MSG" | |
else | |
echo "$MSG" | |
fi | |
[ -z "$LOGFILE" ] || echo "$MSG" >> "$LOGFILE" 2>/dev/null | |
} | |
command_exists() | |
{ | |
command -v "$1" >/dev/null 2>&1 | |
} | |
ensure_command_exists() | |
{ | |
for cmd in "$@" | |
do | |
if ! command_exists "$cmd" | |
then | |
log "Error: $cmd is required but not installed. Aborting." | |
exit 1 | |
fi | |
done | |
} | |
ensure_command_exists "jq" "curl" | |
capture() | |
{ | |
if is_mac | |
then | |
ensure_command_exists "screencapture" | |
screencapture -io "$1" | |
else | |
ensure_command_exists "maim" | |
maim -us "$1" | |
fi | |
} | |
clipboard() | |
{ | |
result="$(echo "$1" | tr -d "\r\n")" | |
if is_mac | |
then | |
if ! command_exists "pbcopy" | |
then | |
return 1 | |
fi | |
echo "$result" | pbcopy | |
else | |
if ! command_exists "xclip" | |
then | |
return 1 | |
fi | |
echo "$result" | xclip -i -sel c -f | xclip -i -sel p | |
fi | |
} | |
if [ -z "$UPLOAD_FILE" ] | |
then | |
NAME="sharexen_${USER}_$(date +%s%N).png" | |
cd "/tmp" && capture "$NAME" || exit 2 | |
else | |
[ -f "$UPLOAD_FILE" ] && NAME="$UPLOAD_FILE" || exit -1 | |
fi | |
PRMS="-sL -F endpoint=upload" | |
[ $# -gt 0 ] && PRMS="$PRMS -F filename=$1" | |
if ! RES=$(curl $PRMS -F "image=@$NAME" -F "token=$TOKEN" "$ADDRESS") | |
then | |
log "Error: upload failed (curl). Please check your token and script address." | |
exit 3 | |
fi | |
STATUS=$(echo "$RES" | jq -r ".status") | |
if [ "$STATUS" = "success" ] | |
then | |
URL=$(echo "$RES" | jq -r ".url") | |
DEL=$(echo "$RES" | jq -r ".deletion_url") | |
MSG="Image uploaded" | |
if clipboard "$URL" | |
then | |
MSG="$MSG and URL copied to clipboard" | |
else | |
MSG="$MSG successfully" | |
fi | |
notify "$MSG.\nLink: $URL" | |
log "Uploaded $URL successfully. Deletion URL: $DEL" | |
[ -z "$UPLOAD_FILE" ] || exit 0 | |
if [ -z "$FOLDER" ] | |
then | |
rm "$NAME" 2>/dev/null | |
else | |
mkdir -p "$FOLDER" && cd "$FOLDER" || exit 4 | |
mv "/tmp/$NAME" "$(echo "$RES" | jq -r ".filename")" || exit 5 | |
fi | |
else | |
ERR=$(echo "$RES" | jq -r ".error") | |
log "Error: upload failed ($ERR)." | |
exit 6 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment