local/bin/transfer: Add support for multiple files

master
Pierre Neidhardt 2018-08-12 16:16:51 +02:00
parent a4f4ebe174
commit 05adc7c537
1 changed files with 31 additions and 38 deletions

View File

@ -1,12 +1,7 @@
#!/bin/sh #!/bin/sh
## Adapted from https://gist.github.com/nl5887/a511f172d3fb3cd0e42d. ## Adapted from https://gist.github.com/nl5887/a511f172d3fb3cd0e42d.
## Original author:
# ## Remco Verhoef <remco@dutchcoders.io>
# Defines transfer alias and provides easy command line file and folder sharing.
#
# Authors:
# Remco Verhoef <remco@dutchcoders.io>
#
curl --version 2>&1 > /dev/null curl --version 2>&1 > /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -14,53 +9,51 @@ if [ $? -ne 0 ]; then
exit 1 exit 1
fi fi
# check arguments if [ $# -eq 0 ]; then
if [ $# -eq 0 ];
then
cat<<EOF cat<<EOF
No arguments specified. No arguments specified.
Usage: transfer /tmp/test.md Usage: transfer FILES
cat /tmp/test.md | transfer test.md cat FILE | transfer FOO
EOF EOF
exit 1 exit 1
fi fi
# get temporarily filename, output is written to this file show progress can be showed # Get temporarily filename, output is written to this file so progress can be showed.
tmpfile=$( mktemp -t transferXXX ) tmpfile=$( mktemp -t transferXXX )
# upload stdin or file # upload stdin or file
file=$1 if tty -s; then
for file; do
basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
if tty -s; if [ ! -e "$file" ]; then
then echo "File $file doesn't exists."
basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g') exit 1
fi
if [ ! -e $file ]; if [ -d "$file" ]; then
then # zip directory and transfer
echo "File $file doesn't exists." zipfile=$( mktemp -t transferXXX.zip )
exit 1 cd $(dirname $file) && zip -r -q - $(basename $file) > $zipfile
fi curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" > $tmpfile
rm -f $zipfile
if [ -d $file ]; else
then # transfer file
# zip directory and transfer curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" > $tmpfile
zipfile=$( mktemp -t transferXXX.zip ) # cat output link
cd $(dirname $file) && zip -r -q - $(basename $file) >> $zipfile out="$out
curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" >> $tmpfile $(cat $tmpfile)"
rm -f $zipfile fi
else done
# transfer file echo "$out"
curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >> $tmpfile
fi
else else
# transfer pipe # transfer pipe
curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile
# cat output link
cat $tmpfile
echo
fi fi
# cat output link
cat $tmpfile
echo
# cleanup # cleanup
rm -f $tmpfile rm -f $tmpfile