-
Notifications
You must be signed in to change notification settings - Fork 8
/
mediacrush
executable file
·290 lines (257 loc) · 6.77 KB
/
mediacrush
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
282
283
284
285
286
287
288
289
290
#!/bin/bash
# MediaCrush (https://imgrush.com) uploader
# Jerome Leclanche <[email protected]>
version=1.2.0
server="https://imgrush.com"
create_album=false
dry_run=false
recursive=false
copy=false
uploads=""
usage() {
cat << EOF
usage: $(basename $0) $version [options] [files...]
Upload files to https://imgrush.com
For more details see man mediacrush
-a, --album Create an album from the uploaded files
--open Open the uploaded images (or the album if there is one) in the browser
after upload has completed
--dry-run Do not upload, only display the urls the files would be uploaded to.
-r, --recursive Recursively upload directories
--interactive Force interactive mode
--non-interactive Force non-interactive mode
-s, --server <server> Specifies an alternate MediaCrush server. (Default: https://imgrush.com)
-h, --help Display this help and exit
--version Output version information and exit
-c, --copy Send url to clipboard using xsel or xclip
EOF
}
err() {
echo "$@" 1>&2
}
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
GETOPT=`getopt -o hcars: --longoptions help,version,album,dry-run,open,recursive,copy,interactive,non-interactive,server: -- "$@"`
if [[ $? != 0 ]]; then
exit 1
fi
eval set -- "$GETOPT"
while true; do
case "$1" in
-h|--help)
usage
exit
;;
--version)
echo $(basename $0) $version
exit
;;
-a|--album)
create_album=true
shift 1
;;
--dry-run)
dry_run=true
shift 1
;;
--open)
open_files=true
shift 1
;;
-r|--recursive)
recursive=true
shift 1
;;
-s|--server)
server="$2"
shift 2
;;
-c|--copy)
copy=true
shift 1
;;
--interactive)
interactive=true
shift 1
;;
--non-interactive)
interactive=false
shift 1
;;
--)
shift
break
;;
*)
echo "Internal error"
exit 1
;;
esac
done
xxdrp() {
for i in `fold -2`; do
echo -ne "\x$i"
done;
}
calculate_hash() {
md5sum "$1" | cut -d " " -f 1 | xxdrp | base64 | tr "+/" "-_" | cut -c 1-12
}
get_error() {
echo "$1" | sed -nre 's#.*"error": ([0-9]+) .*#\1#p'
}
get_hash() {
echo "$1" | sed -nre 's#.*"hash": "([^"]+)".*#\1#p'
}
send() {
curl --silent "$@"
if [[ $? -ne 0 ]]; then
err "Could not reach $url"
return $?
fi
}
handle_response() {
hash=$(get_hash "$1")
if [[ -n "$hash" ]]; then
echo "$hash"
return 0
fi
error=$(get_error "$1")
case "$error" in
"415")
type=$(file --mime "$f")
err "The file type for $type is not supported"
return 415
;;
"420")
err "You are uploading too much, too fast. Try again later."
# abort everything, no sense trying to upload more
exit 420
;;
*)
err "Unknown error $error $1"
return 1
;;
esac
}
check_exists() {
url="$server/api/$1/exists"
send "$url" | sed -nre 's#.*"exists": (true|false)#\1#p'
}
upload_file() {
url="$server/api/upload/file"
out=$(send -F "file=@$1" "$url")
handle_response "$out"
}
upload_url() {
url="$server/api/upload/url"
out=$(send -F "url=$1" "$url")
handle_response "$out"
}
upload() {
if [ -z $interactive ]; then
if [ -t 1 ]; then
interactive=true
else
interactive=false
fi
fi
for f in "$@"; do
if [[ -d "$f" ]]; then
if [[ "$recursive" == "true" ]]; then
upload "$f"/*
else
err "omitting directory: $f"
continue
fi
elif [[ -f "$f" ]]; then
hash=$(calculate_hash "$f")
elif [[ "$f" == http://* || "$f" == https://* ]]; then
is_url="true"
else
err "No such file or directory: $f"
continue
fi
if [[ "$interactive" == true ]]; then
echo -n "Uploading $f ..."
echo -en "\r\033[K"
fi
if [[ "$dry_run" == "true" ]]; then
if [[ "$is_url" == "true" ]]; then
err "Dry-run not supported for URLs"
else
if [[ "$interactive" == true ]]; then
echo "Uploaded: $server/$hash"
else
echo "$server/$hash"
fi
fi
continue
fi
if [[ "$is_url" == "true" ]]; then
hash=$(upload_url "$f")
else
if [[ $(check_exists "$hash") != "true" ]]; then
hash=$(upload_file "$f")
fi
fi
if [[ "$uploads" == "" ]]; then
uploads="$hash"
else
uploads="$uploads,$hash"
fi
if [[ "$interactive" == true ]]; then
echo "Uploaded: $server/$hash"
else
echo "$server/$hash"
fi
done
if [[ "$create_album" == "true" ]]; then
if [[ "$dry_run" == "true" ]]; then
if [[ "$interactive" == true ]]; then
echo "Album: (not available)"
fi
else
if [[ "$interactive" == true ]]; then
echo -n "Creating album..."
echo -en "\r\033[K"
fi
out=$(send -F "list=$uploads" "$server/api/album/create")
hash=$(get_hash "$out")
if [[ "$interactive" == true ]]; then
echo "Album: $server/$hash"
else
echo "$server/$hash"
fi
if [[ "$open_files" == "true" ]]; then
xdg-open "$server/$hash"
fi
fi
elif [[ "$open_files" == "true" ]]; then
for upload in "$uploads"; do
xdg-open "$server/$upload"
done
fi
if [[ "$copy" == true ]]; then
if hash xsel 2>/dev/null; then
usecm=1
elif hash xclip 2>/dev/null; then
usecm=2
else
usecm=3
fi
case $usecm in
1)
echo -n "$server/$hash" | xsel -b
;;
2)
echo -n "$server/$hash" | xclip -selection clipboard
;;
3)
echo "In order to use the --copy argument, xsel or xclip must be installed."
;;
esac
fi
}
upload "$@"