-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.sh
132 lines (99 loc) · 2.96 KB
/
share.sh
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
# Functions ==============================================
# return 1 if global command line program installed, else 0
# example
# echo "node: $(program_is_installed node)"
function program_is_installed {
# set to 1 initially
local return_=1
# set to 0 if not found
type $1 >/dev/null 2>&1 || { local return_=0; }
# return value
echo "$return_"
}
# display a message in red with a cross by it
# example
# echo echo_fail "No"
function echo_fail {
# echo first argument in red
printf "\e[31m✘ ${1}"
printf "\e[31m Make sure the libraries are installed on your system"
# reset colours back to normal
printf "\033\e[0m"
}
# display a message in green with a tick by it
# example
# echo echo_fail "Yes"
function echo_pass {
# echo first argument in green
printf "\e[32m✔ ${1}"
# reset colours back to normal
printf "\033\e[0m"
}
# echo pass or fail
# example
# echo echo_if 1 "Passed"
# echo echo_if 0 "Failed"
function echo_if {
if [ $1 == 1 ]; then
echo_pass $2
else
echo_fail $2
fi
}
# Functions ==============================================
#check if jq and curl has been installed or not
echo "==================================================="
echo "checking jq if already installed $(echo_if $(program_is_installed jq))"
echo "checking curl if already installed $(echo_if $(program_is_installed curl))"
echo "==================================================="
###################################################
FILE_PATH=""
DATA=""
EXP=$3
# "d" | "w" | "m" | "y"
DURATION="d"
NUM_DURATION=14
regex='^([0-9]*)(d|m|w|y)$'
if [[ $EXP =~ $regex ]]
then
DURATION=${BASH_REMATCH[2]}
NUM_DURATION=${BASH_REMATCH[1]}
fi
URL_TO_BE_SHARED=""
while getopts 'f:d:' OPTION; do
case "$OPTION" in
f)
FILE_PATH="$OPTARG"
#check if file exists
if [ ! -f "$FILE_PATH" ]; then
printf "\e[31m✘ Please provide a valid path to the file that you wanna upload and share!"
exit 0
fi
response=$(curl --silent -F "file=@$FILE_PATH" "https://file.io/?expires=$NUM_DURATION$DURATION" | jq '.link')
#converting to array
responseArr=($(echo "$response" | tr '' '\n'))
URL_TO_BE_SHARED=${responseArr[0]}
;;
d)
DATA="$OPTARG"
response=$(curl --silent --data "text=$DATA" "https://file.io/?expires=$NUM_DURATION$DURATION" | jq '.link')
#converting to array
responseArr=($(echo "$response" | tr '' '\n'))
URL_TO_BE_SHARED=${responseArr[0]}
;;
?)
echo "script usage: sh $(basename $0) [-f 'PATH_TO_FILE'] [-d 'TEXT_DATA'] '3d|14w|1m|2y'" >&2
exit 1
;;
esac
done
if [ "${URL_TO_BE_SHARED}" == null ]; then
printf "\e[31m Ooops, we can't make the file as shareable, make sue the file|data are not empty"
printf "\033\e[0m"
exit;
fi
echo "\n "
printf "\e[32m✔ Shareable URL : $URL_TO_BE_SHARED \n\n";
printf "\e[32m✔ Expires After : $NUM_DURATION$DURATION [d=day, w=week, m=month, y=year]";
echo "\n "