Add bash script for uploading/downloading files

This commit is contained in:
Léo 2020-11-05 15:31:55 +01:00
parent 031f620486
commit f388124762

248
scripts/up Executable file
View File

@ -0,0 +1,248 @@
#!/usr/bin/env bash
set -euo pipefail
#######################################################################
# Configuration #
#######################################################################
# Curl function to be used
CURL="curl"
DEFAULT_ARGS_CURL=()
# Defaults field to use. If equal to $UNSET will be oncsiedered as no set
URLPATH="/up"
GETPATH="/d"
USERNAME="-1"
PASSWORD="-1"
URL="-1"
FILEPASSWORD="-1"
VISIBLE="-1"
UNSET="-1"
#######################################################################
# Set defaults #
#######################################################################
GET=0
VERB=0
#######################################################################
# Help #
#######################################################################
function usage()
{
echo "Script to send files to sfs in command line"
echo ""
echo "$0 [options] filenames ..."
echo ""
echo "options:"
echo "\t-h --help : help"
echo "\t-u / --username USERNAME : username for log-in"
echo "\t-p / --password PASSWORD: password for log-in"
echo "\t-s / --server / --url URL : URL of server"
echo "\t-v / --visible : make the file publicly visible"
echo "\t-x / --file-password PASSWORD: set a password for downloading of file"
echo "\t-g / --get : Download file rather than upload"
echo "\t--verbose / --infos : Set to verbose mode"
echo ""
echo "filenames:"
echo "\tList of filenames from disk to upload"
echo "\tor if -g is set filename to download."
echo ""
}
#######################################################################
# Colored terms #
#######################################################################
if [[ -t 1 ]]
then
VERT="\033[32m"
ORANGE="\033[1;33m"
ROUGE="\033[1;31m"
STOPC="\033[0m"
else
VERT=""
ORANGE=""
ROUGE=""
STOPC=""
fi
#######################################################################
# Check prerequisites #
#######################################################################
function check_prerequisite()
{
if ! command -v $CURL &> /dev/null
then
echo -e "${ROUGE}ERROR${STOPC}: $CURL could not be found."
echo "Aborting"
exit 2
fi
}
check_prerequisite
#######################################################################
# Parsing arguments #
#######################################################################
FILES=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
usage
exit 0
;;
-u|--username)
if [ $# -gt 1 ]; then
USERNAME="$2"
shift # past argument
shift # past value
else
echo -e "${ROUGE}ERROR${STOPC}: Could not parse argument username"
echo ""
usage
exit 3
fi
;;
-p|--password)
if [ $# -gt 1 ]; then
PASSWORD="$2"
shift # past argument
shift # past value
else
echo -e "${ROUGE}ERROR${STOPC}: Could not parse argument password"
echo ""
usage
exit 3
fi
;;
-s|--server|--url)
if [ $# -gt 1 ]; then
URL="$2"
shift # past argument
shift # past value
else
echo -e "${ROUGE}ERROR${STOPC}: Could not parse argument server url"
echo ""
usage
exit 3
fi
;;
-v|--visible)
VISIBLE="1"
shift # past argument
;;
-x|--file-password)
if [ $# -gt 1 ]; then
FILEPASSWORD="$2"
shift # past argument
shift # past value
else
echo -e "${ROUGE}ERROR${STOPC}: Could not parse argument file password"
echo ""
usage
exit 3
fi
;;
-g|--get)
GET=1
shift # past argument
;;
--verbose|--infos)
VERB=1
shift # past argument
;;
-*)
echo -e "${ROUGE}ERROR${STOPC}: Unknown argument $1"
echo ""
usage
exit 3
;;
*) # Filename
FILES+=("$1")
shift # past argument
;;
esac
done
#######################################################################
# Check arguments #
#######################################################################
# We need username, password (for dl only), url and at least one filename
# Check for filenames
if [ ! ${#FILES[@]} -gt 0 ]
then
echo -e "${ROUGE}ERROR${STOPC}: Filenames not provided"
echo ""
usage
exit 3
fi
# check for url or ask !
if [ "$URL" = "$UNSET" ]
then
read -p "Server URL: " URL
fi
# check for password or ask !
if [ "$USERNAME" = "$UNSET" ]
then
read -p "Username: " USERNAME
fi
if [ $GET -eq 1 ]
then
[ $VERB -gt 0 ] && echo "Download mode"
else
[ $VERB -gt 0 ] && echo "Upload mode"
# check for password or ask !
if [ "$PASSWORD" = "$UNSET" ]
then
read -s -p "Password: " PASSWORD
fi
fi
#######################################################################
# Prepare the query #
#######################################################################
ARGS=()
if [ $GET -eq 1 ]
then
# File password
[ ! $FILEPASSWORD = "$UNSET" ] && ARGS+=("-F" "filepwd=$FILEPASSWORD")
# Add files
for PATH in "${FILES[@]}"
do
FULLURL="$URL"
[ ! "$GETPATH" = "$UNSET" ] && FULLURL+="$GETPATH"
FULLURL+="/$USERNAME/$FILES"
ARGS+=("$FULLURL")
done
else
# Default args
ARGS+=("-F" "username=$USERNAME" "-F" "password=$PASSWORD")
# Visible
[ $VISIBLE -gt 0 ] && ARGS+=("-F" "visible=1")
# File password
[ ! $FILEPASSWORD = "$UNSET" ] && ARGS+=("-F" "filepwd=$FILEPASSWORD")
# Add files
for FPATH in "${FILES[@]}"
do
ARGS+=("-F" "file[]=@$FPATH")
done
FULLURL="$URL"
[ ! "$URLPATH" = "$UNSET" ] && FULLURL+="$URLPATH"
ARGS+=("$FULLURL")
fi
[ $VERB -gt 0 ] && echo "> $CURL ${ARGS[@]}"
$CURL ${ARGS[@]} ${DEFAULT_ARGS_CURL[@]}