diff options
Diffstat (limited to '.local/bin/rupload')
-rwxr-xr-x | .local/bin/rupload | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/.local/bin/rupload b/.local/bin/rupload new file mode 100755 index 0000000..925fc0e --- /dev/null +++ b/.local/bin/rupload @@ -0,0 +1,65 @@ +#!/bin/sh + +if [ $# -eq 0 ]; then + printf '%s\n' "usage: $0 [-n] [-x override_ext] <filename>" >&2 + printf '\t%s\n' "-n: dry run (don't actually upload anything)" >&2 + printf '\t%s\n' "-x override_ext: set extension to override_ext on uploaded filename" >&2 + exit 1 +fi + +dry_run=0 +while [ $# -gt 1 ]; do + if [ "$1" = "-x" ]; then + override_ext=".$2" + shift 2 + elif [ "$1" = "-n" ]; then + dry_run=1 + shift + fi +done + +if [ ! -f "$1" ]; then + printf %s "$1: file does not exist" >&2 + exit 1 +fi + +REMOTE="do" +URLBASE="https://ubq323.website/files/" +REMOTEDIR="/srv/web/ubq/files/" + +ext="${1#*.}" +if [ "$ext" = "$1" ]; then + ext='.file' + bname="$(basename "$1")" +else + ext=".$ext" + bname="$(basename "$1" "$ext")" +fi +ext=${override_ext:-$ext} + +rbname="$(md5sum "$1"|cut -d' ' -f1|tr 'a-z' 'A-Z'|basenc -d --base16|basenc --base64|head -c8)" +rname="$rbname$ext" + +exists_already() { + ssh "$REMOTE" stat "$REMOTEDIR$1" 2>/dev/null >/dev/null + return $? +} + +while exists_already "$rname"; do + printf "%s exists already\n" "$rname" + rbname="${rbname}_" + rname="$rbname$ext" +done + +printf 'uploading to %s\n' "$rname" +if [ "$dry_run" -eq 1 ]; then + echo dryrun, not uploading anything + exit 0 +fi + +scp "$1" "${REMOTE}:$REMOTEDIR$rname" +ssh "$REMOTE" chmod a+r "$REMOTEDIR$rname" + +url="$URLBASE$rname" +printf %s "$url" | wl-copy +notify-send 'copied url to clipboard' "$url" |