forked from diddip21/openHAB-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudsend.sh
444 lines (331 loc) · 13.6 KB
/
cloudsend.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env bash
############################################################
#
# Tavinus Cloud Sender 2
# cloudsend.sh
# https://github.com/tavinus/cloudsend.sh
#
# Uses curl to send files to a shared
# Nextcloud/Owncloud folder
#
# Usage: ./cloudsend.sh <file> <folderLink>
# Help: ./cloudsend.sh -h
#
# Gustavo Arnosti Neves
# https://github.com/tavinus
#
# Contributors:
# @MG2R @gessel
#
# Get this script to current folder with:
# curl -O 'https://raw.githubusercontent.com/tavinus/cloudsend.sh/master/cloudsend.sh' && chmod +x cloudsend.sh
#
# NOTE: Cloudsend 2 changed the way password is handled and
# is NOT compatible with cloudsend 1 calls. The -e parameter
# now does what -p parameter did (environment passwords),
# while the -p parameter receives the password directly.
#
############################################################
CS_VERSION="2.1.14"
TRUE=0
FALSE=1
CLOUDURL=""
FOLDERTOKEN=""
PUBSUFFIX="public.php/webdav"
HEADER='X-Requested-With: XMLHttpRequest'
CLOUDSEND_PARAMS=()
INSECURE=''
OUTFILE=''
RENAMING=$FALSE
QUIETMODE=$FALSE
GLOBBING=$FALSE
GLOBCMD=' -g'
VERBOSE=' --progress-bar'
STTYBIN="$(command -v stty 2>/dev/null)"
SCREENSIZE="40 80"
################################################################
#### CURL CALL EXAMPLE
# https://cloud.mydomain.net/s/fLDzToZF4MLvG28
# curl -k -T myFile.ext -u "fLDzToZF4MLvG28:" -H 'X-Requested-With: XMLHttpRequest' https://cloud.mydomain.net/public.php/webdav/myFile.ext
################################################################
#### MESSAGES
################################################################
# Logs message to stdout
log() {
# [ "$VERBOSE" == " -s" ] || printf "%s\n" "$1"
isQuietMode || printf "%s\n" "$@"
}
# Prints program name and version
printVersion() {
printf "%s\n" "Tavinus Cloud Sender v$CS_VERSION"
}
# Prints error messages and exits
initError() {
printVersion >&2
printf "%s\n" "Init Error! $1" >&2
printf "%s\n" "Try: $0 --help" >&2
exit 5
}
# Prints usage information (help)
usage() {
printVersion
printf "%s" "
Parameters:
-h | --help Print this help and exits
-q | --quiet Disables verbose messages
-V | --version Prints version and exits
-r | --rename <file.xxx> Change the destination file name
-g | --glob Disable input file checking to use curl globs
-k | --insecure Uses curl with -k option (https insecure)
-p | --password <pass> Uses <pass> as shared folder password
-e | --envpass Uses env var \$CLOUDSEND_PASSWORD as share password
You can 'export CLOUDSEND_PASSWORD' at your system, or set it at the call
Please remeber to also call -e to use the password set
Use:
./cloudsend.sh [options] <filepath> <folderLink>
CLOUDSEND_PASSWORD='MySecretPass' ./cloudsend.sh -e [options] <filepath> <folderLink>
Passwords:
Cloudsend 2 changed the way password works
Cloudsend 0.x.x used the '-p' parameter for the Environment password (changed to -e in v2+)
Please use EITHER -e OR -p, but not both. The last to be called will be used
Env Pass > Set the variable CLOUDSEND_PASSWORD='MySecretPass' and use the option '-e'
Param Pass > Send the password as a parameter with '-p <password>'
Input Globbing:
You can use input globbing (wildcards) by setting the -g option
This will ignore input file checking and pass the glob to curl to be used
You MUST NOT rename files when globbing, input file names will be used
Glob examples: '{file1.txt,file2.txt,file3.txt}'
'img[1-100].png'
Send from stdin (pipe):
You can send piped content by using - or . as the input file name (curl specs)
You MUST set a destination file name to use stdin as input (-r <name>)
Use the file name '-' (a single dash) to use stdin instead of a given file
Alternately, the file name '.' (a single period) may be specified instead of '-' to use
stdin in non-blocking mode to allow reading server output while stdin is being uploaded
Examples:
CLOUDSEND_PASSWORD='MySecretPass' ./cloudsend.sh -e './myfile.txt' 'https://cloud.mydomain.net/s/fLDzToZF4MLvG28'
./cloudsend.sh './myfile.txt' 'https://cloud.mydomain.net/s/fLDzToZF4MLvG28'
./cloudsend.sh -r 'RenamedFile.txt' './myfile.txt' 'https://cloud.mydomain.net/s/fLDzToZF4MLvG28'
./cloudsend.sh -p 'MySecretPass' './myfile.txt' 'https://cloud.mydomain.net/s/fLDzToZF4MLvG28'
./cloudsend.sh -p 'MySecretPass' -r 'RenamedFile.txt' './myfile.txt' 'https://cloud.mydomain.net/s/fLDzToZF4MLvG28'
./cloudsend.sh -g -p 'MySecretPass' '{file1,file2,file3}' 'https://cloud.mydomain.net/s/fLDzToZF4MLvG28'
cat file | ./cloudsend.sh - 'https://cloud.mydomain.net/s/fLDzToZF4MLvG28' -r destFileName
Send folder examples:
find ./ -maxdepth 1 -type f -exec ./cloudsend.sh {} https://cloud.mydomain.tld/s/TxWdsNX2Ln3X5kxG -p yourPassword \;
find /home/myname/myfolder -type f -exec ./cloudsend.sh {} https://cloud.mydomain.tld/s/TxWdsNX2Ln3X5kxG -p yourPassword \;
tar cf - \"\$(pwd)\" | gzip -9 -c | ./cloudsend.sh - 'https://cloud.mydomain.tld/s/TxWdsNX2Ln3X5kxG' -r myfolder.tar.gz
tar cf - /home/myname/myfolder | gzip -9 -c | ./cloudsend.sh - 'https://cloud.mydomain.tld/s/TxWdsNX2Ln3X5kxG' -r myfolder.tar.gz
zip -q -r -9 - /home/myname/myfolder | ./cloudsend.sh - 'https://cloud.mydomain.tld/s/TxWdsNX2Ln3X5kxG' -r myfolder.zip
"
}
################################################################
#### GET OPTIONS
################################################################
# Checks only for quiet/verbose mode and ignores all else
parseQuietMode(){
while :; do
case "$1" in
-q|--quiet)
QUIETMODE=$TRUE
VERBOSE=" -s" ; break ;;
*)
isEmpty "$1" && break || shift ;;
esac
done
}
# Parses CLI options and parameters
parseOptions() {
while :; do
case "$1" in
-h|--help)
usage ; exit 0 ;;
-V|--version)
printVersion ; exit 0 ;;
-q|--quiet)
shift ;; # already checked
-k|--insecure)
INSECURE=' -k'
log " > Insecure mode ON"
shift ;;
-e|--envpass|--environment)
loadPassword "${CLOUDSEND_PASSWORD}"
log " > Using password from environment"
shift ;;
-p|--password)
loadPassword "$2"
log " > Using password from parameter"
shift ; shift ;;
-r|--rename)
loadOutFile "${2}"
log " > Destination file will be renamed to \"$OUTFILE\""
RENAMING=$TRUE
shift ; shift ;;
-g|--glob)
GLOBBING=$TRUE
GLOBCMD=''
log " > Glob mode on, input file checkings disabled"
shift ;;
*)
if isEmpty "$1"; then
break ;
else
CLOUDSEND_PARAMS=("${CLOUDSEND_PARAMS[@]}" "$1")
shift ;
fi
esac
done
CLOUDURL=''
FILENAME="${CLOUDSEND_PARAMS[0]}"
CLOUDSHARE="${CLOUDSEND_PARAMS[1]}"
# if we have index.php in the URL, process accordingly
if [[ "$CLOUDSHARE" == *"index.php"* ]]; then
CLOUDURL="${CLOUDSHARE%/index.php/s/*}"
else
CLOUDURL="${CLOUDSHARE%/s/*}"
fi
FOLDERTOKEN="${CLOUDSHARE##*/s/}"
if isGlobbing; then
if isRenaming; then
initError $'Cannot rename output files when using globbing on input.\nAll files would get the same output name and then be overwritten.\nSend individual files if you need renaming.'
elif isPiped "$FILENAME"; then
initError $'Cannot use globbing and send piped input at the same time.\nDo either one or the other.'
fi
else
if ! isFile "$FILENAME" && ! isPiped "$FILENAME"; then
initError "Invalid input file: $FILENAME"
fi
if isPiped "$FILENAME" && ! isRenaming; then
initError $'No output file name!\nYou need to set a destination name when reading from a pipe!\nPlease add -r <filename.ext> to your call.'
fi
fi
if isEmpty "$CLOUDURL"; then
initError "Empty URL! Nowhere to send..."
fi
if isEmpty "$FOLDERTOKEN"; then
initError "Empty Folder Token! Nowhere to send..."
fi
}
# Parses password to var or exits
loadPassword() {
if [ -z "$@" ]; then
initError "Trying to set an empty password"
fi
PASSWORD="$@"
}
# Parses destination file name to var or exits
loadOutFile() {
if [ -z "$@" ]; then
initError "Trying to set an empty destination file name"
fi
OUTFILE="$@"
}
################################################################
#### VALIDATORS
################################################################
# Dependency check
checkCurl() {
CURLBIN="$(command -v curl 2>/dev/null)"
isExecutable "$CURLBIN" && return $TRUE
CURLBIN='/usr/bin/curl'
isExecutable "$CURLBIN" && return $TRUE
CURLBIN='/usr/local/bin/curl'
isExecutable "$CURLBIN" && return $TRUE
CURLBIN='/usr/share/curl'
isExecutable "$CURLBIN" && return $TRUE
initError "No curl found on system! Please install curl and try again!"
exit 6
}
# Adjust Columns so the progess bar shows correctly
getScreenSize() {
if [ -x "$STTYBIN" ]; then
SCREENSIZE="$($STTYBIN size)"
fi
#export LINES=${SCREENSIZE% *}
#export COLUMNS=$((${SCREENSIZE#* } - 1))
export COLUMNS=${SCREENSIZE#* }
#echo "LINES..: $LINES"
#echo "COLUMNS: $COLUMNS"
}
# Returns $TRUE if $1 is a file, $FALSE otherwise
isFile() {
[[ -f "$1" ]] && return $TRUE
return $FALSE
}
# Returns $TRUE if $1 is executable, $FALSE otherwise
isExecutable() {
[[ -x "$1" ]] && return $TRUE
return $FALSE
}
# Returns $TRUE if $1 is empty, $FALSE otherwise
isEmpty() {
[[ -z "$1" ]] && return $TRUE
return $FALSE
}
# Returns $TRUE if $1 is not empty, $FALSE otherwise
isNotEmpty() {
[[ -z "$1" ]] && return $FALSE
return $TRUE
}
# Checks if the input file is stdin (either - or .)
isPiped() {
if [ "$1" = '-' ] || [ "$1" = '.' ] ; then
return $TRUE
fi
return $FALSE
}
################################################################
#### FLAG CHECKERS
################################################################
# If we are renaming the output, return $TRUE, else $FALSE
isRenaming() {
return $RENAMING
}
# If we are running in Quiet Mode, return $TRUE, else $FALSE
isQuietMode() {
return $QUIETMODE
}
# If we are globbing the input, return $TRUE, else $FALSE
isGlobbing() {
return $GLOBBING
}
################################################################
#### RUNNERS
################################################################
# Logs succes or failure from curl
logResult() {
#echo "LOGRESULT: $1"
local fileString="$(/usr/bin/basename "$FILENAME")"
isRenaming && fileString="$(/usr/bin/basename "$FILENAME") (renamed as $OUTFILE)"
if [ $1 -eq 0 ]; then
log " > Curl exited without errors"$'\n'" > Attempt to send completed > $fileString"
exit 0
fi
log " > Curl error when sending file > $fileString"
exit $1
}
# Execute curl send
sendFile() {
if isGlobbing; then
OUTFILE=''
elif isEmpty "$OUTFILE"; then # If we are not renaming, use the input file name
OUTFILE="$(/usr/bin/basename "$FILENAME")"
fi
getScreenSize
# Send file
#echo "$CURLBIN"$INSECURE$VERBOSE -T "$FILENAME" -u "$FOLDERTOKEN":"$PASSWORD" -H "$HEADER" "$CLOUDURL/$PUBSUFFIX/$OUTFILE"
"$CURLBIN"$INSECURE$VERBOSE$GLOBCMD -T "$FILENAME" -u "$FOLDERTOKEN":"$PASSWORD" -H "$HEADER" "$CLOUDURL/$PUBSUFFIX/$OUTFILE" | cat ; test ${PIPESTATUS[0]} -eq 0
logResult $?
}
################################################################
#### RUN #######################################################
################################################################
parseQuietMode "${@}"
parseOptions "${@}"
checkCurl
sendFile
################################################################
#### RUN #######################################################
################################################################
################################################################
exit 88 ; # should never get here
################################################################