-
Notifications
You must be signed in to change notification settings - Fork 10
/
cloud-dl
executable file
·265 lines (243 loc) · 9.33 KB
/
cloud-dl
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
#!/bin/bash
version="1.7.1"
conf="$HOME/.cloudconf"
list(){
dir=$(echo $1 | sed 's/ /%20/g')
res=$(curl $extra_opts $options -X PROPFIND $dav_url/$dir 2>/dev/null)
[ $? -gt 0 ] && echo '[!] Error listing files' && exit
quota=$(parse_xml "$res" "d:quota-available-bytes" | uniq | sort | tail -n 1)
[ -z $trail ] || trail=${trail}/
if [ ! -z "$quota" ]; then
[ "$quota" -lt 0 ] && quota="Unlimited" || quota="$((quota/(1024*1024)))"MB
echo -en "\t\t\t[Quota available: $quota]\n"
fi
echo
l="$(parse_xml "$res" "d:href" | sed -e "s|/$trail$path||g" -e 's/%20/ /g')"
echo "$l" | grep --color=never -e "/$"
echo "$l" | grep --color=never -v -e "/$"
}
download(){
[ -z "$1" ] && echo "[!] Error. No file specified" && exit
fname=$(basename "$1")
pname=$(echo $1 | sed 's/ /%20/g')
if [ $(exists $pname) == 1 ];then
echo "[+] Downloading $fname..."
curl --progress-bar -o "$fname" $extra_opts $options -X GET $dav_url/$pname
[ $? -gt 0 ] && echo "[!] Error downloading the file" || echo "[+] Done"
else
echo "[!] File does not exist"
fi
}
upload(){
[ -z "$1" ] && echo "[!] Error. No file specified" && exit
file=$(echo $1 | sed 's/ /%20/g')
target=$(echo $2 | sed 's/ /$20/g')
bname=$(basename "$file")
if [ -f "$1" ];then
echo "[+] Uploading '$1'..."
curl --progress-bar $extra_opts $options -T "$1" $dav_url/$target/$bname >/dev/null
[ $? -eq 0 ] && echo "[+] Done" || echo "[!] Error uploading the file. Verify target directory exists"
else
echo "[!] File '$1' not found"
fi
}
delete(){
[ -z "$1" ] && echo "[!] Error. No file specified" && exit
read -p "Are you sure you want to delete '$1'? [y/n]: " response
[ "$response" != "y" ] && exit
file=$(echo $1 | sed 's/ /%20/g')
echo "[+] Deleting '$1'..."
curl $extra_opts $options -X DELETE $dav_url/$file 2>/dev/null
[ $? -eq 0 ] && echo "[+] Done" || echo "[!] There was an error deleting '$1'"
}
_mkdir(){
[ -z "$1" ] && echo "[!] Error. No directory specified" && exit
dirs=$(echo "$1" | tr '/' ' ')
for d in $dirs
do
dir=${dir}/$(echo $d | sed 's/ /%20/g')
curl $extra_opts $options -X MKCOL $dav_url/$dir 2>/dev/null
done
[ $? -eq 0 ] && echo "[+] Done" || echo "[!] Unable to create directory"
}
_mv(){
[ -z "$1" ] && echo "[!] Error. No file specified" && exit
[ -z "$2" ] && echo "[!] Error. You must specify destination" && exit
source=$(echo $1 | sed 's/ /%20/g')
target=$(echo $2 | sed 's/ /%20/g')
[ $(exists $1) == 0 ] && echo "[!] Error. File $1 does not exist" && exit
typ=$(exists $2)
if [ $typ == 2 ];then
echo "[!] Error. The destination has to include the file name"
exit
elif [ $typ == 1 ];then
read -p "'$2' already exists. Overwrite? [y/n]: " response
[ "$response" != "y" ] && exit
fi
curl $extra_opts $options -X MOVE $dav_url/$source --header "Destination: $dav_url/$target" 2>/dev/null
[ $? -eq 0 ] && echo "[+] Done" || echo "[!] Unable to move file"
}
list_shares(){
res=$(curl $options $ocs_header $OCS_SHARE_API/shares 2>/dev/null)
[ $? -gt 0 ] && echo "[!] There was an error listing shares" && exit
type_a=();path_a=();url_al=()
[[ "$res" == *"<statuscode>404</statuscode>"* ]] && echo "[!] Unable to list shares" && exit
for t in $(parse_xml "$res" "item_type"); do type_a+=($t); done
for p in $(parse_xml "$res" "path" | sed 's/ /\*\*space\*\*/g'); do path_a+=($p); done
for url in $(parse_xml "$res" "url" | sed 's/\&/\&/g'); do url_a+=($url); done
for((n=0;n<${#type_a[@]};++n)); do
[ "${type_a[$n]}" == "folder" ] && ftype='D' || ftype='F'
path=$(echo "${path_a[$n]}" | sed 's/\*\*space\*\*/ /g')
echo "[$ftype] $path => ${url_a[$n]}"
done
[[ "$n" == "0" ]] && echo "[*] No shares found"
}
share(){
[ -z "$1" ] && echo "[!] Error. No file specified" && exit
shr_pwd=""
for arg in "$@"
do
[[ $arg == "-p" ]] && read -sp "Password: " pwd && shr_pwd="--data-urlencode password=$pwd" && echo
[[ $arg == "-q" ]] && quiet=1
done
res=$(curl $options $ocs_header --data-urlencode "path=$1" --data-urlencode "shareType=3" $shr_pwd $OCS_SHARE_API/shares 2>/dev/null )
[ $? -gt 0 ] && echo "[!] There was an error sharing '$1'" && exit
message=$(parse_xml "$res" "message")
status=$(parse_xml "$res" "statuscode")
[[ "$status" != "200" ]] && echo "[!] Unable to share '$1'. $message" && exit
url=$(parse_xml "$res" "url" | sed 's/\&/\&/g')
url_line=""
[ -z $quiet ] && echo "[+] Done" && url_line="[+] URL: "
url_line="${url_line}${url}"
echo $url_line
}
unshare(){
[ -z "$1" ] && echo "[!] Error. No share specified" && exit
path=$(echo $1 | sed 's/ /%20/g')
res=$(curl $options $ocs_header $OCS_SHARE_API/shares?path=$path 2>/dev/null)
[ $? -gt 0 ] && echo "[!] There was an error getting id" && exit
[[ "$res" == *"<statuscode>404</statuscode>"* ]] && echo "[!] Unable to delete share '$1'. Wrong path" && exit
id=$(parse_xml "$res" "id")
curl $options $ocs_header -X DELETE "$OCS_SHARE_API/shares/$id" &>/dev/null
echo "[+] Done"
}
exists(){ # if exists (1 = file, 2 = dir, 0 = doesn't exist)
res=$(list "$1" | grep -v "Quota available" | head -n2 | tail -n1)
if [ "$res" == "[!] Error listing files" ];then
echo 0
else
[ "${res: -1}" == "/" ] && echo 2 || echo 1
fi
}
parse_args(){
[ "$#" == "0" ] && usage && exit
while [[ $# > 0 ]];do
opt="$1"
case $opt in
-d|--download) download "$2"
shift
;;
-l|--list) list "$2"
shift
;;
-u|--upload) upload "$2" "$3"; exit
shift
;;
-D|--delete) delete "$2"
shift
;;
-k|--mkdir) _mkdir "$2"
shift
;;
-M|--move) _mv "$2" "$3"; exit
shift
;;
-L|--list-shares) list_shares
shift
;;
-s|--share) share "$2" "$3" "$4"; exit
shift
;;
-U|--unshare) unshare "$2"
shift
;;
--configure) create_conf
shift
;;
-v|--version) echo "Version: $version"
shift
;;
-h|--help) usage; exit
;;
*) echo -en "Unknown option '$opt'\n\n" && usage && exit
esac
shift
done
}
usage(){
echo "Usage: $0 <options> [file|dir]"
echo "Options:"
echo " -l/--list [dir] List root directory or [dir]"
echo " -d/--download <file> Download <file> to current location"
echo " -u/--upload <file> [dir] Upload <file>. Optionally uploads <file> to [dir]"
echo " -D/--delete <file|dir> Delete file or directory"
echo " -k/--mkdir <dir> Create new directory"
echo " -M/--move <source> <target> Move file from remote <source> to remote <target> (e.g. --move file.txt somedir/file.txt)"
echo " -s/--share <file|dir> [-p] [-q] Create a public share and shows the url. Optionally -p prompts for a password, -q returns only the share URL"
echo " -L/--list-shares List shares"
echo " -U/--unshare <file|dir> Delete a public share"
echo " --configure Change connection configuration"
echo " -h/--help Show this help"
echo
}
read_dom(){
local IFS=\>
read -d \< ENTITY CONTENT
}
parse_xml(){
if [ "$platform" == "Linux" ]; then
echo "$1" | grep -oP "(?<=<$2>)[^<]+"
else
echo "$1" | while read_dom; do
[ "$ENTITY" == "$2" ] && echo $CONTENT
done
fi
}
create_conf(){
echo "[+] Creating new config file:"
read -p " Username: " username
read -s -p " Password: " password; echo
read -p " Server (e.g. https://example.com/nextcloud): " host
[ "${host: -1}" == "/" ] && host=$(echo $host | rev | sed -e 's|/||' | rev)
[[ "$host" == *"https"* ]] && read -p " Trust certificate [yes]: " trust_cert
trail=$(echo $host | cut -d '/' -f4)
[ -z $trust_cert ] && trust_cert="yes"; path="remote.php/webdav"
echo -en "username=$username\npassword=$password\nhost=$host\ntrust_cert=$trust_cert\ntrail=$trail\npath=$path\n" > $conf && chmod 600 $conf
setenv
testCon
}
depCheck(){
for b in curl grep sed basename ;do
which $b &>/dev/null
[ $? != 0 ] && echo "[!] Some dependencies are missing ($b)" && exit
done
[ ! -f "$conf" ] && echo "[!] Config file not found" && create_conf && exit
}
testCon(){
res=$(curl $extra_opts $options -X PROPFIND $dav_url/$dir 2>/dev/null)
[ $? -gt 0 ] && echo "Unable to connect to server. Check your configuration" && exit
}
setenv(){
for l in $(cat $conf); do [ "${l::1}" != "#" ] && export $l; done
[ "$trust_cert" == "yes" ] && trust="-k" || trust=''
options="$trust --user $username:$password"
extra_opts="-f" # We want -f switch for webdav, but not for OCS API, due to status codes
ocs_header="-H OCS-APIRequest:true"
oc_url=$host
dav_url=$oc_url/$path
OCS_SHARE_API="$oc_url/ocs/v2.php/apps/files_sharing/api/v1"
}
depCheck
platform=$(uname)
setenv
parse_args "$@"