-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathupload
executable file
·105 lines (89 loc) · 2.22 KB
/
upload
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
#!/bin/bash -e
# Usage info
show_help() {
cat <<EOF
Usage: ${0##*/} [-hn] [-d date] [FILE]...
-n no manipulation
-h display this help and exit
-d date date other than today's date
EOF
}
# Initialize our own variables:
nomanipulation=0
date=$(date +%Y-%m-%d)
OPTIND=1 # Reset is necessary if getopts was used previously in the script. It is a good idea to make this local in a function.
while getopts "nhd:" opt; do
case "$opt" in
h)
show_help
exit 0
;;
n)
nomaniuplation=$((nomaniuplation + 1))
;;
d)
date=$(date -d "$OPTARG" +%Y-%m-%d)
;;
'?')
show_help >&2
exit 1
;;
esac
done
shift "$((OPTIND - 1))" # Shift off the options and optional --.
tmp=$(mktemp -u)
mkdir -p "$tmp/$date"
trap 'rm -rf "$tmp"' EXIT
for src in "$@"; do
if test -d "$src"; then
tar="$(basename $src).tar"
tar cvf "$tar" "$src"
src="$tar"
fi
if ! test -f "$src"; then
echo Missing filename "$src" >&2
continue
fi
chmod +r "$src"
test $nomaniuplation || case $(file "$src") in
*JPEG*)
if hash cwebp; then
webptmp=$(mktemp --suffix=.webp)
cwebp "$src" -o $webptmp
echo "Squashing jpeg $(du -h "$src" "$webptmp")"
src=${src%.*}.webp
mv "$webptmp" "$src"
fi
;;
*MOV*)
if hash ffmpeg; then
mp4tmp=$(mktemp -d)/${source_file##*/}
target_file="$(mktemp -d)/${src##*/}"
mp4tmp="${target_file%.*}.mp4"
ffmpeg -i "$src" -c:v libx265 -tag:v hvc1 -c:a aac -b:a 192k $mp4tmp
echo "Squashing mov $(du -h "$src" "$mp4tmp")"
src=${mp4tmp}
fi
;;
*PNG*)
hash pngquant && pngquant --ext .png -f "$src"
;;
*)
echo Not compressing $src
;;
esac
dst=$(basename "$src")
cp -v "$src" "$tmp/$date/$dst"
if ! aws sts get-caller-identity --profile mine; then
BROWSER="chrome --profile-directory='Profile 2' %s" aws sso login --sso-session mine
fi
if aws --profile mine s3 cp --acl public-read "$tmp/$date/$dst" "s3://s.natalian.org/$date/"; then
logger "https://s.natalian.org/$date/$dst"
if hash pbcopy 2>/dev/null; then
echo -n "https://s.natalian.org/$date/$dst" | pbcopy
else
echo -n "https://s.natalian.org/$date/$dst" | xsel -b
fi
aws --profile mine cloudfront create-invalidation --distribution-id E18Y8IGITQI2OG --paths "/$date/$dst"
fi
done