From d1d4eeac72acf712b3fc5fe78000687d1d39d247 Mon Sep 17 00:00:00 2001 From: ant Date: Fri, 27 Sep 2024 23:28:21 +0200 Subject: [PATCH] feat(url): Automatically select best mirror for Xiaomi URLs Co-authored-by: Jyotiraditya Panda --- extract_and_push.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/extract_and_push.sh b/extract_and_push.sh index 08d5b00..9528350 100755 --- a/extract_and_push.sh +++ b/extract_and_push.sh @@ -140,6 +140,31 @@ else terminate 1 } + # 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 down. Trying with other mirror(s)..." + else + echo "[INFO] Found best available mirror." + break + fi + done + fi + # Properly check for different hosting websties. case ${URL} in *drive.google.com*)