-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-dir.sh
executable file
·110 lines (97 loc) · 2.64 KB
/
upload-dir.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
#set -o xtrace
print_usage()
{
printf "Uploads all files in a directory.\n"
printf "\n"
printf "Usage: %s options [TARGET_URI]\n" "$0"
printf "\n"
printf "Options:\n"
printf " -f, --cert-pem-file CERT_FILE .pem file with the WebID certificate of the agent\n"
printf " -p, --cert-password CERT_PASSWORD Password of the WebID certificate\n"
printf " -b, --base BASE_URI Base URI of the application\n"
printf "\n"
printf " -d, --directory ABS_PATH Absolute path to the directory\n"
printf " -r, --recurse Recurse into sub-directories\n"
}
hash curl 2>/dev/null || { echo >&2 "curl not on \$PATH. Aborting."; exit 1; }
recurse=false
args=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-f|--cert-pem-file)
cert_pem_file="$2"
shift # past argument
shift # past value
;;
-p|--cert-password)
cert_password="$2"
shift # past argument
shift # past value
;;
-b|--base)
base="$2"
shift # past argument
shift # past value
;;
-d|--directory)
directory="$2"
shift # past argument
shift # past value
;;
-r|--recurse)
recurse=true
shift # past argument
;;
*) # unknown arguments
args+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${args[@]}" # restore args
if [ -z "$cert_pem_file" ] ; then
print_usage
exit 1
fi
if [ -z "$cert_password" ] ; then
print_usage
exit 1
fi
if [ -z "$base" ] ; then
print_usage
exit 1
fi
if [ -z "$directory" ] ; then
print_usage
exit 1
fi
urlencode()
{
# python -c 'from urllib.parse import urlencode; import sys; print(urlencode(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1]))' "$1"
python2 -c 'import urllib, sys; print urllib.quote(sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read()[0:-1])' "$1"
}
ns="${base}ns/domain/system#"
class="${ns}File"
forClass=$(urlencode "$class")
container="${base}files/"
# if target URL is not provided, it equals container
if [ -z "$1" ] ; then
target="${container}?forClass=${forClass}"
else
target="${1}?forClass=${forClass}"
fi
if [ "$recurse" = true ] ; then
maxdepth=""
else
maxdepth="-maxdepth 1"
fi
upload_file="./upload-file.sh \
-f $cert_pem_file \
-p $cert_password \
-b $base \
--title \`basename {}\` \
--file {}"
find "$directory" $maxdepth -type f -exec bash -c "$upload_file" \;