-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashDownloadFile
More file actions
executable file
·166 lines (143 loc) · 6 KB
/
Copy pathbashDownloadFile
File metadata and controls
executable file
·166 lines (143 loc) · 6 KB
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
#!/bin/bash
#include
. bashCheckForCRFolder
#Bold colors
BWHITE='\033[1;37m'
BGREEN='\033[1;32m'
BRED='\033[1;31m'
BYELLOW='\033[1;33m'
BBLUE='\033[1;34m'
UBLUE='\033[4;34m'
NC='\033[0m' # No color
#create listFiles variable
listFiles="$(ls -d clientResourcesFolder/*essages.json 2>&1)"
lsError=$(cut -d ':' -f3 <<< $listFiles)
#Message ID and markRead variable
messageId=
fileRead=false
markRead=""
skipMarkReadLoop="n"
#Move to files directory
cd files
#Create listFolders array
listFolders=($(ls -d */))
folders=()
#Remove / from end of folder name at each index in array and add to folders array
for ((i=0; i<${#listFolders[@]}; i++)); do
folders+=(${listFolders[i]%?})
done
#Create variable for client resource folder to compare to folders array
inboundFolder="inbound"
fileExists=false
#Compare folder name at each index of folders array to see if inboundFolder name exists and
#set fileExists to true if inboundFolder name exists and move back to sea-client-nodejs directory
for ((i=0; i<${#folders[@]}; i++)); do
if [[ "${folders[$i]}" == "$inboundFolder" ]]
then
fileExists=true
cd ..
fi
done
#If fileExists is not true then make a directory with the inboundFolder name
if [ $fileExists != true ]
then
mkdir $inboundFolder
cd ..
fi
#Option for message ID
while getopts ":m:" option; do
case $option in
m) # give mailboxId
messageId=$OPTARG
markRead="n"
skipMarkReadLoop="y"
while getopts r option; do
case $option in
r) # mark file as read
fileRead=true;;
\?) #Invalid option
echo -e "\n${BRED}ERROR: Invalid option\n${NC}"
exit;;
esac
done;;
\?) #Invalid option
echo -e "\n${BRED}ERROR: Invalid option\n${NC}"
exit;;
esac
done
#Check if error exists, display message to run command for getting unread messages if unreadMessages.json file does not exist
if [[ "$lsError" == " No such file or directory" ]] && ! [[ $messageId ]]
then
echo -e "\n${BWHITE}\"sea-client-nodejs/clientResourcesFolder/unreadMessages.json\"${BYELLOW} nor a date ranged message file currently exist to pull a ${BWHITE}messageId ${BYELLOW}from ${BBLUE}${UBLUE}please run one of the below commands to pull unread messages or date ranged messages for a ${NC}${BWHITE}messageId ${BBLUE}${UBLUE}before running this commmand again${NC}${BYELLOW} or enter ${BWHITE}7${BYELLOW} OR ${BWHITE}9${BYELLOW} from menu options when you run ${BGREEN}./bashMenu${BYELLOW}:"
echo -e "\n\n${BWHITE}Command: ${BGREEN}./bashCurlGetUnread"
echo -e "\n\n${BWHITE}Command: ${BGREEN}./bashGetDeliveredFilesByDateRange\n"
exit 1
fi
#While messageId variable empty prompt user of messageId
while ! [ $messageId ]; do
read -p "$(echo -e "\n${BYELLOW}Please enter ${BWHITE}messageId${BYELLOW} of message you want to download from ${BWHITE}\"sea-client-nodejs/$checkFolder/unreadMessages.json\"${BYELLOW} OR from a date ranged file that you have within ${BWHITE}\"sea-client-nodejs/$checkFolder/\"${BYELLOW}:${NC} ")" messageId
echo -e "\n\n"
done
#Run getClientFile node function and set fileDownload variable to output
fileDownload=$(node getClientFile -k $messageId)
#Cut file name from output fileDownload string and remove the leading space
downloadedFile=$(cut -d ':' -f30 <<< $fileDownload)
downloadedFile=$(cut -c1- <<< $downloadedFile)
catchError=$(cut -d ':' -f5 <<< $fileDownload)
catchError=$(cut -c1- <<< $catchError)
#If error exists in output of fileDownload display error message and example of command
if [ "$catchError" == "Filename not retrieved." ]
then
echo -e "\n${BRED}ERROR: You have entered an ${BWHITE}invalid messageId${BRED}! Please run one of the below commands again with a valid messageId."
echo -e "\n\n${BWHITE}Example without mark read option: ${BGREEN}./bashDownloadFile -m <messageId>${NC}"
echo -e "\n\n${BWHITE}Example with mark read option: ${BGREEN}./bashDownloadFile -m <messageId> -r${NC}"
echo -e "\n\n${BYELLOW}OR...${NC}"
echo -e "\n\n${BWHITE}Example: ${BGREEN}./bashDownloadFile${NC}\n"
echo -e "\n\n${NC}${BYELLOW}OR...${NC}"
echo -e "\n\n${NC}${BWHITE}Run this command and enter ${BBLUE}11${BWHITE} from Menu options: ${BGREEN}./bashMenu\n${NC}"
exit 1
else
#Display location of downloaded file
echo -e "\n\n${BGREEN}$downloadedFile has been downloaded to ${BWHITE}\"sea-client-nodejs/files/inbound/$downloadedFile\"${BGREEN}.${NC}\n\n\n"
fi
#While markRead is empty ask user for valid input
notValid=
while [ "$markRead" != "y" ] && [ "$markRead" != "n" ] && [ "$skipMarkReadLoop" == "n" ]; do
if [[ "$markRead" != "" ]] && [[ "$markRead" != "y" ]] && [[ "$markRead" != "n" ]] || [[ "$notValid" = 1 ]]
then
read -p "$(echo -e "${BRED}Please enter a valid reponse of ${BWHITE}y ${BRED}or ${BWHITE}n${BRED}: ${NC}")" markRead
echo -e "\n\n"
notValid=1
else
read -p "$(echo -e "\n${BYELLOW}Would you like to mark ${BWHITE}$downloadedFile${BYELLOW} as read ${BWHITE}(y/n)${BYELLOW}:${NC} ")" markRead
echo -e "\n\n"
notValid=1
fi
done
#If markRead is y then set fileRead boolean as true
if [ "$markRead" == "y" ]
then
fileRead=true
elif [ "$fileRead" = true ]
then
doNothing=
else
echo -e "${BBLUE}If you want to mark this file as read in the future and it is not already marked as read you can do so by running the below command:"
echo -e "\n\n${BWHITE}Command: ${BGREEN}./bashMarkAsRead -m $messageId${NC}\n"
fi
#If $fileRead boolean is true then markFile as read
if [ "$fileRead" = true ]
then
fileMarkedRead=$(node markFileRead -m $messageId)
#Set checkError variable to field 8 of cut string from markAsRead variable
checkError=$(cut -d ' ' -f8 <<< $fileMarkedRead)
#See if Failed exists in checkError variable and if do display error message
if [ "$checkError" == "Failed" ]
then
echo -e "\n${BYELLOW}Cannot mark message as read! ${BWHITE}$downloadedFile${BYELLOW} has already been marked as read!${NC}\n"
exit 1
#Else display success message
else
echo -e "${BWHITE}$downloadedFile${BGREEN} has been marked as read.\n\n\n"
fi
fi