Check for file size before trying to send fax

This commit is contained in:
Gandalf 2021-03-17 13:40:13 +01:00
parent 4870aca1fd
commit f9c39da3bb
2 changed files with 11 additions and 2 deletions

11
fax.sh
View file

@ -15,6 +15,13 @@ FILE=$1
echo -n "Sending file ${FILE} " echo -n "Sending file ${FILE} "
REC=$(echo "$2" | sed -e 's:[^+0-9]::g' -e 's:^0\([1-9]\):+49\1:') REC=$(echo "$2" | sed -e 's:[^+0-9]::g' -e 's:^0\([1-9]\):+49\1:')
echo "to $REC" echo "to $REC"
#Checking for file size:
SIZE=$(ls -l | cut -d " " -f5)
if [ $SIZE -gt 10485760 ]
then
echo "File too big. Cannot be more than 10MB."
exit 2
fi
#Needs to be base64 encoded: #Needs to be base64 encoded:
CONTENT=$(base64 $FILE -w 0) CONTENT=$(base64 $FILE -w 0)
if [ -z "$CONTENT" ]; if [ -z "$CONTENT" ];
@ -25,7 +32,9 @@ fi
#save payload to tempfile to prevent curl from throwing 'argument list too long' error. #save payload to tempfile to prevent curl from throwing 'argument list too long' error.
echo '{"faxlineId":"f0", "recipient":"'${REC}'", "filename":"fax.pdf", "base64Content":"'${CONTENT}'"}' > /tmp/sipgatedata.txt echo '{"faxlineId":"f0", "recipient":"'${REC}'", "filename":"fax.pdf", "base64Content":"'${CONTENT}'"}' > /tmp/sipgatedata.txt
#Send fax #Send fax
FAXID=$(curl --request POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header "Authorization: Bearer $AUTH_TOKEN" --data @/tmp/sipgatedata.txt "https://api.sipgate.com/v2/sessions/fax" -s | jq '.sessionId' | sed -e 's:"::g' ) REPLY=$(curl --request POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header "Authorization: Bearer $AUTH_TOKEN" --data @/tmp/sipgatedata.txt "https://api.sipgate.com/v2/sessions/fax" -s)
FAXID=$(echo $REPLY | jq '.sessionId' | sed -e 's:"::g' )
echo $REPLY > /tmp/sipgatereply.txt
#remove tempfile #remove tempfile
rm /tmp/sipgatedata.txt rm /tmp/sipgatedata.txt
if [ -z $FAXID ] if [ -z $FAXID ]

View file

@ -68,5 +68,5 @@ else
fi fi
if [ $DF = true ] if [ $DF = true ]
then then
grep -v $ID $BASEPATH/.fax_history > $BASEPATH/.fax_history echo "$(grep -v "^$ID$" $BASEPATH/.fax_history)" > $BASEPATH/.fax_history
fi fi