Skip to content

Instantly share code, notes, and snippets.

@GermainZ
Last active March 27, 2022 04:30
Show Gist options
  • Save GermainZ/9177423 to your computer and use it in GitHub Desktop.
Save GermainZ/9177423 to your computer and use it in GitHub Desktop.
Upload text/images to clbin.com from the command line
#!/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
}
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 $@ ]] && echo "File not found." && exit 2;
mime="$(file --mime-type "$@")"
mime="${mime##* }"
case "$mime" in
image/png|image/jpg|image/jpeg|image/gif)
resp="$(curl -sfF "clbin=@$@" https://clbin.com)"
;;
text/*)
resp="$(cat "$@" | 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
resp="$(cat "$@" | curl -sfF "clbin=<-" https://clbin.com)"
fi
echo "$resp"
clip "$resp"
@achilleas-k
Copy link

Send URL to clipboard if xclip is installed
https://clbin.com/sLx2s

EDIT: Handles files with space in the name

@GermainZ
Copy link
Author

GermainZ commented Mar 6, 2014

Thanks, I've cleaned up your revision a bit and added a check for xsel as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment