forked from vanityURLs/vanityURLs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlnk
executable file
·108 lines (94 loc) · 2.73 KB
/
lnk
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
LOCAL_CONFIG="$HOME/.vanityURLs.conf"
STATIC_FILE="static.lnk"
MAX_ATTEMPTS=100
function print_usage() {
echo "There are two valid ways to call this script:"
echo -e "1) A random shortcode will be generated automatically:\n\t./scripts/lnk [LONG_URL]"
echo -e "2) Specify the shortcode you want:\n\t./scripts/lnk [LONG_URL] [SHORTCODE]"
exit 1
}
# Function to generate a random character from a given set
function random_char() {
# Potentially ambiguous characters (e.g. '1', 'I', and 'l') are
# ignored.
local characters="34789abcdefghijmnpqrstwxyABCDEFHJLMNPQRTUVWXY"
local N_CHARS=${#characters}
local index=$((RANDOM % $N_CHARS))
echo -n "${characters:index:1}"
}
function generate_shortcode() {
# Generate a random string
random_string=""
for i in $(seq 1 $SHORTCODE_LENGTH); do
random_string="${random_string}$(random_char)"
done
# Check if "/$random_string is already in the file
counter=0 # Initialize the counter
while [ "$counter" -le $MAX_ATTEMPTS ]; do
if grep -q "^/$random_string" "$STATIC_FILE"; then
((counter++))
else
echo $random_string
break
fi
done
}
if [ -f "$LOCAL_CONFIG" ]; then
source $LOCAL_CONFIG
else
echo "Error: $LOCAL_CONFIG not found."
echo "Hint: Did you do \`cp $(basename $LOCAL_CONFIG) $HOME\` and set your values?"
exit 1
fi
# Expand REPO_DIR in case it starts with '~/'
eval REPO_DIR=$REPO_DIR
cd $REPO_DIR
case "$#" in
0)
print_usage
;;
1)
LONG_URL="$1"
SHORTCODE=$(generate_shortcode)
if [ "$SHORTCODE" = "" ]; then
echo "Error: $MAX_ATTEMPTS attempts were made to find a unique random string for your URL in $STATIC_FILE, but no success.
Aborting. Consider raising the value of SHORTCODE_LENGTH in '$LOCAL_CONFIG'."
exit 1
fi
;;
2)
LONG_URL="$1"
SHORTCODE="$2"
if grep -q "^/$SHORTCODE" "$STATIC_FILE"; then
echo "Error: /$SHORTCODE is already taken in $STATIC_FILE."
exit 1
fi
;;
*)
print_usage
;;
esac
# If the long URL is already being redirected, notify the user
if grep -q -P "$LONG_URL( |$)" $STATIC_FILE; then
echo "This URL is already being redirected:"
grep -P "$LONG_URL( |$)" $STATIC_FILE
exit 1
fi
# Validate long URL given by user
$REPO_DIR/scripts/validateURL $LONG_URL
if [ "$?" -ne 0 ]; then
exit 1
fi
if [ "$DRY_RUN" = true ]; then
echo "Dry run, files will not be modified."
else
git fetch; git pull origin $(git branch --show-current)
echo "/$SHORTCODE $LONG_URL" >> $STATIC_FILE
git add $STATIC_FILE
git commit -m "add redirect /$SHORTCODE --> $LONG_URL"
git push origin main
fi
echo "Your shortened URL is https://$MY_DOMAIN/$SHORTCODE"
echo "Please give ~15 seconds for your link to become valid"
cd - > /dev/null