forked from jiggak/tinyurl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyurl.in
More file actions
76 lines (64 loc) · 1.59 KB
/
tinyurl.in
File metadata and controls
76 lines (64 loc) · 1.59 KB
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
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <josh@slashdev.ca> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return Josh Kropf
# ----------------------------------------------------------------------------
SCRIPT_URL=@SCRIPT_URL@
DATA_DIR=@DATA_DIR@
TOKEN_LEN=@TOKEN_LEN@
MAX_TOKEN=
for ((i=0; i<${TOKEN_LEN}; ++i)); do
MAX_TOKEN=${MAX_TOKEN}f
done
usage() {
echo -n "\
usage: tinyurl
help This help message
del|rm [token] Remove url with token
add [url] Add new url and print shortened version
list|ls List all short and long url's (default command)
"
exit 1
}
random_token() {
# random integer value between 0 and MAX_TOKEN
val=$((${RANDOM} % 0x${MAX_TOKEN}))
# convert back to hex
val=$(printf '%x' ${val})
# pad with zeros if too short
while [ ${#val} -lt ${TOKEN_LEN} ]; do
val=0${val}
done
echo $val
}
list() {
for f in $(ls ${DATA_DIR}); do
echo "${SCRIPT_URL}${f} => $(cat ${DATA_DIR}/${f})"
done
}
remove() {
if ! rm ${DATA_DIR}/${1} 2>/dev/null; then
echo "token '${1}' not found" >&2
exit 1
fi
}
add() {
token=$(random_token)
echo -n "${1}" >${DATA_DIR}/${token}
echo "${SCRIPT_URL}${token} => ${1}"
}
case ${1:-ls} in
del|rm)
[ "${2}" == "" ] && usage
remove ${2} ;;
add)
[ "${2}" == "" ] && usage
add ${2} ;;
list|ls)
list ;;
help|*)
usage ;;
esac
exit 0