Skip to content

Instantly share code, notes, and snippets.

@jadedgnome
Forked from makefu/clbin
Last active August 29, 2015 14:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jadedgnome/494f9b76a0d20c1cd77b 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/
clip() {
if command -v xclip &> /dev/null; then
xclip -selection clip <<< "$@"
elif command -v xsel &> /dev/null; then
xsel -b -i <<< "$@"
fi
}
get_imgfile(){
mime="$(file --mime-type "${1?must provide file as \$1}")"
mime="${mime##* }"
case "$mime" in
image/png|image/jpg|image/jpeg|image/gif)
newtmp=${tmp}.${mime##*/};
cp "$tmp" "$newtmp";
echo "$newtmp";;
esac
exit 0;
}
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