-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshorten
executable file
·33 lines (29 loc) · 1.14 KB
/
shorten
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
#!/bin/bash
# shorten <url> [path]
# uses my URL shortener (https://github.com/purarue/no-db-shorturl)
# to shorten a URL to some random string
# if 'path' (second argument) isn't provided,
# the server generates a random hash
# e.g.
# shorten "https://wiki.archlinux.org/index.php/File_opener" open
# would return "https://purarue.xyz/s/open"
# which now redirects to the archwiki link
# handle user input
readonly UPLOAD_TO="https://purarue.xyz/s/"
readonly SHORTURL_TOKEN="${SHORTURL_TOKEN:?No shorturl token set}"
readonly URL="${1:?No url provided to shorten}"
readonly HASH="$2" # is fine if this is empty
# create JSON string
declare data
data="$(jq -n --arg TOKEN "$SHORTURL_TOKEN" --arg URL "$URL" --arg HASH "$HASH" '{"key":$TOKEN, "url":$URL,"hash":$HASH}')"
# make request to server
declare SHORTURL_PART
SHORTURL_PART="$(curl --silent "Content-Type: application/json" --request POST --data "$data" "$UPLOAD_TO")"
# check for 'error' in response text
if grep -qi error <<<"$SHORTURL_PART"; then
echo "${SHORTURL_PART}"
else
echo "${UPLOAD_TO}${SHORTURL_PART}"
# copy the url to my clipboard
echo -n "${UPLOAD_TO}${SHORTURL_PART}" | clipcopy
fi