dotfiles2/base/bin/upfile
2022-08-05 22:16:17 -07:00

63 lines
1.2 KiB
Bash
Executable file

#!/bin/ksh
usage() {
echo "usage: $0 <file to upload>" >&2
}
if [ "$1" = "-x" ] ; then
with_message=1
shift
fi
CURL="curl"
if [ "$1" = "-q" ] ; then
with_interop=1
CURL="$CURL -sS"
shift
fi
CREDFILE="$HOME/.config/upfile/credentials"
if [ ! -r "$CREDFILE" ] ; then
echo "err: can't find credentials file. must be at $CREDFILE" >&2
exit 1
fi
upload_file() {
up_file="$1"
if [ -z "$up_file" ] ; then
usage
return 2
fi
if [ ! -f "$up_file" ] ; then
echo "err: file $up_file doesn't exist?" >&2
return 1
fi
res=$($CURL -f -X POST -F "sendfile=@\"$up_file\"" \
-u "$(cat "$CREDFILE")" \
https://f.2ki.xyz/cgi-bin/aperture.cgi)
if [ $? -ne 0 ] ; then
echo "Upload failed: $res" >&2
return 1
fi
CLIP_URL="$res"
}
upload_file "$1" || exit $?
echo -n "$CLIP_URL" | xclip -selection primary
echo -n "$CLIP_URL" | xclip -selection clipboard
if [ -n "$with_interop" ] ; then
echo "$CLIP_URL"
elif [ -n "$with_message" ]; then
notify-send "File uploaded" "Uploaded to $CLIP_URL. URL copied to clipboard."
else
echo "Uploaded to $CLIP_URL. Copied to clipboard."
fi