Skip to content

Commit 8fdb561

Browse files
committed
v0.1.9 - Escapes whitespaces as %20 on files and folders when sending to curl; Adds -v --verbose flag and execution log messages
1 parent 07fdd4d commit 8fdb561

1 file changed

Lines changed: 62 additions & 14 deletions

File tree

cloudmanager.sh

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
# Gustavo Arnosti Neves
1313
#
1414
# Created..: 12 / 06 / 2018
15-
# Modified.: 31 / 03 / 2020
15+
# Modified.: 03 / 04 / 2020
1616
#
1717
##############################################################################
1818

1919

20-
C_VERSION='0.1.8'
20+
C_VERSION='0.1.9'
2121

2222
# working languages should be added here
2323
C_SUPPORTED_LANGUAGES=(en_US pt_BR)
@@ -77,6 +77,8 @@ setMessagesEnUs() {
7777
M_ACCOUNTSCONFIGRESETERROR=$'Error when trying to reset the accounts config file.\n'"$M_MAYNEEDPERMISSIONS"
7878
M_RESETSERVERCONFIG='You can create a new/empty server config file template with'
7979
M_RESETACCOUNTSCONFIG='You can create a new/empty accounts config file template with'
80+
M_REMOVING='Removing'
81+
M_CREATINGFOLDER='Creating Folder'
8082

8183

8284
### HELP
@@ -92,6 +94,7 @@ CONFIG
9294
OPTIONS
9395
9496
-V, --version Prints version to screen and finishes execution
97+
-v, --verbose Prints execution information to screen
9598
-h, --help Prints this help to screen and finishes execution
9699
--resetServerConfig Resets server config file to default
97100
--resetAccountsConfig Resets accounts config file to default
@@ -188,6 +191,8 @@ setMessagesPtBr() {
188191
M_ACCOUNTSCONFIGRESETERROR=$'Erro ao tentar recriar o arquivo de configuracao das contas.\n'"$M_MAYNEEDPERMISSIONS"
189192
M_RESETSERVERCONFIG='Voce pode criar um novo arquivo de config do servidor usando'
190193
M_RESETACCOUNTSCONFIG='Voce pode criar um novo arquivo de config das contas usando'
194+
M_REMOVING='Removendo'
195+
M_CREATINGFOLDER='Criando Pasta'
191196

192197

193198
### HELP
@@ -203,6 +208,7 @@ ARQUIVOS DE CONFIG
203208
OPCOES
204209
205210
-V, --version Imprime versao na tela e termina execucao
211+
-v, --verbose Imprime informacoes de execucao na tela
206212
-h, --help Imprime esta ajuda e termina execucao
207213
-r, --resetServerConfig Reseta / recria o arquivo de configuracoes de servidor
208214
-r, --resetAccountsConfig Reseta / recria o arquivo de configuracoes de contas
@@ -355,6 +361,7 @@ initVars() {
355361

356362
TRUE=0
357363
FALSE=1
364+
VERBOSE=$FALSE
358365
}
359366

360367

@@ -603,7 +610,7 @@ listFromXml() {
603610

604611

605612
# encode URL escaping
606-
rawurlencode() {
613+
rawUrlEncode() {
607614
local string="${1}"
608615
local strlen=${#string}
609616
local encoded=""
@@ -622,6 +629,31 @@ rawurlencode() {
622629
}
623630

624631

632+
escapeWhitespaces() {
633+
local string="${1}"
634+
local strlen=${#string}
635+
local encoded=""
636+
local pos c o
637+
638+
for (( pos=0 ; pos<strlen ; pos++ )); do
639+
c=${string:$pos:1}
640+
case "$c" in
641+
" " ) o='%20' ;;
642+
* ) o="${c}" ;;
643+
esac
644+
encoded+="${o}"
645+
done
646+
echo "${encoded}" # You can either set a return variable (FASTER)
647+
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
648+
649+
}
650+
651+
652+
#testVar='https://cloud.arnosti.net.br/dav/test/what ever you want'
653+
#echo "$(rawUrlEncode "$testVar")"
654+
#echo "$(escapeWhitespaces "$testVar")"
655+
656+
625657
# Returns the lowercase version of $@
626658
lowercase() {
627659
echo -ne "$@" | tr '[:upper:]' '[:lower:]'
@@ -675,7 +707,8 @@ uppercase() {
675707
listCloud() {
676708
local tgt=""
677709
if ! isEmpty "$1"; then
678-
tgt="$1"
710+
#tgt="$1"
711+
tgt="$(escapeWhitespaces "$1")"
679712
fi
680713
local xml="$("$CURLBIN" -s -u $C_USERNAME:$C_PASS -X PROPFIND "$C_FDAV_URL/$C_USERNAME/$tgt")"
681714
#"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X PROPFIND "$C_FDAV_URL/$C_USERNAME/$tgt"
@@ -700,8 +733,9 @@ sendFile() {
700733
elif endsWithDash "$2"; then # Appends source name to target folder
701734
__target="$2$(basename "$1")"
702735
fi
703-
echo "source: $1"$'\n'"target: $__target"
704-
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -T "$1" "$C_FDAV_URL/$C_USERNAME/$__target"
736+
#echo "source: $1"$'\n'"target: $__target"
737+
log "$M_SOURCE: $1"$'\n'"$M_DESTINATION: $__target"
738+
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -T "$1" "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$__target")"
705739
}
706740

707741

@@ -713,7 +747,9 @@ createFolder() {
713747
logErr "$M_TRYHELP"
714748
return $FALSE
715749
fi
716-
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X MKCOL "$C_FDAV_URL/$C_USERNAME/$1"
750+
#echo "$CURLBIN" -s -u $C_USERNAME:$C_PASS -X MKCOL "$C_FDAV_URL/$C_USERNAME/$1"
751+
log "$M_CREATINGFOLDER: $1"
752+
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X MKCOL "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$1")"
717753
return $?
718754
}
719755

@@ -726,7 +762,8 @@ deleteFileFolder() {
726762
logErr "$M_TRYHELP"
727763
return $FALSE
728764
fi
729-
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X DELETE "$C_FDAV_URL/$C_USERNAME/$1"
765+
log "$M_REMOVING: $1"
766+
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X DELETE "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$1")"
730767
return $?
731768
}
732769

@@ -748,7 +785,8 @@ moveFileFolder() {
748785
__target="$2$(basename "$1")"
749786
fi
750787
#echo "source: $1"$'\n'"target: $__target"
751-
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X MOVE --header "Destination: $C_FDAV_URL/$C_USERNAME/$__target" "$C_FDAV_URL/$C_USERNAME/$1"
788+
log "$M_SOURCE: $1"$'\n'"$M_DESTINATION: $__target"
789+
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X MOVE --header "Destination: $C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$__target")" "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$1")"
752790
return $?
753791
}
754792

@@ -769,7 +807,8 @@ copyFileFolder() {
769807
__target="$2$(basename "$1")"
770808
fi
771809
#echo "source: $1"$'\n'"target: $__target"
772-
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X COPY --header "Destination: $C_FDAV_URL/$C_USERNAME/$__target" "$C_FDAV_URL/$C_USERNAME/$1"
810+
log "$M_SOURCE: $1"$'\n'"$M_DESTINATION: $__target"
811+
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X COPY --header "Destination: $C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$__target")" "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$1")"
773812
return $?
774813
}
775814

@@ -803,9 +842,9 @@ getFile() {
803842
logErr "$M_ERRORGETFILE!"$'\n'"$M_ITEMEMPTY"$'\n'"$M_CLOUDFILE: $1"$'\n'"$M_LOCALFILE: $__target"
804843
return $FALSE
805844
fi
806-
#log "$M_CLOUDFILE: $1"$'\n'"$M_LOCALFILE: $__target"
845+
log "$M_CLOUDFILE: $1"$'\n'"$M_LOCALFILE: $__target"
807846
#"$CURLBIN" --silent -u $C_USERNAME:$C_PASS -X GET "$C_FDAV_URL/$C_USERNAME/$1" --output "$__target" 2>>log.txt
808-
"$CURLBIN" --silent -u $C_USERNAME:$C_PASS "$C_FDAV_URL/$C_USERNAME/$1" --output "$__target"
847+
"$CURLBIN" --silent -u $C_USERNAME:$C_PASS "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$1")" --output "$__target"
809848
local ret=$?
810849
#echo "ret: $ret"
811850
if [[ $ret -ne 0 ]]; then
@@ -842,7 +881,7 @@ getFile() {
842881
listShares() {
843882
local tgt=""
844883
if ! isEmpty "$1"; then
845-
tgt="$(rawurlencode "$1")"
884+
tgt="$(rawUrlEncode "$1")"
846885
#tgt='?path='"$tgt"'&reshares=true&shared_with_me=1'
847886
tgt='?path='"$tgt"'&reshares=true'
848887
fi
@@ -963,9 +1002,16 @@ printLinks() {
9631002
#### PRINTING TO SCREEN
9641003

9651004

1005+
isVerbose() {
1006+
return $VERBOSE
1007+
}
1008+
1009+
9661010
# Prints message to stdout
9671011
log() {
968-
printf "%s\n" "$@"
1012+
if isVerbose; then
1013+
printf "%s\n" "$@"
1014+
fi
9691015
}
9701016

9711017

@@ -1052,6 +1098,8 @@ runCloudManager() {
10521098
case "$1" in
10531099
-V|--version|--Version)
10541100
printBanner 'nopadding' ; exit 0 ;;
1101+
-v|--verbose)
1102+
VERBOSE=$TRUE ; shift ;;
10551103
-h|--help|--Help)
10561104
printHelp ; exit 0 ;;
10571105
-u|--user)

0 commit comments

Comments
 (0)