First commit
This commit is contained in:
commit
0d9bf4b951
|
@ -0,0 +1,132 @@
|
|||
#! /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"
|
||||
|
||||
upload_dir()
|
||||
{
|
||||
local SRC="$1"
|
||||
local DIR="$2"
|
||||
local N=$(cd "$1" && ls | wc -l)
|
||||
local I=1
|
||||
|
||||
test $N -eq 0 && return
|
||||
|
||||
while read f
|
||||
do
|
||||
printf "[%d/%d] %s -> %s DIR=%s SRC=%s" "$I" "$N" "$f" "$DIR$SRC/" "$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/$SRC$DIR$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]" >&2
|
||||
sleep 5
|
||||
done
|
||||
|
||||
printf " [done]\n"
|
||||
fi
|
||||
|
||||
I=$(($I + 1))
|
||||
done <<-EOF
|
||||
$(cd "$1" && ls)
|
||||
EOF
|
||||
}
|
||||
|
||||
slcl_mkdir "$SRC" "$DIR"
|
||||
upload_dir "$SRC" "$DIR"
|
Loading…
Reference in New Issue