-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcloudmanager.sh
executable file
·1284 lines (992 loc) · 41 KB
/
cloudmanager.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
##############################################################################
#
# Tavinus Cloud Manager
# https://github.com/tavinus/cloudmanager
#
# CLI App to use Nextcloud/Owncloud via Webdav
# Uses curl to perform webdav calls
# Tries to be feature-complete with NC's webdav functions
#
# Gustavo Arnosti Neves
#
# Created..: 12 / 06 / 2018
# Modified.: 03 / 04 / 2020
#
##############################################################################
C_VERSION='0.1.9'
# working languages should be added here
C_SUPPORTED_LANGUAGES=(en_US pt_BR)
C_USER_LANGUAGE='en_US' # default language
##############################################################################
## MESSAGES TRANSLATIONS
# US English Messages
setMessagesEnUs() {
# ONLY en_US should have these vars
M_EMPTYMESSAGE=$''
M_SEP=$'-------------------------------------------------------\n'
# All languages should have these vars
M_DEPMISS=$'Error! Dependency Missing.\nYou need to install or add the missing dependency to your $PATH'
M_ERRORCONNECT=$'Error! Could not contact the destination server with NC.\nServer seems to be offline:'
M_TRYHELP="Try: $0 --help"
M_NAME='Name'
M_FILE='File'
M_ERRORCONFIGNOTFOUND=$'Error! Config file not found!'
M_ERRORACCSNOTFOUND=$'Error! Accounts file not found!'
M_ERRORSENDFILE=$'Error when sending file.'
M_FILEORIGINNOTFOUND=$'Source file does not exist!'
M_ERRORCREATEFOLDER=$'Error when creating folder!'
M_FOLDEREMPTY=$'Folder name is not set / it is empty.'
M_ERRORERASE="Error when removing folder or file!"
M_ITEMEMPTY="One item is empty / not specified."
M_ERRORMOVE="Error when moving folder or file!"
M_SOURCE="Source"
M_DESTINATION="Destination"
M_ERRORUSERNOTFOUND="Error! Could not locate the user"
M_CHECKUSER="Plase check the login name or the accounts file."
M_ERRORCOPY="Error when copying folder or file!"
M_ERRORFILENOTFOUND="Error! File not found!"
M_INVALIDOPTION="Invalid Option"
M_NOCOMMANDSELECTED="No command selected!"
M_ERRORGETFILE="Error when downloading file"
M_CLOUDFILE="Cloud File"
M_LOCALFILE="Local File"
M_ERRORDOWNLOAD="Download error detected, removing file..."
M_USINGHSORTLANGUAGE='Using short notation for language'
M_INVALIDLANGUAGE='Invalid language selected will be ignored'
M_EDITCONFIGFILE='Please edit the new config file before using CloudManager.'
M_MAYNEEDPERMISSIONS='May need elevated permissions?'
M_SERVERCONFIGWASRESET=$'The server config file was reset!\n'"$M_EDITCONFIGFILE"
M_ACCOUNTSCONFIGWASRESET=$'The accounts config file was reset!\n'"$M_EDITCONFIGFILE"
M_SERVERCONFIGRESETERROR=$'Error when trying to reset the server config file.\n'"$M_MAYNEEDPERMISSIONS"
M_ACCOUNTSCONFIGRESETERROR=$'Error when trying to reset the accounts config file.\n'"$M_MAYNEEDPERMISSIONS"
M_RESETSERVERCONFIG='You can create a new/empty server config file template with'
M_RESETACCOUNTSCONFIG='You can create a new/empty accounts config file template with'
M_REMOVING='Removing'
M_CREATINGFOLDER='Creating Folder'
### HELP
M_HELPINFO="
CONFIG
- cloudmanager.server Edit this file to configure host domain, protocol and port
- cloudmanager.accounts Edit this file to add nextcloud accounts
The first user of the list is the default user
You may use an \"app\" token instead of the user password (safer)
Both config files MUST be populated before you try to use Cloud Manager
OPTIONS
-V, --version Prints version to screen and finishes execution
-v, --verbose Prints execution information to screen
-h, --help Prints this help to screen and finishes execution
--resetServerConfig Resets server config file to default
--resetAccountsConfig Resets accounts config file to default
--resetAll Resets all config files
-u, --user <username> Loads the user <username> from the cloudmanager.accounts file
-l, --language <lang> Force the language <lang>
Languages currently supported:
${C_SUPPORTED_LANGUAGES[@]}
NOTES
- Target locations ending with a forward dash "/" will be treated as folders and
will have the source name file appended to it as a target location.
COMMANDS
> ls,list [target]
Lists a folder or file (lists root folder if empty)
Ex: $C_SNAME list
> mkdir,makeFolder <foldername>
Creates a folder at the cloud
Ex: $C_SNAME mkdir MyTestFolder
> send,upload <localfolder/localfile> [cloudfolder/cloudfile]
Sends a local file to the cloud instance
Ex: $C_SNAME send '/home/user/MyFile.xml' 'CloudFolder/MyFile.xml'
> mv,move <source> <target>
Moves a folder or file inside the cloud instance
Ex: $C_SNAME move 'OneFolder/MyFile.txt' 'AnotherFolder/MyFile.txt'
> cp,copy <source> <target>
Copies a folder or file inside the cloud instance
Ex: $C_SNAME copy 'OneFolder/MyFile.txt' 'AnotherFolder/MyFile.txt'
> del,delete <target>
Erases a folder or file inside the cloud instance (may move to Trashbin)
Ex: $C_SNAME del 'MyFolder/MyFile.txt'
> get,download <source> [target]
Downloads a file from the cloud to a local folder
Target defaults to current folder with remote name if blank
Ex: $C_SNAME get 'RemoteFolder/RemoteFile.txt' '/home/user/MyFile.txt'
> shares,listShares [folder]
Prints table with shares (183 cols)
Specify a folder or leave blank to list all
Ex: $C_SNAME shares
> links,sharedLinks
List shares by links using the format <URL>,<FILE NAME>
Ex: $C_SNAME listShares
"
}
# Brazilian Portuguese Messages
setMessagesPtBr() {
M_DEPMISS=$'Erro! Dependencia nao encontrada.\nVoce precisa instalar ou adicionar o programa em questao ao seu $PATH'
M_ERRORCONNECT=$'Erro! Nao foi possivel conectar ao servidor de destino.\nServidor parece estar offline:'
M_TRYHELP="Tente: $0 --help"
M_NAME='Nome'
M_FILE='Arquivo'
M_ERRORCONFIGNOTFOUND=$'Erro! Arquivo de configuracao nao foi encontrado!'
M_ERRORACCSNOTFOUND=$'Erro! Arquivo de contas e senhas nao foi encontrado!'
M_ERRORSENDFILE=$'Erro ao enviar arquivo.'
M_FILEORIGINNOTFOUND=$'Arquivo de origem nao existe!'
M_ERRORCREATEFOLDER=$'Erro ao criar pasta!'
M_FOLDEREMPTY=$'Nome da pasta em branco / nao especificada.'
M_ERRORERASE="Erro ao apagar pasta ou arquivo!"
M_ITEMEMPTY="A item em branco / nao especificado."
M_ERRORMOVE="Erro ao mover a pasta ou arquivo!"
M_SOURCE="Origem"
M_DESTINATION="Destino"
M_ERRORUSERNOTFOUND="Erro! Nao foi possivel localizar o usuario"
M_CHECKUSER="Verifique o nome do usuario ou o arquivo de contas."
M_ERRORCOPY="Erro ao copiar a pasta ou arquivo!"
M_ERRORFILENOTFOUND="Erro! Arquivo nao encontrado!"
M_INVALIDOPTION="Opcao Invalida"
M_NOCOMMANDSELECTED="Nenhum comando selecionado!"
M_ERRORGETFILE="Erro ao baixar arquivo"
M_CLOUDFILE="Arquivo Cloud"
M_LOCALFILE="Arquivo Local"
M_ERRORDOWNLOAD="Erro de download detectado, removendo arquivo..."
M_USINGHSORTLANGUAGE='Usando notacao minimalist para idioma'
M_INVALIDLANGUAGE='Idioma invalido sera ignorado'
M_EDITCONFIGFILE='Edite o novo arquivo de configuracao antes de usar o CloudManager.'
M_MAYNEEDPERMISSIONS='Talvez precise de permissoes elevadas?'
M_SERVERCONFIGWASRESET=$'A configuracao do servidor foi restaurada!\n'"$M_EDITCONFIGFILE"
M_ACCOUNTSCONFIGWASRESET=$'A configuracao das contas foi restaurada!\n'"$M_EDITCONFIGFILE"
M_SERVERCONFIGRESETERROR=$'Erro ao tentar recriar o arquivo de configuracao do servidor.\n'"$M_MAYNEEDPERMISSIONS"
M_ACCOUNTSCONFIGRESETERROR=$'Erro ao tentar recriar o arquivo de configuracao das contas.\n'"$M_MAYNEEDPERMISSIONS"
M_RESETSERVERCONFIG='Voce pode criar um novo arquivo de config do servidor usando'
M_RESETACCOUNTSCONFIG='Voce pode criar um novo arquivo de config das contas usando'
M_REMOVING='Removendo'
M_CREATINGFOLDER='Criando Pasta'
### HELP
M_HELPINFO="
ARQUIVOS DE CONFIG
- cloudmanager.server Edite este arquivo para configurar o dominio, porta e protocolo
- cloudmanager.accounts Edite este arquivo para configurar as contas de acesso
O primeiro usuario e' o padrao
Voce pode utilizar um token de aplicativo ao inves da senha do usuario (mais seguro)
Ambos os arquivos de configuracao TEM que ser preenchidos antes de usar o Cloud Manager
OPCOES
-V, --version Imprime versao na tela e termina execucao
-v, --verbose Imprime informacoes de execucao na tela
-h, --help Imprime esta ajuda e termina execucao
-r, --resetServerConfig Reseta / recria o arquivo de configuracoes de servidor
-r, --resetAccountsConfig Reseta / recria o arquivo de configuracoes de contas
-r, --resetAll Reseta / recria todos os arquivos de configuracoes
-u, --user <username> Carrega o usuario <username> do arquivo cloudmanager.accounts
-l, --language <lang> Forca o idioma <lang>
Idiomas aceitos nesta versao:
${C_SUPPORTED_LANGUAGES[@]}
NOTAS
- Destinos que terminarem com uma barra para frente "/" serao tratados como uma
pasta e terao o nome do arquivo de origem adicionados ao caminho como destino.
COMANDOS
> ls,list [destino]
Lista uma pasta ou arquivo
Ex: $C_SNAME list
> mkdir,makeFolder <nomedapasta>
Cria uma pasta no cloud
Ex: $C_SNAME mkdir PastaTeste
> send,upload <pasta/arquivo.local> [pasta/arquivo.cloud]
Envia um arquivo local para o cloud
Ex: $C_SNAME send '/user/nfe/meuarquivo.xml' 'ARCO/nfe/meuarquivo.xml'
> mv,move <origem> <destino>
Move um arquivo ou pasta dentro do cloud
Ex: $C_SNAME move 'ARCO/meuarquivo.txt' 'ATL/meuarquivo.txt'
> cp,copy <origem> <destino>
Copia um arquivo ou pasta dentro do cloud
Ex: $C_SNAME copy 'ARCO/meuarquivo.txt' 'ATL/meuarquivo.txt'
> del,delete <destino>
Apaga um arquivo ou pasta dentro do cloud (move para a lixeira)
Ex: $C_SNAME del 'ARCO/meuarquivo.txt'
> get,download <origem> [destino]
Faz download de um arquivo do cloud para um disco local
Ex: $C_SNAME get 'ARCO/meuarquivo.txt' '/user/nfe/meuarquivo.txt'
> shares,listShares [pasta]
Imprime tabela com compartilhamentos (183 cols)
Especifique uma pasta ou deixe em branco para listar tudo.
Ex: $C_SNAME shares
> links,sharedLinks
Lista os compartilhamentos por links em formato <URL>,<NOME ARQUIVO>
Ex: $C_SNAME listShares
"
}
##############################################################################
## LANGUAGE HANDLING
# Loads language strings based on env vars and/or cli params
initLanguage() {
# Load english defaults to make sure we have all messages
setMessagesEnUs
# Get language from env var $LANG and use it as new default
if [[ ! -z ${LANG%.*} ]]; then
C_USER_LANGUAGE=${LANG%.*}
setMessages
fi
# Now we try to parse a forced language from cli
#echo " ${C_SUPPORTED_LANGUAGES[@]} " ; echo "$(lowercase "${C_SUPPORTED_LANGUAGES[@]}")"
local lsup="$(lowercase "${C_SUPPORTED_LANGUAGES[@]}")"
local ssup=() # short versions
for i in "${C_SUPPORTED_LANGUAGES[@]}"; do
ssup=( "${ssup[@]}" "${i%_*}" )
done
ssupSTR="$(lowercase "${ssup[@]}")"
# get language from cli parameter (if set)
while :; do
case "$1" in
-l|--language)
if [[ " $lsup " =~ " $(lowercase ${2}) " ]]; then
C_USER_LANGUAGE="$2"
elif [[ " $ssupSTR " =~ " $(lowercase ${2}) " ]]; then
C_USER_LANGUAGE="$2"
#log "$M_USINGHSORTLANGUAGE: $2"
else
logErr "$M_INVALIDLANGUAGE: $2"
fi
break ;;
*)
isEmpty "$1" && break || shift ;;
esac
done
setMessages
}
# Load messages based on language set
setMessages() {
# use lowercase version for comparison (case insensitiveness)
local lang="$(lowercase $C_USER_LANGUAGE)"
#log "C_USER_LANGUAGE > $C_USER_LANGUAGE" ; log "lang > $lang"
# load secondary languages or default
# Default is en_US (English US)
if [[ $lang == 'en_us' ]] || [[ ${lang%_*} == 'en' ]]; then # default for en_*
setMessagesEnUs
elif [[ $lang == 'pt_br' ]] || [[ ${lang%_*} == 'pt' ]]; then # default for pt_*
setMessagesPtBr
fi
}
##############################################################################
## INITIAL SETUP
# Initial Variables
initVars() {
C_SNAME="$(basename "$0")"
C_DNAME="$(dirname "$0")"
C_REALPATH="$(realpath "$0")"
C_REALSNAME="$(basename "$C_REALPATH")"
C_REALDNAME="$(dirname "$C_REALPATH")"
# Uncomment to debug
# echo "\$0 > $C_SNAME" ; echo "C_SNAME > $C_SNAME" ; echo "C_DNAME > $C_DNAME" ; echo "C_REALPATH > $C_REALPATH" ; echo "C_REALSNAME > $C_REALSNAME" ; echo "C_REALDNAME > $C_REALDNAME" ;
C_CONFIG_FILE="$C_REALDNAME"/cloudmanager.server # Edit server info here
C_ACCOUNTS_FILE="$C_REALDNAME"/cloudmanager.accounts # Edit accounts here
C_ACC_USERS=()
C_ACC_PASSWORDS=()
TRUE=0
FALSE=1
VERBOSE=$FALSE
}
# Creates the file cloudmanager.server with the default options
resetServerConfig() {
echo -ne "# Edit the shell variables below
CLOUDSERVERDOMAIN='cloud.domain.tld'
CLOUDSERVERPROTOCOL='https://'
CLOUDSERVERPORT=443
" > "$C_CONFIG_FILE"
if [[ $? -eq 0 ]]; then
log "$M_SEP$M_SERVERCONFIGWASRESET"$'\n'" > $C_CONFIG_FILE"
else
logErr "$M_SEP$M_SERVERCONFIGRESETERROR"$'\n'" > $C_CONFIG_FILE"
fi
}
# Creates the file cloudmanager.accounts with the default options
resetAccountsConfig() {
echo -ne "---- Add accounts one per line, as in USERNAME:PASSWORD
---- You can use an \"App Password\" to access your account (instead of your regular password)
---- Settings > Personal > Security > Enter App Name > Create new app password
---- The first user is the default user, the rest can be used with -u <usernamme>
---- Lines starting with four dashes will be ignored
myUsername:myPassword
" > "$C_ACCOUNTS_FILE"
if [[ $? -eq 0 ]]; then
log "$M_SEP$M_ACCOUNTSCONFIGWASRESET"$'\n'" > $C_ACCOUNTS_FILE"
else
logErr "$M_SEP$M_ACCOUNTSCONFIGRESETERROR"$'\n'" > $C_ACCOUNTS_FILE"
fi
}
# Resets all config files with the default options
resetAll() {
resetServerConfig
resetAccountsConfig
}
# Initial setups and checks
initCloudManager() {
# initVars
if [[ -f "$C_CONFIG_FILE" ]]; then
. "$C_CONFIG_FILE"
else
logErr "$M_ERRORCONFIGNOTFOUND"$'\n > '"$C_CONFIG_FILE"$'\n'"$M_RESETSERVERCONFIG"$'\n'"$0 --resetServerConfig"
exit 33
fi
if [[ -f "$C_ACCOUNTS_FILE" ]]; then
C_ACCOUNTS="$(grep -v '^----' "$C_ACCOUNTS_FILE")"
#echo -e "$C_ACCOUNTS"
else
logErr "$M_ERRORACCSNOTFOUND"$'\n > '"$C_ACCOUNTS_FILE"$'\n'"$M_RESETACCOUNTSCONFIG"$'\n'"$0 --resetAccountsConfig"
exit 33
fi
parseAccounts
# The default username is the first account in the config file
#C_USER="$(echo "$C_ACCOUNTS" | head -n 1)"
#C_USERNAME=${C_USER%:*}
#C_PASS=${C_USER#*:}
C_USER="${C_ACC_USERS[0]}:${C_ACC_PASSWORDS[0]}"
C_USERNAME="${C_ACC_USERS[0]}"
C_PASS="${C_ACC_PASSWORDS[0]}"
# Uncomment to debug
# echo "CUSER > $C_USER" ; echo "C_USERNAME > $C_USERNAME" ; echo "C_PASS > $C_PASS"
# https://cloud.domain.net/remote.php/dav/files/<username>/
C_BASE_URL="$CLOUDSERVERPROTOCOL""$CLOUDSERVERDOMAIN"":""$CLOUDSERVERPORT"
C_FDAV_URL="$C_BASE_URL/remote.php/dav/files"
C_OCS_URL="$C_BASE_URL/ocs/v2.php/apps/files_sharing/api/v1"
CURLBIN="$(command -v curl 2>/dev/null)"
STRBIN="$(command -v strings 2>/dev/null)"
NCBIN="$(command -v nc 2>/dev/null)"
### INITIAL ENVIRONMENT VALIDATION
checkDep "$CURLBIN"
checkDep "$STRBIN"
checkDep "$NCBIN"
}
# Load account info into Bash arrays
parseAccounts() {
C_ACC_USERS=()
C_ACC_PASSWORDS=()
while IFS= read -r acc; do
#echo $'accounts > '"$acc"
C_ACC_USERS=( "${C_ACC_USERS[@]}" "${acc%:*}" )
C_ACC_PASSWORDS=( "${C_ACC_PASSWORDS[@]}" "${acc#*:}" )
done <<< "$C_ACCOUNTS"
#echo "------------------" ; echo $'C_ACC_USERS > '"${C_ACC_USERS[@]}" ; echo $'C_ACC_PASSWORDS > '"${C_ACC_PASSWORDS[@]}" ; echo "------------------"
#echo "${C_ACC_USERS[1]}"
}
################################################################
#### VALIDATORS
# Dependency check
checkDep() {
isExecutable "$1" && return $TRUE
logErr "$M_DEPMISS"$'\n'" > $1"
exit 6
}
# 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
}
# Prints error message and exits with status code
initError() {
isEmpty "$1" || printf "$1"
ret=33
isEmpty "$2" || ret=$2
exit $ret
}
# Check if the server is reachable with nc
checkCloud() {
"$NCBIN" -w 3 -z "$CLOUDSERVERDOMAIN" $CLOUDSERVERPORT &>/dev/null
if [[ $? -ne 0 ]]; then
logErr "$M_ERRORCONNECT"$'\n'" > $C_BASE_URL"
exit 2
fi
}
# Prints last char of a string
strLastChar() {
local __str="$@"
echo -ne "${__str: -1}"
}
# Returns $TRUE if a string ends with a dash (/), $FALSE otherwise
endsWithDash() {
local __str="$@"
[[ $(strLastChar "$__str") == '/' ]] && return $TRUE
return $FALSE
}
################################################################
#### XML PARSING
# XML Iterator
read_dom () {
local IFS=\>
read -d \< ENTITY CONTENT
local RET=$?
TAG_NAME=${ENTITY%% *}
ATTRIBUTES=${ENTITY#* }
return $RET
}
# simple XML STRING print
dumpXmlString() {
local readme="$1"
while read_dom; do
echo "$ENTITY => $CONTENT"
done < <(printf '%s\n' "$readme")
}
# simple XML FILE print
dumpXmlFile() {
while read_dom; do
echo "$ENTITY => $CONTENT"
done < "$1"
}
# ls behaviour from XML list
listFromXml() {
local readme="$1"
local isFirst=$TRUE
while read_dom; do
if [[ "$ENTITY" = "d:href" ]]; then
if [[ $isFirst -eq $TRUE ]]; then
isFirst=$FALSE
continue # ignore ROOT folder
fi
# ${haystack//needle/replacement}
printf "%s\n" "$(basename "${CONTENT//%20/ }")"
fi
done < <(printf '%s\n' "$readme")
}
################################################################
#### HELPER FUNCTIONS
# encode URL escaping
rawUrlEncode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}" # You can either set a return variable (FASTER)
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}
escapeWhitespaces() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
" " ) o='%20' ;;
* ) o="${c}" ;;
esac
encoded+="${o}"
done
echo "${encoded}" # You can either set a return variable (FASTER)
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}
#testVar='https://cloud.arnosti.net.br/dav/test/what ever you want'
#echo "$(rawUrlEncode "$testVar")"
#echo "$(escapeWhitespaces "$testVar")"
# Returns the lowercase version of $@
lowercase() {
echo -ne "$@" | tr '[:upper:]' '[:lower:]'
}
# Returns the uppercase version of $@
uppercase() {
echo -ne "$@" | tr '[:lower:]' '[:upper:]'
}
################################################################
#### NEXTCLOUD API INFO
####################################################
## Share API Statuscodes:
## 100 - successful
## 400 - wrong or no update parameter given
## 403 - public upload disabled by the admin
## 404 - couldn’t update share
####################################################
# The OCS Share API allows you to access the sharing API from outside over pre-defined OCS calls.
# The base URL for all calls to the share API is: <nextcloud_base_url>/ocs/v2.php/apps/files_sharing/api/v1
# All calls to OCS endpoints require the OCS-APIRequest header to be set to true
####################################################
# https://doc.owncloud.org/server/10.0/developer_manual/core/ocs-share-api.html
# /shares
# /shares?path=/<path-file>&reshares=true
# /shares/<share_id>
#
################################################################
#### WEBDAV OPERATIONS
# Lists the contents of a cloud instance folder
# LIST remote.php/dav/files/user/path/to/fileOrfolder
listCloud() {
local tgt=""
if ! isEmpty "$1"; then
#tgt="$1"
tgt="$(escapeWhitespaces "$1")"
fi
local xml="$("$CURLBIN" -s -u $C_USERNAME:$C_PASS -X PROPFIND "$C_FDAV_URL/$C_USERNAME/$tgt")"
#"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X PROPFIND "$C_FDAV_URL/$C_USERNAME/$tgt"
#printf "%s\n" "$xml"
listFromXml "$xml"
return $?
}
# Sends a file into a cloud instance tree
# curl -u user:pass -T error.log "https://example.com/nextcloud/remote.php/dav/files/USERNAME/$(date '+%d-%b-%Y')/error.log"
sendFile() {
local __target="$2"
if ! isFile "$1"; then
logErr "$M_ERRORSENDFILE $M_FILEORIGINNOTFOUND"
logErr "$M_FILE: $1"
logErr "$M_TRYHELP"
return $FALSE
fi
if isEmpty "$2"; then # Send to root with source name
__target="$(basename "$1")"
elif endsWithDash "$2"; then # Appends source name to target folder
__target="$2$(basename "$1")"
fi
#echo "source: $1"$'\n'"target: $__target"
log "$M_SOURCE: $1"$'\n'"$M_DESTINATION: $__target"
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -T "$1" "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$__target")"
}
# Creates a folder into a cloud instance tree
# curl -u user:pass -X MKCOL "https://example.com/nextcloud/remote.php/dav/files/USERNAME/$(date '+%d-%b-%Y')"
createFolder() {
if isEmpty "$1"; then
logErr "$M_ERRORCREATEFOLDER $M_FOLDEREMPTY"
logErr "$M_TRYHELP"
return $FALSE
fi
#echo "$CURLBIN" -s -u $C_USERNAME:$C_PASS -X MKCOL "$C_FDAV_URL/$C_USERNAME/$1"
log "$M_CREATINGFOLDER: $1"
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X MKCOL "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$1")"
return $?
}
# Deletes a folder or file from a cloud instance (may send to trash)
# DELETE remote.php/dav/files/user/path/to/file
deleteFileFolder() {
if isEmpty "$1"; then
logErr "$M_ERRORERASE"$'\n'"$M_ITEMEMPTY"
logErr "$M_TRYHELP"
return $FALSE
fi
log "$M_REMOVING: $1"
"$CURLBIN" -s -u $C_USERNAME:$C_PASS -X DELETE "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$1")"
return $?
}
# Moves a folder or file from-to a cloud instance
# MOVE remote.php/dav/files/user/path/to/file
# Destination: https://cloud.example/remote.php/dav/files/user/new/location
# curl -u user:pass -X MOVE --header 'Destination: https://example.com/nextcloud/remote.php/dav/files/USERNAME/target.jpg' https://example.com/nextcloud/remote.php/dav/files/USERNAME/source.jpg
moveFileFolder() {
local __target="$2"
if isEmpty "$1" || isEmpty "$2"; then
logErr "$M_ERRORMOVE"$'\n'"$M_ITEMEMPTY"
logErr "$M_SOURCE: $1"
logErr "$M_DESTINATION: $2"
logErr "$M_TRYHELP"
return $FALSE
fi
if endsWithDash "$2"; then # Appends source name to target folder
__target="$2$(basename "$1")"
fi
#echo "source: $1"$'\n'"target: $__target"
log "$M_SOURCE: $1"$'\n'"$M_DESTINATION: $__target"
"$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")"
return $?
}
# Copies a folder or file from-to a cloud instance
# COPY remote.php/dav/files/user/path/to/file
# Destination: https://cloud.example/remote.php/dav/files/user/new/location
copyFileFolder() {
local __target="$2"
if isEmpty "$1" || isEmpty "$2"; then
logErr "$M_ERRORCOPY"$'\n'"$M_ITEMEMPTY"
logErr "$M_SOURCE: $1"
logErr "$M_DESTINATION: $2"
logErr "$M_TRYHELP"
return $FALSE
fi
if endsWithDash "$2"; then # Appends source name to target folder
__target="$2$(basename "$1")"
fi
#echo "source: $1"$'\n'"target: $__target"
log "$M_SOURCE: $1"$'\n'"$M_DESTINATION: $__target"
"$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")"
return $?
}
# Checks file for error and prints error messages
fileHasError() {
# echo "$1 -- $2"
if ! isFile "$1"; then
logErr "$M_ERRORFILENOTFOUND"
logErr " > $1"
logErr "$M_TRYHELP"
return $FALSE
fi
"$STRBIN" "$1" | grep -m 1 '<?xml version="1.0" encoding="utf-8"?>
<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">' >/dev/null || return $FALSE
"$STRBIN" "$1" | grep -m 1 "$2" >/dev/null || return $FALSE
return $TRUE
}
# Downloads a file
# GET remote.php/dav/files/user/path/to/file
getFile() {
local __target="$2"
if isEmpty "$2"; then
__target="$(basename "$1")"
elif endsWithDash "$2"; then # Appends source name to target folder
__target="$2$(basename "$1")"
fi
if isEmpty "$1" || isEmpty "$__target"; then
logErr "$M_ERRORGETFILE!"$'\n'"$M_ITEMEMPTY"$'\n'"$M_CLOUDFILE: $1"$'\n'"$M_LOCALFILE: $__target"
return $FALSE
fi
log "$M_CLOUDFILE: $1"$'\n'"$M_LOCALFILE: $__target"
#"$CURLBIN" --silent -u $C_USERNAME:$C_PASS -X GET "$C_FDAV_URL/$C_USERNAME/$1" --output "$__target" 2>>log.txt
"$CURLBIN" --silent -u $C_USERNAME:$C_PASS "$C_FDAV_URL/$C_USERNAME/$(escapeWhitespaces "$1")" --output "$__target"
local ret=$?
#echo "ret: $ret"
if [[ $ret -ne 0 ]]; then
isFile "$__target" || return $ret
logErr "$M_ERRORGETFILE: $__target"
>&2 cat "$__target"
rm "$__target"
fi
if fileHasError "$__target" "$1"; then
logErr "$M_ERRORDOWNLOAD"
>&2 cat "$__target" 2>/dev/null
rm "$__target"
return 36
fi
return $ret
}
################################################################
#### OCS SHARE API OPERATIONS
# curl -u "myuser:mypassword" -H "OCS-APIRequest: true" \
# -X POST https://nextcloud/ocs/v1.php/apps/files_sharing/api/v1/shares \
# -d path="globalshare/0002/pic.jpg" -d shareType=3 -d permissions=1 -d expireDate="2017-07-20"
# List shares for a folder (defaults to root folder)
listShares() {
local tgt=""
if ! isEmpty "$1"; then
tgt="$(rawUrlEncode "$1")"
#tgt='?path='"$tgt"'&reshares=true&shared_with_me=1'
tgt='?path='"$tgt"'&reshares=true'
fi
#echo "tgt: $tgt"
local xml="$("$CURLBIN" -s -u $C_USERNAME:$C_PASS -H "OCS-APIRequest: true" -X GET "$C_OCS_URL/shares$tgt")"
printSharesTable "$xml"
return $?
}
# Prints shares as a formatted table
printSharesTable() {
isEmpty "$1" && return $FALSE
local ITEMS=()
local ID="-"
local SHARE_TYPE="-"
local SHARE_OWNER="-"
local FILE_OWNER="-"
local EXPIRATION="-"
local PATH="-"
local SHARE_WITH="-"
local TOKEN="-"
local DIVTEMPLATE="+-------+------+------------+------------+------------+------------------+------------------------------------+----------------------------------------------------------------------+\n"
local PFTEMPLATE='| %-5.5s | %-4.4s | %-10.10s | %-10.10s | %-10.10s | %-16.16s | %-34.34s | %-68.68s |\n'
local expGet=()
while read_dom; do
# 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
# ID | SHARE_TYPE | SHARE_OWNER | FILE_OWNER | EXPIRATION | TOKEN | SHARE_WITH | PATH
if [[ "$ENTITY" = "id" ]]; then
IDS+=("${CONTENT}")
ID="$CONTENT"
elif [[ "$ENTITY" = "share_type" ]]; then
SHARE_TYPE="$CONTENT"
elif [[ "$ENTITY" = "uid_owner" ]]; then
SHARE_OWNER="$CONTENT"
elif [[ "$ENTITY" = "uid_file_owner" ]]; then
FILE_OWNER="$CONTENT"
elif [[ "$ENTITY" = "expiration" ]]; then
expGet=($CONTENT)
EXPIRATION="${expGet[0]}"
elif [[ "$ENTITY" = "path" ]]; then
PATH="${CONTENT// /%20}"
elif [[ "$ENTITY" = "share_with" ]]; then
SHARE_WITH="$CONTENT"
elif [[ "$ENTITY" = "token" ]]; then
TOKEN="$CONTENT"
elif [[ "$ENTITY" = "/element" ]]; then
if [[ "$ID" = "-" ]]; then
SHARE_TYPE="-" ; SHARE_OWNER="-" ; FILE_OWNER="-" ; EXPIRATION="-" ; PATH="-" ; SHARE_WITH="-" ; TOKEN="-"
continue
fi
#ITEMS+=("$ID $SHARE_TYPE $SHARE_OWNER $FILE_OWNER $EXPIRATION $SHARE_WITH $TOKEN $PATH")
ITEMS+=("$ID $SHARE_TYPE $SHARE_OWNER $FILE_OWNER $EXPIRATION $TOKEN $SHARE_WITH $PATH")
ID="-" ; SHARE_TYPE="-" ; SHARE_OWNER="-" ; FILE_OWNER="-" ; EXPIRATION="-" ; PATH="-" ; SHARE_WITH="-" ; TOKEN="-"
fi
done < <(printf '%s\n' "$1")
printf "$DIVTEMPLATE"
printf "$PFTEMPLATE" "ID" "TYPE" "SHOWNER" "FOWNER" "EXPIRATION" "TOKEN" "SHARED_TO" "PATH"
printf "$DIVTEMPLATE"
for i in "${ITEMS[@]}"; do
i=($i)
passDetect='1|$'
shareTo="${i[6]}"
if [[ $shareTo = "$passDetect"* ]]; then
shareTo="Shared With Password"
fi
printf "$PFTEMPLATE" "${i[0]}" "${i[1]}" "${i[2]}" "${i[3]}" "${i[4]}" "${i[5]}" "${shareTo}" "${i[7]//%20/ }"
done
printf "$DIVTEMPLATE"
}
# Prints the formatted links list
# <URL>,<PATH>
listLinks() {
local xml="$("$CURLBIN" -s -u $C_USERNAME:$C_PASS -H "OCS-APIRequest: true" -X GET "$C_OCS_URL/shares")"
printLinks "$xml"
return $?
}
# Parses the list link to be printed
printLinks() {
isEmpty "$1" && return $FALSE
local ITEMS=()
local PATH="-"
local TOKEN="-"
while read_dom; do
# PATH,TOKEN
if [[ "$ENTITY" = "path" ]]; then
PATH="${CONTENT// /%20}"
elif [[ "$ENTITY" = "token" ]]; then
TOKEN="$CONTENT"
elif [[ "$ENTITY" = "/element" ]]; then
if [[ "$PATH" = "-" ]] || [[ "$TOKEN" = "-" ]]; then
PATH="-" ; TOKEN="-"
continue
fi
#ITEMS+=("$ID $SHARE_TYPE $SHARE_OWNER $FILE_OWNER $EXPIRATION $SHARE_WITH $TOKEN $PATH")
ITEMS+=("$C_BASE_URL/s/$TOKEN,$PATH")
PATH="-" ; TOKEN="-"
fi
done < <(printf '%s\n' "$1")
for i in "${ITEMS[@]}"; do
printf "%s\n" "${i//%20/ }"
done
}