-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.sh
executable file
·88 lines (79 loc) · 3.16 KB
/
upload.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
#!/bin/bash
cr=`echo $'\n.'`
cr=${cr%.}
usage(){
echo
echo "Usage: $0 <rootDir> <baseUrl> <username:password>"
echo
echo " rootDir The base directory to recursively upload xml files from. This script uses the path of each file to determine the type (i.e. 'collections' or 'granules' must be in the path)."
echo " baseUrl The target host and context path, e.g. for a locally-running OneStop API: http://localhost/onestop/api/registry"
echo " username:password (optional) The username and password for basic auth protected endpoints."
echo
exit 1
}
postToRegistry() { # assumes API_BASE, AUTH are defined, UUID and FILE are read on stdin (use pipe to send file input)
local COLLECTION_UUID=""
local outputDir="output"
`mkdir $outputDir`
while read UUID FILE; do
local TYPE="collection"
local MEDIA="xml"
if [[ $FILE = *"collection"* ]]; then
COLLECTION_UUID="$UUID"
fi
if [[ $FILE =~ ".json" ]]; then
MEDIA="json"
fi
if [[ $FILE = *"granule"* ]]; then
TYPE="granule"
fi
local UPLOAD="$API_BASE/metadata/$TYPE/$UUID"
echo "`date` - Uploading $MEDIA $FILE with $UUID to $UPLOAD"
local outputDir="output"
local response=""
if [[ -z $AUTH ]] ; then
response=`curl -k -L -sS $UPLOAD -H "Content-Type: application/$MEDIA" --data-binary "@$FILE" --create-dirs -o output/$UUID -w "%{http_code}" --silent --output /dev/null`
if [[ $TYPE == "granule" && $MEDIA == "xml" ]]; then
echo "`date` - Patching relationship $MEDIA $FILE with $COLLECTION_UUID to $UPLOAD"
response=`curl --request PATCH -k -L -sS $UPLOAD -H "Content-Type: application/json" -d "{\"relationships\":[{\"id\":\"$COLLECTION_UUID\",\"type\":\"COLLECTION\"}]}" --create-dirs -o output/$UUID -w "%{http_code}" --silent --output /dev/null`
fi
else
response=`curl -k -u $AUTH -L -sS $UPLOAD -H "Content-Type: application/$MEDIA" --data-binary "@$FILE" --create-dirs -o output/$UUID -w "%{http_code}" --silent --output /dev/null`
if [[ $TYPE == "granule" && $MEDIA == "xml" ]]; then
echo "`date` - Patching relationship $MEDIA $FILE with $COLLECTION_UUID to $UPLOAD"
response=`curl --request PATCH -k -u $AUTH -L -sS $UPLOAD -H "Content-Type: application/json" -d "{\"relationships\":[{\"id\":\"$COLLECTION_UUID\",\"type\":\"COLLECTION\"}]}" --create-dirs -o output/$UUID -w "%{http_code}" --silent --output /dev/null`
fi
fi
printf "HTTP Response: %s\n\n" "$response"
done
}
postItems(){
while read MANIFEST; do
if [[ $API_BASE ]]; then
echo "Begin upload..."
cat $MANIFEST | postToRegistry
echo "Finished upload."
else echo "No files uploaded. Specify a URL to upload files."
fi
done
}
ARGS_COUNT=$#
if [[ $ARGS_COUNT -eq 2 || $ARGS_COUNT -eq 3 ]]; then
#args
BASEDIR=$1
MANIFESTS=$(find $BASEDIR -name "manifest.txt")
API_BASE=$2
AUTH=$3
# display configuration settings
echo $cr
echo "Working config - confirm before proceeding."
echo "BASEDIR - $BASEDIR"
echo "MANIFEST FILES - $MANIFESTS"
echo "API_BASE - $API_BASE"
echo "AUTH - $AUTH"
echo $cr
# upload
echo $MANIFESTS | xargs -n 1 | postItems
else
usage
fi