144 lines
2.1 KiB
Bash
Executable File
144 lines
2.1 KiB
Bash
Executable File
#! /bin/bash
|
|
|
|
# Use of bash is required because "upload_dir" is recursive and
|
|
# thus needs local variables.
|
|
|
|
set -eu
|
|
|
|
usage()
|
|
{
|
|
printf "$0 <srcdir> <dstdir> <url>\n"
|
|
}
|
|
|
|
escape_url()
|
|
{
|
|
perl -MURI::Escape -e \
|
|
'print uri_escape shift, , q{^A-Za-z0-9\-._~/:}' -- "$1"
|
|
}
|
|
|
|
if [ $# != 3 ]
|
|
then
|
|
usage >&2
|
|
exit 1
|
|
fi
|
|
|
|
SRC="$1"
|
|
DIR="$2"
|
|
URL="$3"
|
|
|
|
echo Username: >&2
|
|
IFS= read -r USER
|
|
|
|
if printf '%s' "$USER" | grep -qe '[[:space:]]'
|
|
then
|
|
echo Username cannot contain whitespaces >&2
|
|
exit 1
|
|
fi
|
|
|
|
TTYCFG=$(stty -g)
|
|
trap "stty $TTYCFG" INT QUIT TERM EXIT
|
|
stty -echo
|
|
echo Password: >&2
|
|
IFS= read -r PWD
|
|
stty echo
|
|
# Force newline
|
|
echo
|
|
|
|
COOKIE="$(mktemp)"
|
|
|
|
cleanup()
|
|
{
|
|
rm -f $COOKIE
|
|
}
|
|
|
|
slcl_mkdir()
|
|
{
|
|
if ! curl --head \
|
|
--fail-with-body \
|
|
--no-progress-meter \
|
|
-s \
|
|
-b "$COOKIE" \
|
|
-o /dev/null \
|
|
"$(escape_url "$URL/user$2/$1/")"
|
|
then
|
|
curl -X POST \
|
|
--fail-with-body \
|
|
-b "$COOKIE" \
|
|
--data-urlencode "name=$1" --data-urlencode "dir=$2/" \
|
|
"$URL/mkdir"
|
|
fi
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
curl -X POST \
|
|
--fail-with-body \
|
|
-c "$COOKIE" \
|
|
--data-urlencode "username=$USER" --data-urlencode "password=$PWD" \
|
|
"$URL/login"
|
|
|
|
LEVEL=0
|
|
|
|
upload_dir()
|
|
{
|
|
local SRC="$1"
|
|
local DIR="$2"
|
|
local N=$(cd "$1" && ls | wc -l)
|
|
local I=1
|
|
|
|
test $N -eq 0 && return
|
|
|
|
LEVEL=$(($LEVEL + 1))
|
|
|
|
while read f
|
|
do
|
|
for i in $(seq $LEVEL)
|
|
do
|
|
printf "-"
|
|
done
|
|
|
|
printf " [%d/%d] %s -> %s " "$I" "$N" "$f" "$DIR$SRC/"
|
|
|
|
if test -d "$SRC/$f"
|
|
then
|
|
printf "[entering directory]\n"
|
|
slcl_mkdir "$f" "$DIR$SRC"
|
|
upload_dir "$SRC/$f" "$DIR"
|
|
elif ! test -f "$SRC/$f"
|
|
then
|
|
printf "[not a file]\n"
|
|
elif curl --head \
|
|
--fail-with-body \
|
|
--no-progress-meter \
|
|
-s \
|
|
-b "$COOKIE" \
|
|
-o /dev/null \
|
|
"$(escape_url "$URL/user$DIR$SRC/$f")"
|
|
then
|
|
printf "[skipping]\n"
|
|
else
|
|
while ! curl -X POST \
|
|
--fail-with-body \
|
|
-b "$COOKIE" \
|
|
-F dir="$DIR$SRC/" \
|
|
-F "file=@$SRC/$f;filename=$(basename "$f")" \
|
|
"$(escape_url "$URL/upload")"
|
|
do
|
|
printf "[retrying]"
|
|
sleep 5
|
|
done
|
|
|
|
printf "[done]\n"
|
|
fi
|
|
|
|
I=$(($I + 1))
|
|
done <<-EOF
|
|
$(cd "$1" && ls)
|
|
EOF
|
|
|
|
LEVEL=$(($LEVEL - 1))
|
|
}
|
|
|
|
slcl_mkdir "$SRC" "$DIR"
|
|
upload_dir "$SRC" "$DIR"
|