-
Notifications
You must be signed in to change notification settings - Fork 1
/
wshare.sh
executable file
·281 lines (224 loc) · 5.62 KB
/
wshare.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
set \
-o errexit \
-o pipefail \
-o nounset
shopt -s \
nullglob \
expand_aliases
readonly WSHARE_VERSION=""
readonly WSHARE_HOME="$HOME/.wshare"
readonly WSHARE_BIN="$HOME/bin/wshare"
readonly ERR_UNSUPPORTED_OS=1
readonly ERR_INVALID_USAGE=2
readonly ERR_FILE_NOT_FOUND=3
die() {
local message="$1"
local -i exit_code="${2:-1}"
echo "Error $exit_code: $message" >&2
exit $exit_code
}
alias call="eval"
clipboard_copy() {
if [[ -n "$(type -t pbcopy)" ]]; then
pbcopy
elif [[ -n "$(type -t xsel)" ]]; then
xsel --clipboard --input
elif [[ -n "$(type -t xclip)" ]]; then
xclip -selection clipboard
else
:
fi
}
result() {
declare -p $* | sed "s/^declare /local /"
}
upload() {
local file="$1"
[[ -r "$file" ]] || die "File $file does not exist or is not accessible" $ERR_FILE_NOT_FOUND
local response="$(
cat "$file" \
| gzip \
| curl -s \
"http://0paste.com/pastes.txt" \
-F "paste[is_private]=1" \
-F "paste[paste_gzip]=<-" \
)"
local regex="\(http://0paste.com/\([[:digit:]]\{1,\}\(-[[:alnum:]]\{1,\}\)\{0,\}\)\)\( (key: \([[:alnum:]]\{1,\}\))\)\{0,\}"
local upload_id="$(echo "$response" | sed -n "s%$regex%\2%p")"
local upload_url="$(echo "$response" | sed -n "s%$regex%\1%p").txt"
local upload_key="$(echo "$response" | sed -n "s%$regex%\5%p")"
result upload_id upload_url upload_key
}
shorten() {
local url="$1"
local -i ttl="${2:-5}"
local response="$(curl -s -d "url=$url" -d "ttl=$((ttl * 60))" "http://shoutkey.com/new")" || die "Cannot access the URL shortener service"
local regex=".*<h1>\<a href=\"\(http://shoutkey.com/[[:alpha:]]\{1,\}\)\">\([[:alpha:]]\{1,\}\)</a></h1>.*"
local shorturl_url="$(echo "$response" | sed -n "s%$regex%\1%p")"
local shorturl_key="$(echo "$response" | sed -n "s%$regex%\2%p")"
local shorturl_ttl=$ttl
result shorturl_url shorturl_key shorturl_ttl
}
delete() {
local file
local upload_id
local upload_key
local shorturl_url
local response
for file; do
[[ "$file" == "$WSHARE_HOME"* ]] || continue
upload_id="$(basename "$file")"
{
read -r upload_key
while read -r shorturl_url; do
echo "Deleting $shorturl_url at http://0paste.com/$upload_id with key $upload_key"
done
} < "$file"
response="$(curl -sL "http://0paste.com/$upload_id" -F "_method=delete" -F "paste[key]=$upload_key")"
rm "$file"
rmdir "$(dirname "$file")" 2>/dev/null || true
done
rmdir "$WSHARE_HOME" 2>/dev/null || true
}
cleanup() {
[[ -d "$WSHARE_HOME" ]] || return
local ttl_dir
local upload_path
local upload_id
local upload_key
local ttl
local -i any=0
for ttl_dir in "$WSHARE_HOME"/*; do
if [[ -n "$ttl_dir" && -d "$ttl_dir" ]]; then
if [[ "$(ls -A "$ttl_dir")" ]]; then
ttl="$(basename "$ttl_dir")"
find "$ttl_dir"/* -maxdepth 0 -type f -mmin +$ttl -exec $0 --delete {} \;
any=1
fi
fi
done
if [[ $any -eq 0 ]]; then
echo "No expired uploads found"
fi
}
share() {
local uri="$1"
local ttl="${2:-5}"
[[ $ttl -ge 5 ]] || die "TTL cannot be less than 5 minutes"
if [[ "$uri" =~ ^http?:// ]]; then
local upload_url="$uri"
else
call "$(upload "$uri")"
fi
call "$(shorten "$upload_url" "$ttl")"
if [[ -n "${upload_id:-}" ]]; then
local db_dir="$WSHARE_HOME/$shorturl_ttl"
mkdir -p "$db_dir"
local db_file="$db_dir/$upload_id"
if [[ -n "$upload_key" ]]; then
cat > "$db_file" <<-EOF
$upload_key
$shorturl_url
EOF
elif [[ -w "$db_file" ]]; then
echo "$shorturl_url" >> "$db_file"
fi
fi
echo -n "$shorturl_url" | clipboard_copy
echo "$shorturl_url"
}
get_latest_version() {
basename "$(curl -s -o /dev/null -I -w "%{redirect_url}" https://github.com/nikolay/wshare/releases/latest)"
}
show_usage() {
cat <<-EOF
Usage: $(basename $0) COMMAND
Commands:
-h|--help Shows usage
-c|--clean|--cleanup Deletes expired uploads
[-s|--share] FILE|URL [TTL] Shares a file/URL; TTL in minutes (default: 5)
-u|--upgrade|--update Upgrades wshare to the latest version
-d|--delete FILE... Deletes files under $WSHARE_HOME (used internally)
EOF
}
usage() {
show_usage
exit $ERR_INVALID_USAGE
}
main() {
[[ $# -gt 0 ]] || usage
local command="${1:-}"
case "$command" in
("-h" | "--help")
shift
show_usage
;;
("-c" | "--clean" | "--cleanup")
shift
cleanup
;;
("-d" | "--delete")
shift
delete "$@"
;;
("-u" | "--upgrade" | "--update")
shift
install
;;
("-s" | "--share")
shift
share "$@"
;;
(*)
main --share "$@"
;;
esac
}
assignment() {
local variable="${1:-}"
local value="${2:-}"
echo "$variable=\"$value\""
}
do_install() {
mkdir -p "$(dirname "$WSHARE_BIN")"
rm -f "$WSHARE_BIN"
local self="${BASH_EXECUTION_STRING:-$(curl -sL git.io/wshare)}"
echo "${self/$(assignment WSHARE_VERSION)/$(assignment WSHARE_VERSION "$(get_latest_version)")}" > "$WSHARE_BIN"
chmod +x "$WSHARE_BIN"
}
check_install() {
[[ "$(type -t wshare)" == "file" ]]
}
install() {
local os="$(uname)"
case "$os" in
(Linux | Darwin)
[[ "$(whoami)" != "root" ]] || die "Don't install with sudo or as root"
local -i upgrade=0
check_install && upgrade=1
do_install
if ! check_install; then
cat <<-EOF
In order to make wshare work, you need to add the following
to your .bashrc/.bash_profile/.profile file:
export PATH="$HOME/bin:$PATH"
EOF
fi
local version="$(get_latest_version)"
if [[ $upgrade -eq 1 ]]; then
echo "Successfully upgraded wshare to $version"
else
echo "Successfully installed wshare $version"
fi
;;
(*)
die "Unsupported OS: $os"
;;
esac
}
if [[ -z "${BASH_SOURCE[0]:-}" ]]; then
install
else
main "$@"
fi