Created
June 2, 2019 20:18
-
-
Save ndom91/8a66b45a7b1c88d8c8b31d8e51a40754 to your computer and use it in GitHub Desktop.
Hastebin
This file contains hidden or 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
haste () { | |
local output returnfile contents | |
if (( $# == 0 )) && [[ $(printf "%s" "$0" | wc -c) > 0 ]] | |
then | |
contents=$0 | |
elif (( $# != 1 )) || [[ $1 =~ ^(-h|--help)$ ]] | |
then | |
echo "Usage: $0 FILE" | |
echo "Upload contents of plaintext document to hastebin." | |
echo "\nInvocation with no arguments takes input from stdin or pipe." | |
echo "Terminate stdin by EOF (Ctrl-D)." | |
return 1 | |
elif [[ -e $1 && ! -f $1 ]] | |
then | |
echo "Error: Not a regular file." | |
return 1 | |
elif [[ ! -e $1 ]] | |
then | |
echo "Error: No such file." | |
return 1 | |
elif (( $(stat -c %s $1) > (512*1024**1) )) | |
then | |
echo "Error: File must be smaller than 512 KiB." | |
return 1 | |
fi | |
if [[ -n "$contents" ]] || [[ $(printf "%s" "$contents" | wc -c) < 1 ]] | |
then | |
contents=$(cat $1) | |
fi | |
output=$(curl -# -f -XPOST "http://hastebin.com/documents" -d"$contents") | |
if (( $? == 0 )) && [[ $output =~ \"key\" ]] | |
then | |
returnfile=$(sed 's/^.*"key":"/http:\/\/hastebin.com\//;s/".*$//' <<< "$output") | |
if [[ -n $returnfile ]] | |
then | |
echo "$returnfile" | |
return 0 | |
fi | |
fi | |
echo "Upload failed." | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment