Skip to content

Instantly share code, notes, and snippets.

@Prajjwal
Forked from nl5887/transfer.fish
Last active March 20, 2020 20:07
Show Gist options
  • Save Prajjwal/63f5df1176ea05ee3679c04e0fa389ec to your computer and use it in GitHub Desktop.
Save Prajjwal/63f5df1176ea05ee3679c04e0fa389ec to your computer and use it in GitHub Desktop.
Pure zsh client for transfersh, supports optional archiving and yanking to clipboard.
#!/bin/zsh
# Defines transfer alias and provides easy command line file.
#
# Authors:
# Remco Verhoef <[email protected]>
# Prajjwal Singh <[email protected]>
#
# Dependencies:
# * curl
# * zsh
#
# Optional Dependencies:
# * tar
# * xclip
#
# TODO:
# * Add support for sharing folders.
# * Add support for encrypting files before upload.
MAX_DOWNLOADS=5
MAX_DAYS=1
BASE_URL="https://transfer.sh"
usage() {
cat <<EOF
Usage: transfer.sh [OPTIONS] [FILES]
Options:
-c Specify maximum download count. Defaults to \$MAX_DOWNLOADS
-t Specify TTL of file in days. Defaults to \$Max_days
-h Display this help.
Examples:
transfer.sh file1.txt
transfer.sh -c 1 -t 5 file.txt
EOF
exit 1
}
if ! hash curl 2> /dev/null
then
echo "Could not find curl."
usage
fi
while getopts "c:t:h" opt; do
case $opt in
c)
MAX_DOWNLOADS=$OPTARG
;;
t)
MAX_DAYS=$OPTARG
;;
h)
usage
;;
esac
done
# Get all files after options
files=(${@:$OPTIND})
file_count=${#files[@]}
# Print usage & quit if no files were specified
[ $file_count -eq 0 ] && usage
links=""
# Upload a file to file.io, and append resulting url to $urls
function upload() {
link=$(curl --silent -H "Max-Downloads: $MAX_DOWNLOADS" -H "Max-Days: $MAX_DAYS" --upload-file $1 "$BASE_URL/`basename $1`")
if [[ "$link" == "null" ]]; then
exit 1
else
if [ -z $links ]; then
links=$link
else
links="$links\n$link"
fi
fi
}
# Upload each input file
for file in $files; do
upload $file
done
# Copy url to clipboard if xclip is installed.
hash xclip 2> /dev/null && echo $links | xclip -selection c
# Simply write links to STDOUT otherwise.
echo $links
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment