-
Notifications
You must be signed in to change notification settings - Fork 13
/
hastebin.sh
42 lines (42 loc) · 1.06 KB
/
hastebin.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
}