-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpomfload
executable file
·104 lines (88 loc) · 3.15 KB
/
pomfload
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
#!/bin/sh
# Requires jq (https://stedolan.github.io/jq): sudo pacman -S jq
usage() {
printf %s\\n \
"Usage: $(basename -- "$0") [OPTIONS] --upload upload-url [--] FILE [FILES...]" \
"Options:" \
" --upload URL -u The endpoint to upload to" \
" --download URL -d The prefix to add to the URL the server returns" \
" --[no-]progress -p/-n [Do not] show a progress indicator" \
" --upload-field FIELD -f The form field the server expects the file to be in" \
" --raw-reply -r Expect a single URL from the server rather than JSON" \
" --redirects -R Expect a redirection from the server rather than JSON" \
" -- Marks end of options" \
>&2
}
[ "$#" -lt 1 ] && { usage; exit 1; }
if tty > /dev/null; then
show_progress=true
else
show_progress=false
fi
upload_url=
download_url=
upload_field='files[]'
raw_reply=false
redirects=false
eval "set -- $(getopt -n "$0" -o u:d:f:rRpn -l upload:,download:,upload-field:,raw-reply,redirects,progress,no-progress -- "$@")"
while [ "$#" != 0 ]; do
case $1 in
--upload|-u) upload_url=$2; shift;;
--download|-d) download_url=$2; shift;;
--progress|-p) show_progress=true;;
--no-progress|-n) show_progress=false;;
--upload-field|-f) upload_field=$2; shift;;
--raw-reply|-r) raw_reply=true;;
--redirects|-R) redirects=true;;
--) break;;
*) usage; exit 1;;
esac; shift
done; shift
newline='
'
$redirects && raw_reply=true
send_file() {
local f
f=$1
set -- -sS
if $redirects; then
set -- "$@" -w "%{url_effective}" -L -o /dev/null
fi
if $show_progress; then
set -- "$@" --progress-bar
fi
curl "$@" -F "$upload_field=@$f" -- "${upload_url:?}"
}
result=0
for f do
if ! pomf_reply="$(send_file "$f")"; then
echo "Failed to upload $f" >&2
printf %s\\n "$url" >&2 # Maybe it contains some error message now?
result=1
else
if $raw_reply; then
pomf_id=$pomf_reply
else
if ! pomf_reply_parsed=$(printf %s\\n "$pomf_reply" | jq -r '.success, .error, .files[0].url'); then
echo "Failed to parse uploader response to $f" >&2
printf "Response:\n%s\n" "$pomf_reply" >&2
result=1
continue
fi
pomf_success=${pomf_reply_parsed%%"$newline"*}; pomf_reply_parsed=${pomf_reply_parsed#*"$newline"}
pomf_error_message=${pomf_reply_parsed%%"$newline"*}; pomf_reply_parsed=${pomf_reply_parsed#*"$newline"}
pomf_id=$pomf_reply_parsed
if [ "$pomf_success" != true ]; then
echo "Failed to upload $f" >&2
echo "Server error: $pomf_error_message" >&2
result=1
continue
fi
fi
url=$download_url$pomf_id
[ "$#" -gt 1 ] && printf "%s: " "$f"
printf %s\\n "$url"
printf '%s\t%s\t%s\n' "$(date --rfc-3339=seconds)" "$url" "$f" >> ~/.pomf_history
fi
done
exit "$result"