From 5d0f729cbc01311fd1f6d9fc71b1511083cdb5c6 Mon Sep 17 00:00:00 2001 From: Cyteen May Date: Sun, 17 Jun 2018 11:07:24 +0100 Subject: [PATCH] Add function/alias to allow uploading files to http://transfer.sh from the commandline. --- 020_zsh.sh | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/020_zsh.sh b/020_zsh.sh index 96f4f54..592c2e8 100644 --- a/020_zsh.sh +++ b/020_zsh.sh @@ -263,5 +263,75 @@ alias ipython="_py_version" alias jupyter="_py_version" EOF +cat > .zsh_aliases.d/003-transfer.zsh <<'EOF' +# +# Defines transfer alias and provides easy command line file and folder sharing. +# +# Authors: +# Remco Verhoef +# + +curl --version 2>&1 > /dev/null +if [ $? -ne 0 ]; then + echo "Could not find curl." + return 1 +fi + +transfer() { + # check arguments + if [ $# -eq 0 ]; + then + echo -e "No arguments specified.\n\nUsage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md" + return 1 + fi + + # get temporarily filename, output is written to this file show progress can be showed + tmpfile=$( mktemp -t transferXXX ) + + # upload stdin or file + file=$1 + + if tty -s; + then + basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g') + + if [ ! -e $file ]; + then + echo "File $file doesn't exists." + return 1 + fi + + if [ -d $file ]; + then + # zip directory and transfer + zipfile=$( mktemp -t transferXXX.zip ) + cd $(dirname $file) && zip -r -q - $(basename $file) >> $zipfile + curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" >> $tmpfile + rm -f $zipfile + else + # transfer file + curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >> $tmpfile + fi + else + # transfer pipe + curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile + fi + + # cat output link + cat $tmpfile + + # log file link + APPLICATION="${0##*/}" + RIGHTNOW="$(date)" + EXPIRES="$(date -d "+14 days")" + echo -e "$(cat "$tmpfile") - uploaded $RIGHTNOW - expires $EXPIRES\n" >> ~/$APPLICATION.log + echo "See ~/$APPLICATION.log for all transfers." + + # cleanup + rm -f $tmpfile +} +EOF + + chown -R root.root /usr/local/share/zsh/site-functions chmod -R 755 /usr/local/share/zsh/site-functions