From 5710955e1a5badbb19e7c0eff7f7595c12ca094c Mon Sep 17 00:00:00 2001 From: Botspot Date: Wed, 1 Sep 2021 11:16:43 -0500 Subject: [PATCH] manage & api: intercept wget calls to use aria2c --- api | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ manage | 1 + 2 files changed, 57 insertions(+) diff --git a/api b/api index 3324849..ab3d20d 100755 --- a/api +++ b/api @@ -496,6 +496,62 @@ ${blue}┃ ${red}⬛⬛⬛ ${blue}┃\e[97m ▕ ▕ \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 [[ "$0" == */api ]];then if [ ! -z "$1" ];then diff --git a/manage b/manage index 436cd14..c741929 100755 --- a/manage +++ b/manage @@ -14,6 +14,7 @@ if [ -z "$1" ];then error "You need to specify an operation, and in most cases, which app to operate on." fi +set -a #make all functions in the api available to apps 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.