manage & api: intercept wget calls to use aria2c

pull/942/head
Botspot 3 years ago
parent 320633e01f
commit 5710955e1a

56
api

@ -496,6 +496,62 @@ ${blue}┃ ${red}⬛⬛⬛ ${blue}┃\e[97m ▕ ▕
\e[0m\e[0m" \e[0m\e[0m"
} }
wget() { #this function intercepts all wget commands being used in app scripts. It uses aria2c if possible.
local file=''
local url=''
local use=aria2c
if command -v aria2c; then
#convert wget arguments to newline-separated list
local IFS=$'\n'
local opts="$(IFS=$'\n'; echo "$*")"
for opt in $opts ;do
if [[ "$opt" == '-'* ]]; then
#this opt is the beginning of a flag
if [ "$opt" == '-qO' ] || [ "$opt" == '-O' ] || [ "$opt" == '-q' ]; then
true
else #any other wget command-flags other than '-qO', '-O', '-q'
use=wget
break
fi
elif [[ "$opt" == *'://'* ]]; then
#this opt is web address
url="$opt"
elif [[ "$opt" == '/'* ]]; then
#this opt is file output
if [ -z "$file" ];then
file="$opt"
else #file var already populated
use=wget
break
fi
else
#this opt does not begin with '-', contain '://', or begin with '/'. Assume output file specified shorthand
if [ -z "$file" ];then
file="$(pwd)/${opt}"
else #file var already populated
use=wget
break
fi
fi
done
else
#aria2c command not found
use=wget
fi
if [ "$use" == wget ];then
#run the true wget binary with all this function's args
command wget "$@"
elif [ "$use" == aria2c ];then
#make default filename if $file empty
if [ -z "$file" ];then
file="$(pwd)/$(basename "$url")"
fi
aria2c -c -x 16 -s 16 -m 10 --retry-wait 30 "$url" --dir '/' -o "${file:1}" --allow-overwrite --summary-interval=1
fi
}
#if this script is being run standalone, not sourced #if this script is being run standalone, not sourced
if [[ "$0" == */api ]];then if [[ "$0" == */api ]];then
if [ ! -z "$1" ];then if [ ! -z "$1" ];then

@ -14,6 +14,7 @@ if [ -z "$1" ];then
error "You need to specify an operation, and in most cases, which app to operate on." error "You need to specify an operation, and in most cases, which app to operate on."
fi fi
set -a #make all functions in the api available to apps
source "${DIRECTORY}/api" || error "failed to source ${DIRECTORY}/api" source "${DIRECTORY}/api" || error "failed to source ${DIRECTORY}/api"
#remove old mcpi repositories - this runonce is here so that terminal-only users will still receive the fix. #remove old mcpi repositories - this runonce is here so that terminal-only users will still receive the fix.

Loading…
Cancel
Save