Skip to content

Instantly share code, notes, and snippets.

@jadedgnome
Created November 9, 2014 21:55
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 jadedgnome/312649a924d85aa1b765 to your computer and use it in GitHub Desktop.
Save jadedgnome/312649a924d85aa1b765 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Upload text/images to clbin.com from the command line
# License: ISC http://www.isc.org/downloads/software-support-policy/isc-license/
set -euf
clip() {
if command -v xclip &> /dev/null; then
xclip -selection clip <<< "$@"
elif command -v xsel &> /dev/null; then
xsel -b -i <<< "$@"
fi
}
defer(){
#close enough
trapstr="$1;${trapstr:-exit}"
trap "$trapstr" INT TERM EXIT KILL
}
get_imgfile(){
tmp=$(mktemp)
touch "$tmp"
trap "rm -f $tmp" INT TERM EXIT KILL
cat "${1?must provide file as \$1}" > "$tmp"
mime="$(file --mime-type "$tmp")"
mime="${mime##* }"
case "$mime" in
image/png|image/jpg|image/jpeg|image/gif)
newtmp=${tmp}.${mime##*/}
mv "$tmp" "$newtmp"
echo "$newtmp"
exit 0;;
*)
exit 0;;
esac
}
if [[ $# != 0 ]]; then
if [[ $@ == "-h" || $@ == "--help" ]]; then
echo "Usage:"
echo " clbin <file>"
echo " <command> | clbin"
echo " 'clbin' alone reads STDIN till ^D is pressed"
echo "Supported files: text files and png/jpg/jpeg/gif images"
exit 1;
fi
[[ ! -e "$1" ]] && echo "File not found." && exit 2;
img=$(get_tmpfile "$1")
trap "rm -f $img" INT TERM EXIT KILL
mime="$(file --mime-type "$1")"
mime="${mime##* }"
case "$mime" in
image/png|image/jpg|image/jpeg|image/gif)
resp="$(curl -sfF "clbin=@$img" https://clbin.com)"
;;
text/*)
resp="$(cat "$1" | curl -sfF "clbin=<-" https://clbin.com)"
;;
*)
echo "Unsupported file type: $mime"
exit 3;;
esac
if [[ "$?" != 0 ]]; then
echo "An error has occured. curl exit code: $?"
exit 4
fi
else
this=$(mktemp)
trap "rm -f $this" INT TERM EXIT KILL
cat > "$this"
img=$(get_imgfile "$this")
if test -z "$img";then
resp="$(cat "$this"| curl -sfF "clbin=<-" https://clbin.com)"
else
resp="$(curl -sfF "clbin=@${img}" https://clbin.com)"
fi
fi
echo "$resp"
clip "$resp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment