Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 98 lines (77 sloc) 1.69 KB
#!/bin/zsh
# Upload one or more files to https://file.io. Also yanks the url(s) to
# clipboard if you have `xclip, or pbcopy` installed.
#
# ---------------------------------
# Author:
# ---------------------------------
# Prajjwal Singh
# sin@prajjwal.com
# https://prajjwal.com/about.html
#
# Dependencies:
# - jq
#
# Optional Dependencies:
# - xclip | pbcopy (MacOS)
source "${0%/*}/include/util.sh"
ensure_presence_of jq
TTL="1d" # Files live for 1 day by default
BASE_URL="https://file.io"
usage() {
cat <<EOF
Usage: fileio [OPTIONS] [FILES]
Options:
-t Specify TTL of file in [d]ays, [w]eeks, [m]onths, or [y]ears.
Examples:
fileio -t 2d file.txt file-2.txt
fileio -t 1y file.txt
EOF
exit 1
}
while getopts ":t:h" opt; do
case $opt in
t)
TTL=$OPTARG
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "Option -$OPTARG requires an argument" >&2
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() {
response=$(curl -s -F "file=@$1" "$BASE_URL/?expires=$TTL")
link=$(echo $response | jq '.["link"]' | cut -d '"' -f2)
error=$(echo $response | jq '.["error"]')
if [[ "$link" == "null" ]]; then
echo $error
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 URLS to clipboard
copy_to_clipboard $links
echo $links