-
Notifications
You must be signed in to change notification settings - Fork 1
/
post-image.sh
executable file
·86 lines (67 loc) · 1.78 KB
/
post-image.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
#!/usr/bin/env bash
cloud_config=${CLOUD_CONFIG:-"config"}
[[ -f "$HOME/.config/cloud/$cloud_config" ]] && source "$HOME/.config/cloud/$cloud_config"
cloud_bucket=${CLOUD_BUCKET?"Missing CLOUD_BUCKET config"}
cloud_domain=${CLOUD_DOMAIN?"Missing CLOUD_DOMAIN config"}
cloud_queue=${CLOUD_QUEUE?"Missing CLOUD_QUEUE config"}
cloud_profile=${CLOUD_PROFILE?"Missing CLOUD_PROFILE config"}
unset AWS_DEFAULT_REGION
echoerr() {
echo "$@" 1>&2
}
usage() {
echoerr "usage: $0 PATH"
echoerr ""
echoerr "Host an image in the cloud"
exit 1
}
if [[ $# == 0 ]]; then
usage
fi
if ! hash jq 2>/dev/null; then
echoerr "Needs more jq."
exit 1
fi
file=$1
if [[ ! -r $file ]]; then
echoerr "File $file does not exist or is not readable."
exit 1
fi
filename=$(basename "$file")
if ! aws s3 cp "$file" "s3://$cloud_bucket/assets/$filename" --profile "$cloud_profile" > /dev/null; then
echoerr "Upload failed!"
exit 1
fi
message_json=""
attempts=0
while [[ $attempts -lt 6 ]]; do
attempts=$(( attempts + 1 ))
message_json=$( aws sqs receive-message \
--wait-time-seconds 10 \
--queue-url "$cloud_queue" \
--profile "$cloud_profile"
)
if [[ -z $message_json ]]; then
echoerr "Failed to receive message with image information. Retrying..."
continue
fi
body=$( echo "$message_json" | jq -r '.Messages[].Body' )
handle=$( echo "$message_json" | jq -r '.Messages[].ReceiptHandle' )
if [[ $body =~ ^$filename= ]]; then
break
else
echoerr "Received a message but it wasn't for us!"
body=""
continue
fi
done
if [[ -z "$body" ]]; then
echoerr "Failed to fetch URL."
exit 1
fi
cloud_path=$( echo "$body" | cut -d= -f2 | tr -d ' ' )
echo -n "https://$cloud_domain/$cloud_path"
aws sqs delete-message \
--queue-url "$cloud_queue" \
--receipt-handle "$handle" \
--profile "$cloud_profile"