forked from eoli3n/upaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
up
executable file
·153 lines (132 loc) · 2.78 KB
/
up
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env bash
autocopy=0
if [ ! -x /usr/bin/curl ]; then
printf "\n"
echo "Install curl!"
printf "\n"
exit 1
fi
# Functions
usage() {
cat <<- EOF
Paste client
Usage :
-h Print this usage
-a Enable autocopy in clipboard
-t Set extension
Use a \${XDG_CONFIG_HOME}/upaste.conf file to select a different paste
service. List all URL choices there, one in each line. If a single URL
is present that will be chosen. If more than one is present, a random
working one will be chosen.
Default is https://0x0.st/
Working instances are: https://x0.at/, https://0x0.st/,
https://xzz.dk/, https://envs.sh/ and https://null.slipfox.xyz/
Examples:
echo "Text here" | up
cat file.[ext] | up [-t] [ext]
up filename.[ext]
Links are logged in \${XDG_CACHE_HOME}/upaste_history
EOF
}
conf_dir=${XDG_CONFIG_HOME:-$HOME/.config}
cache_dir=${XDG_CACHE_HOME:-$HOME/.cache}
urlcheck(){
if [ -f "$conf_dir"/upaste.conf ]; then
while read -r url
do
printf "$url"
curl -sf "$url" > /dev/null 2>&1
if [ ! $? = 6 ];then
echo "$url is up"
echo "$url" >> "$cache_dir"/upaste.tmp
else
echo "$url is down"
fi
done < "$conf_dir"/upaste.conf
uploadurl=$(shuf -n 1 "$cache_dir"/upaste.tmp)
echo "Choosing $uploadurl"
rm -- "$cache_dir"/upaste.tmp
else
uploadurl=https://0x0.st/
echo "Choosing the default $uploadurl"
fi
}
upload(){
urlcheck
if [ -n "$extension" ]; then
ext="filename=.$extension"
fi
if [ -z "$1" ]; then
file="file=@-"
if [ -z "$ext" ]; then
ext="filename=.txt"
fi
else
file="file=@\"$1\""
fi
args="$file;$ext"
curl -sF "$args" "$uploadurl"|tee -a "$cache_dir"/upaste_history
}
autocopy(){
if [ -n "$DISPLAY" ]; then
if [ "$XDG_SESSION_TYPE" = "x11" ]; then
if which xclip >/dev/null 2>&1; then
echo "$1" | xclip
fi
fi
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
if which wl-copy >/dev/null 2>&1; then
wl-copy -p "$1"
fi
fi
fi
}
is_file(){
if [ ! -f "$(realpath "$1")" ]; then
echo "File \"$1\" does not exist or is not a regular file"
exit 2
fi
}
main(){
if [ -t 0 ]; then
if [ -z "$1" ]; then
url=$(upload)
printf "\n"
else
if [ -n "$1" ]; then
is_file "$1"
fi
url=$(upload "$1")
fi
else
if [ -n "$1" ]; then
echo "Warning: ignoring file $1..."
fi
url=$(upload)
fi
echo "$url"
if [ "$autocopy" = "1" ]; then
autocopy "$url"
fi
}
while getopts "hat:" arg
do
case $arg in
a)
autocopy=1
;;
h)
usage
exit
;;
t)
extension=${OPTARG}
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
main "$@"