From 348930051eb636a8bde67d05761e08cd0b792591 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 4 May 2024 18:57:35 +0200 Subject: [PATCH] feat: Show download percentage (#98) --- Dockerfile | 2 +- src/define.sh | 63 +++++++++++++++++++++-------- src/install.sh | 105 +++++++++++++++++++++++++++++++++++++------------ 3 files changed, 126 insertions(+), 44 deletions(-) diff --git a/Dockerfile b/Dockerfile index efcd744..9bab24c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM scratch -COPY --from=qemux/qemu-arm:1.18 / / +COPY --from=qemux/qemu-arm:1.19 / / ARG DEBCONF_NOWARNINGS "yes" ARG DEBIAN_FRONTEND "noninteractive" diff --git a/src/define.sh b/src/define.sh index 02ede7c..e45ce02 100644 --- a/src/define.sh +++ b/src/define.sh @@ -149,20 +149,28 @@ getLink1() { local ret="$2" local url="" local sum="" + local size="" local host="https://dl.bobpony.com/windows" case "${id,,}" in "win11${PLATFORM,,}") + size=5946128384 sum="0c8edeae3202cf6f4bf8bb65c9f6176374c48fdcbcc8d0effa8547be75e9fd20" - url="$host/windows/11/en-us_windows_11_23h2_${PLATFORM,,}.iso" + url="$host/11/en-us_windows_11_23h2_${PLATFORM,,}.iso" ;; "win10${PLATFORM,,}") + size=4957009920 sum="64461471292b79d18cd9cced6cc141d7773b489a9b3e12de7b120312e63bfaf1" - url="$host/windows/10/en-us_windows_10_22h2_${PLATFORM,,}.iso" + url="$host/10/en-us_windows_10_22h2_${PLATFORM,,}.iso" ;; esac - [ -z "$ret" ] && echo "$url" || echo "$sum" + case "${ret,,}" in + "sum" ) echo "$sum" ;; + "size" ) echo "$size" ;; + *) echo "$url";; + esac + return 0 } @@ -174,32 +182,50 @@ getLink2() { local ret="$2" local url="" local sum="" + local size="" local host="https://drive.massgrave.dev" case "${id,,}" in "win11${PLATFORM,,}") + size=7010680832 sum="3da19e8c8c418091081186e362fb53a1aa68dad255d1d28ace81e2c88c3f99ba" url="$host/SW_DVD9_Win_Pro_11_23H2.2_Arm64_English_Pro_Ent_EDU_N_MLF_X23-68023.ISO" ;; "win10${PLATFORM,,}") + size=5190453248 sum="bd96b342193f81c0a2e6595d8d8b8dc01dbf789d19211699f6299fec7b712197" url="$host/SW_DVD9_Win_Pro_10_22H2.15_Arm64_English_Pro_Ent_EDU_N_MLF_X23-67223.ISO" ;; esac - [ -z "$ret" ] && echo "$url" || echo "$sum" + case "${ret,,}" in + "sum" ) echo "$sum" ;; + "size" ) echo "$size" ;; + *) echo "$url";; + esac + + return 0 +} + +getValue() { + + local val="" + local id="$3" + local type="$2" + local func="getLink$1" + + if [ "$1" -gt 0 ] && [ "$1" -le "$MIRRORS" ]; then + val=$($func "$id" "$type") + fi + + echo "$val" return 0 } getLink() { local url="" - local id="$2" - local func="getLink$1" - - if [ "$1" -gt 0 ] && [ "$1" -le "$MIRRORS" ]; then - url=$($func "$id" "") - fi + url=$(getValue "$1" "" "$2") echo "$url" return 0 @@ -208,24 +234,27 @@ getLink() { getHash() { local sum="" - local id="$2" - local func="getLink$1" - - if [ "$1" -gt 0 ] && [ "$1" -le "$MIRRORS" ]; then - sum=$($func "$id" "sum") - fi + sum=$(getValue "$1" "sum" "$2") echo "$sum" return 0 } +getSize() { + + local size="" + size=$(getValue "$1" "size" "$2") + + echo "$size" + return 0 +} + validVersion() { local id="$1" local url isESD "$id" && return 0 - isMido "$id" && return 0 for ((i=1;i<=MIRRORS;i++)); do diff --git a/src/install.sh b/src/install.sh index d310838..eaf09e5 100644 --- a/src/install.sh +++ b/src/install.sh @@ -210,7 +210,7 @@ getESD() { local eFile="esd_edition.xml" local fFile="products_filter.xml" - { wget "$winCatalog" -O "$dir/$wFile" -q; rc=$?; } || : + { wget "$winCatalog" -O "$dir/$wFile" -q --timeout=10; rc=$?; } || : (( rc != 0 )) && error "Failed to download $winCatalog , reason: $rc" && return 1 cd "$dir" @@ -240,34 +240,86 @@ getESD() { error "Failed to find Windows product in $eFile!" && return 1 fi - ESD=$(xmllint --nonet --xpath '//FilePath' "$dir/$eFile" | sed -E -e 's/<[\/]?FilePath>//g') + local tag="FilePath" + ESD=$(xmllint --nonet --xpath "//$tag" "$dir/$eFile" | sed -E -e "s/<[\/]?$tag>//g") if [ -z "$ESD" ]; then error "Failed to find ESD URL in $eFile!" && return 1 fi + tag="Sha1" + ESD_SUM=$(xmllint --nonet --xpath "//$tag" "$dir/$eFile" | sed -E -e "s/<[\/]?$tag>//g") + tag="Size" + ESD_SIZE=$(xmllint --nonet --xpath "//$tag" "$dir/$eFile" | sed -E -e "s/<[\/]?$tag>//g") + rm -rf "$dir" return 0 } +doMido() { + + local iso="$1" + local version="$2" + local desc="$3" + local rc + + rm -f "$iso" + rm -f "$iso.PART" + + local msg="Downloading $desc..." + info "$msg" && html "$msg" + /run/progress.sh "$iso.PART" "" "Downloading $desc ([P])..." & + + cd "$TMP" + { /run/mido.sh "${version,,}"; rc=$?; } || : + cd /run + + fKill "progress.sh" + + if (( rc == 0 )) && [ -f "$iso" ]; then + if [ "$(stat -c%s "$iso")" -gt 100000000 ]; then + html "Download finished successfully..." && return 0 + fi + fi + + rm -f "$iso" + rm -f "$iso.PART" + + return 1 +} + verifyFile() { local iso="$1" - local check="$2" + local size="$2" + local total="$3" + local check="$4" + + if [ -n "$size" ] && [[ "$total" != "$size" ]]; then + [[ "$size" != "0" ]] && warn "The download file has an unexpected size: $total" + fi + local hash="" + local algo="SHA256" [ -z "$check" ] && return 0 + [[ "$VERIFY" != [Yy1]* ]] && return 0 + [[ "${#check}" == "40" ]] && algo="SHA1" - html "Verifying downloaded ISO..." - info "Calculating SHA256 checksum of the ISO file..." + local msg="Verifying downloaded ISO..." + info "$msg" && html "$msg" - hash=$(sha256sum "$iso" | cut -f1 -d' ') + if [[ "${algo,,}" != "sha256" ]]; then + hash=$(sha1sum "$iso" | cut -f1 -d' ') + else + hash=$(sha256sum "$iso" | cut -f1 -d' ') + fi if [[ "$hash" == "$check" ]]; then info "Succesfully verified that the checksum was correct!" && return 0 fi - error "Invalid sha256 checksum: $hash , but expected value is: $check ! Please report this at $SUPPORT/issues" + error "Invalid $algo checksum: $hash , but expected value is: $check ! Please report this at $SUPPORT/issues" rm -f "$iso" return 1 @@ -278,8 +330,9 @@ downloadFile() { local iso="$1" local url="$2" local sum="$3" - local desc="$4" - local rc progress domain dots + local size="$4" + local desc="$5" + local rc total progress domain dots rm -f "$iso" @@ -291,6 +344,7 @@ downloadFile() { fi local msg="Downloading $desc..." + html "$msg" domain=$(echo "$url" | awk -F/ '{print $3}') dots=$(echo "$domain" | tr -cd '.' | wc -c) @@ -300,18 +354,17 @@ downloadFile() { msg="Downloading $desc from $domain..." fi - info "$msg" && html "$msg" - /run/progress.sh "$iso" "Downloading $desc ([P])..." & + info "$msg" + /run/progress.sh "$iso" "$size" "Downloading $desc ([P])..." & - { wget "$url" -O "$iso" -q --show-progress "$progress"; rc=$?; } || : + { wget "$url" -O "$iso" -q --timeout=10 --show-progress "$progress"; rc=$?; } || : fKill "progress.sh" if (( rc == 0 )) && [ -f "$iso" ]; then - if [ "$(stat -c%s "$iso")" -gt 100000000 ]; then - if [[ "$VERIFY" == [Yy1]* ]] && [ -n "$sum" ]; then - ! verifyFile "$iso" "$sum" && return 1 - fi + total=$(stat -c%s "$iso") + if [ "$total" -gt 100000000 ]; then + ! verifyFile "$iso" "$size" "$total" "$sum" && return 1 html "Download finished successfully..." && return 0 fi fi @@ -327,11 +380,11 @@ downloadImage() { local iso="$1" local version="$2" local tried="n" - local url sum desc + local url sum size desc if [[ "${version,,}" == "http"* ]]; then desc=$(fromFile "$BASE") - downloadFile "$iso" "$version" "" "$desc" && return 0 + downloadFile "$iso" "$version" "" "" "$desc" && return 0 return 1 fi @@ -341,14 +394,13 @@ downloadImage() { desc=$(printVersion "$version" "") - if [[ "${PLATFORM,,}" == "x64" ]]; then - if isMido "$version"; then - tried="y" - doMido "$iso" "$version" "$desc" && return 0 - fi - switchEdition "$version" + if isMido "$version"; then + tried="y" + doMido "$iso" "$version" "$desc" && return 0 fi + switchEdition "$version" + if isESD "$version"; then if [[ "$tried" != "n" ]]; then @@ -359,7 +411,7 @@ downloadImage() { if getESD "$TMP/esd" "$version"; then ISO="$TMP/$version.esd" - downloadFile "$ISO" "$ESD" "" "$desc" && return 0 + downloadFile "$ISO" "$ESD" "$ESD_SUM" "$ESD_SIZE" "$desc" && return 0 ISO="$TMP/$BASE" fi @@ -374,8 +426,9 @@ downloadImage() { info "Failed to download $desc, will try another mirror now..." fi tried="y" + size=$(getSize "$i" "$version") sum=$(getHash "$i" "$version") - downloadFile "$iso" "$url" "$sum" "$desc" && return 0 + downloadFile "$iso" "$url" "$sum" "$size" "$desc" && return 0 fi done