forked from ksauraj/global_index_source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ksau
executable file
·459 lines (408 loc) · 15.1 KB
/
ksau
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#!/bin/bash
readonly KSAU_VERSION=311
readonly KSAU_VERSION_STRING="Version 3.1.1"
readonly KSAU_CACHE_DIR=$HOME/.ksau
readonly CACHED_FREESPACE_TIMEOUT=$((60 * 60)) # 60s * 60m = 1h in seconds unit
mkdir -p "$KSAU_CACHE_DIR"
readonly REMOTES=(
"oned"
"hakimionedrive"
"saurajcf"
)
SELECTED_REMOTE=oned # default
#Define colors
normal=$(echo -en "\e[0m")
orange=$(echo -en "\e[33m")
aqua=$(echo -en "\e[36m")
lightgreen=$(echo -en "\e[92m")
Normal='\033[0m'
Black='\033[0;30m' # Black
Red=$'\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue=$'\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
UPLINE=$(tput cuu1)
CLEARLINE=$(tput el)
set -o pipefail
readonly status_dir="$(mktemp -d)"
trap "rm -rf $status_dir" EXIT
upline() {
for _ in $(seq $1); do
echo -ne "$UPLINE"
done
}
clearline() {
echo -ne "$CLEARLINE"
}
dependencies() {
if declare -p TERMUX_VERSION &>/dev/null; then
echo "Starting Installation...."
pkg update >/dev/null 2>&1
pkg install rclone jq -y >/dev/null 2>&1
echo ""
echo "${orange}Installation Completed. ${normal}"
return
fi
echo ""
echo "Make sure you have sudo privilages"
echo "This will only work for Linux/macOS/BSD systems."
echo ""
sudo curl https://rclone.org/install.sh 2>/dev/null | sudo bash >/dev/null 2>&1
echo ""
echo "${orange}Please install jq manually, No script found"
echo "Possibly with
${aqua}sudo apt-get install jq ${normal}OR
${aqua}sudo pacman -S jq ${normal}OR
${aqua}sudo dnf install jq ${normal}OR
${aqua}sudo zypper install jq ${normal}OR
${aqua}apk add jq ${normal}OR
${aqua}pkg install jq${normal}"
echo ''
}
sanity_checks() {
mkdir -p ~/.config/rclone
if ! command -v wget &>/dev/null || ! command -v jq &>/dev/null || ! command -v rclone &>/dev/null; then
echo "${Red}Dependencies are not installed!${normal}"
echo "${aqua}Please install jq, wget and rclone.${normal}"
exit 1
fi
}
refresh() {
echo "${aqua}Downloading latest rclone config${normal}"
wget https://gist.githubusercontent.com/hakimifr/aabebeea8c1ce0c5a6968fc0d78ed5ae/raw/rclone.conf -O rclone.conf &>/dev/null
if [ "$?" -ne 0 ]; then
echo "Config download failed!"
return
fi
mv rclone.conf ~/.config/rclone/rclone.conf
echo "${lightgreen}rclone config updated successfully.${normal}"
}
# Generates new filename with random strings at last (before extension if present).
# new name for 'myfile.txt' -> 'myfile-78e11b8c.txt'
# new name for 'myfile' -> 'myfile-78e11b8c'
add_random_string() {
local filename="$1"
local extension="${filename##*.}"
local name="${filename%.*}"
local random_string="$(date +%s | sha256sum | base64 | head -c 8)"
if [ -z "$extension" ]; then
new_filename="$name-$random_string"
else
new_filename="$name-$random_string.$extension"
fi
}
# Select remote randomly.
select_remote_random() {
SELECTED_REMOTE=$(printf "%s\n" "${REMOTES[@]}" | shuf -n1)
echo "Using randomly selected remote: $SELECTED_REMOTE"
}
# Select remote with most free space
# Will be used for larger files
select_remote_most_free() {
echo -en "Checking free space for each remote...\r"
local remotes_free_space=()
for remote in "${REMOTES[@]}"; do
echo -en "$(clearline)Checking free space for each remote -- remote: ${lightgreen}${remote}${normal}\r"
if ! remotes_free_space+=("$(rclone about "$remote:" --full | grep Free | cut -d: -f2 | tr -d '[:space:]')"); then
echo "${Red}Error checking free space. Please run ksau refresh. Falling back to random remote.${normal}"
select_remote_random
return
fi
done
clearline
upline 1
local curr_max_index=0
for fsi in "${!remotes_free_space[@]}"; do
if [ "${remotes_free_space[$fsi]}" -gt "${remotes_free_space[$curr_max_index]}" ]; then
curr_max_index=$fsi
fi
done
SELECTED_REMOTE="${REMOTES[$curr_max_index]}"
# Update cache
echo "$SELECTED_REMOTE" >"$KSAU_CACHE_DIR/most_free_remote"
date +%s >"$KSAU_CACHE_DIR/cached_freespace_update_time"
}
# Select remote with most free space from cached value
select_remote_most_free_cached() {
if [ ! -f "$KSAU_CACHE_DIR/cached_freespace_update_time" ] ||
[ "$(($(date +%s) - $(cat "$KSAU_CACHE_DIR/cached_freespace_update_time")))" -ge "$CACHED_FREESPACE_TIMEOUT" ]; then
select_remote_most_free
else
SELECTED_REMOTE=$(cat "$KSAU_CACHE_DIR/most_free_remote")
echo "Using cached remote with most free space: $SELECTED_REMOTE"
fi
}
# Simple progressbar animation.
# Usage : progress_bar ${percentage} ${upload_speed} $(eta) ${status}
progress_bar() {
if [[ ${1} -lt 10 ]]; then
echo "${Yellow}Progress ${Green}[=>---------${Cyan}${1}%${Normal}${Green}---------<=]"
elif [[ ${1} -lt 20 ]]; then
echo "${Yellow}Progress ${Green}[==>--------${Cyan}${1}%${Normal}${Green}--------<==]"
elif [[ ${1} -lt 30 ]]; then
echo "${Yellow}Progress ${Green}[===>-------${Cyan}${1}%${Normal}${Green}-------<===]"
elif [[ ${1} -lt 40 ]]; then
echo "${Yellow}Progress ${Green}[====>------${Cyan}${1}%${Normal}${Green}------<====]"
elif [[ ${1} -lt 50 ]]; then
echo "${Yellow}Progress ${Green}[=====>-----${Cyan}${1}%${Normal}${Green}-----<=====]"
elif [[ ${1} -lt 60 ]]; then
echo "${Yellow}Progress ${Green}[======>----${Cyan}${1}%${Normal}${Green}----<======]"
elif [[ ${1} -lt 70 ]]; then
echo "${Yellow}Progress ${Green}[=======>---${Cyan}${1}%${Normal}${Green}---<=======]"
elif [[ ${1} -lt 80 ]]; then
echo "${Yellow}Progress ${Green}[========>--${Cyan}${1}%${Normal}${Green}--<========]"
elif [[ ${1} -lt 90 ]]; then
echo "${Yellow}Progress ${Green}[=========>-${Cyan}${1}%${Normal}${Green}-<=========]"
elif [[ ${1} -lt 100 ]]; then
echo "${Yellow}Progress ${Green}[==========>${Cyan}${1}%${Normal}${Green}<==========]"
elif [[ ${1} == 100 ]]; then
echo "${Yellow}Progress ${Green}[===========${Cyan}${1}%${Normal}${Green}===========]"
fi
}
# Upload file to give folder and use new filename if -r argument was passed and printing out new animation.
rclone_progress_bar() {
[ "$KSAUOPT_QUIET" = false ] && tput civis
echo "Starting upload..."
rm -f log
touch log
if [ -z "$new_filename" ]; then new_filename=$(basename "$file"); fi
case $SELECTED_REMOTE in
hakimionedrive)
rclone -P --checkers=32 --onedrive-chunk-size 60M copyto "$file" "hakimionedrive:/Public/${u_folder}/$new_filename" | tee log >/dev/null || touch "$status_dir/failed" &
;;
oned)
rclone -P --checkers=32 --onedrive-chunk-size 60M copyto "$file" "oned:/${u_folder}/$new_filename" | tee log >/dev/null || touch "$status_dir/failed" &
;;
saurajcf)
rclone -P --checkers=32 --onedrive-chunk-size 60M copyto "$file" "saurajcf:/MY_BOMT_STUFFS/${u_folder}/$new_filename" | tee log >/dev/null || touch "$status_dir/failed" &
;;
esac
until [ -z "$(jobs -r)" ]; do
if [ -f "$status_dir/failed" ]; then
echo
echo "${Red}Upload failed!${Normal}"
break
fi
local percent=$(grep -Eo '[0-9]{1,3}'% log | tail -n1 | cut -d '%' -f 1)
local ups=$(grep 'ETA' log | tail -n1 | cut -d ',' -f5)
local eta=$(grep 'ETA' log | tail -n1 | cut -d ',' -f6 | sed 's/ETA //')
local status=$(grep 'ETA' log | tail -n1 | cut -d ',' -f3 | sed 's/^.* //')
progbar=$(progress_bar "$percent")
echo -e "$(clearline)$progbar
$(clearline)${Yellow} UPS:${Normal} $ups
$(clearline)${Yellow} ETA:${Normal} $eta
$(clearline)${Yellow}STATUS:${Normal} $status
$(upline 5)"
done #|| echo -ne "\rDownload Can't be completed, Check download link." && tput cnorm && exit 1
[ "$KSAUOPT_QUIET" = false ] && tput cnorm
}
# Below function is copied from - https://gist.github.com/jaytaylor/5a90c49e0976aadfe0726a847ce58736
# Credit to @jkishner for https://gist.github.com/jkishner/2fccb24640a27c2d7ac9
function url_encode() {
builtin echo "$@" |
sed \
-e 's/%/%25/g' \
-e 's/ /%20/g' \
-e 's/!/%21/g' \
-e 's/"/%22/g' \
-e "s/'/%27/g" \
-e 's/#/%23/g' \
-e 's/(/%28/g' \
-e 's/)/%29/g' \
-e 's/+/%2b/g' \
-e 's/,/%2c/g' \
-e 's/:/%3a/g' \
-e 's/;/%3b/g' \
-e 's/?/%3f/g' \
-e 's/@/%40/g' \
-e 's/\$/%24/g' \
-e 's/\&/%26/g' \
-e 's/\*/%2a/g' \
-e 's/\//%2f/g' \
-e 's/\[/%5b/g' \
-e 's/\\/%5c/g' \
-e 's/\]/%5d/g' \
-e 's/\^/%5e/g' \
-e 's/`/%60/g' \
-e 's/{/%7b/g' \
-e 's/|/%7c/g' \
-e 's/}/%7d/g' \
-e 's/~/%7e/g'
}
# rclone_progress_bar -> progress_bar -> Print dowload link.
upload() {
if [[ $KSAUOPT_ADD_RAND_STR == true ]]; then
add_random_string "$(basename "$file")"
fi
echo "Initializing process, might take up to 10 seconds..."
rclone_progress_bar
wait
if [ -f "$status_dir/failed" ]; then
echo -e "\rUpload Failed .......\n"
exit 1
fi
url_file=$(url_encode "${new_filename}")
url_folder=$(jq -rn --arg x "${u_folder%/}" '$x|@uri')
echo -e "\n$(clearline)$(upline 1)$(clearline)$(upline 1)"
echo "$(clearline) Upload Completed "
case $SELECTED_REMOTE in
hakimionedrive)
echo "$(clearline) Download link - ${lightgreen}https://onedrive-vercel-index-kohl-eight-30.vercel.app/${url_folder}/${url_file}${normal} "
;;
oned)
echo "$(clearline) Download link - ${lightgreen}https://index.sauraj.eu.org/${url_folder}/${url_file}${normal} "
;;
saurajcf)
echo "$(clearline) Download link - ${lightgreen}https://my-index-azure.vercel.app/${url_folder}/${url_file}${normal} "
;;
esac
clearline
if [[ $KSAUOPT_QUIET == true ]]; then
case $SELECTED_REMOTE in
hakimionedrive)
builtin echo "https://onedrive-vercel-index-kohl-eight-30.vercel.app/${url_folder}/${url_file}"
;;
oned)
builtin echo "https://index.sauraj.eu.org/${url_folder}/${url_file}"
;;
saurajcf)
builtin echo "https://my-index-azure.vercel.app/${url_folder}/${url_file}"
;;
esac
fi
}
help() {
echo "
${aqua}Usage : ksau [-r] [OPTION] [FILE]${normal}
${aqua}Options:${normal}
-r Add random string to the end of filename (but before extension) when uploading
-q Suppress all output, printing only the link after upload is finished
-c Upload to a specific remote
Note that all options must be passed ${Red}BEFORE${normal} other arguments.
${aqua}upload [-r] [FILE] [FOLDER]${normal}: Uploads the given file to the given folder
on index.
${aqua}list${normal} : List available remotes and show their usage info.
${aqua}refresh${normal} : Refresh rclone config. Try this if upload does not work.
${aqua}update${normal} : Fetch and install latest version.
available.
${aqua}dependencies${normal} : Install required dependencies.
${aqua}help${normal} : Show this message.
${aqua}version${normal} : Show ksau version.
Example: ${lightgreen}ksau upload test.txt Public${normal}
${Blue}Note:${normal} Each time ksau is run, even if not for uploading, it will attempt to refresh
the remote with most free space cache. This might cause a few seconds delay,
depending on the internet connection.
${orange}Tool By Sauraj (@Ksauraj) and @hakimifr${normal}
${orange}Join our Telegram channel for updates:${normal}
${aqua} https://t.me/ksau_update${normal}
"
}
_update_copy() {
local file=$1
if declare -p TERMUX_VERSION &>/dev/null; then
cp -f "$file" "$PREFIX/bin/ksau"
chmod +x "$PREFIX/bin/ksau"
else
sudo cp -f "$file" /usr/local/bin/ksau
chmod +x "/usr/local/bin/ksau"
fi
echo "Updated ksau copied."
}
update() {
if ! ping -c2 google.com &>/dev/null; then
echo "Please check your internet connection"
return
fi
local tmpfilename new_version new_version_string
tmpfilename=ksau-$RANDOM
echo "Fetching"
curl --progress-bar -Lo "$tmpfilename" https://raw.githubusercontent.com/ksauraj/global_index_source/master/ksau
upline 1
clearline
upline 1
clearline
new_version=$(grep 'KSAU_VERSION=' "$tmpfilename" | head -n1 | cut -d= -f2)
new_version_string=$(grep 'KSAU_VERSION_STRING=' "$tmpfilename" | head -n1 | cut -d= -f2)
if [[ $new_version -gt $KSAU_VERSION ]]; then
echo -e "New version found $Cyan($KSAU_VERSION_STRING -> $new_version_string)$Normal"
_update_copy "$tmpfilename"
else
echo "Already on latest release."
fi
rm -f "$tmpfilename"
}
version() {
echo "${orange}${KSAU_VERSION_STRING} ${normal}"
}
KSAUOPT_ADD_RAND_STR=false
KSAUOPT_QUIET=false
KSAUOPT_SPECIFIC_REMOTE=
while getopts "rqc:" opt; do
case $opt in
r)
KSAUOPT_ADD_RAND_STR=true
;;
q)
KSAUOPT_QUIET=true
# Override all echo call.
echo() { true; }
;;
c)
if printf '%s\0' "${REMOTES[@]}" | grep -Fxqz -- "$OPTARG"; then
KSAUOPT_SPECIFIC_REMOTE=$OPTARG
else
echo "${Red}Such remote does not exist. Please view available remotes with ksau list.${normal}"
fi
;;
?)
# Getopts already print error for us so just exit
exit 1
;;
esac
done
# Remove all args parsed by getopts
shift $((OPTIND - 1))
sanity_checks
if [[ $1 == "refresh" ]]; then
refresh
fi
# Call this because even if user's not uploading, we want to
# refresh the cache either way if its older than set timeout
select_remote_most_free_cached
if [[ $1 == "upload" ]]; then
readonly file="$2"
readonly u_folder="$3"
if [ -n "$KSAUOPT_SPECIFIC_REMOTE" ]; then
SELECTED_REMOTE=$KSAUOPT_SPECIFIC_REMOTE
upline 1
clearline
echo "Using requested remote: $SELECTED_REMOTE"
elif [ "$(stat -c '%s' "$file")" -lt 104857600 ]; then # -lt 100MiB
upline 1
clearline
echo "File size is <100MiB, selecting random remote"
select_remote_random # override most free cached
fi
upload
elif [[ $1 == "dependencies" ]]; then
dependencies
elif [[ $1 == "update" ]]; then
update
elif [[ $1 == "version" ]]; then
version
elif [[ $1 == "list" ]]; then
for remote in "${REMOTES[@]}"; do
remote_about=$(rclone about ${remote}:)
printf '%sRemote: %s%s\n%s%s%s\n\n' "${orange}" "${remote}" "${normal}" "${aqua}" "${remote_about}" "${normal}"
done
elif [[ $1 == "help" ]]; then
help
elif [[ $1 == "" ]]; then
help
else
echo "Unknown Argument passed."
help
fi