summaryrefslogtreecommitdiff
path: root/.local/bin/rupload
blob: 925fc0ec77619dfdb363d7d27bacc035d931d46e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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"