-
-
Save ChristianBagley/6adb520f3e46e683a77e14ba60d032f3 to your computer and use it in GitHub Desktop.
Bash and zsh alias for transfer.sh. Transfers files and directories to transfer.sh.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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