forked from electricdusk/rushlink
123 lines
3.1 KiB
Bash
Executable File
123 lines
3.1 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
|
|
set -e
|
|
|
|
LINK=https://hashru.link/
|
|
DELFILE="${XDG_DATA_HOME:-$HOME/.local/share}/rushlink/delete"
|
|
|
|
link() {
|
|
case "$1" in
|
|
https://*|http://*)
|
|
printf '%s' "$1" | curl -sS -F'shorten=<-' "$LINK"
|
|
;;
|
|
*)
|
|
curl -sS -Ffile=@- "$LINK" < "$1"
|
|
;;
|
|
esac | sed -n '1p;/deleteToken/{s@.*\(https://\)@\1@;p;q}' | (
|
|
IFS= read -r link
|
|
IFS= read -r deletelink
|
|
echo "$link"
|
|
mkdir -p "$(dirname "$DELFILE")"
|
|
echo "$deletelink" >> "$DELFILE"
|
|
)
|
|
}
|
|
|
|
del() {
|
|
case "$1" in
|
|
https://*|http://*)
|
|
URL="$1"
|
|
;;
|
|
*)
|
|
URL="$LINK$1"
|
|
;;
|
|
esac
|
|
# Remove file extension, if any.
|
|
NAME="${URL##*/}"
|
|
URL="${URL%/*}/${NAME%%.*}"
|
|
if DELURL=$(grep -s -m1 "$URL?deleteToken=" "$DELFILE"); then
|
|
echo "Deleting $URL..." >&2
|
|
curl -sS -X DELETE "$DELURL" | grep deleted
|
|
else
|
|
echo "Delete token for $URL is not known." >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
screenshot() {
|
|
if command -v maim >/dev/null 2>&1; then
|
|
CMD="maim -ks"
|
|
elif command -v import >/dev/null 2>&1; then
|
|
CMD="import png:-"
|
|
else
|
|
echo "Neither maim nor import were found. One of these is needed to make screenshots." >&2
|
|
exit 1
|
|
fi
|
|
FILE=$(mktemp)
|
|
if $CMD > "$FILE"; then
|
|
LINK=$(link "$FILE")
|
|
rm -f "$FILE"
|
|
echo "$LINK.png"
|
|
if command -v xdg-open >/dev/null 2>&1; then
|
|
xdg-open "$LINK.png"
|
|
fi
|
|
else
|
|
rm -f "$FILE"
|
|
exit $?
|
|
fi
|
|
}
|
|
|
|
if [ $# -gt 0 ]; then
|
|
case "$1" in
|
|
--help|-h)
|
|
CMD=$(basename "$0")
|
|
echo "$CMD - Command line tool for $LINK"
|
|
echo
|
|
echo "Usage:"
|
|
echo " $CMD https://example.com/"
|
|
echo " Shorten link."
|
|
echo " $CMD file.txt"
|
|
echo " $CMD < file.txt"
|
|
echo " Upload file."
|
|
echo " $CMD"
|
|
echo " echo hi | $CMD"
|
|
echo " Upload file from standard input."
|
|
echo " $CMD (--screenshot|-s)"
|
|
echo " Select a window or an area of your screen, and upload it as png."
|
|
echo " $CMD (--delete|-d) ${LINK}xd42"
|
|
echo " $CMD (--delete|-d) xd42"
|
|
echo " Delete file or shortened link."
|
|
echo
|
|
echo "Delete tokens are stored in $DELFILE"
|
|
exit 0
|
|
;;
|
|
--delete|-d)
|
|
shift
|
|
for url in "$@"; do
|
|
del "$url"
|
|
done
|
|
exit 0
|
|
;;
|
|
--screenshot|-s)
|
|
shift
|
|
if [ $# -gt 0 ]; then
|
|
echo "No arguments expected to --screenshot" >&2
|
|
exit 1
|
|
fi
|
|
screenshot
|
|
exit 0
|
|
;;
|
|
*)
|
|
for url in "$@"; do
|
|
link "$url"
|
|
done
|
|
exit 0
|
|
esac
|
|
fi
|
|
|
|
if [ -t 0 ]; then
|
|
echo "Sending standard input to $LINK" >&2
|
|
echo "^C to cancel, ^D to send." >&2
|
|
fi
|
|
|
|
link /dev/stdin
|