Skip to content

Commit

Permalink
feat(url): Automatically select best mirror for Xiaomi URLs
Browse files Browse the repository at this point in the history
Co-authored-by: Jyotiraditya Panda <[email protected]>
  • Loading branch information
AntoninoScordino and imjyotiraditya committed Sep 27, 2024
1 parent e3eb12f commit 20b657e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions extract_and_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ else
sendTG() { :; } && sendTG_edit_wrapper() { :; }
fi

# If the device contains a link from Xiaomi's CDN, replace it (when applicable)
# with the fastest one
if [[ "${URL}" == *"d.miui.com"* ]] && [ ! "$(echo "${URL}" | grep -q cdnorg)" ] && [ ! "$(echo "${URL}" | grep -q bkt-sgp-miui-ota-update-alisgp)" ]; then
# Set '${URL_ORIGINAL}' in case we might need to roll back
URL_ORIGINAL=${URL%/*}
FILE_PATH=${URL#*d.miui.com/}

# Array of different possible mirrors
MIRRORS=(
"https://cdnorg.d.miui.com"
"https://bkt-sgp-miui-ota-update-alisgp.oss-ap-southeast-1.aliyuncs.com"
"${URL_ORIGINAL}"
)

for URLS in "${MIRRORS[@]}"; do
URL=${URLS}/${FILE_PATH}
if [ $(curl -s -o /dev/null -w "%{http_code}" "${URL}") == "404" ]; then
echo "[ERROR] ${URLS} is not available. Trying with other mirror(s)..."
else
echo "[INFO] Found best available mirror."
break
fi
done
fi

# Confirm download has started
sendTG_edit_wrapper permanent "${MESSAGE_ID}" "${MESSAGE}"$'\n'"<code>Downloading the file..</code>" > /dev/null
echo "[INFO] Started downloading... ($(date +%R:%S))"
Expand Down

0 comments on commit 20b657e

Please sign in to comment.